2019-12-11 12:04:02 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarksController < ApplicationController
|
|
|
|
requires_login
|
|
|
|
|
|
|
|
def create
|
2022-05-23 08:07:15 +08:00
|
|
|
params.require(:bookmarkable_id)
|
|
|
|
params.require(:bookmarkable_type)
|
2022-05-24 09:13:21 +08:00
|
|
|
params.permit(
|
|
|
|
:bookmarkable_id,
|
|
|
|
:bookmarkable_type,
|
|
|
|
:name,
|
|
|
|
:reminder_at,
|
|
|
|
:auto_delete_preference,
|
|
|
|
)
|
2019-12-11 12:04:02 +08:00
|
|
|
|
2021-01-19 06:53:49 +08:00
|
|
|
RateLimiter.new(
|
|
|
|
current_user,
|
|
|
|
"create_bookmark",
|
|
|
|
SiteSetting.max_bookmarks_per_day,
|
|
|
|
1.day.to_i,
|
|
|
|
).performed!
|
|
|
|
|
2020-03-12 08:16:00 +08:00
|
|
|
bookmark_manager = BookmarkManager.new(current_user)
|
2022-05-24 09:13:21 +08:00
|
|
|
bookmark =
|
|
|
|
bookmark_manager.create_for(
|
|
|
|
bookmarkable_id: params[:bookmarkable_id],
|
|
|
|
bookmarkable_type: params[:bookmarkable_type],
|
2019-12-11 12:04:02 +08:00
|
|
|
name: params[:name],
|
2020-05-07 11:37:39 +08:00
|
|
|
reminder_at: params[:reminder_at],
|
|
|
|
options: {
|
2022-12-16 06:50:31 +08:00
|
|
|
auto_delete_preference: params[:auto_delete_preference],
|
2020-05-07 11:37:39 +08:00
|
|
|
},
|
2022-05-23 08:07:15 +08:00
|
|
|
)
|
2019-12-11 12:04:02 +08:00
|
|
|
|
2020-04-20 11:30:04 +08:00
|
|
|
return render json: success_json.merge(id: bookmark.id) if bookmark_manager.errors.empty?
|
2020-03-12 08:16:00 +08:00
|
|
|
|
|
|
|
render json: failed_json.merge(errors: bookmark_manager.errors.full_messages), status: 400
|
2019-12-11 12:04:02 +08:00
|
|
|
end
|
2020-02-13 14:26:02 +08:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
params.require(:id)
|
2022-03-30 10:43:11 +08:00
|
|
|
destroyed_bookmark = BookmarkManager.new(current_user).destroy(params[:id])
|
|
|
|
render json:
|
|
|
|
success_json.merge(BookmarkManager.bookmark_metadata(destroyed_bookmark, current_user))
|
2020-02-13 14:26:02 +08:00
|
|
|
end
|
2020-04-17 09:08:07 +08:00
|
|
|
|
|
|
|
def update
|
|
|
|
params.require(:id)
|
2022-05-24 09:13:21 +08:00
|
|
|
params.permit(:id, :name, :reminder_at, :auto_delete_preference)
|
2020-04-17 09:08:07 +08:00
|
|
|
|
|
|
|
bookmark_manager = BookmarkManager.new(current_user)
|
|
|
|
bookmark_manager.update(
|
|
|
|
bookmark_id: params[:id],
|
|
|
|
name: params[:name],
|
2020-05-07 11:37:39 +08:00
|
|
|
reminder_at: params[:reminder_at],
|
|
|
|
options: {
|
2022-12-16 06:50:31 +08:00
|
|
|
auto_delete_preference: params[:auto_delete_preference],
|
2020-05-07 11:37:39 +08:00
|
|
|
},
|
2020-04-17 09:08:07 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
return render json: success_json if bookmark_manager.errors.empty?
|
|
|
|
|
|
|
|
render json: failed_json.merge(errors: bookmark_manager.errors.full_messages), status: 400
|
|
|
|
end
|
2021-03-22 07:50:22 +08:00
|
|
|
|
|
|
|
def toggle_pin
|
|
|
|
params.require(:bookmark_id)
|
|
|
|
|
|
|
|
bookmark_manager = BookmarkManager.new(current_user)
|
|
|
|
bookmark_manager.toggle_pin(bookmark_id: params[:bookmark_id])
|
|
|
|
|
|
|
|
return render json: success_json if bookmark_manager.errors.empty?
|
|
|
|
|
|
|
|
render json: failed_json.merge(errors: bookmark_manager.errors.full_messages), status: 400
|
|
|
|
end
|
2024-05-22 23:50:21 +08:00
|
|
|
|
|
|
|
def bulk
|
|
|
|
if params[:bookmark_ids].present?
|
|
|
|
unless Array === params[:bookmark_ids]
|
|
|
|
raise Discourse::InvalidParameters.new(
|
|
|
|
"Expecting bookmark_ids to contain a list of bookmark ids",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
bookmark_ids = params[:bookmark_ids].map { |t| t.to_i }
|
|
|
|
else
|
|
|
|
raise ActionController::ParameterMissing.new(:bookmark_ids)
|
|
|
|
end
|
|
|
|
|
|
|
|
operation = params.require(:operation).permit(:type).to_h.symbolize_keys
|
|
|
|
|
|
|
|
raise ActionController::ParameterMissing.new(:operation_type) if operation[:type].blank?
|
|
|
|
operator = BookmarksBulkAction.new(current_user, bookmark_ids, operation)
|
|
|
|
changed_bookmark_ids = operator.perform!
|
|
|
|
render_json_dump bookmark_ids: changed_bookmark_ids
|
|
|
|
end
|
2019-12-11 12:04:02 +08:00
|
|
|
end
|