favourites_controller.rb 850 Bytes
Newer Older
1 2 3 4 5
# frozen_string_literal: true

class Api::V1::Statuses::FavouritesController < Api::BaseController
  include Authorization

6
  before_action -> { doorkeeper_authorize! :write, :'write:favourites' }
7
  before_action :require_user!
8
  before_action :set_status
9 10 11 12

  respond_to :json

  def create
13
    FavouriteService.new.call(current_account, @status)
14
    render json: @status, serializer: REST::StatusSerializer
15 16 17
  end

  def destroy
18 19
    UnfavouriteWorker.perform_async(current_account.id, @status.id)
    render json: @status, serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new([@status], current_account.id, favourites_map: { @status.id => false })
20 21 22 23
  end

  private

24 25 26 27 28
  def set_status
    @status = Status.find(params[:status_id])
    authorize @status, :show?
  rescue Mastodon::NotPermittedError
    not_found
29 30
  end
end