2017-06-10 09:39:26 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-11-13 15:53:22 +01:00
|
|
|
class Api::V1::Statuses::FavouritesController < Api::V1::Statuses::BaseController
|
2018-07-05 18:31:35 +02:00
|
|
|
before_action -> { doorkeeper_authorize! :write, :'write:favourites' }
|
2017-06-10 09:39:26 +02:00
|
|
|
before_action :require_user!
|
2023-11-13 15:53:22 +01:00
|
|
|
skip_before_action :set_status, only: [:destroy]
|
2017-06-10 09:39:26 +02:00
|
|
|
|
|
|
|
def create
|
2020-02-27 12:32:54 +01:00
|
|
|
FavouriteService.new.call(current_account, @status)
|
2017-07-07 04:02:06 +02:00
|
|
|
render json: @status, serializer: REST::StatusSerializer
|
2017-06-10 09:39:26 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2020-11-21 06:18:09 +01:00
|
|
|
fav = current_account.favourites.find_by(status_id: params[:status_id])
|
|
|
|
|
|
|
|
if fav
|
|
|
|
@status = fav.status
|
2023-07-19 09:02:30 +02:00
|
|
|
count = [@status.favourites_count - 1, 0].max
|
2020-11-21 06:18:09 +01:00
|
|
|
UnfavouriteWorker.perform_async(current_account.id, @status.id)
|
|
|
|
else
|
|
|
|
@status = Status.find(params[:status_id])
|
2023-07-19 09:02:30 +02:00
|
|
|
count = @status.favourites_count
|
2020-11-21 06:18:09 +01:00
|
|
|
authorize @status, :show?
|
|
|
|
end
|
|
|
|
|
2023-07-19 09:02:30 +02:00
|
|
|
relationships = StatusRelationshipsPresenter.new([@status], current_account.id, favourites_map: { @status.id => false }, attributes_map: { @status.id => { favourites_count: count } })
|
|
|
|
render json: @status, serializer: REST::StatusSerializer, relationships: relationships
|
2020-11-21 06:18:09 +01:00
|
|
|
rescue Mastodon::NotPermittedError
|
|
|
|
not_found
|
2017-06-10 09:39:26 +02:00
|
|
|
end
|
|
|
|
end
|