2020-03-11 20:16:00 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarkReminderNotificationHandler
|
2022-05-08 19:37:23 -04:00
|
|
|
attr_reader :bookmark
|
|
|
|
|
|
|
|
def initialize(bookmark)
|
|
|
|
@bookmark = bookmark
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_notification
|
2020-03-11 20:16:00 -04:00
|
|
|
return if bookmark.blank?
|
|
|
|
Bookmark.transaction do
|
2022-05-08 19:37:23 -04:00
|
|
|
# TODO (martin) [POLYBOOK] Can probably change this to call the
|
|
|
|
# can_send_reminder? on the registered bookmarkable directly instead
|
|
|
|
# of having can_send_reminder?
|
|
|
|
if !can_send_reminder?
|
|
|
|
clear_reminder
|
2021-09-14 20:16:54 -04:00
|
|
|
else
|
2022-05-08 19:37:23 -04:00
|
|
|
create_notification
|
2020-03-11 20:16:00 -04:00
|
|
|
|
2021-04-21 05:36:32 -04:00
|
|
|
if bookmark.auto_delete_when_reminder_sent?
|
|
|
|
BookmarkManager.new(bookmark.user).destroy(bookmark.id)
|
|
|
|
end
|
2020-05-06 23:37:39 -04:00
|
|
|
|
2022-05-08 19:37:23 -04:00
|
|
|
clear_reminder
|
2020-05-06 23:37:39 -04:00
|
|
|
end
|
2020-03-11 20:16:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-08 19:37:23 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def clear_reminder
|
2020-03-11 20:16:00 -04:00
|
|
|
Rails.logger.debug(
|
2021-09-15 19:56:54 -04:00
|
|
|
"Clearing bookmark reminder for bookmark_id #{bookmark.id}. reminder at: #{bookmark.reminder_at}"
|
2020-03-11 20:16:00 -04:00
|
|
|
)
|
|
|
|
|
2022-03-08 12:44:18 -05:00
|
|
|
if bookmark.auto_clear_reminder_when_reminder_sent?
|
|
|
|
bookmark.reminder_at = nil
|
|
|
|
end
|
|
|
|
|
2020-07-28 19:43:32 -04:00
|
|
|
bookmark.clear_reminder!
|
2020-03-11 20:16:00 -04:00
|
|
|
end
|
|
|
|
|
2022-05-08 19:37:23 -04:00
|
|
|
def can_send_reminder?
|
|
|
|
if SiteSetting.use_polymorphic_bookmarks
|
|
|
|
bookmark.registered_bookmarkable.can_send_reminder?(bookmark)
|
|
|
|
else
|
|
|
|
bookmark.post.present? && bookmark.topic.present?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_notification
|
|
|
|
if SiteSetting.use_polymorphic_bookmarks
|
|
|
|
bookmark.registered_bookmarkable.send_reminder_notification(bookmark)
|
|
|
|
else
|
|
|
|
bookmark.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: bookmark.user.username,
|
|
|
|
bookmark_name: bookmark.name
|
|
|
|
}.to_json
|
|
|
|
)
|
|
|
|
end
|
2020-03-11 20:16:00 -04:00
|
|
|
end
|
|
|
|
end
|