From 18004bf22723b677345f417b24729c7e17dac36e Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 22 Jan 2024 08:55:37 -0500 Subject: [PATCH] Add `Account.matches_uri_prefix` scope and use in activitypub/followers_synchronizations controller (#28820) --- .../followers_synchronizations_controller.rb | 2 +- app/models/account.rb | 1 + spec/models/account_spec.rb | 25 +++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app/controllers/activitypub/followers_synchronizations_controller.rb b/app/controllers/activitypub/followers_synchronizations_controller.rb index 976caa3445..d2942104e5 100644 --- a/app/controllers/activitypub/followers_synchronizations_controller.rb +++ b/app/controllers/activitypub/followers_synchronizations_controller.rb @@ -24,7 +24,7 @@ class ActivityPub::FollowersSynchronizationsController < ActivityPub::BaseContro end def set_items - @items = @account.followers.where(Account.arel_table[:uri].matches("#{Account.sanitize_sql_like(uri_prefix)}/%", false, true)).or(@account.followers.where(uri: uri_prefix)).pluck(:uri) + @items = @account.followers.matches_uri_prefix(uri_prefix).pluck(:uri) end def collection_presenter diff --git a/app/models/account.rb b/app/models/account.rb index 2fdfc2d514..05e1f943ca 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -123,6 +123,7 @@ class Account < ApplicationRecord scope :bots, -> { where(actor_type: %w(Application Service)) } scope :groups, -> { where(actor_type: 'Group') } scope :alphabetic, -> { order(domain: :asc, username: :asc) } + scope :matches_uri_prefix, ->(value) { where(arel_table[:uri].matches("#{sanitize_sql_like(value)}/%", false, true)).or(where(uri: value)) } scope :matches_username, ->(value) { where('lower((username)::text) LIKE lower(?)', "#{value}%") } scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) } scope :without_unapproved, -> { left_outer_joins(:user).merge(User.approved.confirmed).or(remote) } diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 8488ccea45..7ef5ca94cc 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -835,6 +835,31 @@ RSpec.describe Account do end describe 'scopes' do + describe 'matches_uri_prefix' do + let!(:alice) { Fabricate :account, domain: 'host.example', uri: 'https://host.example/user/a' } + let!(:bob) { Fabricate :account, domain: 'top-level.example', uri: 'https://top-level.example' } + + it 'returns accounts which start with the value' do + results = described_class.matches_uri_prefix('https://host.example') + + expect(results.size) + .to eq(1) + expect(results) + .to include(alice) + .and not_include(bob) + end + + it 'returns accounts which equal the value' do + results = described_class.matches_uri_prefix('https://top-level.example') + + expect(results.size) + .to eq(1) + expect(results) + .to include(bob) + .and not_include(alice) + end + end + describe 'auditable' do let!(:alice) { Fabricate :account } let!(:bob) { Fabricate :account }