2016-11-15 07:56:29 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-24 09:25:04 -08:00
|
|
|
class PostStatusService < BaseService
|
2019-02-02 10:11:38 -08:00
|
|
|
include Redisable
|
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
MIN_SCHEDULE_OFFSET = 5.minutes.freeze
|
2019-07-15 11:34:05 -07:00
|
|
|
VISIBILITY_RANK = {
|
|
|
|
'public' => 0,
|
|
|
|
'unlisted' => 1,
|
|
|
|
'local' => 1,
|
|
|
|
'private' => 2,
|
|
|
|
'direct' => 3,
|
|
|
|
'limited' => 3,
|
|
|
|
'chat' => 4
|
|
|
|
}
|
2019-01-05 03:43:28 -08:00
|
|
|
|
2016-02-24 09:50:16 -08:00
|
|
|
# Post a text status update, fetch and notify remote users mentioned
|
|
|
|
# @param [Account] account Account from which to post
|
2016-11-23 01:46:48 -08:00
|
|
|
# @param [Hash] options
|
2019-01-05 03:43:28 -08:00
|
|
|
# @option [String] :text Message
|
|
|
|
# @option [Status] :thread Optional status to reply to
|
2019-05-06 10:43:23 -07:00
|
|
|
# @option [Tag] :tags Optional tags to include
|
2019-05-10 08:49:27 -07:00
|
|
|
# @option [Time] :created_at Optional time which status was originally posted
|
2016-11-23 01:46:48 -08:00
|
|
|
# @option [Boolean] :sensitive
|
2017-01-15 05:01:33 -08:00
|
|
|
# @option [String] :visibility
|
2019-05-09 07:58:37 -07:00
|
|
|
# @option [Boolean] :local_only
|
2019-04-20 18:57:41 -07:00
|
|
|
# @option [String] :sharekey
|
2017-01-12 20:54:26 -08:00
|
|
|
# @option [String] :spoiler_text
|
2019-01-05 03:43:28 -08:00
|
|
|
# @option [String] :language
|
|
|
|
# @option [String] :scheduled_at
|
2019-03-03 13:18:23 -08:00
|
|
|
# @option [Hash] :poll Optional poll to attach
|
2016-11-23 01:46:48 -08:00
|
|
|
# @option [Enumerable] :media_ids Optional array of media IDs to attach
|
2017-01-15 05:01:33 -08:00
|
|
|
# @option [Doorkeeper::Application] :application
|
2017-04-25 06:04:49 -07:00
|
|
|
# @option [String] :idempotency Optional idempotency key
|
2016-02-24 09:50:16 -08:00
|
|
|
# @return [Status]
|
2019-01-05 03:43:28 -08:00
|
|
|
def call(account, options = {})
|
|
|
|
@account = account
|
|
|
|
@options = options
|
|
|
|
@text = @options[:text] || ''
|
2019-05-18 10:58:54 -07:00
|
|
|
@footer = @options[:footer]
|
2019-01-05 03:43:28 -08:00
|
|
|
@in_reply_to = @options[:thread]
|
2019-07-15 11:34:05 -07:00
|
|
|
@tags = @options[:tags] || []
|
2019-05-09 07:58:37 -07:00
|
|
|
@local_only = @options[:local_only]
|
2019-05-10 01:48:11 -07:00
|
|
|
@sensitive = (@account.force_sensitive? ? true : @options[:sensitive])
|
2019-07-15 11:34:05 -07:00
|
|
|
@preloaded_tags = @options[:preloaded_tags] || []
|
2019-01-05 03:43:28 -08:00
|
|
|
|
|
|
|
return idempotency_duplicate if idempotency_given? && idempotency_duplicate?
|
|
|
|
|
|
|
|
validate_media!
|
|
|
|
preprocess_attributes!
|
|
|
|
|
|
|
|
if scheduled?
|
|
|
|
schedule_status!
|
|
|
|
else
|
|
|
|
process_status!
|
|
|
|
postprocess_status!
|
|
|
|
bump_potential_friendship!
|
2017-04-25 06:04:49 -07:00
|
|
|
end
|
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
redis.setex(idempotency_key, 3_600, @status.id) if idempotency_given?
|
2017-07-14 10:47:53 -07:00
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
@status
|
|
|
|
end
|
2018-12-24 10:06:14 -08:00
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
private
|
2017-07-14 10:47:53 -07:00
|
|
|
|
2019-05-18 10:58:54 -07:00
|
|
|
def set_footer_from_i_am
|
2019-07-16 19:30:33 -07:00
|
|
|
return if @footer.present? || @options[:no_footer]
|
|
|
|
name = @account.user.vars['_they:are']
|
2019-05-18 10:58:54 -07:00
|
|
|
return if name.blank?
|
2019-07-16 19:30:33 -07:00
|
|
|
@footer = @account.user.vars["_they:are:#{name}"]
|
2019-07-15 11:34:05 -07:00
|
|
|
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']) && @account.silenced? || @account.force_unlisted
|
|
|
|
end
|
|
|
|
|
|
|
|
def limit_visibility_to_reply
|
|
|
|
return if @in_reply_to.nil?
|
|
|
|
@visibility = @in_reply_to.visibility if @visibility.nil? ||
|
|
|
|
VISIBILITY_RANK[@visibility] < VISIBILITY_RANK[@in_reply_to.visibility]
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_local_only
|
|
|
|
@local_only = true if @account.user_always_local_only? || @in_reply_to&.local_only
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_chat
|
|
|
|
if @in_reply_to.present?
|
|
|
|
unless @in_reply_to.chat_tags.blank?
|
|
|
|
@preloaded_tags |= @in_reply_to.chat_tags
|
|
|
|
@visibility = :chat
|
|
|
|
@in_reply_to = nil
|
|
|
|
end
|
|
|
|
elsif @tags.present? && @tags.any? { |t| t.start_with?('chat.', '.chat.') }
|
|
|
|
@visibility = :chat
|
|
|
|
@local_only = true if @tags.any? { |t| t.in?(%w(chat.local .chat.local)) || t.start_with?('chat.local.', '.chat.local.') }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# move tags out of body so we can format them later
|
|
|
|
def extract_tags
|
|
|
|
chunks = []
|
2019-07-15 13:00:29 -07:00
|
|
|
@text.split(/^((?:#[\w:._·\-]+\s*)+)/).each do |chunk|
|
|
|
|
if chunk.start_with?('#') && !chunk.start_with?('#!')
|
2019-07-15 11:34:05 -07:00
|
|
|
@tags |= chunk[1..-1].split(/\s+/)
|
|
|
|
else
|
|
|
|
chunks << chunk
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@text = chunks.join
|
2019-05-18 10:58:54 -07:00
|
|
|
end
|
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
def preprocess_attributes!
|
2019-01-10 10:12:10 -08:00
|
|
|
if @text.blank? && @options[:spoiler_text].present?
|
|
|
|
@text = '.'
|
|
|
|
@text = @media.find(&:video?) ? '📹' : '🖼' if @media.size > 0
|
2018-10-20 10:14:46 -07:00
|
|
|
end
|
2019-05-06 06:36:44 -07:00
|
|
|
|
2019-07-15 11:34:05 -07:00
|
|
|
set_footer_from_i_am
|
|
|
|
extract_tags
|
|
|
|
set_chat
|
|
|
|
set_local_only
|
2019-05-06 06:36:44 -07:00
|
|
|
|
2019-07-15 11:34:05 -07:00
|
|
|
unless @visibility == :chat
|
|
|
|
set_initial_visibility
|
|
|
|
limit_visibility_if_silenced
|
|
|
|
limit_visibility_to_reply
|
2019-05-06 06:36:44 -07:00
|
|
|
end
|
|
|
|
|
2019-05-10 03:55:03 -07:00
|
|
|
@sensitive = (@account.user_defaults_to_sensitive? || @options[:spoiler_text].present?) if @sensitive.nil?
|
2019-05-10 01:48:11 -07:00
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
@scheduled_at = @options[:scheduled_at]&.to_datetime
|
|
|
|
@scheduled_at = nil if scheduled_in_the_past?
|
2019-01-19 13:01:01 -08:00
|
|
|
rescue ArgumentError
|
|
|
|
raise ActiveRecord::RecordInvalid
|
2019-01-05 03:43:28 -08:00
|
|
|
end
|
2017-07-14 10:47:53 -07:00
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
def process_status!
|
|
|
|
# The following transaction block is needed to wrap the UPDATEs to
|
|
|
|
# the media attachments when the status is created
|
2018-12-24 10:06:14 -08:00
|
|
|
|
2017-04-25 18:47:44 -07:00
|
|
|
ApplicationRecord.transaction do
|
2019-01-05 03:43:28 -08:00
|
|
|
@status = @account.statuses.create!(status_attributes)
|
2017-04-25 18:47:44 -07:00
|
|
|
end
|
2017-07-14 10:47:53 -07:00
|
|
|
|
2019-07-15 11:34:05 -07:00
|
|
|
process_hashtags_service.call(@status, @tags, @preloaded_tags)
|
2019-01-05 03:43:28 -08:00
|
|
|
process_mentions_service.call(@status)
|
2016-02-24 09:25:04 -08:00
|
|
|
end
|
2016-11-28 04:36:47 -08:00
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
def schedule_status!
|
2019-01-21 11:03:04 -08:00
|
|
|
status_for_validation = @account.statuses.build(status_attributes)
|
2019-03-15 05:36:38 -07:00
|
|
|
|
2019-01-21 11:03:04 -08:00
|
|
|
if status_for_validation.valid?
|
|
|
|
status_for_validation.destroy
|
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
# The following transaction block is needed to wrap the UPDATEs to
|
|
|
|
# the media attachments when the scheduled status is created
|
2017-06-05 15:34:08 -07:00
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
ApplicationRecord.transaction do
|
|
|
|
@status = @account.scheduled_statuses.create!(scheduled_status_attributes)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise ActiveRecord::RecordInvalid
|
2017-06-05 15:34:08 -07:00
|
|
|
end
|
2019-01-05 03:43:28 -08:00
|
|
|
end
|
2016-11-28 04:36:47 -08:00
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
def postprocess_status!
|
|
|
|
LinkCrawlWorker.perform_async(@status.id) unless @status.spoiler_text?
|
2019-05-10 08:49:27 -07:00
|
|
|
DistributionWorker.perform_async(@status.id) unless @options[:distribute] == false
|
2019-03-11 03:23:50 -07:00
|
|
|
|
2019-05-10 08:49:27 -07:00
|
|
|
unless @status.local_only? || @options[:distribute] == false || @options[:federate] == false
|
2019-01-10 10:12:10 -08:00
|
|
|
ActivityPub::DistributionWorker.perform_async(@status.id)
|
2017-04-25 06:04:49 -07:00
|
|
|
end
|
2019-03-11 03:23:50 -07:00
|
|
|
|
2019-03-10 16:49:31 -07:00
|
|
|
PollExpirationNotifyWorker.perform_at(@status.poll.expires_at, @status.poll.id) if @status.poll
|
2016-02-24 09:25:04 -08:00
|
|
|
end
|
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
def validate_media!
|
|
|
|
return if @options[:media_ids].blank? || !@options[:media_ids].is_a?(Enumerable)
|
2017-02-26 14:23:06 -08:00
|
|
|
|
2019-04-18 13:08:55 -07:00
|
|
|
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.too_many') if @options[:media_ids].size > 6 || @options[:poll].present?
|
2017-02-26 14:23:06 -08:00
|
|
|
|
2019-04-18 13:08:55 -07:00
|
|
|
@media = @account.media_attachments.where(status_id: nil).where(id: @options[:media_ids].take(6).map(&:to_i))
|
2017-02-26 14:23:06 -08:00
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
raise Mastodon::ValidationError, I18n.t('media_attachments.validations.images_and_video') if @media.size > 1 && @media.find(&:video?)
|
2017-02-04 19:03:24 -08:00
|
|
|
end
|
|
|
|
|
2018-05-07 00:30:53 -07:00
|
|
|
def language_from_option(str)
|
|
|
|
ISO_639.find(str)&.alpha2
|
|
|
|
end
|
|
|
|
|
2016-02-24 15:17:01 -08:00
|
|
|
def process_mentions_service
|
2017-10-27 10:08:30 -07:00
|
|
|
ProcessMentionsService.new
|
2016-02-24 09:44:03 -08:00
|
|
|
end
|
2016-11-05 07:20:05 -07:00
|
|
|
|
|
|
|
def process_hashtags_service
|
2017-10-27 10:08:30 -07:00
|
|
|
ProcessHashtagsService.new
|
2016-11-05 07:20:05 -07:00
|
|
|
end
|
2017-04-25 06:04:49 -07:00
|
|
|
|
2019-01-05 03:43:28 -08: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
|
2018-07-16 09:35:43 -07:00
|
|
|
ActivityTracker.increment('activity:interactions')
|
2019-01-05 03:43:28 -08: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
|
|
|
|
{
|
2019-05-20 23:51:34 -07:00
|
|
|
created_at: @options[:created_at] ? @options[:created_at].to_datetime.utc : Time.now.utc,
|
2019-01-05 03:43:28 -08:00
|
|
|
text: @text,
|
2019-05-18 10:58:54 -07:00
|
|
|
footer: @footer,
|
2019-01-05 03:43:28 -08:00
|
|
|
media_attachments: @media || [],
|
|
|
|
thread: @in_reply_to,
|
2019-03-27 20:44:59 -07:00
|
|
|
poll_attributes: poll_attributes,
|
2019-05-10 01:48:11 -07:00
|
|
|
sensitive: @sensitive,
|
2019-01-05 03:43:28 -08:00
|
|
|
spoiler_text: @options[:spoiler_text] || '',
|
|
|
|
visibility: @visibility,
|
2019-05-09 07:58:37 -07:00
|
|
|
local_only: @local_only,
|
2019-04-20 18:57:41 -07:00
|
|
|
sharekey: @sharekey,
|
2019-05-10 03:58:37 -07:00
|
|
|
language: language_from_option(@options[:language]) || @account.user_default_language&.presence || 'en',
|
2019-01-05 03:43:28 -08:00
|
|
|
application: @options[:application],
|
2019-05-13 05:42:39 -07:00
|
|
|
content_type: @options[:content_type] || @account.user&.setting_default_content_type,
|
2019-03-15 05:36:38 -07:00
|
|
|
}.compact
|
2019-01-05 03:43:28 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def scheduled_status_attributes
|
|
|
|
{
|
|
|
|
scheduled_at: @scheduled_at,
|
|
|
|
media_attachments: @media || [],
|
|
|
|
params: scheduled_options,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-03-15 05:36:38 -07:00
|
|
|
def poll_attributes
|
|
|
|
return if @options[:poll].blank?
|
|
|
|
|
|
|
|
@options[:poll].merge(account: @account)
|
|
|
|
end
|
|
|
|
|
2019-01-05 03:43:28 -08:00
|
|
|
def scheduled_options
|
|
|
|
@options.tap do |options_hash|
|
2019-01-06 03:03:27 -08: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
|
2019-01-05 03:43:28 -08: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 16:47:56 -07:00
|
|
|
end
|
2016-02-24 09:25:04 -08:00
|
|
|
end
|