FEATURE: Send notifications for time-based and At Desktop bookmark reminders (#9071)

* This PR implements the scheduling and notification system for bookmark reminders. Every 5 minutes a schedule runs to check any reminders that need to be sent before now, limited to **300** reminders at a time. Any leftover reminders will be sent in the next run. This is to avoid having to deal with fickle sidekiq and reminders in the far-flung future, which would necessitate having a background job anyway to clean up any missing `enqueue_at` reminders.

* If a reminder is sent its `reminder_at` time is cleared and the `reminder_last_sent_at` time is filled in. Notifications are only user-level notifications for now.

* All JavaScript and frontend code related to displaying the bookmark reminder notification is contained here. The reminder functionality is now re-enabled in the bookmark modal as well.

* This PR also implements the "Remind me next time I am at my desktop" bookmark reminder functionality. When the user is on a mobile device they are able to select this option. When they choose this option we set a key in Redis saying they have a pending at desktop reminder. The next time they change devices we check if the new device is desktop, and if it is we send reminders using a DistributedMutex. There is also a job to ensure consistency of these reminders in Redis (in case Redis drops the ball) and the at desktop reminders expire after 20 days.

* Also in this PR is a fix to delete all Bookmarks for a user via `UserDestroyer`
This commit is contained in:
Martin Brennan
2020-03-12 10:16:00 +10:00
committed by GitHub
parent b9aaa9718d
commit 793f39139a
31 changed files with 860 additions and 60 deletions

View File

@ -6,33 +6,24 @@ class BookmarksController < ApplicationController
def create
params.require(:post_id)
existing_bookmark = Bookmark.find_by(post_id: params[:post_id], user_id: current_user.id)
if existing_bookmark.present?
return render json: failed_json.merge(errors: [I18n.t("bookmarks.errors.already_bookmarked_post")]), status: 422
end
bookmark = Bookmark.create(
user_id: current_user.id,
topic_id: Post.select(:topic_id).find(params[:post_id]).topic_id,
bookmark_manager = BookmarkManager.new(current_user)
bookmark = bookmark_manager.create(
post_id: params[:post_id],
name: params[:name],
reminder_type: Bookmark.reminder_types[params[:reminder_type].to_sym],
reminder_type: params[:reminder_type],
reminder_at: params[:reminder_at]
)
return render json: success_json if bookmark.save
render json: failed_json.merge(errors: bookmark.errors.full_messages), status: 400
if bookmark_manager.errors.empty?
return render json: success_json
end
render json: failed_json.merge(errors: bookmark_manager.errors.full_messages), status: 400
end
def destroy
params.require(:id)
bookmark = Bookmark.find_by(id: params[:id])
raise Discourse::NotFound if bookmark.blank?
raise Discourse::InvalidAccess.new if !guardian.can_delete?(bookmark)
bookmark.destroy
BookmarkManager.new(current_user).destroy(params[:id])
render json: success_json
end
end

View File

@ -487,7 +487,7 @@ class TopicsController < ApplicationController
topic = Topic.find(params[:topic_id].to_i)
if SiteSetting.enable_bookmarks_with_reminders?
Bookmark.where(user_id: current_user.id, topic_id: topic.id).destroy_all
BookmarkManager.new(current_user).destroy_for_topic(topic)
else
PostAction.joins(:post)
.where(user_id: current_user.id)
@ -548,10 +548,12 @@ class TopicsController < ApplicationController
first_post = topic.ordered_posts.first
if SiteSetting.enable_bookmarks_with_reminders?
if Bookmark.exists?(user: current_user, post: first_post)
return render_json_error(I18n.t("bookmark.topic_already_bookmarked"), status: 403)
bookmark_manager = BookmarkManager.new(current_user)
bookmark_manager.create(post_id: first_post.id)
if bookmark_manager.errors
return render_json_error(bookmark_manager, status: 400)
end
Bookmark.create(user: current_user, post: first_post, topic: topic)
else
result = PostActionCreator.create(current_user, first_post, :bookmark)
return render_json_error(result) if result.failed?