2013-03-01 07:07:44 -05:00
|
|
|
require_dependency 'enum'
|
2013-02-07 10:45:24 -05:00
|
|
|
|
2013-03-01 07:07:44 -05:00
|
|
|
class Notification < ActiveRecord::Base
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :topic
|
|
|
|
|
|
|
|
validates_presence_of :data
|
|
|
|
validates_presence_of :notification_type
|
|
|
|
|
2013-02-28 13:54:12 -05:00
|
|
|
scope :unread, lambda { where(read: false) }
|
2015-06-22 08:32:45 -04:00
|
|
|
scope :recent, lambda { |n=nil| n ||= 10; order('notifications.created_at desc').limit(n) }
|
2015-06-22 16:14:22 -04:00
|
|
|
scope :visible , lambda { joins('LEFT JOIN topics ON notifications.topic_id = topics.id')
|
|
|
|
.where('topics.id IS NULL OR topics.deleted_at IS NULL') }
|
2013-02-28 13:54:12 -05:00
|
|
|
|
2017-06-12 03:41:39 -04:00
|
|
|
attr_accessor :skip_send_email
|
|
|
|
|
|
|
|
after_commit :send_email, on: :create
|
2017-03-20 00:06:37 -04:00
|
|
|
# This is super weird because the tests fail if we don't specify `on: :destroy`
|
2017-06-12 03:41:39 -04:00
|
|
|
# TODO: Revert back to default in Rails 5
|
2017-03-20 00:06:37 -04:00
|
|
|
after_commit :refresh_notification_count, on: :destroy
|
|
|
|
after_commit :refresh_notification_count, on: [:create, :update]
|
2016-12-21 23:29:34 -05:00
|
|
|
|
2013-05-16 03:50:14 -04:00
|
|
|
def self.ensure_consistency!
|
2017-04-24 16:51:09 -04:00
|
|
|
Notification.exec_sql <<-SQL
|
|
|
|
DELETE
|
|
|
|
FROM notifications n
|
|
|
|
WHERE notification_type = #{Notification.types[:private_message]}
|
|
|
|
AND NOT EXISTS (
|
|
|
|
SELECT 1
|
|
|
|
FROM posts p
|
|
|
|
JOIN topics t ON t.id = p.topic_id
|
|
|
|
WHERE p.deleted_at IS NULL
|
|
|
|
AND t.deleted_at IS NULL
|
|
|
|
AND p.post_number = n.post_number
|
|
|
|
AND t.id = n.topic_id
|
|
|
|
)
|
|
|
|
SQL
|
2013-05-16 03:50:14 -04:00
|
|
|
end
|
|
|
|
|
2013-03-01 07:07:44 -05:00
|
|
|
def self.types
|
2016-01-08 05:53:52 -05:00
|
|
|
@types ||= Enum.new(mentioned: 1,
|
|
|
|
replied: 2,
|
|
|
|
quoted: 3,
|
|
|
|
edited: 4,
|
|
|
|
liked: 5,
|
|
|
|
private_message: 6,
|
|
|
|
invited_to_private_message: 7,
|
|
|
|
invitee_accepted: 8,
|
|
|
|
posted: 9,
|
|
|
|
moved_post: 10,
|
|
|
|
linked: 11,
|
|
|
|
granted_badge: 12,
|
|
|
|
invited_to_topic: 13,
|
|
|
|
custom: 14,
|
2016-01-27 05:38:14 -05:00
|
|
|
group_mentioned: 15,
|
2016-07-06 15:56:40 -04:00
|
|
|
group_message_summary: 16,
|
2017-05-16 14:49:42 -04:00
|
|
|
watching_first_post: 17,
|
|
|
|
topic_reminder: 18
|
2016-01-27 05:38:14 -05:00
|
|
|
)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 02:42:42 -05:00
|
|
|
def self.mark_posts_read(user, topic_id, post_numbers)
|
2016-01-05 18:22:03 -05:00
|
|
|
count = Notification
|
|
|
|
.where(user_id: user.id,
|
|
|
|
topic_id: topic_id,
|
|
|
|
post_number: post_numbers,
|
|
|
|
read: false)
|
|
|
|
.update_all("read = 't'")
|
|
|
|
|
2016-11-16 03:20:38 -05:00
|
|
|
if count > 0
|
|
|
|
user.publish_notifications_state
|
|
|
|
end
|
2016-01-06 04:33:55 -05:00
|
|
|
|
|
|
|
count
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2016-09-16 02:14:00 -04:00
|
|
|
def self.read(user, notification_ids)
|
2017-04-24 16:51:09 -04:00
|
|
|
count = Notification.where(user_id: user.id)
|
|
|
|
.where(id: notification_ids)
|
|
|
|
.where(read: false)
|
|
|
|
.update_all(read: true)
|
2016-11-16 03:20:38 -05:00
|
|
|
|
2017-04-24 16:51:09 -04:00
|
|
|
user.publish_notifications_state if count > 0
|
2016-09-16 02:14:00 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
def self.interesting_after(min_date)
|
|
|
|
result = where("created_at > ?", min_date)
|
|
|
|
.includes(:topic)
|
2015-02-18 20:40:00 -05:00
|
|
|
.visible
|
2013-02-05 14:16:51 -05:00
|
|
|
.unread
|
|
|
|
.limit(20)
|
2013-03-01 07:07:44 -05:00
|
|
|
.order("CASE WHEN notification_type = #{Notification.types[:replied]} THEN 1
|
|
|
|
WHEN notification_type = #{Notification.types[:mentioned]} THEN 2
|
2013-02-05 14:16:51 -05:00
|
|
|
ELSE 3
|
|
|
|
END, created_at DESC").to_a
|
|
|
|
|
|
|
|
# Remove any duplicates by type and topic
|
|
|
|
if result.present?
|
2013-02-07 10:45:24 -05:00
|
|
|
seen = {}
|
2013-02-05 14:16:51 -05: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 10:45:24 -05:00
|
|
|
to_remove << r.id
|
2013-02-05 14:16:51 -05:00
|
|
|
else
|
|
|
|
seen[r.notification_type] << r.topic_id
|
|
|
|
end
|
|
|
|
end
|
2013-02-07 10:45:24 -05:00
|
|
|
result.reject! {|r| to_remove.include?(r.id) }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2014-05-26 13:26:28 -04:00
|
|
|
# Clean up any notifications the user can no longer see. For example, if a topic was previously
|
|
|
|
# public then turns private.
|
|
|
|
def self.remove_for(user_id, topic_id)
|
|
|
|
Notification.where(user_id: user_id, topic_id: topic_id).delete_all
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
# Be wary of calling this frequently. O(n) JSON parsing can suck.
|
|
|
|
def data_hash
|
|
|
|
@data_hash ||= begin
|
|
|
|
return nil if data.blank?
|
2016-02-22 19:34:16 -05:00
|
|
|
|
2015-09-03 23:34:21 -04:00
|
|
|
parsed = JSON.parse(data)
|
|
|
|
return nil if parsed.blank?
|
|
|
|
|
|
|
|
parsed.with_indifferent_access
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
2016-02-01 13:12:10 -05:00
|
|
|
topic.relative_url(post_number) if topic.present?
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def post
|
2013-02-28 13:54:12 -05:00
|
|
|
return if topic_id.blank? || post_number.blank?
|
2014-05-06 09:41:59 -04:00
|
|
|
Post.find_by(topic_id: topic_id, post_number: post_number)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-16 01:03:03 -04:00
|
|
|
|
2014-02-13 01:12:17 -05:00
|
|
|
def self.recent_report(user, count = nil)
|
2016-03-05 17:21:38 -05:00
|
|
|
return unless user && user.user_option
|
|
|
|
|
2014-02-13 15:20:56 -05:00
|
|
|
count ||= 10
|
2015-02-18 20:40:00 -05:00
|
|
|
notifications = user.notifications
|
|
|
|
.visible
|
|
|
|
.recent(count)
|
|
|
|
.includes(:topic)
|
2016-03-05 17:21:38 -05:00
|
|
|
|
|
|
|
if user.user_option.like_notification_frequency == UserOption.like_notification_frequency_type[:never]
|
|
|
|
notifications = notifications.where('notification_type <> ?', Notification.types[:liked])
|
|
|
|
end
|
|
|
|
|
|
|
|
notifications = notifications.to_a
|
2014-02-13 01:12:17 -05:00
|
|
|
|
|
|
|
if notifications.present?
|
2016-02-15 03:29:35 -05:00
|
|
|
|
|
|
|
ids = Notification.exec_sql("
|
|
|
|
SELECT n.id FROM notifications n
|
|
|
|
WHERE
|
|
|
|
n.notification_type = 6 AND
|
|
|
|
n.user_id = #{user.id.to_i} AND
|
|
|
|
NOT read
|
|
|
|
ORDER BY n.id ASC
|
|
|
|
LIMIT #{count.to_i}
|
|
|
|
").values.map do |x,_|
|
|
|
|
x.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
if ids.length > 0
|
|
|
|
notifications += user
|
|
|
|
.notifications
|
|
|
|
.order('notifications.created_at DESC')
|
|
|
|
.where(id: ids)
|
|
|
|
.joins(:topic)
|
|
|
|
.limit(count)
|
|
|
|
end
|
|
|
|
|
|
|
|
notifications.uniq(&:id).sort do |x,y|
|
2014-02-13 01:27:35 -05:00
|
|
|
if x.unread_pm? && !y.unread_pm?
|
|
|
|
-1
|
|
|
|
elsif y.unread_pm? && !x.unread_pm?
|
|
|
|
1
|
|
|
|
else
|
|
|
|
y.created_at <=> x.created_at
|
|
|
|
end
|
|
|
|
end.take(count)
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2014-02-13 01:12:17 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def unread_pm?
|
|
|
|
Notification.types[:private_message] == self.notification_type && !read
|
|
|
|
end
|
2013-05-16 01:03:03 -04:00
|
|
|
|
2016-01-26 20:19:49 -05:00
|
|
|
def post_id
|
|
|
|
Post.where(topic: topic_id, post_number: post_number).pluck(:id).first
|
|
|
|
end
|
|
|
|
|
2013-05-16 01:03:03 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def refresh_notification_count
|
2014-05-22 21:34:34 -04:00
|
|
|
user.publish_notifications_state
|
2013-05-16 01:03:03 -04:00
|
|
|
end
|
|
|
|
|
2016-12-21 23:29:34 -05:00
|
|
|
def send_email
|
2017-06-12 03:41:39 -04:00
|
|
|
NotificationEmailer.process_notification(self) if !skip_send_email
|
2016-12-21 23:29:34 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-23 22:48:32 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: notifications
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# notification_type :integer not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# data :string(1000) not null
|
|
|
|
# read :boolean default(FALSE), not null
|
2014-08-27 01:19:25 -04:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2013-05-23 22:48:32 -04:00
|
|
|
# topic_id :integer
|
|
|
|
# post_number :integer
|
|
|
|
# post_action_id :integer
|
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2015-09-17 20:41:10 -04:00
|
|
|
# idx_notifications_speedup_unread_count (user_id,notification_type)
|
|
|
|
# index_notifications_on_post_action_id (post_action_id)
|
|
|
|
# index_notifications_on_user_id_and_created_at (user_id,created_at)
|
2016-02-22 18:33:53 -05:00
|
|
|
# index_notifications_on_user_id_and_id (user_id,id) UNIQUE
|
2015-09-17 20:41:10 -04:00
|
|
|
# index_notifications_on_user_id_and_topic_id_and_post_number (user_id,topic_id,post_number)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|