From 94cbd808b5b3e7999c7e77dc724b7e8c9dd2bdec Mon Sep 17 00:00:00 2001 From: Vyr Cossont Date: Sat, 18 Mar 2023 23:47:54 -0700 Subject: Webhooks for local status.create, status.update, account.update (#24133) --- app/models/account.rb | 7 +++++++ app/models/status.rb | 11 +++++++++++ app/models/webhook.rb | 3 +++ 3 files changed, 21 insertions(+) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index 1ff083e54..c4df48878 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -122,6 +122,8 @@ class Account < ApplicationRecord scope :not_excluded_by_account, ->(account) { where.not(id: account.excluded_from_timeline_account_ids) } scope :not_domain_blocked_by_account, ->(account) { where(arel_table[:domain].eq(nil).or(arel_table[:domain].not_in(account.excluded_from_timeline_domains))) } + after_update_commit :trigger_update_webhooks + delegate :email, :unconfirmed_email, :current_sign_in_at, @@ -593,4 +595,9 @@ class Account < ApplicationRecord CanonicalEmailBlock.where(reference_account: self).delete_all end + + # NOTE: the `account.created` webhook is triggered by the `User` model, not `Account`. + def trigger_update_webhooks + TriggerWebhookWorker.perform_async('account.updated', 'Account', id) if local? + end end diff --git a/app/models/status.rb b/app/models/status.rb index e7ea191a8..2e32c3f16 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -111,6 +111,9 @@ class Status < ApplicationRecord where('NOT EXISTS (SELECT * FROM statuses_tags forbidden WHERE forbidden.status_id = statuses.id AND forbidden.tag_id IN (?))', tag_ids) } + after_create_commit :trigger_create_webhooks + after_update_commit :trigger_update_webhooks + cache_associated :application, :media_attachments, :conversation, @@ -535,4 +538,12 @@ class Status < ApplicationRecord reblog&.decrement_count!(:reblogs_count) if reblog? thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && distributable? end + + def trigger_create_webhooks + TriggerWebhookWorker.perform_async('status.created', 'Status', id) if local? + end + + def trigger_update_webhooks + TriggerWebhookWorker.perform_async('status.updated', 'Status', id) if local? + end end diff --git a/app/models/webhook.rb b/app/models/webhook.rb index 4aafb1257..9a056a386 100644 --- a/app/models/webhook.rb +++ b/app/models/webhook.rb @@ -17,7 +17,10 @@ class Webhook < ApplicationRecord EVENTS = %w( account.approved account.created + account.updated report.created + status.created + status.updated ).freeze scope :enabled, -> { where(enabled: true) } -- cgit From 38c84f57b6d2de1683974f91d726c30ba7f1491b Mon Sep 17 00:00:00 2001 From: Takeshi Umeda Date: Tue, 21 Mar 2023 18:32:58 +0900 Subject: Refactoring relations_map (#24195) --- app/models/concerns/account_interactions.rb | 15 +++++++++++++++ app/models/concerns/status_threading_concern.rb | 14 +------------- app/services/import_service.rb | 12 +----------- app/services/search_service.rb | 12 +----------- 4 files changed, 18 insertions(+), 35 deletions(-) (limited to 'app/models') diff --git a/app/models/concerns/account_interactions.rb b/app/models/concerns/account_interactions.rb index 1898516b0..48ab1349d 100644 --- a/app/models/concerns/account_interactions.rb +++ b/app/models/concerns/account_interactions.rb @@ -292,6 +292,21 @@ module AccountInteractions end end + def relations_map(account_ids, domains = nil, **options) + relations = { + blocked_by: Account.blocked_by_map(account_ids, id), + following: Account.following_map(account_ids, id), + } + + return relations if options[:skip_blocking_and_muting] + + relations.merge!({ + blocking: Account.blocking_map(account_ids, id), + muting: Account.muting_map(account_ids, id), + domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, id), + }) + end + private def remove_potential_friendship(other_account) diff --git a/app/models/concerns/status_threading_concern.rb b/app/models/concerns/status_threading_concern.rb index 8b628beea..2ca3b66c2 100644 --- a/app/models/concerns/status_threading_concern.rb +++ b/app/models/concerns/status_threading_concern.rb @@ -79,7 +79,7 @@ module StatusThreadingConcern statuses = Status.with_accounts(ids).to_a account_ids = statuses.map(&:account_id).uniq domains = statuses.filter_map(&:account_domain).uniq - relations = relations_map_for_account(account, account_ids, domains) + relations = account&.relations_map(account_ids, domains) || {} statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? } @@ -108,16 +108,4 @@ module StatusThreadingConcern arr end - - def relations_map_for_account(account, account_ids, domains) - return {} if account.nil? - - { - blocking: Account.blocking_map(account_ids, account.id), - blocked_by: Account.blocked_by_map(account_ids, account.id), - muting: Account.muting_map(account_ids, account.id), - following: Account.following_map(account_ids, account.id), - domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id), - } - end end diff --git a/app/services/import_service.rb b/app/services/import_service.rb index 940c236d4..56f191c1f 100644 --- a/app/services/import_service.rb +++ b/app/services/import_service.rb @@ -120,7 +120,7 @@ class ImportService < BaseService end account_ids = statuses.map(&:account_id) - preloaded_relations = relations_map_for_account(@account, account_ids) + preloaded_relations = @account.relations_map(account_ids, skip_blocking_and_muting: true) statuses.keep_if { |status| StatusPolicy.new(@account, status, preloaded_relations).show? } @@ -138,14 +138,4 @@ class ImportService < BaseService def import_data Paperclip.io_adapters.for(@import.data).read.force_encoding(Encoding::UTF_8) end - - def relations_map_for_account(account, account_ids) - { - blocking: {}, - blocked_by: Account.blocked_by_map(account_ids, account.id), - muting: {}, - following: Account.following_map(account_ids, account.id), - domain_blocking_by_domain: {}, - } - end end diff --git a/app/services/search_service.rb b/app/services/search_service.rb index 93b72fa0c..b1ce5453f 100644 --- a/app/services/search_service.rb +++ b/app/services/search_service.rb @@ -49,7 +49,7 @@ class SearchService < BaseService results = definition.limit(@limit).offset(@offset).objects.compact account_ids = results.map(&:account_id) account_domains = results.map(&:account_domain) - preloaded_relations = relations_map_for_account(@account, account_ids, account_domains) + preloaded_relations = @account.relations_map(account_ids, account_domains) results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? } rescue Faraday::ConnectionFailed, Parslet::ParseFailed @@ -111,16 +111,6 @@ class SearchService < BaseService @options[:type].blank? || @options[:type] == 'statuses' end - def relations_map_for_account(account, account_ids, domains) - { - blocking: Account.blocking_map(account_ids, account.id), - blocked_by: Account.blocked_by_map(account_ids, account.id), - muting: Account.muting_map(account_ids, account.id), - following: Account.following_map(account_ids, account.id), - domain_blocking_by_domain: Account.domain_blocking_map_by_domain(domains, account.id), - } - end - def parsed_query SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query)) end -- cgit From 2626097869c9520c21aaba73c9bcfe72c0f25f28 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 22 Mar 2023 15:47:44 +0100 Subject: Fix Rails cache namespace being overriden with `v2` for cached statuses (#24202) --- app/controllers/concerns/cache_concern.rb | 4 ++-- app/models/status.rb | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/controllers/concerns/cache_concern.rb b/app/controllers/concerns/cache_concern.rb index e606218ac..a5a9ba3e1 100644 --- a/app/controllers/concerns/cache_concern.rb +++ b/app/controllers/concerns/cache_concern.rb @@ -187,7 +187,7 @@ module CacheConcern return [] if raw.empty? cached_keys_with_value = begin - Rails.cache.read_multi(*raw, namespace: 'v2').transform_keys(&:id).transform_values { |r| ActiveRecordCoder.load(r) } + Rails.cache.read_multi(*raw).transform_keys(&:id).transform_values { |r| ActiveRecordCoder.load(r) } rescue ActiveRecordCoder::Error {} # The serialization format may have changed, let's pretend it's a cache miss. end @@ -200,7 +200,7 @@ module CacheConcern uncached = klass.where(id: uncached_ids).with_includes.index_by(&:id) uncached.each_value do |item| - Rails.cache.write(item, ActiveRecordCoder.dump(item), namespace: 'v2') + Rails.cache.write(item, ActiveRecordCoder.dump(item)) end end diff --git a/app/models/status.rb b/app/models/status.rb index 2e32c3f16..2757497db 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -140,6 +140,10 @@ class Status < ApplicationRecord REAL_TIME_WINDOW = 6.hours + def cache_key + "v2:#{super}" + end + def searchable_by(preloaded = nil) ids = [] -- cgit