2016-11-15 15:56:29 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-24 17:25:04 +00:00
|
|
|
class PostStatusService < BaseService
|
2019-02-02 18:11:38 +00:00
|
|
|
include Redisable
|
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
MIN_SCHEDULE_OFFSET = 5.minutes.freeze
|
2021-07-16 06:38:50 +00:00
|
|
|
MIN_EXPIRE_OFFSET = 40.seconds.freeze # The original intention is 60 seconds, but we have a margin of 20 seconds.
|
2019-01-05 11:43:28 +00:00
|
|
|
|
2016-02-24 17:50:16 +00:00
|
|
|
# Post a text status update, fetch and notify remote users mentioned
|
|
|
|
# @param [Account] account Account from which to post
|
2016-11-23 09:46:48 +00:00
|
|
|
# @param [Hash] options
|
2019-01-05 11:43:28 +00:00
|
|
|
# @option [String] :text Message
|
|
|
|
# @option [Status] :thread Optional status to reply to
|
2016-11-23 09:46:48 +00:00
|
|
|
# @option [Boolean] :sensitive
|
2017-01-15 13:01:33 +00:00
|
|
|
# @option [String] :visibility
|
2017-01-13 04:54:26 +00:00
|
|
|
# @option [String] :spoiler_text
|
2019-01-05 11:43:28 +00:00
|
|
|
# @option [String] :language
|
|
|
|
# @option [String] :scheduled_at
|
2020-06-15 22:56:38 +00:00
|
|
|
# @option [String] :expires_at
|
|
|
|
# @option [String] :expires_action
|
2020-09-05 07:33:17 +00:00
|
|
|
# @option [Circle] :circle Optional circle to target the status to
|
2019-03-03 21:18:23 +00:00
|
|
|
# @option [Hash] :poll Optional poll to attach
|
2016-11-23 09:46:48 +00:00
|
|
|
# @option [Enumerable] :media_ids Optional array of media IDs to attach
|
2017-01-15 13:01:33 +00:00
|
|
|
# @option [Doorkeeper::Application] :application
|
2017-04-25 13:04:49 +00:00
|
|
|
# @option [String] :idempotency Optional idempotency key
|
2020-03-08 14:17:39 +00:00
|
|
|
# @option [Boolean] :with_rate_limit
|
2016-02-24 17:50:16 +00:00
|
|
|
# @return [Status]
|
2019-01-05 11:43:28 +00:00
|
|
|
def call(account, options = {})
|
|
|
|
@account = account
|
|
|
|
@options = options
|
|
|
|
@text = @options[:text] || ''
|
|
|
|
@in_reply_to = @options[:thread]
|
2020-02-24 02:36:40 +00:00
|
|
|
@quote_id = @options[:quote_id]
|
2020-09-05 07:33:17 +00:00
|
|
|
@circle = @options[:circle]
|
2019-01-05 11:43:28 +00:00
|
|
|
|
|
|
|
return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
|
|
|
|
|
|
|
|
validate_media!
|
2021-07-16 06:38:50 +00:00
|
|
|
validate_expires!
|
2019-01-05 11:43:28 +00:00
|
|
|
preprocess_attributes!
|
2019-11-29 03:10:59 +00:00
|
|
|
preprocess_quote!
|
2019-01-05 11:43:28 +00:00
|
|
|
|
|
|
|
if scheduled?
|
|
|
|
schedule_status!
|
|
|
|
else
|
|
|
|
process_status!
|
|
|
|
postprocess_status!
|
|
|
|
bump_potential_friendship!
|
2017-04-25 13:04:49 +00:00
|
|
|
end
|
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
redis.setex(idempotency_key, 3_600, @status.id) if idempotency_given?
|
2017-07-14 17:47:53 +00:00
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
@status
|
|
|
|
end
|
2018-12-24 18:06:14 +00:00
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
private
|
2017-07-14 17:47:53 +00:00
|
|
|
|
2019-11-29 03:10:59 +00:00
|
|
|
def status_from_uri(uri)
|
|
|
|
ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
|
|
|
|
end
|
|
|
|
|
|
|
|
def quote_from_url(url)
|
|
|
|
return nil if url.nil?
|
|
|
|
|
|
|
|
quote = ResolveURLService.new.call(url)
|
|
|
|
status_from_uri(quote.uri) if quote
|
|
|
|
rescue
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
def preprocess_attributes!
|
2020-06-15 22:56:38 +00:00
|
|
|
@sensitive = (@options[:sensitive].nil? ? @account.user&.setting_default_sensitive : @options[:sensitive]) || @options[:spoiler_text].present?
|
|
|
|
@text = @options.delete(:spoiler_text) if @text.blank? && @options[:spoiler_text].present?
|
|
|
|
@visibility = @options[:visibility] || @account.user&.setting_default_privacy
|
|
|
|
@visibility = :unlisted if @visibility&.to_sym == :public && @account.silenced?
|
2020-09-05 07:33:17 +00:00
|
|
|
@visibility = :limited if @circle.present?
|
|
|
|
@visibility = :limited if @visibility&.to_sym != :direct && @in_reply_to&.limited_visibility?
|
2021-08-23 17:42:21 +00:00
|
|
|
@scheduled_at = @options[:scheduled_at].is_a?(Time) ? @options[:scheduled_at] : @options[:scheduled_at]&.to_datetime&.to_time
|
2020-09-05 07:33:17 +00:00
|
|
|
@scheduled_at = nil if scheduled_in_the_past?
|
2019-11-29 03:10:59 +00:00
|
|
|
if @quote_id.nil? && md = @text.match(/QT:\s*\[\s*(https:\/\/.+?)\s*\]/)
|
|
|
|
@quote_id = quote_from_url(md[1])&.id
|
|
|
|
@text.sub!(/QT:\s*\[.*?\]/, '')
|
|
|
|
end
|
2019-01-19 21:01:01 +00:00
|
|
|
rescue ArgumentError
|
|
|
|
raise ActiveRecord::RecordInvalid
|
2019-01-05 11:43:28 +00:00
|
|
|
end
|
2016-11-28 12:36:47 +00:00
|
|
|
|
2020-02-24 02:36:40 +00:00
|
|
|
def preprocess_quote!
|
|
|
|
if @quote_id.present?
|
|
|
|
quote = Status.find(@quote_id)
|
|
|
|
@quote_id = quote.reblog_of_id.to_s if quote.reblog?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
def process_status!
|
|
|
|
# The following transaction block is needed to wrap the UPDATEs to
|
|
|
|
# the media attachments when the status is created
|
2016-11-28 12:36:47 +00:00
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
ApplicationRecord.transaction do
|
|
|
|
@status = @account.statuses.create!(status_attributes)
|
2020-09-05 07:33:17 +00:00
|
|
|
@status.capability_tokens.create! if @status.limited_visibility?
|
2017-04-25 13:04:49 +00:00
|
|
|
end
|
|
|
|
|
2020-09-05 07:33:17 +00:00
|
|
|
ProcessHashtagsService.new.call(@status)
|
|
|
|
ProcessMentionsService.new.call(@status, @circle)
|
2022-03-17 14:25:16 +00:00
|
|
|
ProcessStatusReferenceService.new.call(@status, status_reference_ids: (@options[:status_reference_ids] || []) + [@quote_id], urls: @options[:status_reference_urls])
|
2016-02-24 17:25:04 +00:00
|
|
|
end
|
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
def schedule_status!
|
2019-01-21 19:03:04 +00:00
|
|
|
status_for_validation = @account.statuses.build(status_attributes)
|
2019-03-15 12:36:38 +00:00
|
|
|
|
2019-01-21 19:03:04 +00:00
|
|
|
if status_for_validation.valid?
|
|
|
|
status_for_validation.destroy
|
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
# The following transaction block is needed to wrap the UPDATEs to
|
|
|
|
# the media attachments when the scheduled status is created
|
2016-02-24 17:25:04 +00:00
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
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)
|
2019-03-10 23:49:31 +00:00
|
|
|
PollExpirationNotifyWorker.perform_at(@status.poll.expires_at, @status.poll.id) if @status.poll
|
2021-07-16 06:38:50 +00:00
|
|
|
@status.status_expire.queue_action if expires_soon?
|
|
|
|
end
|
|
|
|
|
|
|
|
def expires_soon?
|
|
|
|
expires_at = @status&.status_expire&.expires_at
|
|
|
|
expires_at.present? && expires_at <= Time.now.utc + MIN_SCHEDULE_OFFSET
|
2019-01-05 11:43:28 +00:00
|
|
|
end
|
2017-02-26 22:23:06 +00:00
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
def validate_media!
|
|
|
|
return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
|
2017-02-26 22:23:06 +00:00
|
|
|
|
2019-03-03 23:40:21 +00:00
|
|
|
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 4 || @options[:poll].present?
|
2017-02-26 22:23:06 +00:00
|
|
|
|
2019-01-26 22:59:39 +00:00
|
|
|
@media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(4).map(&:to_i))
|
2017-02-26 22:23:06 +00:00
|
|
|
|
2019-06-19 21:42:38 +00:00
|
|
|
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:audio_or_video?)
|
2020-03-08 22:56:18 +00:00
|
|
|
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.not_ready') if @media.any?(&:not_processed?)
|
2017-02-05 03:03:24 +00:00
|
|
|
end
|
|
|
|
|
2021-07-16 06:38:50 +00:00
|
|
|
def validate_expires!
|
|
|
|
return if @options[:expires_at].blank?
|
|
|
|
|
2021-07-27 12:05:40 +00:00
|
|
|
@expires_at = @options[:expires_at].is_a?(Time) ? @options[:expires_at] : @options[:expires_at]&.to_time
|
2021-07-16 06:38:50 +00:00
|
|
|
|
|
|
|
raise Mastodon::ValidationError, I18n.t('status_expire.validations.invalid_expire_at') if @expires_at.nil?
|
2021-08-23 17:42:21 +00:00
|
|
|
raise Mastodon::ValidationError, I18n.t('status_expire.validations.expire_in_the_past') if @expires_at <= (@options[:scheduled_at]&.to_datetime&.to_time || Time.now.utc) + MIN_EXPIRE_OFFSET
|
2021-07-16 06:38:50 +00:00
|
|
|
|
|
|
|
@expires_action = begin
|
|
|
|
case @options[:expires_action]&.to_sym
|
|
|
|
when :hint, :mark, nil
|
|
|
|
:mark
|
|
|
|
when :delete
|
|
|
|
:delete
|
|
|
|
else
|
|
|
|
raise Mastodon::ValidationError, I18n.t('status_expire.validations.invalid_expire_action')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-07 07:30:53 +00:00
|
|
|
def language_from_option(str)
|
|
|
|
ISO_639.find(str)&.alpha2
|
|
|
|
end
|
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
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?
|
2021-07-27 12:05:40 +00:00
|
|
|
@scheduled_at.present? && @scheduled_at <= Time.now.utc + MIN_SCHEDULE_OFFSET - 20.seconds
|
2019-01-05 11:43:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def bump_potential_friendship!
|
|
|
|
return if !@status.reply? || @account.id == @status.in_reply_to_account_id
|
2018-07-16 16:35:43 +00:00
|
|
|
ActivityTracker.increment('activity:interactions')
|
2019-01-05 11:43:28 +00:00
|
|
|
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,
|
|
|
|
media_attachments: @media || [],
|
|
|
|
thread: @in_reply_to,
|
2019-03-28 03:44:59 +00:00
|
|
|
poll_attributes: poll_attributes,
|
2020-03-25 21:40:58 +00:00
|
|
|
sensitive: @sensitive,
|
2019-01-05 11:43:28 +00:00
|
|
|
spoiler_text: @options[:spoiler_text] || '',
|
|
|
|
visibility: @visibility,
|
2020-09-05 07:33:17 +00:00
|
|
|
circle: @circle,
|
2019-01-05 11:43:28 +00:00
|
|
|
language: language_from_option(@options[:language]) || @account.user&.setting_default_language&.presence || LanguageDetector.instance.detect(@text, @account),
|
|
|
|
application: @options[:application],
|
2020-03-08 14:17:39 +00:00
|
|
|
rate_limit: @options[:with_rate_limit],
|
2020-02-24 02:36:40 +00:00
|
|
|
quote_id: @quote_id,
|
2020-06-15 22:56:38 +00:00
|
|
|
expires_at: @expires_at,
|
|
|
|
expires_action: @expires_action,
|
2019-03-15 12:36:38 +00:00
|
|
|
}.compact
|
2019-01-05 11:43:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def scheduled_status_attributes
|
|
|
|
{
|
|
|
|
scheduled_at: @scheduled_at,
|
|
|
|
media_attachments: @media || [],
|
|
|
|
params: scheduled_options,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-15 12:36:38 +00:00
|
|
|
def poll_attributes
|
|
|
|
return if @options[:poll].blank?
|
|
|
|
|
2019-09-29 20:58:01 +00:00
|
|
|
@options[:poll].merge(account: @account, voters_count: 0)
|
2019-03-15 12:36:38 +00:00
|
|
|
end
|
|
|
|
|
2019-01-05 11:43:28 +00:00
|
|
|
def scheduled_options
|
|
|
|
@options.tap do |options_hash|
|
2020-03-08 14:17:39 +00:00
|
|
|
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
|
|
|
|
options_hash[:with_rate_limit] = false
|
2019-01-05 11:43:28 +00:00
|
|
|
end
|
Re-add follow recommendations API (#7918)
* Re-add follow recommendations API
GET /api/v1/suggestions
Removed in 8efa081f210d72ed450c39ac4cde0fd84fb3d3fb due to Neo4J
dependency. The algorithm uses triadic closures, takes into account
suspensions, blocks, mutes, domain blocks, excludes locked and moved
accounts, and prefers more recently updated accounts.
* Track interactions with people you don't follow
Replying to, favouriting and reblogging someone you're not following
will make them show up in follow recommendations. The interactions
have different weights:
- Replying is 1
- Favouriting is 10 (decidedly positive interaction, but private)
- Reblogging is 20
Following them, muting or blocking will remove them from the list,
obviously.
* Remove triadic closures, ensure potential friendships are trimmed
2018-07-02 23:47:56 +00:00
|
|
|
end
|
2016-02-24 17:25:04 +00:00
|
|
|
end
|