about summary refs log tree commit diff
path: root/app/controllers/api/v1/statuses/publishing_controller.rb
blob: 5124b100926a71f8c4ba6ffbae8b467cdb3ed09e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# frozen_string_literal: true

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

  before_action -> { doorkeeper_authorize! :write, :'write:statuses:publish' }
  before_action :require_user!
  before_action :set_status

  def create
    @status.update!(published: true)

    LinkCrawlWorker.perform_in(rand(1..30).seconds, @status.id) unless @status.spoiler_text?
    DistributionWorker.perform_async(@status.id)
    ActivityPub::DistributionWorker.perform_async(@status.id) if @status.local? && !@status.local_only?

    mentions = @status.active_mentions.includes(:account).where(id: @new_mention_ids, accounts: { domain: nil })
    mentions.each { |mention| LocalNotificationWorker.perform_async(mention.account.id, mention.id, mention.class.name) }

    render json: @status,
           serializer: (@status.is_a?(ScheduledStatus) ? REST::ScheduledStatusSerializer : REST::StatusSerializer),
           source_requested: truthy_param?(:source)
  end

  private

  def set_status
    @status = Status.unpublished.find(params[:status_id])
    authorize @status, :destroy?
  rescue Mastodon::NotPermittedError
    not_found
  end
end