mirror of
https://github.com/discourse/discourse.git
synced 2024-11-27 09:33:37 +08:00
0c42a1e5f3
Allows creating a bookmark with the `for_topic` flag introduced ind1d2298a4c
set to true. This happens when clicking on the Bookmark button in the topic footer when no other posts are bookmarked. In a later PR, when clicking on these topic-level bookmarks the user will be taken to the last unread post in the topic, not the OP. Only the OP can have a topic level bookmark, and users can also make a post-level bookmark on the OP of the topic. I had to do some pretty heavy refactors because most of the bookmark code in the JS topics controller was centred around instances of Post JS models, but the topic level bookmark is not centred around a post. Some refactors were just for readability as well. Also removes some missed reminderType code from the purge in41e19adb0d
70 lines
1.8 KiB
Ruby
70 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class BookmarksController < ApplicationController
|
|
requires_login
|
|
|
|
def create
|
|
params.require(:post_id)
|
|
|
|
RateLimiter.new(
|
|
current_user, "create_bookmark", SiteSetting.max_bookmarks_per_day, 1.day.to_i
|
|
).performed!
|
|
|
|
bookmark_manager = BookmarkManager.new(current_user)
|
|
bookmark = bookmark_manager.create(
|
|
post_id: params[:post_id],
|
|
name: params[:name],
|
|
reminder_at: params[:reminder_at],
|
|
for_topic: params[:for_topic] == "true",
|
|
options: {
|
|
auto_delete_preference: params[:auto_delete_preference] || 0
|
|
}
|
|
)
|
|
|
|
if bookmark_manager.errors.empty?
|
|
return render json: success_json.merge(id: bookmark.id)
|
|
end
|
|
|
|
render json: failed_json.merge(errors: bookmark_manager.errors.full_messages), status: 400
|
|
end
|
|
|
|
def destroy
|
|
params.require(:id)
|
|
result = BookmarkManager.new(current_user).destroy(params[:id])
|
|
render json: success_json.merge(result)
|
|
end
|
|
|
|
def update
|
|
params.require(:id)
|
|
|
|
bookmark_manager = BookmarkManager.new(current_user)
|
|
bookmark_manager.update(
|
|
bookmark_id: params[:id],
|
|
name: params[:name],
|
|
reminder_at: params[:reminder_at],
|
|
options: {
|
|
auto_delete_preference: params[:auto_delete_preference] || 0
|
|
}
|
|
)
|
|
|
|
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 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
|
|
end
|