Class: PostActionNotifier
- Inherits:
-
Object
- Object
- PostActionNotifier
- Defined in:
- app/services/post_action_notifier.rb
Class Method Summary collapse
- .add_post_revision_notifier_recipients(&block) ⇒ Object
- .after_create_post_revision(post_revision) ⇒ Object
- .after_post_unhide(post, flaggers) ⇒ Object
- .alerter ⇒ Object
- .custom_post_revision_notifier_recipients ⇒ Object
- .disable ⇒ Object
- .enable ⇒ Object
- .post_action_created(post_action) ⇒ Object
- .post_action_deleted(post_action) ⇒ Object
- .refresh_like_notification(post, read) ⇒ Object
-
.reset! ⇒ Object
For testing purposes.
Class Method Details
.add_post_revision_notifier_recipients(&block) ⇒ Object
156 157 158 |
# File 'app/services/post_action_notifier.rb', line 156 def self.add_post_revision_notifier_recipients(&block) custom_post_revision_notifier_recipients << block end |
.after_create_post_revision(post_revision) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'app/services/post_action_notifier.rb', line 99 def self.after_create_post_revision(post_revision) return if @disabled post = post_revision.post return unless post return if post_revision.user.blank? return if post.topic.blank? return if post.topic. return if notification_is_disabled?(post_revision) user_ids = [] user_ids << post.user_id if post_revision.user_id != post.user_id # Notify all users watching the topic when the OP of a wiki topic is edited # or if the topic category allows unlimited owner edits on the OP. if post.is_first_post? && (post.wiki? || post.topic.category_allows_unlimited_owner_edits_on_first_post?) user_ids.concat( TopicUser .watching(post.topic_id) .where.not(user_id: post_revision.user_id) .where(topic: post.topic) .pluck(:user_id), ) end custom_post_revision_notifier_recipients.each do |block| user_ids.concat(Array(block.call(post_revision))) end if user_ids.present? DB.after_commit do Jobs.enqueue(:notify_post_revision, user_ids: user_ids, post_revision_id: post_revision.id) end end end |
.after_post_unhide(post, flaggers) ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'app/services/post_action_notifier.rb', line 138 def self.after_post_unhide(post, flaggers) return if @disabled || post.last_editor.blank? || flaggers.blank? flaggers.each do |flagger| alerter.create_notification( flagger, Notification.types[:edited], post, display_username: post.last_editor.username, acting_user_id: post.last_editor.id, ) end end |
.alerter ⇒ Object
17 18 19 |
# File 'app/services/post_action_notifier.rb', line 17 def self.alerter @alerter ||= PostAlerter.new end |
.custom_post_revision_notifier_recipients ⇒ Object
152 153 154 |
# File 'app/services/post_action_notifier.rb', line 152 def self.custom_post_revision_notifier_recipients @custom_post_revision_notifier_recipients ||= Set.new end |
.disable ⇒ Object
4 5 6 |
# File 'app/services/post_action_notifier.rb', line 4 def self.disable @disabled = true end |
.enable ⇒ Object
8 9 10 |
# File 'app/services/post_action_notifier.rb', line 8 def self.enable @disabled = false end |
.post_action_created(post_action) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/services/post_action_notifier.rb', line 80 def self.post_action_created(post_action) return if @disabled # We only notify on likes for now return unless post_action.is_like? post = post_action.post return if post_action.user.blank? || post.blank? alerter.create_notification( post.user, Notification.types[:liked], post, display_username: post_action.user.username, post_action_id: post_action.id, user_id: post_action.user_id, ) end |
.post_action_deleted(post_action) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/services/post_action_notifier.rb', line 52 def self.post_action_deleted(post_action) return if @disabled # We only care about deleting post actions for now return if post_action.deleted_at.blank? if post_action.post_action_type_id == PostActionType.types[:like] && post_action.post read = true Notification .where( topic_id: post_action.post.topic_id, user_id: post_action.post.user_id, post_number: post_action.post.post_number, notification_type: Notification.types[:liked], ) .each do |notification| read = false unless notification.read notification.destroy end refresh_like_notification(post_action.post, read) else # not using destroy_all cause we want stuff to trigger Notification.where(post_action_id: post_action.id).each(&:destroy) end end |
.refresh_like_notification(post, read) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/services/post_action_notifier.rb', line 21 def self.refresh_like_notification(post, read) return unless post && post.user_id && post.topic usernames = post .post_actions .where(post_action_type_id: PostActionType.types[:like]) .joins(:user) .order("post_actions.created_at desc") .where("post_actions.created_at > ?", 1.day.ago) .pluck(:username) if usernames.length > 0 data = { topic_title: post.topic.title, username: usernames[0], display_username: usernames[0], username2: usernames[1], count: usernames.length, } Notification.create( notification_type: Notification.types[:liked], topic_id: post.topic_id, post_number: post.post_number, user_id: post.user_id, read: read, data: data.to_json, ) end end |
.reset! ⇒ Object
For testing purposes
13 14 15 |
# File 'app/services/post_action_notifier.rb', line 13 def self.reset! @custom_post_revision_notifier_recipients = nil end |