2019-07-02 08:10:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class PostStatusService < BaseService
|
|
|
|
include Redisable
|
|
|
|
|
|
|
|
MIN_SCHEDULE_OFFSET = 5.minutes.freeze
|
|
|
|
|
|
|
|
# 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
|
2020-04-22 06:00:11 +01:00
|
|
|
# @option [String] :markdown Optional message in markdown
|
2019-07-02 08:10:25 +01:00
|
|
|
# @option [Status] :thread Optional status to reply to
|
|
|
|
# @option [Boolean] :sensitive
|
|
|
|
# @option [String] :visibility
|
|
|
|
# @option [String] :spoiler_text
|
|
|
|
# @option [String] :language
|
|
|
|
# @option [String] :scheduled_at
|
2020-07-25 01:05:31 +01:00
|
|
|
# @option [String] :expires_at
|
2019-07-02 08:10:25 +01:00
|
|
|
# @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
|
|
|
|
# @option [String] :group Optional group id
|
|
|
|
# @return [Status]
|
|
|
|
def call(account, options = {})
|
|
|
|
@account = account
|
|
|
|
@options = options
|
|
|
|
@text = @options[:text] || ''
|
2020-06-17 21:27:58 +01:00
|
|
|
@markdown = @options[:markdown] if @account.is_pro
|
2019-07-02 08:10:25 +01:00
|
|
|
@in_reply_to = @options[:thread]
|
2020-07-15 04:31:54 +01:00
|
|
|
@autoJoinGroup = @options[:autoJoinGroup] || false
|
2020-09-14 17:12:25 +01:00
|
|
|
@isPrivateGroup = @options[:isPrivateGroup] || false
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
|
|
|
|
|
2020-12-09 04:19:10 +00:00
|
|
|
validate_links!
|
2019-07-02 08:10:25 +01:00
|
|
|
validate_media!
|
|
|
|
validate_group!
|
|
|
|
preprocess_attributes!
|
|
|
|
|
|
|
|
if scheduled?
|
|
|
|
schedule_status!
|
|
|
|
else
|
|
|
|
process_status!
|
|
|
|
postprocess_status!
|
|
|
|
bump_potential_friendship!
|
|
|
|
end
|
|
|
|
|
2021-01-17 22:36:20 +00:00
|
|
|
redis.with do |conn|
|
|
|
|
conn.setex(idempotency_key, 3_600, @status.id) if idempotency_given?
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
@status
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def preprocess_attributes!
|
|
|
|
@text = @options.delete(:spoiler_text) if @text.blank? && @options[:spoiler_text].present?
|
|
|
|
@visibility = @options[:visibility] || @account.user&.setting_default_privacy
|
2020-05-01 17:46:31 +01:00
|
|
|
@visibility = :unlisted if @visibility == :public && @account.silenced?
|
2020-09-14 17:12:25 +01:00
|
|
|
@visibility = :private_group if @isPrivateGroup
|
2020-12-19 06:33:33 +00:00
|
|
|
@expires_at = nil
|
|
|
|
if @account.is_pro
|
|
|
|
case @options[:expires_at]
|
|
|
|
when 'five_minutes'
|
|
|
|
@expires_at = Time.now + 5.minutes
|
|
|
|
when 'one_hour'
|
|
|
|
@expires_at = Time.now + 1.hour
|
|
|
|
when 'six_hours'
|
|
|
|
@expires_at = Time.now + 6.hours
|
|
|
|
when 'one_day'
|
|
|
|
@expires_at = Time.now + 1.day
|
|
|
|
when 'three_days'
|
|
|
|
@expires_at = Time.now + 3.days
|
|
|
|
when 'one_week'
|
|
|
|
@expires_at = Time.now + 1.week
|
|
|
|
end
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
@scheduled_at = @options[:scheduled_at]&.to_datetime
|
|
|
|
@scheduled_at = nil if scheduled_in_the_past?
|
|
|
|
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_hashtags_service.call(@status)
|
|
|
|
process_mentions_service.call(@status)
|
2019-08-13 07:16:01 +01:00
|
|
|
if @status.quote
|
|
|
|
process_quote_service.call(@status)
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
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!
|
2020-12-09 04:19:10 +00:00
|
|
|
LinkCrawlWorker.perform_async(@status.id)
|
2021-01-25 20:52:52 +00:00
|
|
|
DistributionWorker.perform_async(@status.id)
|
2020-07-25 01:05:31 +01:00
|
|
|
ExpiringStatusWorker.perform_at(@status.expires_at, @status.id) if @status.expires_at && @account.is_pro
|
2019-07-02 08:10:25 +01:00
|
|
|
PollExpirationNotifyWorker.perform_at(@status.poll.expires_at, @status.poll.id) if @status.poll
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_group!
|
|
|
|
group_id = @options[:group_id]
|
2019-08-13 07:16:01 +01:00
|
|
|
|
2020-12-23 20:00:30 +00:00
|
|
|
# All good if no group
|
2019-07-02 08:10:25 +01:00
|
|
|
return if group_id.blank?
|
2020-12-23 20:00:30 +00:00
|
|
|
|
|
|
|
# If removed from group, return error immediately
|
|
|
|
if GroupRemovedAccount.where(account: @account, group_id: group_id).exists?
|
|
|
|
raise GabSocial::ValidationError, I18n.t('statuses.not_a_member_of_group')
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return ok if autojoin flag exists
|
2020-07-15 04:31:54 +01:00
|
|
|
return if @autoJoinGroup
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-12-23 20:00:30 +00:00
|
|
|
# Return ok if is reply
|
|
|
|
return unless @in_reply_to.nil?
|
2020-12-16 07:39:07 +00:00
|
|
|
|
2020-12-23 20:00:30 +00:00
|
|
|
# If is normal post and tries without being a member, return error
|
|
|
|
unless GroupAccount.where(account: @account, group_id: group_id).exists?
|
|
|
|
raise GabSocial::ValidationError, I18n.t('statuses.not_a_member_of_group')
|
|
|
|
return
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def validate_media!
|
|
|
|
return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
|
|
|
|
|
|
|
|
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
|
|
|
|
|
2020-12-27 06:57:18 +00:00
|
|
|
@account.media_attachments.connection.stick_to_master!
|
2019-07-02 08:10:25 +01:00
|
|
|
@media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
|
|
|
|
|
2020-05-02 07:25:55 +01:00
|
|
|
hasVideoOrGif = @media.find(&:video?) || @media.find(&:gifv?)
|
|
|
|
raise GabSocial::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && hasVideoOrGif
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
2020-12-09 04:19:10 +00:00
|
|
|
def validate_links!
|
|
|
|
raise GabSocial::NotPermittedError if LinkBlock.block?(@text)
|
|
|
|
end
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
def language_from_option(str)
|
|
|
|
ISO_639.find(str)&.alpha2
|
|
|
|
end
|
|
|
|
|
2019-08-08 14:25:28 +01:00
|
|
|
def process_quote_service
|
|
|
|
ProcessQuoteService.new
|
|
|
|
end
|
2019-08-13 07:16:01 +01:00
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
def process_mentions_service
|
|
|
|
ProcessMentionsService.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def process_hashtags_service
|
|
|
|
ProcessHashtagsService.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def scheduled?
|
2019-09-19 00:43:43 +01:00
|
|
|
return false unless @account.is_pro
|
2019-07-02 08:10:25 +01:00
|
|
|
@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?
|
2021-01-17 22:36:20 +00:00
|
|
|
redis.with do |conn|
|
|
|
|
@idempotency_duplicate = conn.get(idempotency_key)
|
|
|
|
end
|
|
|
|
@idempotency_duplicate
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def scheduled_in_the_past?
|
|
|
|
@scheduled_at.present? && @scheduled_at <= Time.now.utc + MIN_SCHEDULE_OFFSET
|
|
|
|
end
|
|
|
|
|
|
|
|
def bump_potential_friendship!
|
|
|
|
return if !@status.reply? || @account.id == @status.in_reply_to_account_id
|
|
|
|
ActivityTracker.increment('activity:interactions')
|
|
|
|
return if @account.following?(@status.in_reply_to_account_id)
|
|
|
|
PotentialFriendshipTracker.record(@account.id, @status.in_reply_to_account_id, :reply)
|
|
|
|
end
|
|
|
|
|
|
|
|
def status_attributes
|
|
|
|
{
|
|
|
|
text: @text,
|
2020-04-22 06:00:11 +01:00
|
|
|
markdown: @markdown,
|
2020-07-25 01:05:31 +01:00
|
|
|
expires_at: @expires_at,
|
2019-07-02 08:10:25 +01:00
|
|
|
group_id: @options[:group_id],
|
2019-07-30 01:26:35 +01:00
|
|
|
quote_of_id: @options[:quote_of_id],
|
2019-07-02 08:10:25 +01:00
|
|
|
media_attachments: @media || [],
|
|
|
|
thread: @in_reply_to,
|
|
|
|
poll_attributes: poll_attributes,
|
|
|
|
sensitive: (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?,
|
|
|
|
spoiler_text: @options[:spoiler_text] || '',
|
|
|
|
visibility: @visibility,
|
|
|
|
language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
|
|
|
|
application: @options[:application],
|
|
|
|
}.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
|