From a7aa2544e4f5926414af75e5c0acaa06c2ec7873 Mon Sep 17 00:00:00 2001 From: multiple creatures Date: Fri, 8 Feb 2019 23:09:47 +0000 Subject: [PATCH] community world tl + networked home tl --- app/models/status.rb | 35 ++++++++++++++++++++++-- app/services/fan_out_on_write_service.rb | 33 ++++++++++++++++------ app/services/process_hashtags_service.rb | 4 +-- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/app/models/status.rb b/app/models/status.rb index 21a0ac84f..0af3c45b5 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -35,6 +35,9 @@ class Status < ApplicationRecord include Cacheable include StatusThreadingConcern + LOCAL_DOMAINS = ENV.fetch('LOCAL_DOMAINS', '').chomp.split(/\.?\s+/).freeze + LOCAL_URIS = LOCAL_DOMAINS.map { |domain| "https://#{domain}/%" }.freeze + # If `override_timestamps` is set at creation time, Snowflake ID creation # will be based on current time instead of `created_at` attr_accessor :override_timestamps @@ -84,9 +87,11 @@ class Status < ApplicationRecord scope :recent, -> { reorder(id: :desc) } scope :remote, -> { where(local: false).or(where.not(uri: nil)) } scope :local, -> { where(local: true).or(where(uri: nil)) } + scope :network, -> { where(local: true).or(where(uri: nil)).or(where('statuses.uri LIKE ANY (array[?])', LOCAL_URIS)) } scope :without_replies, -> { where('statuses.reply = FALSE OR statuses.in_reply_to_account_id = statuses.account_id') } scope :without_reblogs, -> { where('statuses.reblog_of_id IS NULL') } + scope :reblogs, -> { where('statuses.reblog_of_id IS NOT NULL') } # all reblogs scope :with_public_visibility, -> { where(visibility: :public) } scope :tagged_with, ->(tag) { joins(:statuses_tags).where(statuses_tags: { tag_id: tag }) } scope :excluding_silenced_accounts, -> { left_outer_joins(:account).where(accounts: { silenced_at: nil }) } @@ -163,6 +168,14 @@ class Status < ApplicationRecord attributes['local'] || uri.nil? end + def network? + attributes['local'] || uri.nil? || account.domain.in?(LOCAL_DOMAINS) + end + + def relayed? + account.username == :relay && account.actor_type.in?(Set[:Application, :Service]) + end + def reblog? !reblog_of_id.nil? end @@ -333,8 +346,26 @@ class Status < ApplicationRecord end def as_public_timeline(account = nil, local_only = false) - query = timeline_scope(local_only) - query = query.without_replies unless Setting.show_replies_in_public_timelines + # instead of our ftl being a noisy irrelevant firehose + # only show public stuff boosted by the community + query = Status.network + if local_only then + # we don't want to change the ltl + query = query + .with_public_visibility + .without_replies + .without_reblogs + else # but on the ftl + query = query.without_replies unless Setting.show_replies_in_public_timelines + # grab the stuff we boosted + subquery = query.reblogs.select(:reblog_of_id) + .reorder(nil) + .distinct + # map those ids to actual statuses + # THIS QUERY IS EXPENSIVE AS FUCK!!!!!!! + # but it does the job + query = Status.where(id: subquery).with_public_visibility + end apply_timeline_filters(query, account, local_only) end diff --git a/app/services/fan_out_on_write_service.rb b/app/services/fan_out_on_write_service.rb index b66dc342e..2065acde3 100644 --- a/app/services/fan_out_on_write_service.rb +++ b/app/services/fan_out_on_write_service.rb @@ -21,15 +21,29 @@ class FanOutOnWriteService < BaseService deliver_to_lists(status) end - return if status.account.silenced? || !status.public_visibility? return if status.reblog? && !Setting.show_reblogs_in_public_timelines + return if status.account.silenced? - deliver_to_hashtags(status) + deliver_to_hashtags(status) if !status.reblog? && status.distributable? + # we want to let community users decide what goes on the ftl with boosts + return unless status.network? || status.relayed? + deliver_to_local = true + + if status.reblog? then + status = Status.find(status.reblog_of_id) + render_anonymous_payload(status) + deliver_to_local = status.network? + end + + return if status.account.silenced? || !status.public_visibility? return if status.reply? && status.in_reply_to_account_id != status.account_id && !Setting.show_replies_in_public_timelines - deliver_to_public(status) - deliver_to_media(status) if status.media_attachments.any? + if deliver_to_local then + deliver_to_local(status) + else + deliver_to_public(status) + end end private @@ -85,14 +99,15 @@ class FanOutOnWriteService < BaseService Rails.logger.debug "Delivering status #{status.id} to public timeline" Redis.current.publish('timeline:public', @payload) - Redis.current.publish('timeline:public:local', @payload) if status.local? + Redis.current.publish('timeline:public:media', @payload) if status.media_attachments.any? end - def deliver_to_media(status) - Rails.logger.debug "Delivering status #{status.id} to media timeline" + def deliver_to_local(status) + Rails.logger.debug "Delivering status #{status.id} to local timeline" - Redis.current.publish('timeline:public:media', @payload) - Redis.current.publish('timeline:public:local:media', @payload) if status.local? + return unless status.network? + Redis.current.publish('timeline:public:local', @payload) + Redis.current.publish('timeline:public:local:media', @payload) if status.media_attachments.any? end def deliver_to_direct_timelines(status) diff --git a/app/services/process_hashtags_service.rb b/app/services/process_hashtags_service.rb index d5ec076a8..fb89f6c5b 100644 --- a/app/services/process_hashtags_service.rb +++ b/app/services/process_hashtags_service.rb @@ -2,7 +2,7 @@ class ProcessHashtagsService < BaseService def call(status, tags = []) - tags = Extractor.extract_hashtags(status.text) if status.local? + tags = Extractor.extract_hashtags(status.text) if status.network? records = [] tags.map { |str| str.mb_chars.downcase }.uniq(&:to_s).each do |name| @@ -11,7 +11,7 @@ class ProcessHashtagsService < BaseService status.tags << tag records << tag - TrendingTags.record_use!(tag, status.account, status.created_at) if status.public_visibility? + TrendingTags.record_use!(tag, status.account, status.created_at) if status.distributable? end return unless status.public_visibility? || status.unlisted_visibility?