bookmarks_controller.rb 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::BookmarksController < Api::BaseController
  3. include Authorization
  4. before_action -> { doorkeeper_authorize! :write, :'write:bookmarks' }
  5. before_action :require_user!
  6. before_action :set_status, only: [:create]
  7. def create
  8. current_account.bookmarks.find_or_create_by!(account: current_account, status: @status)
  9. render json: @status, serializer: REST::StatusSerializer
  10. end
  11. def destroy
  12. bookmark = current_account.bookmarks.find_by(status_id: params[:status_id])
  13. if bookmark
  14. @status = bookmark.status
  15. else
  16. @status = Status.find(params[:status_id])
  17. authorize @status, :show?
  18. end
  19. bookmark&.destroy!
  20. render json: @status, serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new([@status], current_account.id, bookmarks_map: { @status.id => false })
  21. rescue Mastodon::NotPermittedError
  22. not_found
  23. end
  24. private
  25. def set_status
  26. @status = Status.find(params[:status_id])
  27. authorize @status, :show?
  28. rescue Mastodon::NotPermittedError
  29. not_found
  30. end
  31. end