2019-12-10 23:04:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class BookmarksController < ApplicationController
|
|
|
|
requires_login
|
|
|
|
|
|
|
|
def create
|
2022-03-29 22:43:11 -04:00
|
|
|
if SiteSetting.use_polymorphic_bookmarks
|
|
|
|
params.require(:bookmarkable_id)
|
|
|
|
params.require(:bookmarkable_type)
|
|
|
|
else
|
|
|
|
params.require(:post_id)
|
|
|
|
end
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2021-01-18 17:53:49 -05:00
|
|
|
RateLimiter.new(
|
|
|
|
current_user, "create_bookmark", SiteSetting.max_bookmarks_per_day, 1.day.to_i
|
|
|
|
).performed!
|
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
bookmark_manager = BookmarkManager.new(current_user)
|
2022-03-29 22:43:11 -04:00
|
|
|
|
|
|
|
create_params = {
|
2019-12-10 23:04:02 -05:00
|
|
|
name: params[:name],
|
2020-05-06 23:37:39 -04:00
|
|
|
reminder_at: params[:reminder_at],
|
|
|
|
options: {
|
2020-07-20 20:00:39 -04:00
|
|
|
auto_delete_preference: params[:auto_delete_preference] || 0
|
2020-05-06 23:37:39 -04:00
|
|
|
}
|
2022-03-29 22:43:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if SiteSetting.use_polymorphic_bookmarks
|
|
|
|
bookmark = bookmark_manager.create_for(
|
|
|
|
**create_params.merge(
|
|
|
|
bookmarkable_id: params[:bookmarkable_id],
|
|
|
|
bookmarkable_type: params[:bookmarkable_type]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
bookmark = bookmark_manager.create(
|
|
|
|
**create_params.merge(
|
|
|
|
post_id: params[:post_id],
|
|
|
|
for_topic: params[:for_topic] == "true",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
end
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2020-03-11 20:16:00 -04:00
|
|
|
if bookmark_manager.errors.empty?
|
2020-04-19 23:30:04 -04:00
|
|
|
return render json: success_json.merge(id: bookmark.id)
|
2020-03-11 20:16:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
render json: failed_json.merge(errors: bookmark_manager.errors.full_messages), status: 400
|
2019-12-10 23:04:02 -05:00
|
|
|
end
|
2020-02-13 01:26:02 -05:00
|
|
|
|
|
|
|
def destroy
|
|
|
|
params.require(:id)
|
2022-03-29 22:43:11 -04: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 01:26:02 -05:00
|
|
|
end
|
2020-04-16 21:08:07 -04:00
|
|
|
|
|
|
|
def update
|
|
|
|
params.require(:id)
|
|
|
|
|
|
|
|
bookmark_manager = BookmarkManager.new(current_user)
|
|
|
|
bookmark_manager.update(
|
|
|
|
bookmark_id: params[:id],
|
|
|
|
name: params[:name],
|
2020-05-06 23:37:39 -04:00
|
|
|
reminder_at: params[:reminder_at],
|
|
|
|
options: {
|
2020-07-20 20:00:39 -04:00
|
|
|
auto_delete_preference: params[:auto_delete_preference] || 0
|
2020-05-06 23:37:39 -04:00
|
|
|
}
|
2020-04-16 21:08:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2021-03-21 19:50:22 -04:00
|
|
|
|
|
|
|
def toggle_pin
|
|
|
|
params.require(:bookmark_id)
|
|
|
|
|
|
|
|
bookmark_manager = BookmarkManager.new(current_user)
|
|
|
|
bookmark_manager.toggle_pin(bookmark_id: params[:bookmark_id])
|
|
|
|
|
|
|
|
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
|
2019-12-10 23:04:02 -05:00
|
|
|
end
|