about summary refs log tree commit diff
path: root/app/services/post_status_service.rb
blob: c9e81808de72f72e5cd6438a9bb73ea0ed4c9790 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# frozen_string_literal: true

class PostStatusService < BaseService
  include Redisable

  MIN_SCHEDULE_OFFSET = 5.minutes.freeze
  MIN_DESTRUCT_OFFSET = 30.seconds.freeze

  VISIBILITY_RANK = {
    'public'    => 0,
    'unlisted'  => 1,
    'local'     => 1,
    'private'   => 2,
    'direct'    => 3,
    'limited'   => 3,
  }

  # Post a text status update, fetch and notify remote users mentioned
  # @param [Account] account Account from which to post
  # @param [Hash] options
  # @option [String] :text Message
  # @option [Status] :thread Optional status to reply to
  # @option [Tag] :tags Optional tags to include
  # @option [Time] :created_at Optional time which status was originally posted
  # @option [Boolean] :sensitive
  # @option [String] :visibility
  # @option [Boolean] :hidden
  # @option [Boolean] :local_only
  # @option [String] :sharekey
  # @option [String] :spoiler_text
  # @option [String] :language
  # @option [String] :scheduled_at
  # @option [String] :delete_after
  # @option [String] :defederate_after
  # @option [Account] :mentions Optional accounts to mention out-of-body
  # @option [Boolean] :noreplies Author does not accept replies
  # @option [Boolean] :nocrawl Optional skip link card generation
  # @option [Boolean] :nomentions Optional skip mention processing
  # @option [Boolean] :delayed Optional publishing delay of 30 secs
  # @option [Hash] :poll Optional poll to attach
  # @option [Enumerable] :media_ids Optional array of media IDs to attach
  # @option [Doorkeeper::Application] :application
  # @option [String] :idempotency Optional idempotency key
  # @return [Status]
  def call(account, options = {})
    @account     = account
    @options     = options
    @text        = @options[:text] || ''
    @footer      = @options[:footer]
    @in_reply_to = @options[:thread]
    @tags        = @options[:tags] || []
    @local_only  = @options[:local_only]
    @sensitive   = (@account.force_sensitive? ? true : @options[:sensitive])
    @hidden      = @options[:hidden]

    @preloaded_tags = @options[:preloaded_tags] || []
    @preloaded_mentions = @options[:preloaded_mentions] || []

    @is_delayed = @options[:delayed].present? || @account.user.delayed_roars?
    @delay_for = @is_delayed ? [5, @account.user.delayed_for].max : 1
    @delay_until = Time.now.utc + @delay_for.seconds

    raise Mastodon::LengthValidationError, I18n.t('statuses.replies_rejected') if recipient_rejects_replies?
    raise Mastodon::LengthValidationError, I18n.t('statuses.kicked') if kicked?

    return idempotency_duplicate if idempotency_given? && idempotency_duplicate?

    validate_media!
    preprocess_attributes!

    if scheduled?
      schedule_status!
    else
      return unless process_status!

      opts = {
        visibility: @visibility,
        local_only: @local_only,
        federate: @options[:federate],
        distribute: @options[:distribute],
        nocrawl: @options[:nocrawl],
        reject_replies: @options[:noreplies] || false,
        hidden: @hidden,
      }.compact

      PostStatusWorker.perform_at(@delay_until, @status.id, opts)
      DistributionWorker.perform_async(@status.id, delayed = true) unless @options[:distribute] == false
    end

    redis.setex(idempotency_key, 3_600, @status.id) if idempotency_given?

    @status
  end

  private

  def recipient_rejects_replies?
    @in_reply_to.present? && @in_reply_to.reject_replies && @in_reply_to.account_id != @account.id
  end

  def kicked?
    @in_reply_to.present? && ConversationKick.where(account_id: @account.id, conversation_id: @in_reply_to.conversation_id).exists?
  end

  def mark_recipient_known
    @in_reply_to.account.mark_known! if @in_reply_to.account.can_be_marked_known? && Setting.mark_known_from_mentions
    raise Mastodon::NotPermittedError("Account @#{@in_reply_to.account.acct} is restricted by an admin policy.") unless @in_reply_to.account.known?
  end

  def set_footer_from_i_am
    return if @footer.present? || @options[:no_footer]
    name = @account.user.vars['_they:are']
    return if name.blank?
    @footer = @account.user.vars["_they:are:#{name}"]
  end

  def set_initial_visibility
    @visibility = @options[:visibility] || @account.user_default_visibility
  end

  def limit_visibility_if_silenced
    @visibility = :unlisted if @visibility.in?([nil, 'public', 'local']) && @account.silenced? || @account.force_unlisted
  end

  def limit_visibility_to_reply
    return if @in_reply_to.visibility.nil?

    @visibility = @in_reply_to.visibility if @visibility.nil? ||
      VISIBILITY_RANK[@visibility] < VISIBILITY_RANK[@in_reply_to.visibility]
  end

  def limit_visibility_if_draft
    if @tags.include?('self.draft') || @preloaded_tags.include?('self.draft')
      @hidden = true
    end
  end

  def unfilter_thread_on_reply
    ConversationKick.where(account_id: @in_reply_to.account_id, conversation: @in_reply_to.conversation_id).destroy_all
  end

  def inherit_reply_rejection
    return unless @in_reply_to.reject_replies && @in_reply_to.account_id == @account.id
    @options[:noreplies] = true
  end

  def set_local_only
    @local_only = true if @account.user_always_local_only? || @in_reply_to&.local_only
  end

  # move tags out of body so we can format them later
  def extract_tags
    return unless '#'.in?(@text)
    @text.gsub!(/^##/, "\ufdd6")
    @text.gsub!('##', "\ufdd9")
    @tags |= Extractor.extract_hashtags(@text)
    @text.strip!
    @text.gsub!(/^(?:#[\w:._·\-]+\s*)+$/, '')
    @text.strip!
    @text.gsub!("\ufdd9", "##")
    @text.gsub!("\ufdd6", "#")
  end

  def protect_leading_spaces
    @text.gsub!(/^ /, "\u200b ")
  end

  def preprocess_attributes!
    if @text.blank? && @options[:spoiler_text].present?
     @text = '.'
     @text = @media.find(&:video?) ? '📹' : '🖼' if @media.size > 0
    end

    @text = @text.dup if @text.frozen?

    set_footer_from_i_am
    extract_tags
    protect_leading_spaces
    set_local_only
    set_initial_visibility
    limit_visibility_if_silenced
    limit_visibility_if_draft

    unless @in_reply_to.nil?
      mark_recipient_known
      inherit_reply_rejection
      limit_visibility_to_reply
      unfilter_thread_on_reply
    end

    @text.freeze

    @sensitive = (@account.user_defaults_to_sensitive? || @options[:spoiler_text].present?) if @sensitive.nil?

    @scheduled_at = @options[:scheduled_at]&.to_datetime
    @scheduled_at = nil if scheduled_in_the_past?

    case @options[:delete_after].class
    when ActiveSupport::Duration
      @delete_after = @options[:delete_after]
    when Integer
      @delete_after = @options[:delete_after].minutes
    when Float
      @delete_after = @options[:delete_after].minutes
    else
      @delete_after = @account.user.roar_lifespan.days unless @account.user.roar_lifespan == 0
    end
    @delete_after = MIN_DESTRUCT_OFFSET if @delete_after.present? && (@delete_after < MIN_DESTRUCT_OFFSET)
    @delete_after += @delay_for.seconds if @delete_after && @is_delayed

    case @options[:defederate_after].class
    when ActiveSupport::Duration
      @defederate_after = @options[:defederate_after]
    when Integer
      @defederate_after = @options[:defederate_after].minutes
    when Float
      @defederate_after = @options[:defederate_after].minutes
    else
      @defederate_after = @account.user.roar_defederate.days unless @account.user.roar_defederate == 0
    end
    @defederate_after = MIN_DESTRUCT_OFFSET if @defederate_after.present? && (@defederate_after < MIN_DESTRUCT_OFFSET)
    @defederate_after += @delay_for.seconds if @defederate_after && @is_delayed
  rescue ArgumentError
    raise ActiveRecord::RecordInvalid
  end

  def process_status!
    # The following transaction block is needed to wrap the UPDATEs to
    # the media attachments when the status is created

    ApplicationRecord.transaction do
      @status = @account.statuses.create!(status_attributes)
    end

    process_bangtags

    return false if @status.destroyed?

    @hidden = @status.keep_hidden? if @hidden.blank?

    set_expirations
    process_hashtags_service.call(@status, @tags, @preloaded_tags)
    process_mentions
    true
  end

  def schedule_status!
    status_for_validation = @account.statuses.build(status_attributes)

    if status_for_validation.valid?
      status_for_validation.destroy

      # The following transaction block is needed to wrap the UPDATEs to
      # the media attachments when the scheduled status is created

      ApplicationRecord.transaction do
        @status = @account.scheduled_statuses.create!(scheduled_status_attributes)
      end
    else
      raise ActiveRecord::RecordInvalid
    end
  end

  def postprocess_status!
    LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
    DistributionWorker.perform_async(@status.id)
    ActivityPub::DistributionWorker.perform_async(@status.id) unless @status.local_only?
    PollExpirationNotifyWorker.perform_at(@status.poll.expires_at, @status.poll.id) if @status.poll
  end

  def validate_media!
    return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)

    raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 6 || @options[:poll].present?

    @media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(6).map(&:to_i))

    raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:audio_or_video?)
  end

  def process_bangtags
    Bangtags.new(@status).process
  end

  def process_mentions
    if @options[:mentions].present?
      @options[:mentions].each do |mentioned_account|
        mentioned_account.mentions.where(status: @status).first_or_create(status: @status)
      end
    end

    process_mentions_service.call(@status, skip_notify: true) unless @options[:nomentions]
  end

  def language_from_option(str)
    ISO_639.find(str)&.alpha2
  end

  def set_expirations
    return if @status.no_clobber_expirations?
    @status.delete_after = @delete_after if @delete_after && @status.delete_after.nil?
    @status.defederate_after = @defederate_after if @defederate_after && @status.defederate_after.nil?
  end

  def process_hashtags_service
    ProcessHashtagsService.new
  end

  def process_mentions_service
    ProcessMentionsService.new
  end

  def scheduled?
    @scheduled_at.present?
  end

  def idempotency_key
    "idempotency:status:#{@account.id}:#{@options[:idempotency]}"
  end

  def idempotency_given?
    @options[:idempotency].present?
  end

  def idempotency_duplicate
    if scheduled?
      @account.schedule_statuses.find(@idempotency_duplicate)
    else
      @account.statuses.find(@idempotency_duplicate)
    end
  end

  def idempotency_duplicate?
    @idempotency_duplicate = redis.get(idempotency_key)
  end

  def scheduled_in_the_past?
    @scheduled_at.present? && @scheduled_at <= Time.now.utc + MIN_SCHEDULE_OFFSET
  end

  def status_attributes
    {
      created_at: @options[:created_at] ? @options[:created_at].to_datetime.utc : Time.now.utc,
      text: @text,
      footer: @footer,
      media_attachments: @media || [],
      thread: @in_reply_to,
      poll_attributes: poll_attributes,
      sensitive: @sensitive,
      spoiler_text: @options[:spoiler_text] || '',
      visibility: @visibility,
      local_only: @local_only,
      reject_replies: @options[:noreplies] || false,
      sharekey: @options[:sharekey],
      language: language_from_option(@options[:language]) || @account.user_default_language&.presence || 'en',
      application: @options[:application],
      content_type: @options[:content_type] || @account.user&.setting_default_content_type,
      hidden: true,
    }.compact
  end

  def scheduled_status_attributes
    {
      scheduled_at: @scheduled_at,
      media_attachments: @media || [],
      params: scheduled_options,
    }
  end

  def poll_attributes
    return if @options[:poll].blank?

    @options[:poll].merge(account: @account)
  end

  def scheduled_options
    @options.tap do |options_hash|
      options_hash[:in_reply_to_id] = options_hash.delete(:thread)&.id
      options_hash[:application_id] = options_hash.delete(:application)&.id
      options_hash[:scheduled_at]   = nil
      options_hash[:idempotency]    = nil
    end
  end
end