2016-04-06 03:12:02 +08:00
|
|
|
class UserFirst < ActiveRecord::Base
|
|
|
|
|
|
|
|
def self.types
|
2016-04-07 02:01:11 +08:00
|
|
|
@types ||= Enum.new(used_emoji: 1,
|
|
|
|
mentioned_user: 2 #unused now
|
|
|
|
)
|
2016-04-06 03:12:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_for(user_id, type, post_id=nil)
|
2016-04-06 11:18:11 +08:00
|
|
|
# the usual case will be that it is already in table, don't try to insert
|
|
|
|
return false if UserFirst.exists?(user_id: user_id, first_type: types[type])
|
|
|
|
|
2016-04-06 03:12:02 +08:00
|
|
|
create!(user_id: user_id, first_type: types[type], post_id: post_id)
|
|
|
|
true
|
|
|
|
rescue PG::UniqueViolation, ActiveRecord::RecordNotUnique
|
|
|
|
# Violating the index just means the user already did it
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|