2013-03-01 20:07:44 +08:00
|
|
|
require_dependency 'enum'
|
2013-02-07 23:45:24 +08:00
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
class Notification < ActiveRecord::Base
|
2013-02-06 03:16:51 +08:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
|
|
|
|
|
|
|
validates_presence_of :data
|
|
|
|
validates_presence_of :notification_type
|
|
|
|
|
2013-03-01 02:54:12 +08:00
|
|
|
scope :unread, lambda { where(read: false) }
|
|
|
|
scope :recent, lambda { order('created_at desc').limit(10) }
|
|
|
|
|
2013-03-01 20:07:44 +08:00
|
|
|
def self.types
|
|
|
|
@types ||= Enum.new(
|
|
|
|
:mentioned, :replied, :quoted, :edited, :liked, :private_message,
|
|
|
|
:invited_to_private_message, :invitee_accepted, :posted, :moved_post
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-25 15:42:42 +08:00
|
|
|
def self.mark_posts_read(user, topic_id, post_numbers)
|
2013-03-01 02:54:12 +08:00
|
|
|
Notification.update_all "read = 't'", user_id: user.id, topic_id: topic_id, post_number: post_numbers, read: false
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.interesting_after(min_date)
|
|
|
|
result = where("created_at > ?", min_date)
|
|
|
|
.includes(:topic)
|
|
|
|
.unread
|
|
|
|
.limit(20)
|
2013-03-01 20:07:44 +08:00
|
|
|
.order("CASE WHEN notification_type = #{Notification.types[:replied]} THEN 1
|
|
|
|
WHEN notification_type = #{Notification.types[:mentioned]} THEN 2
|
2013-02-06 03:16:51 +08:00
|
|
|
ELSE 3
|
|
|
|
END, created_at DESC").to_a
|
|
|
|
|
|
|
|
# Remove any duplicates by type and topic
|
|
|
|
if result.present?
|
2013-02-07 23:45:24 +08:00
|
|
|
seen = {}
|
2013-02-06 03:16:51 +08:00
|
|
|
to_remove = Set.new
|
|
|
|
|
|
|
|
result.each do |r|
|
|
|
|
seen[r.notification_type] ||= Set.new
|
|
|
|
if seen[r.notification_type].include?(r.topic_id)
|
2013-02-07 23:45:24 +08:00
|
|
|
to_remove << r.id
|
2013-02-06 03:16:51 +08:00
|
|
|
else
|
|
|
|
seen[r.notification_type] << r.topic_id
|
|
|
|
end
|
|
|
|
end
|
2013-02-07 23:45:24 +08:00
|
|
|
result.reject! {|r| to_remove.include?(r.id) }
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
# Be wary of calling this frequently. O(n) JSON parsing can suck.
|
|
|
|
def data_hash
|
|
|
|
@data_hash ||= begin
|
|
|
|
return nil if data.blank?
|
2013-03-01 02:54:12 +08:00
|
|
|
JSON.parse(data).with_indifferent_access
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def text_description
|
|
|
|
link = block_given? ? yield : ""
|
2013-03-01 20:07:44 +08:00
|
|
|
I18n.t("notification_types.#{Notification.types[notification_type]}", data_hash.merge(link: link))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
if topic.present?
|
|
|
|
return topic.relative_url(post_number)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def post
|
2013-03-01 02:54:12 +08:00
|
|
|
return if topic_id.blank? || post_number.blank?
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
Post.where(topic_id: topic_id, post_number: post_number).first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|