favourites_controller.rb 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::FavouritesController < Api::BaseController
  3. include Authorization
  4. before_action -> { doorkeeper_authorize! :write, :'write:favourites' }
  5. before_action :require_user!
  6. before_action :set_status, only: [:create]
  7. def create
  8. FavouriteService.new.call(current_account, @status)
  9. render json: @status, serializer: REST::StatusSerializer
  10. end
  11. def destroy
  12. fav = current_account.favourites.find_by(status_id: params[:status_id])
  13. if fav
  14. @status = fav.status
  15. UnfavouriteWorker.perform_async(current_account.id, @status.id)
  16. else
  17. @status = Status.find(params[:status_id])
  18. authorize @status, :show?
  19. end
  20. render json: @status, serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new([@status], current_account.id, favourites_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