discourse/app/controllers/bookmarks_controller.rb
Martin Brennan e1e74abd4f
FEATURE: Improving bookmarks part 2 -- Topic Bookmarking (#8954)
### UI Changes

If `SiteSetting.enable_bookmarks_with_reminders` is enabled:

* Clicking "Bookmark" on a topic will create a new Bookmark record instead of a post + user action
* Clicking "Clear Bookmarks" on a topic will delete all the new Bookmark records on a topic
* The topic bookmark buttons control the post bookmark flags correctly and vice-versa
Disabled selecting the "reminder type" for bookmarks in the UI because the backend functionality is not done yet (of sending users notifications etc.)

### Other Changes

* Added delete bookmark route (but no UI yet)
* Added a rake task to sync the old PostAction bookmarks to the new Bookmark table, which can be run as many times as we want for a site (it will not create duplicates).
2020-02-13 16:26:02 +10:00

39 lines
1.1 KiB
Ruby

# frozen_string_literal: true
class BookmarksController < ApplicationController
requires_login
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,
post_id: params[:post_id],
name: params[:name],
reminder_type: Bookmark.reminder_types[params[:reminder_type].to_sym],
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
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
render json: success_json
end
end