2017-06-10 16:39:26 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::Statuses::FavouritesController < Api::BaseController
|
|
|
|
include Authorization
|
|
|
|
|
2018-07-06 01:31:35 +09:00
|
|
|
before_action -> { doorkeeper_authorize! :write, :'write:favourites' }
|
2017-06-10 16:39:26 +09:00
|
|
|
before_action :require_user!
|
2020-11-21 14:18:09 +09:00
|
|
|
before_action :set_status, only: [:create]
|
2017-06-10 16:39:26 +09:00
|
|
|
|
|
|
|
def create
|
2020-02-27 20:32:54 +09:00
|
|
|
FavouriteService.new.call(current_account, @status)
|
2017-07-07 11:02:06 +09:00
|
|
|
render json: @status, serializer: REST::StatusSerializer
|
2017-06-10 16:39:26 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2020-11-21 14:18:09 +09:00
|
|
|
fav = current_account.favourites.find_by(status_id: params[:status_id])
|
|
|
|
|
|
|
|
if fav
|
|
|
|
@status = fav.status
|
|
|
|
UnfavouriteWorker.perform_async(current_account.id, @status.id)
|
|
|
|
else
|
|
|
|
@status = Status.find(params[:status_id])
|
|
|
|
authorize @status, :show?
|
|
|
|
end
|
|
|
|
|
2020-02-27 20:32:54 +09:00
|
|
|
render json: @status, serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new([@status], current_account.id, favourites_map: { @status.id => false })
|
2020-11-21 14:18:09 +09:00
|
|
|
rescue Mastodon::NotPermittedError
|
|
|
|
not_found
|
2017-06-10 16:39:26 +09:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-02-27 20:32:54 +09:00
|
|
|
def set_status
|
|
|
|
@status = Status.find(params[:status_id])
|
|
|
|
authorize @status, :show?
|
|
|
|
rescue Mastodon::NotPermittedError
|
|
|
|
not_found
|
2017-06-10 16:39:26 +09:00
|
|
|
end
|
|
|
|
end
|