DEV: Add save_user_preferences option to BookmarkManager (#16894)

When saving / creating bookmarks, we have code to save
the user's preference of bookmark_auto_delete_preference
to their user_options.

Unfortunately this can cause weirdness when plugins
have code using BookmarkManager to set the auto delete preference for
only a specific bookmark.

This commit introduces a save_user_preferences option (false
by default) so that this user preference is not saved unless
specified by the consumer of BookmarkManager, so plugins will
not have to worry about it.
This commit is contained in:
Martin Brennan
2022-05-24 11:13:21 +10:00
committed by GitHub
parent 0553788d3b
commit 1ee973e6e2
3 changed files with 64 additions and 21 deletions

View File

@ -6,26 +6,22 @@ class BookmarksController < ApplicationController
def create
params.require(:bookmarkable_id)
params.require(:bookmarkable_type)
params.permit(:bookmarkable_id, :bookmarkable_type, :name, :reminder_at, :auto_delete_preference)
RateLimiter.new(
current_user, "create_bookmark", SiteSetting.max_bookmarks_per_day, 1.day.to_i
).performed!
bookmark_manager = BookmarkManager.new(current_user)
create_params = {
bookmark = bookmark_manager.create_for(
bookmarkable_id: params[:bookmarkable_id],
bookmarkable_type: params[:bookmarkable_type],
name: params[:name],
reminder_at: params[:reminder_at],
options: {
auto_delete_preference: params[:auto_delete_preference] || 0
auto_delete_preference: params[:auto_delete_preference],
save_user_preferences: true
}
}
bookmark = bookmark_manager.create_for(
**create_params.merge(
bookmarkable_id: params[:bookmarkable_id],
bookmarkable_type: params[:bookmarkable_type]
)
)
if bookmark_manager.errors.empty?
@ -43,6 +39,7 @@ class BookmarksController < ApplicationController
def update
params.require(:id)
params.permit(:id, :name, :reminder_at, :auto_delete_preference)
bookmark_manager = BookmarkManager.new(current_user)
bookmark_manager.update(
@ -50,7 +47,8 @@ class BookmarksController < ApplicationController
name: params[:name],
reminder_at: params[:reminder_at],
options: {
auto_delete_preference: params[:auto_delete_preference] || 0
auto_delete_preference: params[:auto_delete_preference],
save_user_preferences: true
}
)