2020-03-12 08:16:00 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarkReminderNotificationHandler
|
|
|
|
def self.send_notification(bookmark)
|
|
|
|
return if bookmark.blank?
|
|
|
|
Bookmark.transaction do
|
2021-09-15 08:16:54 +08:00
|
|
|
# we don't send reminders for deleted posts or topics,
|
|
|
|
# just as we don't allow creation of bookmarks for deleted
|
|
|
|
# posts or topics
|
|
|
|
if bookmark.post.blank? || bookmark.topic.blank?
|
2021-04-21 17:36:32 +08:00
|
|
|
clear_reminder(bookmark)
|
2021-09-15 08:16:54 +08:00
|
|
|
else
|
2021-04-21 17:36:32 +08:00
|
|
|
create_notification(bookmark)
|
2020-03-12 08:16:00 +08:00
|
|
|
|
2021-04-21 17:36:32 +08:00
|
|
|
if bookmark.auto_delete_when_reminder_sent?
|
|
|
|
BookmarkManager.new(bookmark.user).destroy(bookmark.id)
|
|
|
|
end
|
2020-05-07 11:37:39 +08:00
|
|
|
|
2021-04-21 17:36:32 +08:00
|
|
|
clear_reminder(bookmark)
|
2020-05-07 11:37:39 +08:00
|
|
|
end
|
2020-03-12 08:16:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_reminder(bookmark)
|
|
|
|
Rails.logger.debug(
|
2021-09-16 07:56:54 +08:00
|
|
|
"Clearing bookmark reminder for bookmark_id #{bookmark.id}. reminder at: #{bookmark.reminder_at}"
|
2020-03-12 08:16:00 +08:00
|
|
|
)
|
|
|
|
|
2020-07-29 07:43:32 +08:00
|
|
|
bookmark.clear_reminder!
|
2020-03-12 08:16:00 +08: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
|