2016-11-15 09:56:29 -06:00
# frozen_string_literal: true
2016-02-24 11:25:04 -06:00
class PostStatusService < BaseService
2019-02-02 12:11:38 -06:00
include Redisable
2022-02-07 19:41:17 -06:00
include LanguagesHelper
2019-02-02 12:11:38 -06:00
2019-01-05 05:43:28 -06:00
MIN_SCHEDULE_OFFSET = 5 . minutes . freeze
2023-02-13 09:36:29 -06:00
class UnexpectedMentionsError < StandardError
attr_reader :accounts
def initialize ( message , accounts )
super ( message )
@accounts = accounts
end
end
2016-02-24 11:50:16 -06:00
# Post a text status update, fetch and notify remote users mentioned
# @param [Account] account Account from which to post
2016-11-23 03:46:48 -06:00
# @param [Hash] options
2019-01-05 05:43:28 -06:00
# @option [String] :text Message
# @option [Status] :thread Optional status to reply to
2016-11-23 03:46:48 -06:00
# @option [Boolean] :sensitive
2017-01-15 07:01:33 -06:00
# @option [String] :visibility
2017-01-12 22:54:26 -06:00
# @option [String] :spoiler_text
2019-01-05 05:43:28 -06:00
# @option [String] :language
# @option [String] :scheduled_at
2019-03-03 15:18:23 -06:00
# @option [Hash] :poll Optional poll to attach
2016-11-23 03:46:48 -06:00
# @option [Enumerable] :media_ids Optional array of media IDs to attach
2017-01-15 07:01:33 -06:00
# @option [Doorkeeper::Application] :application
2017-04-25 08:04:49 -05:00
# @option [String] :idempotency Optional idempotency key
2020-03-08 09:17:39 -05:00
# @option [Boolean] :with_rate_limit
2023-02-13 09:36:29 -06:00
# @option [Enumerable] :allowed_mentions Optional array of expected mentioned account IDs, raises `UnexpectedMentionsError` if unexpected accounts end up in mentions
2016-02-24 11:50:16 -06:00
# @return [Status]
2019-01-05 05:43:28 -06:00
def call ( account , options = { } )
@account = account
@options = options
@text = @options [ :text ] || ''
@in_reply_to = @options [ :thread ]
return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
validate_media!
preprocess_attributes!
if scheduled?
schedule_status!
else
process_status!
2017-04-25 08:04:49 -05:00
end
2019-01-05 05:43:28 -06:00
redis . setex ( idempotency_key , 3_600 , @status . id ) if idempotency_given?
2017-07-14 12:47:53 -05:00
Fix idempotency when database writes are slow (#21840)
There is an idempotency key generated by clients when authoring a post,
and stored in Redis, to ensure that if a user or client retries posting
the same status, we don't get a duplicate.
Hachyderm.io has been experiencing some filesystem and database
performance issues, causing database writes to be slow. This can mean
that there are successful posts, but the reverse proxy returns 504
Gateway Timeout before the idempotency status has been updated; users or
clients who retry (such as Tusky which retries automatically, see
tuskyapp/Tusky#2951) can re-try the same post with the same idempotency
key before it has actually been recorded in Redis, leading to duplicate
posts.
To address this issue, move all of the database updates after the
initial transaction that creates the status into the
`postprocess_status!` method, so we can insert the idempotency key
immediately after the status has been created, significantly reducing
the window in which the status could be created but the idempotency key
not yet stored.
Note: this has not yet been tested; I'm submitting this PR for
discussion and to offer to the Hachyderm.io admins to try out to fix the
multiple posting problem.
Co-authored-by: Brian Campbell <brcampbell@beta.team>
2022-12-15 11:08:40 -06:00
unless scheduled?
postprocess_status!
bump_potential_friendship!
end
2019-01-05 05:43:28 -06:00
@status
end
2018-12-24 12:06:14 -06:00
2019-01-05 05:43:28 -06:00
private
2017-07-14 12:47:53 -05:00
2023-04-09 04:25:30 -05:00
def fill_blank_text!
return unless @text . blank? && @options [ :spoiler_text ] . present?
2023-05-07 08:06:15 -05:00
@text = begin
if @media & . any? ( & :video? ) || @media & . any? ( & :gifv? )
'📹'
elsif @media & . any? ( & :audio? )
'🎵'
elsif @media & . any? ( & :image? )
'🖼'
else
'.'
end
2018-10-20 12:14:46 -05:00
end
2023-04-09 04:25:30 -05:00
end
def preprocess_attributes!
fill_blank_text!
2020-03-25 16:40:58 -05:00
@sensitive = ( @options [ :sensitive ] . nil? ? @account . user & . setting_default_sensitive : @options [ :sensitive ] ) || @options [ :spoiler_text ] . present?
2019-01-05 05:43:28 -06:00
@visibility = @options [ :visibility ] || @account . user & . setting_default_privacy
2020-03-08 10:11:49 -05:00
@visibility = :unlisted if @visibility & . to_sym == :public && @account . silenced?
2019-01-05 05:43:28 -06:00
@scheduled_at = @options [ :scheduled_at ] & . to_datetime
@scheduled_at = nil if scheduled_in_the_past?
2019-01-19 15:01:01 -06:00
rescue ArgumentError
raise ActiveRecord :: RecordInvalid
2019-01-05 05:43:28 -06:00
end
2017-07-14 12:47:53 -05:00
2019-01-05 05:43:28 -06:00
def process_status!
2023-02-13 09:36:29 -06:00
@status = @account . statuses . new ( status_attributes )
process_mentions_service . call ( @status , save_records : false )
safeguard_mentions! ( @status )
2019-01-05 05:43:28 -06:00
# The following transaction block is needed to wrap the UPDATEs to
# the media attachments when the status is created
2017-04-25 20:47:44 -05:00
ApplicationRecord . transaction do
2023-02-13 09:36:29 -06:00
@status . save!
2017-04-25 20:47:44 -05:00
end
2016-02-24 11:25:04 -06:00
end
2016-11-28 06:36:47 -06:00
2023-02-13 09:36:29 -06:00
def safeguard_mentions! ( status )
return if @options [ :allowed_mentions ] . nil?
2023-02-19 23:58:28 -06:00
2023-02-13 09:36:29 -06:00
expected_account_ids = @options [ :allowed_mentions ] . map ( & :to_i )
unexpected_accounts = status . mentions . map ( & :account ) . to_a . reject { | mentioned_account | expected_account_ids . include? ( mentioned_account . id ) }
return if unexpected_accounts . empty?
raise UnexpectedMentionsError . new ( 'Post would be sent to unexpected accounts' , unexpected_accounts )
end
2019-01-05 05:43:28 -06:00
def schedule_status!
2019-01-21 13:03:04 -06:00
status_for_validation = @account . statuses . build ( status_attributes )
2019-03-15 07:36:38 -05:00
2019-01-21 13:03:04 -06:00
if status_for_validation . valid?
2021-10-14 12:59:21 -05:00
# Marking the status as destroyed is necessary to prevent the status from being
# persisted when the associated media attachments get updated when creating the
# scheduled status.
2019-01-21 13:03:04 -06:00
status_for_validation . destroy
2019-01-05 05:43:28 -06:00
# The following transaction block is needed to wrap the UPDATEs to
# the media attachments when the scheduled status is created
2017-06-05 17:34:08 -05:00
2019-01-05 05:43:28 -06:00
ApplicationRecord . transaction do
@status = @account . scheduled_statuses . create! ( scheduled_status_attributes )
end
else
raise ActiveRecord :: RecordInvalid
2017-06-05 17:34:08 -05:00
end
2019-01-05 05:43:28 -06:00
end
2016-11-28 06:36:47 -06:00
2019-01-05 05:43:28 -06:00
def postprocess_status!
Fix idempotency when database writes are slow (#21840)
There is an idempotency key generated by clients when authoring a post,
and stored in Redis, to ensure that if a user or client retries posting
the same status, we don't get a duplicate.
Hachyderm.io has been experiencing some filesystem and database
performance issues, causing database writes to be slow. This can mean
that there are successful posts, but the reverse proxy returns 504
Gateway Timeout before the idempotency status has been updated; users or
clients who retry (such as Tusky which retries automatically, see
tuskyapp/Tusky#2951) can re-try the same post with the same idempotency
key before it has actually been recorded in Redis, leading to duplicate
posts.
To address this issue, move all of the database updates after the
initial transaction that creates the status into the
`postprocess_status!` method, so we can insert the idempotency key
immediately after the status has been created, significantly reducing
the window in which the status could be created but the idempotency key
not yet stored.
Note: this has not yet been tested; I'm submitting this PR for
discussion and to offer to the Hachyderm.io admins to try out to fix the
multiple posting problem.
Co-authored-by: Brian Campbell <brcampbell@beta.team>
2022-12-15 11:08:40 -06:00
process_hashtags_service . call ( @status )
2021-11-25 06:07:38 -06:00
Trends . tags . register ( @status )
LinkCrawlWorker . perform_async ( @status . id )
2019-01-05 05:43:28 -06:00
DistributionWorker . perform_async ( @status . id )
2019-07-19 11:26:49 -05:00
ActivityPub :: DistributionWorker . perform_async ( @status . id ) unless @status . local_only?
2019-03-10 18:49:31 -05:00
PollExpirationNotifyWorker . perform_at ( @status . poll . expires_at , @status . poll . id ) if @status . poll
2016-02-24 11:25:04 -06:00
end
2019-01-05 05:43:28 -06:00
def validate_media!
2022-03-09 02:06:17 -06:00
if @options [ :media_ids ] . blank? || ! @options [ :media_ids ] . is_a? ( Enumerable )
@media = [ ]
return
end
2017-02-26 16:23:06 -06:00
2019-03-03 17:40:21 -06:00
raise Mastodon :: ValidationError , I18n . t ( 'media_attachments.validations.too_many' ) if @options [ :media_ids ] . size > 4 || @options [ :poll ] . present?
2017-02-26 16:23:06 -06:00
2019-01-26 16:59:39 -06:00
@media = @account . media_attachments . where ( status_id : nil ) . where ( id : @options [ :media_ids ] . take ( 4 ) . map ( & :to_i ) )
2017-02-26 16:23:06 -06:00
2019-06-19 16:42:38 -05:00
raise Mastodon :: ValidationError , I18n . t ( 'media_attachments.validations.images_and_video' ) if @media . size > 1 && @media . find ( & :audio_or_video? )
2020-03-08 17:56:18 -05:00
raise Mastodon :: ValidationError , I18n . t ( 'media_attachments.validations.not_ready' ) if @media . any? ( & :not_processed? )
2017-02-04 21:03:24 -06:00
end
2016-02-24 17:17:01 -06:00
def process_mentions_service
2017-10-27 12:08:30 -05:00
ProcessMentionsService . new
2016-02-24 11:44:03 -06:00
end
2016-11-05 09:20:05 -05:00
def process_hashtags_service
2017-10-27 12:08:30 -05:00
ProcessHashtagsService . new
2016-11-05 09:20:05 -05:00
end
2017-04-25 08:04:49 -05:00
2019-01-05 05:43:28 -06: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?
@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
2023-02-19 23:58:28 -06:00
2018-07-16 11:35:43 -05:00
ActivityTracker . increment ( 'activity:interactions' )
2019-01-05 05:43:28 -06:00
return if @account . following? ( @status . in_reply_to_account_id )
2023-02-19 23:58:28 -06:00
2019-01-05 05:43:28 -06:00
PotentialFriendshipTracker . record ( @account . id , @status . in_reply_to_account_id , :reply )
end
def status_attributes
{
text : @text ,
media_attachments : @media || [ ] ,
2022-03-09 02:06:17 -06:00
ordered_media_attachment_ids : ( @options [ :media_ids ] || [ ] ) . map ( & :to_i ) & @media . map ( & :id ) ,
2019-01-05 05:43:28 -06:00
thread : @in_reply_to ,
2019-03-27 22:44:59 -05:00
poll_attributes : poll_attributes ,
2020-03-25 16:40:58 -05:00
sensitive : @sensitive ,
2019-01-05 05:43:28 -06:00
spoiler_text : @options [ :spoiler_text ] || '' ,
visibility : @visibility ,
2022-03-09 01:46:05 -06:00
language : valid_locale_cascade ( @options [ :language ] , @account . user & . preferred_posting_language , I18n . default_locale ) ,
2019-01-05 05:43:28 -06:00
application : @options [ :application ] ,
2019-05-13 07:42:39 -05:00
content_type : @options [ :content_type ] || @account . user & . setting_default_content_type ,
2020-03-08 09:17:39 -05:00
rate_limit : @options [ :with_rate_limit ] ,
2019-03-15 07:36:38 -05:00
} . compact
2019-01-05 05:43:28 -06:00
end
def scheduled_status_attributes
{
scheduled_at : @scheduled_at ,
media_attachments : @media || [ ] ,
params : scheduled_options ,
}
end
2019-03-15 07:36:38 -05:00
def poll_attributes
return if @options [ :poll ] . blank?
2019-09-29 15:58:01 -05:00
@options [ :poll ] . merge ( account : @account , voters_count : 0 )
2019-03-15 07:36:38 -05:00
end
2019-01-05 05:43:28 -06:00
def scheduled_options
@options . tap do | options_hash |
2020-03-08 09:17:39 -05: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 05:43:28 -06: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 18:47:56 -05:00
end
2016-02-24 11:25:04 -06:00
end