2020-03-11 19:16:00 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarkReminderNotificationHandler
|
|
|
|
def self.send_notification(bookmark)
|
|
|
|
return if bookmark.blank?
|
|
|
|
Bookmark.transaction do
|
2020-03-12 01:10:56 -05:00
|
|
|
if bookmark.post.blank? || bookmark.post.deleted_at.present?
|
2020-03-11 19:16:00 -05:00
|
|
|
return clear_reminder(bookmark)
|
|
|
|
end
|
2020-08-30 18:15:36 -05:00
|
|
|
return unless bookmark.topic
|
2020-03-11 19:16:00 -05:00
|
|
|
|
|
|
|
create_notification(bookmark)
|
2020-05-06 22:37:39 -05:00
|
|
|
|
2020-07-29 02:02:36 -05:00
|
|
|
if bookmark.auto_delete_when_reminder_sent?
|
2020-07-28 18:43:32 -05:00
|
|
|
BookmarkManager.new(bookmark.user).destroy(bookmark.id)
|
2020-05-06 22:37:39 -05:00
|
|
|
end
|
|
|
|
|
2020-03-11 19:16:00 -05:00
|
|
|
clear_reminder(bookmark)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_reminder(bookmark)
|
|
|
|
Rails.logger.debug(
|
|
|
|
"Clearing bookmark reminder for bookmark_id #{bookmark.id}. reminder info: #{bookmark.reminder_at} | #{Bookmark.reminder_types[bookmark.reminder_type]}"
|
|
|
|
)
|
|
|
|
|
2020-07-28 18:43:32 -05:00
|
|
|
bookmark.clear_reminder!
|
2020-03-11 19:16:00 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_notification(bookmark)
|
|
|
|
user = bookmark.user
|
|
|
|
user.notifications.create!(
|
|
|
|
notification_type: Notification.types[:bookmark_reminder],
|
|
|
|
topic_id: bookmark.topic_id,
|
|
|
|
post_number: bookmark.post.post_number,
|
|
|
|
data: {
|
|
|
|
topic_title: bookmark.topic.title,
|
|
|
|
display_username: user.username,
|
|
|
|
bookmark_name: bookmark.name
|
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|