From b5329e0035d455e72dad7249d88bd624b5cb59a0 Mon Sep 17 00:00:00 2001 From: Josh Soref <2119212+jsoref@users.noreply.github.com> Date: Sun, 6 Mar 2022 16:51:40 -0500 Subject: Spelling (#17705) * spelling: account Signed-off-by: Josh Soref * spelling: affiliated Signed-off-by: Josh Soref * spelling: appearance Signed-off-by: Josh Soref * spelling: autosuggest Signed-off-by: Josh Soref * spelling: cacheable Signed-off-by: Josh Soref * spelling: component Signed-off-by: Josh Soref * spelling: conversations Signed-off-by: Josh Soref * spelling: domain.example Clarify what's distinct and use RFC friendly domain space. Signed-off-by: Josh Soref * spelling: environment Signed-off-by: Josh Soref * spelling: exceeds Signed-off-by: Josh Soref * spelling: functional Signed-off-by: Josh Soref * spelling: inefficiency Signed-off-by: Josh Soref * spelling: not Signed-off-by: Josh Soref * spelling: notifications Signed-off-by: Josh Soref * spelling: occurring Signed-off-by: Josh Soref * spelling: position Signed-off-by: Josh Soref * spelling: progress Signed-off-by: Josh Soref * spelling: promotable Signed-off-by: Josh Soref * spelling: reblogging Signed-off-by: Josh Soref * spelling: repetitive Signed-off-by: Josh Soref * spelling: resolve Signed-off-by: Josh Soref * spelling: saturated Signed-off-by: Josh Soref * spelling: similar Signed-off-by: Josh Soref * spelling: strategies Signed-off-by: Josh Soref * spelling: success Signed-off-by: Josh Soref * spelling: targeting Signed-off-by: Josh Soref * spelling: thumbnails Signed-off-by: Josh Soref * spelling: unauthorized Signed-off-by: Josh Soref * spelling: unsensitizes Signed-off-by: Josh Soref * spelling: validations Signed-off-by: Josh Soref * spelling: various Signed-off-by: Josh Soref Co-authored-by: Josh Soref --- spec/controllers/accounts_controller_spec.rb | 8 +++---- .../activitypub/collections_controller_spec.rb | 6 ++--- .../activitypub/outboxes_controller_spec.rb | 6 ++--- .../activitypub/replies_controller_spec.rb | 4 ++-- .../api/v1/accounts/notes_controller_spec.rb | 2 +- .../api/v1/admin/accounts_controller_spec.rb | 2 +- .../favourited_by_accounts_controller_spec.rb | 2 +- .../reblogged_by_accounts_controller_spec.rb | 2 +- .../controllers/api/v1/statuses_controller_spec.rb | 4 ++-- spec/controllers/application_controller_spec.rb | 20 ++++++++--------- spec/controllers/statuses_controller_spec.rb | 6 ++--- spec/helpers/application_helper_spec.rb | 2 +- spec/lib/tag_manager_spec.rb | 26 +++++++++++----------- spec/models/report_spec.rb | 2 +- spec/policies/user_policy_spec.rb | 4 ++-- spec/services/unsuspend_account_service_spec.rb | 16 ++++++------- spec/support/stories/profile_stories.rb | 2 +- 17 files changed, 57 insertions(+), 57 deletions(-) (limited to 'spec') diff --git a/spec/controllers/accounts_controller_spec.rb b/spec/controllers/accounts_controller_spec.rb index 73d124029..662a89927 100644 --- a/spec/controllers/accounts_controller_spec.rb +++ b/spec/controllers/accounts_controller_spec.rb @@ -5,7 +5,7 @@ RSpec.describe AccountsController, type: :controller do let(:account) { Fabricate(:account) } - shared_examples 'cachable response' do + shared_examples 'cacheable response' do it 'does not set cookies' do expect(response.cookies).to be_empty expect(response.headers['Set-Cookies']).to be nil @@ -374,7 +374,7 @@ RSpec.describe AccountsController, type: :controller do expect(response.media_type).to eq 'application/activity+json' end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' it 'renders account' do json = body_as_json @@ -432,7 +432,7 @@ RSpec.describe AccountsController, type: :controller do expect(response.media_type).to eq 'application/activity+json' end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' it 'renders account' do json = body_as_json @@ -499,7 +499,7 @@ RSpec.describe AccountsController, type: :controller do expect(response).to have_http_status(200) end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' end context do diff --git a/spec/controllers/activitypub/collections_controller_spec.rb b/spec/controllers/activitypub/collections_controller_spec.rb index 21a033945..4d87f80ce 100644 --- a/spec/controllers/activitypub/collections_controller_spec.rb +++ b/spec/controllers/activitypub/collections_controller_spec.rb @@ -7,7 +7,7 @@ RSpec.describe ActivityPub::CollectionsController, type: :controller do let!(:private_pinned) { Fabricate(:status, account: account, text: 'secret private stuff', visibility: :private) } let(:remote_account) { nil } - shared_examples 'cachable response' do + shared_examples 'cacheable response' do it 'does not set cookies' do expect(response.cookies).to be_empty expect(response.headers['Set-Cookies']).to be nil @@ -48,7 +48,7 @@ RSpec.describe ActivityPub::CollectionsController, type: :controller do expect(response.media_type).to eq 'application/activity+json' end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' it 'returns orderedItems with pinned statuses' do expect(body[:orderedItems]).to be_an Array @@ -101,7 +101,7 @@ RSpec.describe ActivityPub::CollectionsController, type: :controller do expect(response.media_type).to eq 'application/activity+json' end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' it 'returns orderedItems with pinned statuses' do json = body_as_json diff --git a/spec/controllers/activitypub/outboxes_controller_spec.rb b/spec/controllers/activitypub/outboxes_controller_spec.rb index 1722690db..04f036447 100644 --- a/spec/controllers/activitypub/outboxes_controller_spec.rb +++ b/spec/controllers/activitypub/outboxes_controller_spec.rb @@ -3,7 +3,7 @@ require 'rails_helper' RSpec.describe ActivityPub::OutboxesController, type: :controller do let!(:account) { Fabricate(:account) } - shared_examples 'cachable response' do + shared_examples 'cacheable response' do it 'does not set cookies' do expect(response.cookies).to be_empty expect(response.headers['Set-Cookies']).to be nil @@ -53,7 +53,7 @@ RSpec.describe ActivityPub::OutboxesController, type: :controller do expect(body[:totalItems]).to eq 4 end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' it 'does not have a Vary header' do expect(response.headers['Vary']).to be_nil @@ -98,7 +98,7 @@ RSpec.describe ActivityPub::OutboxesController, type: :controller do expect(body[:orderedItems].all? { |item| item[:to].include?(ActivityPub::TagManager::COLLECTIONS[:public]) || item[:cc].include?(ActivityPub::TagManager::COLLECTIONS[:public]) }).to be true end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' it 'returns Vary header with Signature' do expect(response.headers['Vary']).to include 'Signature' diff --git a/spec/controllers/activitypub/replies_controller_spec.rb b/spec/controllers/activitypub/replies_controller_spec.rb index a2c7f336f..a35957f24 100644 --- a/spec/controllers/activitypub/replies_controller_spec.rb +++ b/spec/controllers/activitypub/replies_controller_spec.rb @@ -8,7 +8,7 @@ RSpec.describe ActivityPub::RepliesController, type: :controller do let(:remote_reply_id) { 'https://foobar.com/statuses/1234' } let(:remote_querier) { nil } - shared_examples 'cachable response' do + shared_examples 'cacheable response' do it 'does not set cookies' do expect(response.cookies).to be_empty expect(response.headers['Set-Cookies']).to be nil @@ -93,7 +93,7 @@ RSpec.describe ActivityPub::RepliesController, type: :controller do expect(response.media_type).to eq 'application/activity+json' end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' context 'without only_other_accounts' do it "returns items with thread author's replies" do diff --git a/spec/controllers/api/v1/accounts/notes_controller_spec.rb b/spec/controllers/api/v1/accounts/notes_controller_spec.rb index 47d595c70..42c2d8a86 100644 --- a/spec/controllers/api/v1/accounts/notes_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/notes_controller_spec.rb @@ -31,7 +31,7 @@ describe Api::V1::Accounts::NotesController do end end - context 'when account note exceends allowed length' do + context 'when account note exceeds allowed length' do let(:comment) { 'a' * 2_001 } it 'returns 422' do diff --git a/spec/controllers/api/v1/admin/accounts_controller_spec.rb b/spec/controllers/api/v1/admin/accounts_controller_spec.rb index bf79ee520..3f61bbc0b 100644 --- a/spec/controllers/api/v1/admin/accounts_controller_spec.rb +++ b/spec/controllers/api/v1/admin/accounts_controller_spec.rb @@ -140,7 +140,7 @@ RSpec.describe Api::V1::Admin::AccountsController, type: :controller do expect(response).to have_http_status(200) end - it 'unsensitives account' do + it 'unsensitizes account' do expect(account.reload.sensitized?).to be false end end diff --git a/spec/controllers/api/v1/statuses/favourited_by_accounts_controller_spec.rb b/spec/controllers/api/v1/statuses/favourited_by_accounts_controller_spec.rb index 439a4738d..7cc77f430 100644 --- a/spec/controllers/api/v1/statuses/favourited_by_accounts_controller_spec.rb +++ b/spec/controllers/api/v1/statuses/favourited_by_accounts_controller_spec.rb @@ -56,7 +56,7 @@ RSpec.describe Api::V1::Statuses::FavouritedByAccountsController, type: :control Fabricate(:favourite, status: status) end - it 'returns http unautharized' do + it 'returns http unauthorized' do get :index, params: { status_id: status.id } expect(response).to have_http_status(404) end diff --git a/spec/controllers/api/v1/statuses/reblogged_by_accounts_controller_spec.rb b/spec/controllers/api/v1/statuses/reblogged_by_accounts_controller_spec.rb index 31320349d..8d4a6f91c 100644 --- a/spec/controllers/api/v1/statuses/reblogged_by_accounts_controller_spec.rb +++ b/spec/controllers/api/v1/statuses/reblogged_by_accounts_controller_spec.rb @@ -56,7 +56,7 @@ RSpec.describe Api::V1::Statuses::RebloggedByAccountsController, type: :controll Fabricate(:status, reblog_of_id: status.id) end - it 'returns http unautharized' do + it 'returns http unauthorized' do get :index, params: { status_id: status.id } expect(response).to have_http_status(404) end diff --git a/spec/controllers/api/v1/statuses_controller_spec.rb b/spec/controllers/api/v1/statuses_controller_spec.rb index 190dfad11..2eb30af74 100644 --- a/spec/controllers/api/v1/statuses_controller_spec.rb +++ b/spec/controllers/api/v1/statuses_controller_spec.rb @@ -130,7 +130,7 @@ RSpec.describe Api::V1::StatusesController, type: :controller do let(:status) { Fabricate(:status, account: user.account, visibility: :private) } describe 'GET #show' do - it 'returns http unautharized' do + it 'returns http unauthorized' do get :show, params: { id: status.id } expect(response).to have_http_status(404) end @@ -141,7 +141,7 @@ RSpec.describe Api::V1::StatusesController, type: :controller do Fabricate(:status, account: user.account, thread: status) end - it 'returns http unautharized' do + it 'returns http unauthorized' do get :context, params: { id: status.id } expect(response).to have_http_status(404) end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb index 0fb4ddbcf..53e163d49 100644 --- a/spec/controllers/application_controller_spec.rb +++ b/spec/controllers/application_controller_spec.rb @@ -187,30 +187,30 @@ describe ApplicationController, type: :controller do controller do before_action :require_admin! - def sucesss + def success head 200 end end before do - routes.draw { get 'sucesss' => 'anonymous#sucesss' } + routes.draw { get 'success' => 'anonymous#success' } end it 'returns a 403 if current user is not admin' do sign_in(Fabricate(:user, admin: false)) - get 'sucesss' + get 'success' expect(response).to have_http_status(403) end it 'returns a 403 if current user is only a moderator' do sign_in(Fabricate(:user, moderator: true)) - get 'sucesss' + get 'success' expect(response).to have_http_status(403) end it 'does nothing if current user is admin' do sign_in(Fabricate(:user, admin: true)) - get 'sucesss' + get 'success' expect(response).to have_http_status(200) end end @@ -219,30 +219,30 @@ describe ApplicationController, type: :controller do controller do before_action :require_staff! - def sucesss + def success head 200 end end before do - routes.draw { get 'sucesss' => 'anonymous#sucesss' } + routes.draw { get 'success' => 'anonymous#success' } end it 'returns a 403 if current user is not admin or moderator' do sign_in(Fabricate(:user, admin: false, moderator: false)) - get 'sucesss' + get 'success' expect(response).to have_http_status(403) end it 'does nothing if current user is moderator' do sign_in(Fabricate(:user, moderator: true)) - get 'sucesss' + get 'success' expect(response).to have_http_status(200) end it 'does nothing if current user is admin' do sign_in(Fabricate(:user, admin: true)) - get 'sucesss' + get 'success' expect(response).to have_http_status(200) end end diff --git a/spec/controllers/statuses_controller_spec.rb b/spec/controllers/statuses_controller_spec.rb index 9986efa51..05fae67fa 100644 --- a/spec/controllers/statuses_controller_spec.rb +++ b/spec/controllers/statuses_controller_spec.rb @@ -5,7 +5,7 @@ require 'rails_helper' describe StatusesController do render_views - shared_examples 'cachable response' do + shared_examples 'cacheable response' do it 'does not set cookies' do expect(response.cookies).to be_empty expect(response.headers['Set-Cookies']).to be nil @@ -108,7 +108,7 @@ describe StatusesController do expect(response.headers['Vary']).to eq 'Accept' end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' it 'returns Content-Type header' do expect(response.headers['Content-Type']).to include 'application/activity+json' @@ -496,7 +496,7 @@ describe StatusesController do expect(response.headers['Vary']).to eq 'Accept' end - it_behaves_like 'cachable response' + it_behaves_like 'cacheable response' it 'returns Content-Type header' do expect(response.headers['Content-Type']).to include 'application/activity+json' diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index f09e32ecc..b9d38d8c6 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -60,7 +60,7 @@ describe ApplicationHelper do end describe 'favicon_path' do - it 'returns /favicon.ico on production enviromnent' do + it 'returns /favicon.ico on production environment' do expect(Rails.env).to receive(:production?).and_return(true) expect(helper.favicon_path).to eq '/favicon.ico' end diff --git a/spec/lib/tag_manager_spec.rb b/spec/lib/tag_manager_spec.rb index 2230f9710..cd9fb936c 100644 --- a/spec/lib/tag_manager_spec.rb +++ b/spec/lib/tag_manager_spec.rb @@ -6,7 +6,7 @@ RSpec.describe TagManager do around do |example| original_local_domain = Rails.configuration.x.local_domain - Rails.configuration.x.local_domain = 'domain.test' + Rails.configuration.x.local_domain = 'domain.example.com' example.run @@ -18,11 +18,11 @@ RSpec.describe TagManager do end it 'returns true if the slash-stripped string equals to local domain' do - expect(TagManager.instance.local_domain?('DoMaIn.Test/')).to eq true + expect(TagManager.instance.local_domain?('DoMaIn.Example.com/')).to eq true end it 'returns false for irrelevant string' do - expect(TagManager.instance.local_domain?('DoMaIn.Test!')).to eq false + expect(TagManager.instance.local_domain?('DoMaIn.Example.com!')).to eq false end end @@ -31,7 +31,7 @@ RSpec.describe TagManager do around do |example| original_web_domain = Rails.configuration.x.web_domain - Rails.configuration.x.web_domain = 'domain.test' + Rails.configuration.x.web_domain = 'domain.example.com' example.run @@ -43,11 +43,11 @@ RSpec.describe TagManager do end it 'returns true if the slash-stripped string equals to web domain' do - expect(TagManager.instance.web_domain?('DoMaIn.Test/')).to eq true + expect(TagManager.instance.web_domain?('DoMaIn.Example.com/')).to eq true end it 'returns false for string with irrelevant characters' do - expect(TagManager.instance.web_domain?('DoMaIn.Test!')).to eq false + expect(TagManager.instance.web_domain?('DoMaIn.Example.com!')).to eq false end end @@ -57,7 +57,7 @@ RSpec.describe TagManager do end it 'returns normalized domain' do - expect(TagManager.instance.normalize_domain('DoMaIn.Test/')).to eq 'domain.test' + expect(TagManager.instance.normalize_domain('DoMaIn.Example.com/')).to eq 'domain.example.com' end end @@ -69,18 +69,18 @@ RSpec.describe TagManager do end it 'returns true if the normalized string with port is local URL' do - Rails.configuration.x.web_domain = 'domain.test:42' - expect(TagManager.instance.local_url?('https://DoMaIn.Test:42/')).to eq true + Rails.configuration.x.web_domain = 'domain.example.com:42' + expect(TagManager.instance.local_url?('https://DoMaIn.Example.com:42/')).to eq true end it 'returns true if the normalized string without port is local URL' do - Rails.configuration.x.web_domain = 'domain.test' - expect(TagManager.instance.local_url?('https://DoMaIn.Test/')).to eq true + Rails.configuration.x.web_domain = 'domain.example.com' + expect(TagManager.instance.local_url?('https://DoMaIn.Example.com/')).to eq true end it 'returns false for string with irrelevant characters' do - Rails.configuration.x.web_domain = 'domain.test' - expect(TagManager.instance.local_url?('https://domainn.test/')).to eq false + Rails.configuration.x.web_domain = 'domain.example.com' + expect(TagManager.instance.local_url?('https://domain.example.net/')).to eq false end end end diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb index 3d29c0219..df32a7c9d 100644 --- a/spec/models/report_spec.rb +++ b/spec/models/report_spec.rb @@ -119,7 +119,7 @@ describe Report do end end - describe 'validatiions' do + describe 'validations' do it 'has a valid fabricator' do report = Fabricate(:report) report.valid? diff --git a/spec/policies/user_policy_spec.rb b/spec/policies/user_policy_spec.rb index 1933ee014..731c041d1 100644 --- a/spec/policies/user_policy_spec.rb +++ b/spec/policies/user_policy_spec.rb @@ -114,13 +114,13 @@ RSpec.describe UserPolicy do permissions :promote? do context 'admin?' do - context 'promoteable?' do + context 'promotable?' do it 'permits' do expect(subject).to permit(admin, john.user) end end - context '!promoteable?' do + context '!promotable?' do it 'denies' do expect(subject).to_not permit(admin, admin.user) end diff --git a/spec/services/unsuspend_account_service_spec.rb b/spec/services/unsuspend_account_service_spec.rb index d52cb6cc0..0593beb6f 100644 --- a/spec/services/unsuspend_account_service_spec.rb +++ b/spec/services/unsuspend_account_service_spec.rb @@ -63,20 +63,20 @@ RSpec.describe UnsuspendAccountService, type: :service do describe 'unsuspending a remote account' do include_examples 'common behavior' do let!(:account) { Fabricate(:account, domain: 'bob.com', uri: 'https://bob.com', inbox_url: 'https://bob.com/inbox', protocol: :activitypub) } - let!(:reslove_account_service) { double } + let!(:resolve_account_service) { double } before do - allow(ResolveAccountService).to receive(:new).and_return(reslove_account_service) + allow(ResolveAccountService).to receive(:new).and_return(resolve_account_service) end context 'when the account is not remotely suspended' do before do - allow(reslove_account_service).to receive(:call).with(account).and_return(account) + allow(resolve_account_service).to receive(:call).with(account).and_return(account) end it 're-fetches the account' do subject.call - expect(reslove_account_service).to have_received(:call).with(account) + expect(resolve_account_service).to have_received(:call).with(account) end it "merges back into local followers' feeds" do @@ -92,7 +92,7 @@ RSpec.describe UnsuspendAccountService, type: :service do context 'when the account is remotely suspended' do before do - allow(reslove_account_service).to receive(:call).with(account) do |account| + allow(resolve_account_service).to receive(:call).with(account) do |account| account.suspend!(origin: :remote) account end @@ -100,7 +100,7 @@ RSpec.describe UnsuspendAccountService, type: :service do it 're-fetches the account' do subject.call - expect(reslove_account_service).to have_received(:call).with(account) + expect(resolve_account_service).to have_received(:call).with(account) end it "does not merge back into local followers' feeds" do @@ -116,12 +116,12 @@ RSpec.describe UnsuspendAccountService, type: :service do context 'when the account is remotely deleted' do before do - allow(reslove_account_service).to receive(:call).with(account).and_return(nil) + allow(resolve_account_service).to receive(:call).with(account).and_return(nil) end it 're-fetches the account' do subject.call - expect(reslove_account_service).to have_received(:call).with(account) + expect(resolve_account_service).to have_received(:call).with(account) end it "does not merge back into local followers' feeds" do diff --git a/spec/support/stories/profile_stories.rb b/spec/support/stories/profile_stories.rb index 75b413330..0c4a14d1c 100644 --- a/spec/support/stories/profile_stories.rb +++ b/spec/support/stories/profile_stories.rb @@ -22,7 +22,7 @@ module ProfileStories def with_alice_as_local_user @alice_bio = '@alice and @bob are fictional characters commonly used as'\ 'placeholder names in #cryptology, as well as #science and'\ - 'engineering 📖 literature. Not affilated with @pepe.' + 'engineering 📖 literature. Not affiliated with @pepe.' @alice = Fabricate( :user, -- cgit From edf09ec747ebba5a170e27eb13663462a116ec6c Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 7 Mar 2022 09:36:47 +0100 Subject: Add `/api/v1/accounts/familiar_followers` to REST API (#17700) * Add `/api/v1/accounts/familiar_followers` to REST API * Change hide network preference to be stored consistently for local and remote accounts * Add dummy classes to migration * Apply suggestions from code review Co-authored-by: Claire Co-authored-by: Claire --- .../v1/accounts/familiar_followers_controller.rb | 25 ++++++++++ app/controllers/follower_accounts_controller.rb | 6 +-- app/controllers/following_accounts_controller.rb | 6 +-- app/controllers/settings/preferences_controller.rb | 1 - app/controllers/settings/profiles_controller.rb | 2 +- app/lib/user_settings_decorator.rb | 5 -- app/models/account.rb | 4 +- app/models/user.rb | 6 +-- app/presenters/familiar_followers_presenter.rb | 17 +++++++ .../rest/familiar_followers_serializer.rb | 11 ++++ app/views/follower_accounts/index.html.haml | 2 +- app/views/following_accounts/index.html.haml | 2 +- .../settings/preferences/other/show.html.haml | 3 -- app/views/settings/profiles/show.html.haml | 5 +- config/locales/simple_form.en.yml | 3 +- config/routes.rb | 1 + config/settings.yml | 1 - ...220304195405_migrate_hide_network_preference.rb | 37 ++++++++++++++ db/schema.rb | 2 +- .../follower_accounts_controller_spec.rb | 2 +- .../following_accounts_controller_spec.rb | 2 +- .../familiar_followers_presenter_spec.rb | 58 ++++++++++++++++++++++ 22 files changed, 169 insertions(+), 32 deletions(-) create mode 100644 app/controllers/api/v1/accounts/familiar_followers_controller.rb create mode 100644 app/presenters/familiar_followers_presenter.rb create mode 100644 app/serializers/rest/familiar_followers_serializer.rb create mode 100644 db/migrate/20220304195405_migrate_hide_network_preference.rb create mode 100644 spec/presenters/familiar_followers_presenter_spec.rb (limited to 'spec') diff --git a/app/controllers/api/v1/accounts/familiar_followers_controller.rb b/app/controllers/api/v1/accounts/familiar_followers_controller.rb new file mode 100644 index 000000000..b0bd8018a --- /dev/null +++ b/app/controllers/api/v1/accounts/familiar_followers_controller.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +class Api::V1::Accounts::FamiliarFollowersController < Api::BaseController + before_action -> { doorkeeper_authorize! :read, :'read:follows' } + before_action :require_user! + before_action :set_accounts + + def index + render json: familiar_followers.accounts, each_serializer: REST::FamiliarFollowersSerializer + end + + private + + def set_accounts + @accounts = Account.without_suspended.where(id: account_ids).select('id, hide_collections').index_by(&:id).values_at(*account_ids).compact + end + + def familiar_followers + FamiliarFollowersPresenter.new(@accounts, current_user.account_id) + end + + def account_ids + Array(params[:id]).map(&:to_i) + end +end diff --git a/app/controllers/follower_accounts_controller.rb b/app/controllers/follower_accounts_controller.rb index b3589a39f..f3f8336c9 100644 --- a/app/controllers/follower_accounts_controller.rb +++ b/app/controllers/follower_accounts_controller.rb @@ -15,13 +15,13 @@ class FollowerAccountsController < ApplicationController format.html do expires_in 0, public: true unless user_signed_in? - next if @account.user_hides_network? + next if @account.hide_collections? follows end format.json do - raise Mastodon::NotPermittedError if page_requested? && @account.user_hides_network? + raise Mastodon::NotPermittedError if page_requested? && @account.hide_collections? expires_in(page_requested? ? 0 : 3.minutes, public: public_fetch_mode?) @@ -82,7 +82,7 @@ class FollowerAccountsController < ApplicationController end def restrict_fields_to - if page_requested? || !@account.user_hides_network? + if page_requested? || !@account.hide_collections? # Return all fields else %i(id type total_items) diff --git a/app/controllers/following_accounts_controller.rb b/app/controllers/following_accounts_controller.rb index 8a72dc475..9d7f4c9bf 100644 --- a/app/controllers/following_accounts_controller.rb +++ b/app/controllers/following_accounts_controller.rb @@ -15,13 +15,13 @@ class FollowingAccountsController < ApplicationController format.html do expires_in 0, public: true unless user_signed_in? - next if @account.user_hides_network? + next if @account.hide_collections? follows end format.json do - raise Mastodon::NotPermittedError if page_requested? && @account.user_hides_network? + raise Mastodon::NotPermittedError if page_requested? && @account.hide_collections? expires_in(page_requested? ? 0 : 3.minutes, public: public_fetch_mode?) @@ -82,7 +82,7 @@ class FollowingAccountsController < ApplicationController end def restrict_fields_to - if page_requested? || !@account.user_hides_network? + if page_requested? || !@account.hide_collections? # Return all fields else %i(id type total_items) diff --git a/app/controllers/settings/preferences_controller.rb b/app/controllers/settings/preferences_controller.rb index 32b5d7948..c7492700c 100644 --- a/app/controllers/settings/preferences_controller.rb +++ b/app/controllers/settings/preferences_controller.rb @@ -47,7 +47,6 @@ class Settings::PreferencesController < Settings::BaseController :setting_system_font_ui, :setting_noindex, :setting_theme, - :setting_hide_network, :setting_aggregate_reblogs, :setting_show_application, :setting_advanced_layout, diff --git a/app/controllers/settings/profiles_controller.rb b/app/controllers/settings/profiles_controller.rb index 0c15447a6..be5b4f302 100644 --- a/app/controllers/settings/profiles_controller.rb +++ b/app/controllers/settings/profiles_controller.rb @@ -20,7 +20,7 @@ class Settings::ProfilesController < Settings::BaseController private def account_params - params.require(:account).permit(:display_name, :note, :avatar, :header, :locked, :bot, :discoverable, fields_attributes: [:name, :value]) + params.require(:account).permit(:display_name, :note, :avatar, :header, :locked, :bot, :discoverable, :hide_collections, fields_attributes: [:name, :value]) end def set_account diff --git a/app/lib/user_settings_decorator.rb b/app/lib/user_settings_decorator.rb index e37bc6d9f..de054e403 100644 --- a/app/lib/user_settings_decorator.rb +++ b/app/lib/user_settings_decorator.rb @@ -31,7 +31,6 @@ class UserSettingsDecorator user.settings['system_font_ui'] = system_font_ui_preference if change?('setting_system_font_ui') user.settings['noindex'] = noindex_preference if change?('setting_noindex') user.settings['theme'] = theme_preference if change?('setting_theme') - user.settings['hide_network'] = hide_network_preference if change?('setting_hide_network') user.settings['aggregate_reblogs'] = aggregate_reblogs_preference if change?('setting_aggregate_reblogs') user.settings['show_application'] = show_application_preference if change?('setting_show_application') user.settings['advanced_layout'] = advanced_layout_preference if change?('setting_advanced_layout') @@ -97,10 +96,6 @@ class UserSettingsDecorator boolean_cast_setting 'setting_noindex' end - def hide_network_preference - boolean_cast_setting 'setting_hide_network' - end - def show_application_preference boolean_cast_setting 'setting_show_application' end diff --git a/app/models/account.rb b/app/models/account.rb index dfdf9045f..1717f1605 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -349,11 +349,11 @@ class Account < ApplicationRecord end def hides_followers? - hide_collections? || user_hides_network? + hide_collections? end def hides_following? - hide_collections? || user_hides_network? + hide_collections? end def object_type diff --git a/app/models/user.rb b/app/models/user.rb index bbf850d84..146bdcd2a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -126,7 +126,7 @@ class User < ApplicationRecord has_many :session_activations, dependent: :destroy delegate :auto_play_gif, :default_sensitive, :unfollow_modal, :boost_modal, :delete_modal, - :reduce_motion, :system_font_ui, :noindex, :theme, :display_media, :hide_network, + :reduce_motion, :system_font_ui, :noindex, :theme, :display_media, :expand_spoilers, :default_language, :aggregate_reblogs, :show_application, :advanced_layout, :use_blurhash, :use_pending_items, :trends, :crop_images, :disable_swiping, @@ -273,10 +273,6 @@ class User < ApplicationRecord settings.notification_emails['trending_tag'] end - def hides_network? - @hides_network ||= settings.hide_network - end - def aggregates_reblogs? @aggregates_reblogs ||= settings.aggregate_reblogs end diff --git a/app/presenters/familiar_followers_presenter.rb b/app/presenters/familiar_followers_presenter.rb new file mode 100644 index 000000000..c1d944b80 --- /dev/null +++ b/app/presenters/familiar_followers_presenter.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class FamiliarFollowersPresenter + class Result < ActiveModelSerializers::Model + attributes :id, :accounts + end + + def initialize(accounts, current_account_id) + @accounts = accounts + @current_account_id = current_account_id + end + + def accounts + map = Follow.includes(account: :account_stat).where(target_account_id: @accounts.map(&:id)).where(account_id: Follow.where(account_id: @current_account_id).joins(:target_account).merge(Account.where(hide_collections: [nil, false])).select(:target_account_id)).group_by(&:target_account_id) + @accounts.map { |account| Result.new(id: account.id, accounts: (account.hide_collections? ? [] : (map[account.id] || [])).map(&:account)) } + end +end diff --git a/app/serializers/rest/familiar_followers_serializer.rb b/app/serializers/rest/familiar_followers_serializer.rb new file mode 100644 index 000000000..0a7e923f8 --- /dev/null +++ b/app/serializers/rest/familiar_followers_serializer.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class REST::FamiliarFollowersSerializer < ActiveModel::Serializer + attribute :id + + has_many :accounts, serializer: REST::AccountSerializer + + def id + object.id.to_s + end +end diff --git a/app/views/follower_accounts/index.html.haml b/app/views/follower_accounts/index.html.haml index 645dd2de1..92de35a9f 100644 --- a/app/views/follower_accounts/index.html.haml +++ b/app/views/follower_accounts/index.html.haml @@ -7,7 +7,7 @@ = render 'accounts/header', account: @account -- if @account.user_hides_network? +- if @account.hide_collections? .nothing-here= t('accounts.network_hidden') - elsif user_signed_in? && @account.blocking?(current_account) .nothing-here= t('accounts.unavailable') diff --git a/app/views/following_accounts/index.html.haml b/app/views/following_accounts/index.html.haml index 17fe79018..9bb1a9edd 100644 --- a/app/views/following_accounts/index.html.haml +++ b/app/views/following_accounts/index.html.haml @@ -7,7 +7,7 @@ = render 'accounts/header', account: @account -- if @account.user_hides_network? +- if @account.hide_collections? .nothing-here= t('accounts.network_hidden') - elsif user_signed_in? && @account.blocking?(current_account) .nothing-here= t('accounts.unavailable') diff --git a/app/views/settings/preferences/other/show.html.haml b/app/views/settings/preferences/other/show.html.haml index b7ae3d2ef..44f4af2eb 100644 --- a/app/views/settings/preferences/other/show.html.haml +++ b/app/views/settings/preferences/other/show.html.haml @@ -10,9 +10,6 @@ .fields-group = f.input :setting_noindex, as: :boolean, wrapper: :with_label - .fields-group - = f.input :setting_hide_network, as: :boolean, wrapper: :with_label - .fields-group = f.input :setting_aggregate_reblogs, as: :boolean, wrapper: :with_label, recommended: true diff --git a/app/views/settings/profiles/show.html.haml b/app/views/settings/profiles/show.html.haml index d325a9ea5..fe9666d84 100644 --- a/app/views/settings/profiles/show.html.haml +++ b/app/views/settings/profiles/show.html.haml @@ -30,7 +30,10 @@ = f.input :bot, as: :boolean, wrapper: :with_label, hint: t('simple_form.hints.defaults.bot') .fields-group - = f.input :discoverable, as: :boolean, wrapper: :with_label, hint: t(Setting.profile_directory ? 'simple_form.hints.defaults.discoverable' : 'simple_form.hints.defaults.discoverable_no_directory'), recommended: true + = f.input :discoverable, as: :boolean, wrapper: :with_label, hint: t('simple_form.hints.defaults.discoverable'), recommended: true + + .fields-group + = f.input :hide_collections, as: :boolean, wrapper: :with_label, label: t('simple_form.labels.defaults.setting_hide_network'), hint: t('simple_form.hints.defaults.setting_hide_network') %hr.spacer/ diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml index c5e75b408..b19b7891f 100644 --- a/config/locales/simple_form.en.yml +++ b/config/locales/simple_form.en.yml @@ -37,8 +37,7 @@ en: current_password: For security purposes please enter the password of the current account current_username: To confirm, please enter the username of the current account digest: Only sent after a long period of inactivity and only if you have received any personal messages in your absence - discoverable: Allow your account to be discovered by strangers through recommendations, profile directory and other features - discoverable_no_directory: Allow your account to be discovered by strangers through recommendations and other features + discoverable: Allow your account to be discovered by strangers through recommendations, trends and other features email: You will be sent a confirmation e-mail fields: You can have up to 4 items displayed as a table on your profile header: PNG, GIF or JPG. At most %{size}. Will be downscaled to %{dimensions}px diff --git a/config/routes.rb b/config/routes.rb index 25eb1558f..9e2f7a648 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -493,6 +493,7 @@ Rails.application.routes.draw do resource :search, only: :show, controller: :search resource :lookup, only: :show, controller: :lookup resources :relationships, only: :index + resources :familiar_followers, only: :index end resources :accounts, only: [:create, :show] do diff --git a/config/settings.yml b/config/settings.yml index e63788ba2..06dd2b3f3 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -17,7 +17,6 @@ defaults: &defaults timeline_preview: true show_staff_badge: true default_sensitive: false - hide_network: false unfollow_modal: false boost_modal: false delete_modal: true diff --git a/db/migrate/20220304195405_migrate_hide_network_preference.rb b/db/migrate/20220304195405_migrate_hide_network_preference.rb new file mode 100644 index 000000000..102ee46d6 --- /dev/null +++ b/db/migrate/20220304195405_migrate_hide_network_preference.rb @@ -0,0 +1,37 @@ +class MigrateHideNetworkPreference < ActiveRecord::Migration[6.1] + disable_ddl_transaction! + + # Dummy classes, to make migration possible across version changes + class Account < ApplicationRecord + has_one :user, inverse_of: :account + scope :local, -> { where(domain: nil) } + end + + class User < ApplicationRecord + belongs_to :account + end + + def up + Account.reset_column_information + + Setting.unscoped.where(thing_type: 'User', var: 'hide_network').find_each do |setting| + account = User.find(setting.thing_id).account + + ApplicationRecord.transaction do + account.update(hide_collections: setting.value) + setting.delete + end + rescue ActiveRecord::RecordNotFound + next + end + end + + def down + Account.local.where(hide_collections: true).includes(:user).find_each do |account| + ApplicationRecord.transaction do + Setting.create(thing_type: 'User', thing_id: account.user.id, var: 'hide_network', value: account.hide_collections?) + account.update(hide_collections: nil) + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 756e5e9ab..3666804ee 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2022_02_27_041951) do +ActiveRecord::Schema.define(version: 2022_03_04_195405) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/spec/controllers/follower_accounts_controller_spec.rb b/spec/controllers/follower_accounts_controller_spec.rb index eb095cf30..4d2a6e01a 100644 --- a/spec/controllers/follower_accounts_controller_spec.rb +++ b/spec/controllers/follower_accounts_controller_spec.rb @@ -103,7 +103,7 @@ describe FollowerAccountsController do context 'when account hides their network' do before do - alice.user.settings.hide_network = true + alice.update(hide_collections: true) end it 'returns followers count' do diff --git a/spec/controllers/following_accounts_controller_spec.rb b/spec/controllers/following_accounts_controller_spec.rb index af5ce0787..bb6d221ca 100644 --- a/spec/controllers/following_accounts_controller_spec.rb +++ b/spec/controllers/following_accounts_controller_spec.rb @@ -103,7 +103,7 @@ describe FollowingAccountsController do context 'when account hides their network' do before do - alice.user.settings.hide_network = true + alice.update(hide_collections: true) end it 'returns followers count' do diff --git a/spec/presenters/familiar_followers_presenter_spec.rb b/spec/presenters/familiar_followers_presenter_spec.rb new file mode 100644 index 000000000..17be4b971 --- /dev/null +++ b/spec/presenters/familiar_followers_presenter_spec.rb @@ -0,0 +1,58 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe FamiliarFollowersPresenter do + describe '#accounts' do + let(:account) { Fabricate(:account) } + let(:familiar_follower) { Fabricate(:account) } + let(:requested_accounts) { Fabricate.times(2, :account) } + + subject { described_class.new(requested_accounts, account.id) } + + before do + familiar_follower.follow!(requested_accounts.first) + account.follow!(familiar_follower) + end + + it 'returns a result for each requested account' do + expect(subject.accounts.map(&:id)).to eq requested_accounts.map(&:id) + end + + it 'returns followers you follow' do + result = subject.accounts.first + + expect(result).to_not be_nil + expect(result.id).to eq requested_accounts.first.id + expect(result.accounts).to match_array([familiar_follower]) + end + + context 'when requested account hides followers' do + before do + requested_accounts.first.update(hide_collections: true) + end + + it 'does not return followers you follow' do + result = subject.accounts.first + + expect(result).to_not be_nil + expect(result.id).to eq requested_accounts.first.id + expect(result.accounts).to be_empty + end + end + + context 'when familiar follower hides follows' do + before do + familiar_follower.update(hide_collections: true) + end + + it 'does not return followers you follow' do + result = subject.accounts.first + + expect(result).to_not be_nil + expect(result.id).to eq requested_accounts.first.id + expect(result.accounts).to be_empty + end + end + end +end -- cgit From 8f6c67bfdeddd1c2c1085067e3dc549fb53f6ff4 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 8 Mar 2022 09:14:39 +0100 Subject: Fix performance of account timelines (#17709) * Fix performance of account timelines * Various fixes and improvements * Fix duplicate results being returned Co-authored-by: Claire * Fix grouping for pinned statuses scope Co-authored-by: Claire --- app/controllers/activitypub/outboxes_controller.rb | 2 +- .../api/v1/accounts/statuses_controller.rb | 41 +--- app/models/account_statuses_filter.rb | 134 ++++++++++++ app/models/status.rb | 22 -- spec/models/account_statuses_filter_spec.rb | 229 +++++++++++++++++++++ spec/models/status_spec.rb | 53 ----- 6 files changed, 366 insertions(+), 115 deletions(-) create mode 100644 app/models/account_statuses_filter.rb create mode 100644 spec/models/account_statuses_filter_spec.rb (limited to 'spec') diff --git a/app/controllers/activitypub/outboxes_controller.rb b/app/controllers/activitypub/outboxes_controller.rb index b2aab56a5..cd3992502 100644 --- a/app/controllers/activitypub/outboxes_controller.rb +++ b/app/controllers/activitypub/outboxes_controller.rb @@ -62,7 +62,7 @@ class ActivityPub::OutboxesController < ActivityPub::BaseController return unless page_requested? @statuses = cache_collection_paginated_by_id( - @account.statuses.permitted_for(@account, signed_request_account), + AccountStatusesFilter.new(@account, signed_request_account).results, Status, LIMIT, params_slice(:max_id, :min_id, :since_id) diff --git a/app/controllers/api/v1/accounts/statuses_controller.rb b/app/controllers/api/v1/accounts/statuses_controller.rb index 2c027ea76..38c9f5a20 100644 --- a/app/controllers/api/v1/accounts/statuses_controller.rb +++ b/app/controllers/api/v1/accounts/statuses_controller.rb @@ -22,53 +22,16 @@ class Api::V1::Accounts::StatusesController < Api::BaseController end def cached_account_statuses - statuses = truthy_param?(:pinned) ? pinned_scope : permitted_account_statuses - - statuses.merge!(only_media_scope) if truthy_param?(:only_media) - statuses.merge!(no_replies_scope) if truthy_param?(:exclude_replies) - statuses.merge!(no_reblogs_scope) if truthy_param?(:exclude_reblogs) - statuses.merge!(hashtag_scope) if params[:tagged].present? - cache_collection_paginated_by_id( - statuses, + AccountStatusesFilter.new(@account, current_account, params).results, Status, limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id) ) end - def permitted_account_statuses - @account.statuses.permitted_for(@account, current_account) - end - - def only_media_scope - Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id) - end - - def pinned_scope - @account.pinned_statuses.permitted_for(@account, current_account) - end - - def no_replies_scope - Status.without_replies - end - - def no_reblogs_scope - Status.without_reblogs - end - - def hashtag_scope - tag = Tag.find_normalized(params[:tagged]) - - if tag - Status.tagged_with(tag.id) - else - Status.none - end - end - def pagination_params(core_params) - params.slice(:limit, :only_media, :exclude_replies).permit(:limit, :only_media, :exclude_replies).merge(core_params) + params.slice(:limit, *AccountStatusesFilter::KEYS).permit(:limit, *AccountStatusesFilter::KEYS).merge(core_params) end def insert_pagination_headers diff --git a/app/models/account_statuses_filter.rb b/app/models/account_statuses_filter.rb new file mode 100644 index 000000000..211f41478 --- /dev/null +++ b/app/models/account_statuses_filter.rb @@ -0,0 +1,134 @@ +# frozen_string_literal: true + +class AccountStatusesFilter + KEYS = %i( + pinned + tagged + only_media + exclude_replies + exclude_reblogs + ).freeze + + attr_reader :params, :account, :current_account + + def initialize(account, current_account, params = {}) + @account = account + @current_account = current_account + @params = params + end + + def results + scope = initial_scope + + scope.merge!(pinned_scope) if pinned? + scope.merge!(only_media_scope) if only_media? + scope.merge!(no_replies_scope) if exclude_replies? + scope.merge!(no_reblogs_scope) if exclude_reblogs? + scope.merge!(hashtag_scope) if tagged? + + scope + end + + private + + def initial_scope + if suspended? + Status.none + elsif anonymous? + account.statuses.where(visibility: %i(public unlisted)) + elsif author? + account.statuses.all # NOTE: #merge! does not work without the #all + elsif blocked? + Status.none + else + filtered_scope + end + end + + def filtered_scope + scope = account.statuses.left_outer_joins(:mentions) + + scope.merge!(scope.where(visibility: follower? ? %i(public unlisted private) : %i(public unlisted)).or(scope.where(mentions: { account_id: current_account.id })).group(Status.arel_table[:id])) + scope.merge!(filtered_reblogs_scope) if reblogs_may_occur? + + scope + end + + def filtered_reblogs_scope + Status.left_outer_joins(:reblog).where(reblog_of_id: nil).or(Status.where.not(reblogs_statuses: { account_id: current_account.excluded_from_timeline_account_ids })) + end + + def only_media_scope + Status.joins(:media_attachments).merge(account.media_attachments.reorder(nil)).group(Status.arel_table[:id]) + end + + def no_replies_scope + Status.without_replies + end + + def no_reblogs_scope + Status.without_reblogs + end + + def pinned_scope + account.pinned_statuses.group(Status.arel_table[:id], StatusPin.arel_table[:created_at]) + end + + def hashtag_scope + tag = Tag.find_normalized(params[:tagged]) + + if tag + Status.tagged_with(tag.id) + else + Status.none + end + end + + def suspended? + account.suspended? + end + + def anonymous? + current_account.nil? + end + + def author? + current_account.id == account.id + end + + def blocked? + account.blocking?(current_account) || (current_account.domain.present? && account.domain_blocking?(current_account.domain)) + end + + def follower? + current_account.following?(account) + end + + def reblogs_may_occur? + !exclude_reblogs? && !only_media? && !tagged? + end + + def pinned? + truthy_param?(:pinned) + end + + def only_media? + truthy_param?(:only_media) + end + + def exclude_replies? + truthy_param?(:exclude_replies) + end + + def exclude_reblogs? + truthy_param?(:exclude_reblogs) + end + + def tagged? + params[:tagged].present? + end + + def truthy_param?(key) + ActiveModel::Type::Boolean.new.cast(params[key]) + end +end diff --git a/app/models/status.rb b/app/models/status.rb index 60dde5045..af3e645dc 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -345,28 +345,6 @@ class Status < ApplicationRecord end end - def permitted_for(target_account, account) - visibility = [:public, :unlisted] - - if account.nil? - where(visibility: visibility) - elsif target_account.blocking?(account) || (account.domain.present? && target_account.domain_blocking?(account.domain)) # get rid of blocked peeps - none - elsif account.id == target_account.id # author can see own stuff - all - else - # followers can see followers-only stuff, but also things they are mentioned in. - # non-followers can see everything that isn't private/direct, but can see stuff they are mentioned in. - visibility.push(:private) if account.following?(target_account) - - scope = left_outer_joins(:reblog) - - scope.where(visibility: visibility) - .or(scope.where(id: account.mentions.select(:status_id))) - .merge(scope.where(reblog_of_id: nil).or(scope.where.not(reblogs_statuses: { account_id: account.excluded_from_timeline_account_ids }))) - end - end - def from_text(text) return [] if text.blank? diff --git a/spec/models/account_statuses_filter_spec.rb b/spec/models/account_statuses_filter_spec.rb new file mode 100644 index 000000000..03f0ffeb0 --- /dev/null +++ b/spec/models/account_statuses_filter_spec.rb @@ -0,0 +1,229 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AccountStatusesFilter do + let(:account) { Fabricate(:account) } + let(:current_account) { nil } + let(:params) { {} } + + subject { described_class.new(account, current_account, params) } + + def status!(visibility) + Fabricate(:status, account: account, visibility: visibility) + end + + def status_with_tag!(visibility, tag) + Fabricate(:status, account: account, visibility: visibility, tags: [tag]) + end + + def status_with_parent!(visibility) + Fabricate(:status, account: account, visibility: visibility, thread: Fabricate(:status)) + end + + def status_with_reblog!(visibility) + Fabricate(:status, account: account, visibility: visibility, reblog: Fabricate(:status)) + end + + def status_with_mention!(visibility, mentioned_account = nil) + Fabricate(:status, account: account, visibility: visibility).tap do |status| + Fabricate(:mention, status: status, account: mentioned_account || Fabricate(:account)) + end + end + + def status_with_media_attachment!(visibility) + Fabricate(:status, account: account, visibility: visibility).tap do |status| + Fabricate(:media_attachment, account: account, status: status) + end + end + + describe '#results' do + let(:tag) { Fabricate(:tag) } + + before do + status!(:public) + status!(:unlisted) + status!(:private) + status_with_parent!(:public) + status_with_reblog!(:public) + status_with_tag!(:public, tag) + status_with_mention!(:direct) + status_with_media_attachment!(:public) + end + + shared_examples 'filter params' do + context 'with only_media param' do + let(:params) { { only_media: true } } + + it 'returns only statuses with media' do + expect(subject.results.all?(&:with_media?)).to be true + end + end + + context 'with tagged param' do + let(:params) { { tagged: tag.name } } + + it 'returns only statuses with tag' do + expect(subject.results.all? { |s| s.tags.include?(tag) }).to be true + end + end + + context 'with exclude_replies param' do + let(:params) { { exclude_replies: true } } + + it 'returns only statuses that are not replies' do + expect(subject.results.none?(&:reply?)).to be true + end + end + + context 'with exclude_reblogs param' do + let(:params) { { exclude_reblogs: true } } + + it 'returns only statuses that are not reblogs' do + expect(subject.results.none?(&:reblog?)).to be true + end + end + end + + context 'when accessed anonymously' do + let(:current_account) { nil } + let(:direct_status) { nil } + + it 'returns only public statuses' do + expect(subject.results.pluck(:visibility).uniq).to match_array %w(unlisted public) + end + + it 'returns public replies' do + expect(subject.results.pluck(:in_reply_to_id)).to_not be_empty + end + + it 'returns public reblogs' do + expect(subject.results.pluck(:reblog_of_id)).to_not be_empty + end + + it_behaves_like 'filter params' + end + + context 'when accessed with a blocked account' do + let(:current_account) { Fabricate(:account) } + + before do + account.block!(current_account) + end + + it 'returns nothing' do + expect(subject.results.to_a).to be_empty + end + end + + context 'when accessed by self' do + let(:current_account) { account } + + it 'returns everything' do + expect(subject.results.pluck(:visibility).uniq).to match_array %w(direct private unlisted public) + end + + it 'returns replies' do + expect(subject.results.pluck(:in_reply_to_id)).to_not be_empty + end + + it 'returns reblogs' do + expect(subject.results.pluck(:reblog_of_id)).to_not be_empty + end + + it_behaves_like 'filter params' + end + + context 'when accessed by a follower' do + let(:current_account) { Fabricate(:account) } + + before do + current_account.follow!(account) + end + + it 'returns private statuses' do + expect(subject.results.pluck(:visibility).uniq).to match_array %w(private unlisted public) + end + + it 'returns replies' do + expect(subject.results.pluck(:in_reply_to_id)).to_not be_empty + end + + it 'returns reblogs' do + expect(subject.results.pluck(:reblog_of_id)).to_not be_empty + end + + context 'when there is a direct status mentioning the non-follower' do + let!(:direct_status) { status_with_mention!(:direct, current_account) } + + it 'returns the direct status' do + expect(subject.results.pluck(:id)).to include(direct_status.id) + end + end + + it_behaves_like 'filter params' + end + + context 'when accessed by a non-follower' do + let(:current_account) { Fabricate(:account) } + + it 'returns only public statuses' do + expect(subject.results.pluck(:visibility).uniq).to match_array %w(unlisted public) + end + + it 'returns public replies' do + expect(subject.results.pluck(:in_reply_to_id)).to_not be_empty + end + + it 'returns public reblogs' do + expect(subject.results.pluck(:reblog_of_id)).to_not be_empty + end + + context 'when there is a private status mentioning the non-follower' do + let!(:private_status) { status_with_mention!(:private, current_account) } + + it 'returns the private status' do + expect(subject.results.pluck(:id)).to include(private_status.id) + end + end + + context 'when blocking a reblogged account' do + let(:reblog) { status_with_reblog!('public') } + + before do + current_account.block!(reblog.reblog.account) + end + + it 'does not return reblog of blocked account' do + expect(subject.results.pluck(:id)).to_not include(reblog.id) + end + end + + context 'when muting a reblogged account' do + let(:reblog) { status_with_reblog!('public') } + + before do + current_account.mute!(reblog.reblog.account) + end + + it 'does not return reblog of muted account' do + expect(subject.results.pluck(:id)).to_not include(reblog.id) + end + end + + context 'when blocked by a reblogged account' do + let(:reblog) { status_with_reblog!('public') } + + before do + reblog.reblog.account.block!(current_account) + end + + it 'does not return reblog of blocked-by account' do + expect(subject.results.pluck(:id)).to_not include(reblog.id) + end + end + + it_behaves_like 'filter params' + end + end +end diff --git a/spec/models/status_spec.rb b/spec/models/status_spec.rb index 67af6d06d..130f4d03f 100644 --- a/spec/models/status_spec.rb +++ b/spec/models/status_spec.rb @@ -348,59 +348,6 @@ RSpec.describe Status, type: :model do end end - describe '.permitted_for' do - subject { described_class.permitted_for(target_account, account).pluck(:visibility) } - - let(:target_account) { alice } - let(:account) { bob } - let!(:public_status) { Fabricate(:status, account: target_account, visibility: 'public') } - let!(:unlisted_status) { Fabricate(:status, account: target_account, visibility: 'unlisted') } - let!(:private_status) { Fabricate(:status, account: target_account, visibility: 'private') } - - let!(:direct_status) do - Fabricate(:status, account: target_account, visibility: 'direct').tap do |status| - Fabricate(:mention, status: status, account: account) - end - end - - let!(:other_direct_status) do - Fabricate(:status, account: target_account, visibility: 'direct').tap do |status| - Fabricate(:mention, status: status) - end - end - - context 'given nil' do - let(:account) { nil } - let(:direct_status) { nil } - it { is_expected.to eq(%w(unlisted public)) } - end - - context 'given blocked account' do - before do - target_account.block!(account) - end - - it { is_expected.to be_empty } - end - - context 'given same account' do - let(:account) { target_account } - it { is_expected.to eq(%w(direct direct private unlisted public)) } - end - - context 'given followed account' do - before do - account.follow!(target_account) - end - - it { is_expected.to eq(%w(direct private unlisted public)) } - end - - context 'given unfollowed account' do - it { is_expected.to eq(%w(direct unlisted public)) } - end - end - describe 'before_validation' do it 'sets account being replied to correctly over intermediary nodes' do first_status = Fabricate(:status, account: bob) -- cgit