From 8069fd636ba14059524303ce76abe2173c4f3d01 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 16 Nov 2018 15:02:18 +0100 Subject: Remove intermediary arrays when creating hash maps from results (#9291) --- spec/models/notification_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'spec/models') diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index c781f2a29..be5545646 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -101,7 +101,7 @@ RSpec.describe Notification, type: :model do before do allow(accounts_with_ids).to receive(:[]).with(stale_account1.id).and_return(account1) allow(accounts_with_ids).to receive(:[]).with(stale_account2.id).and_return(account2) - allow(Account).to receive_message_chain(:where, :map, :to_h).and_return(accounts_with_ids) + allow(Account).to receive_message_chain(:where, :each_with_object).and_return(accounts_with_ids) end let(:cached_items) do -- cgit From d6b9a62e0a13497b8cc40eb1db56d1ec2b3760ea Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 19 Nov 2018 00:43:52 +0100 Subject: Extract counters from accounts table to account_stats table (#9295) --- app/controllers/admin/instances_controller.rb | 2 +- .../v1/accounts/follower_accounts_controller.rb | 2 +- .../v1/accounts/following_accounts_controller.rb | 2 +- app/controllers/api/v1/blocks_controller.rb | 2 +- app/controllers/api/v1/endorsements_controller.rb | 2 +- .../api/v1/follow_requests_controller.rb | 2 +- .../api/v1/lists/accounts_controller.rb | 4 +- .../statuses/favourited_by_accounts_controller.rb | 2 +- .../statuses/reblogged_by_accounts_controller.rb | 2 +- app/models/account.rb | 17 +++---- app/models/account_stat.rb | 26 +++++++++++ app/models/concerns/account_counters.rb | 31 +++++++++++++ app/models/follow.rb | 19 ++++++-- app/models/notification.rb | 2 +- app/models/status.rb | 28 ++++------- app/presenters/instance_presenter.rb | 2 +- db/migrate/20181116165755_create_account_stats.rb | 12 +++++ db/migrate/20181116173541_copy_account_stats.rb | 54 ++++++++++++++++++++++ .../20181116184611_copy_account_stats_cleanup.rb | 13 ++++++ db/schema.rb | 16 +++++-- spec/fabricators/account_stat_fabricator.rb | 6 +++ spec/models/account_spec.rb | 18 -------- spec/models/account_stat_spec.rb | 4 ++ spec/models/notification_spec.rb | 2 +- spec/models/status_stat_spec.rb | 1 - 25 files changed, 203 insertions(+), 68 deletions(-) create mode 100644 app/models/account_stat.rb create mode 100644 app/models/concerns/account_counters.rb create mode 100644 db/migrate/20181116165755_create_account_stats.rb create mode 100644 db/migrate/20181116173541_copy_account_stats.rb create mode 100644 db/post_migrate/20181116184611_copy_account_stats_cleanup.rb create mode 100644 spec/fabricators/account_stat_fabricator.rb create mode 100644 spec/models/account_stat_spec.rb (limited to 'spec/models') diff --git a/app/controllers/admin/instances_controller.rb b/app/controllers/admin/instances_controller.rb index 8ed0ea421..6f8eaf65c 100644 --- a/app/controllers/admin/instances_controller.rb +++ b/app/controllers/admin/instances_controller.rb @@ -31,7 +31,7 @@ module Admin end def subscribeable_accounts - Account.with_followers.remote.where(domain: params[:by_domain]) + Account.remote.where(protocol: :ostatus).where(domain: params[:by_domain]) end def filter_params diff --git a/app/controllers/api/v1/accounts/follower_accounts_controller.rb b/app/controllers/api/v1/accounts/follower_accounts_controller.rb index daa35769e..ec15debb0 100644 --- a/app/controllers/api/v1/accounts/follower_accounts_controller.rb +++ b/app/controllers/api/v1/accounts/follower_accounts_controller.rb @@ -25,7 +25,7 @@ class Api::V1::Accounts::FollowerAccountsController < Api::BaseController end def default_accounts - Account.includes(:active_relationships).references(:active_relationships) + Account.includes(:active_relationships, :account_stat).references(:active_relationships) end def paginated_follows diff --git a/app/controllers/api/v1/accounts/following_accounts_controller.rb b/app/controllers/api/v1/accounts/following_accounts_controller.rb index 6be97b87e..f3e112f2c 100644 --- a/app/controllers/api/v1/accounts/following_accounts_controller.rb +++ b/app/controllers/api/v1/accounts/following_accounts_controller.rb @@ -25,7 +25,7 @@ class Api::V1::Accounts::FollowingAccountsController < Api::BaseController end def default_accounts - Account.includes(:passive_relationships).references(:passive_relationships) + Account.includes(:passive_relationships, :account_stat).references(:passive_relationships) end def paginated_follows diff --git a/app/controllers/api/v1/blocks_controller.rb b/app/controllers/api/v1/blocks_controller.rb index 99c53d59a..4cff04cad 100644 --- a/app/controllers/api/v1/blocks_controller.rb +++ b/app/controllers/api/v1/blocks_controller.rb @@ -19,7 +19,7 @@ class Api::V1::BlocksController < Api::BaseController end def paginated_blocks - @paginated_blocks ||= Block.eager_load(:target_account) + @paginated_blocks ||= Block.eager_load(target_account: :account_stat) .where(account: current_account) .paginate_by_max_id( limit_param(DEFAULT_ACCOUNTS_LIMIT), diff --git a/app/controllers/api/v1/endorsements_controller.rb b/app/controllers/api/v1/endorsements_controller.rb index 0f04b488f..2770c7aef 100644 --- a/app/controllers/api/v1/endorsements_controller.rb +++ b/app/controllers/api/v1/endorsements_controller.rb @@ -27,7 +27,7 @@ class Api::V1::EndorsementsController < Api::BaseController end def endorsed_accounts - current_account.endorsed_accounts + current_account.endorsed_accounts.includes(:account_stat) end def insert_pagination_headers diff --git a/app/controllers/api/v1/follow_requests_controller.rb b/app/controllers/api/v1/follow_requests_controller.rb index e9aca5f8a..e6888154e 100644 --- a/app/controllers/api/v1/follow_requests_controller.rb +++ b/app/controllers/api/v1/follow_requests_controller.rb @@ -33,7 +33,7 @@ class Api::V1::FollowRequestsController < Api::BaseController end def default_accounts - Account.includes(:follow_requests).references(:follow_requests) + Account.includes(:follow_requests, :account_stat).references(:follow_requests) end def paginated_follow_requests diff --git a/app/controllers/api/v1/lists/accounts_controller.rb b/app/controllers/api/v1/lists/accounts_controller.rb index ec4477034..23078263e 100644 --- a/app/controllers/api/v1/lists/accounts_controller.rb +++ b/app/controllers/api/v1/lists/accounts_controller.rb @@ -37,9 +37,9 @@ class Api::V1::Lists::AccountsController < Api::BaseController def load_accounts if unlimited? - @list.accounts.all + @list.accounts.includes(:account_stat).all else - @list.accounts.paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) + @list.accounts.includes(:account_stat).paginate_by_max_id(limit_param(DEFAULT_ACCOUNTS_LIMIT), params[:max_id], params[:since_id]) end end diff --git a/app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb b/app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb index 8f4070bc7..657e57831 100644 --- a/app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb +++ b/app/controllers/api/v1/statuses/favourited_by_accounts_controller.rb @@ -22,7 +22,7 @@ class Api::V1::Statuses::FavouritedByAccountsController < Api::BaseController def default_accounts Account - .includes(:favourites) + .includes(:favourites, :account_stat) .references(:favourites) .where(favourites: { status_id: @status.id }) end diff --git a/app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb b/app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb index 93b83ce48..4315b0283 100644 --- a/app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb +++ b/app/controllers/api/v1/statuses/reblogged_by_accounts_controller.rb @@ -21,7 +21,7 @@ class Api::V1::Statuses::RebloggedByAccountsController < Api::BaseController end def default_accounts - Account.includes(:statuses).references(:statuses) + Account.includes(:statuses, :account_stat).references(:statuses) end def paginated_statuses diff --git a/app/models/account.rb b/app/models/account.rb index 44963f3e6..593ee29f7 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -32,9 +32,6 @@ # suspended :boolean default(FALSE), not null # locked :boolean default(FALSE), not null # header_remote_url :string default(""), not null -# statuses_count :integer default(0), not null -# followers_count :integer default(0), not null -# following_count :integer default(0), not null # last_webfingered_at :datetime # inbox_url :string default(""), not null # outbox_url :string default(""), not null @@ -58,6 +55,7 @@ class Account < ApplicationRecord include AccountInteractions include Attachmentable include Paginable + include AccountCounters enum protocol: [:ostatus, :activitypub] @@ -119,8 +117,6 @@ class Account < ApplicationRecord scope :remote, -> { where.not(domain: nil) } scope :local, -> { where(domain: nil) } - scope :without_followers, -> { where(followers_count: 0) } - scope :with_followers, -> { where('followers_count > 0') } scope :expiring, ->(time) { remote.where.not(subscription_expires_at: nil).where('subscription_expires_at < ?', time) } scope :partitioned, -> { order(Arel.sql('row_number() over (partition by domain)')) } scope :silenced, -> { where(silenced: true) } @@ -385,7 +381,9 @@ class Account < ApplicationRecord LIMIT ? SQL - find_by_sql([sql, limit]) + records = find_by_sql([sql, limit]) + ActiveRecord::Associations::Preloader.new.preload(records, :account_stat) + records end def advanced_search_for(terms, account, limit = 10, following = false) @@ -412,7 +410,7 @@ class Account < ApplicationRecord LIMIT ? SQL - find_by_sql([sql, account.id, account.id, account.id, limit]) + records = find_by_sql([sql, account.id, account.id, account.id, limit]) else sql = <<-SQL.squish SELECT @@ -428,8 +426,11 @@ class Account < ApplicationRecord LIMIT ? SQL - find_by_sql([sql, account.id, account.id, limit]) + records = find_by_sql([sql, account.id, account.id, limit]) end + + ActiveRecord::Associations::Preloader.new.preload(records, :account_stat) + records end private diff --git a/app/models/account_stat.rb b/app/models/account_stat.rb new file mode 100644 index 000000000..d5715268e --- /dev/null +++ b/app/models/account_stat.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# == Schema Information +# +# Table name: account_stats +# +# id :bigint(8) not null, primary key +# account_id :bigint(8) not null +# statuses_count :bigint(8) default(0), not null +# following_count :bigint(8) default(0), not null +# followers_count :bigint(8) default(0), not null +# created_at :datetime not null +# updated_at :datetime not null +# + +class AccountStat < ApplicationRecord + belongs_to :account, inverse_of: :account_stat + + def increment_count!(key) + update(key => public_send(key) + 1) + end + + def decrement_count!(key) + update(key => [public_send(key) - 1, 0].max) + end +end diff --git a/app/models/concerns/account_counters.rb b/app/models/concerns/account_counters.rb new file mode 100644 index 000000000..fa3ec9a3d --- /dev/null +++ b/app/models/concerns/account_counters.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module AccountCounters + extend ActiveSupport::Concern + + included do + has_one :account_stat, inverse_of: :account + after_save :save_account_stat + end + + delegate :statuses_count, + :statuses_count=, + :following_count, + :following_count=, + :followers_count, + :followers_count=, + :increment_count!, + :decrement_count!, + to: :account_stat + + def account_stat + super || build_account_stat + end + + private + + def save_account_stat + return unless account_stat&.changed? + account_stat.save + end +end diff --git a/app/models/follow.rb b/app/models/follow.rb index 7ad56eb78..87fa11425 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -16,11 +16,8 @@ class Follow < ApplicationRecord include Paginable include RelationshipCacheable - belongs_to :account, counter_cache: :following_count - - belongs_to :target_account, - class_name: 'Account', - counter_cache: :followers_count + belongs_to :account + belongs_to :target_account, class_name: 'Account' has_one :notification, as: :activity, dependent: :destroy @@ -39,7 +36,9 @@ class Follow < ApplicationRecord end before_validation :set_uri, only: :create + after_create :increment_cache_counters after_destroy :remove_endorsements + after_destroy :decrement_cache_counters private @@ -50,4 +49,14 @@ class Follow < ApplicationRecord def remove_endorsements AccountPin.where(target_account_id: target_account_id, account_id: account_id).delete_all end + + def increment_cache_counters + account&.increment_count!(:following_count) + target_account&.increment_count!(:followers_count) + end + + def decrement_cache_counters + account&.decrement_count!(:following_count) + target_account&.decrement_count!(:followers_count) + end end diff --git a/app/models/notification.rb b/app/models/notification.rb index 4233532d0..2f0a9b78c 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -75,7 +75,7 @@ class Notification < ApplicationRecord return if account_ids.empty? - accounts = Account.where(id: account_ids).each_with_object({}) { |a, h| h[a.id] = a } + accounts = Account.where(id: account_ids).includes(:account_stat).each_with_object({}) { |a, h| h[a.id] = a } cached_items.each do |item| item.from_account = accounts[item.from_account_id] diff --git a/app/models/status.rb b/app/models/status.rb index 680081724..0449d33e1 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -94,17 +94,16 @@ class Status < ApplicationRecord end } - cache_associated :account, - :application, + cache_associated :application, :media_attachments, :conversation, :status_stat, :tags, :preview_cards, :stream_entry, - active_mentions: :account, + account: :account_stat, + active_mentions: { account: :account_stat }, reblog: [ - :account, :application, :stream_entry, :tags, @@ -112,9 +111,10 @@ class Status < ApplicationRecord :media_attachments, :conversation, :status_stat, - active_mentions: :account, + account: :account_stat, + active_mentions: { account: :account_stat }, ], - thread: :account + thread: { account: :account_stat } delegate :domain, to: :account, prefix: true @@ -348,7 +348,7 @@ class Status < ApplicationRecord return if account_ids.empty? - accounts = Account.where(id: account_ids).each_with_object({}) { |a, h| h[a.id] = a } + accounts = Account.where(id: account_ids).includes(:account_stat).each_with_object({}) { |a, h| h[a.id] = a } cached_items.each do |item| item.account = accounts[item.account_id] @@ -475,12 +475,7 @@ class Status < ApplicationRecord def increment_counter_caches return if direct_visibility? - if association(:account).loaded? - account.update_attribute(:statuses_count, account.statuses_count + 1) - else - Account.where(id: account_id).update_all('statuses_count = COALESCE(statuses_count, 0) + 1') - end - + account&.increment_count!(:statuses_count) reblog&.increment_count!(:reblogs_count) if reblog? thread&.increment_count!(:replies_count) if in_reply_to_id.present? && (public_visibility? || unlisted_visibility?) end @@ -488,12 +483,7 @@ class Status < ApplicationRecord def decrement_counter_caches return if direct_visibility? || marked_for_mass_destruction? - if association(:account).loaded? - account.update_attribute(:statuses_count, [account.statuses_count - 1, 0].max) - else - Account.where(id: account_id).update_all('statuses_count = GREATEST(COALESCE(statuses_count, 0) - 1, 0)') - end - + account&.decrement_count!(:statuses_count) reblog&.decrement_count!(:reblogs_count) if reblog? thread&.decrement_count!(:replies_count) if in_reply_to_id.present? && (public_visibility? || unlisted_visibility?) end diff --git a/app/presenters/instance_presenter.rb b/app/presenters/instance_presenter.rb index 9a9157f1b..457257e26 100644 --- a/app/presenters/instance_presenter.rb +++ b/app/presenters/instance_presenter.rb @@ -22,7 +22,7 @@ class InstancePresenter end def status_count - Rails.cache.fetch('local_status_count') { Account.local.sum(:statuses_count) } + Rails.cache.fetch('local_status_count') { Account.local.joins(:account_stat).sum('account_stats.statuses_count') } end def domain_count diff --git a/db/migrate/20181116165755_create_account_stats.rb b/db/migrate/20181116165755_create_account_stats.rb new file mode 100644 index 000000000..a798e8166 --- /dev/null +++ b/db/migrate/20181116165755_create_account_stats.rb @@ -0,0 +1,12 @@ +class CreateAccountStats < ActiveRecord::Migration[5.2] + def change + create_table :account_stats do |t| + t.belongs_to :account, null: false, foreign_key: { on_delete: :cascade }, index: { unique: true } + t.bigint :statuses_count, null: false, default: 0 + t.bigint :following_count, null: false, default: 0 + t.bigint :followers_count, null: false, default: 0 + + t.timestamps + end + end +end diff --git a/db/migrate/20181116173541_copy_account_stats.rb b/db/migrate/20181116173541_copy_account_stats.rb new file mode 100644 index 000000000..bb523fbbd --- /dev/null +++ b/db/migrate/20181116173541_copy_account_stats.rb @@ -0,0 +1,54 @@ +class CopyAccountStats < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def up + safety_assured do + if supports_upsert? + up_fast + else + up_slow + end + end + end + + def down + # Nothing + end + + private + + def supports_upsert? + version = select_one("SELECT current_setting('server_version_num') AS v")['v'].to_i + version >= 90500 + end + + def up_fast + say 'Upsert is available, importing counters using the fast method' + + Account.unscoped.select('id').find_in_batches(batch_size: 5_000) do |accounts| + execute <<-SQL.squish + INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) + SELECT id, statuses_count, following_count, followers_count, created_at, updated_at + FROM accounts + WHERE id IN (#{accounts.map(&:id).join(', ')}) + ON CONFLICT (account_id) DO UPDATE + SET statuses_count = EXCLUDED.statuses_count, following_count = EXCLUDED.following_count, followers_count = EXCLUDED.followers_count + SQL + end + end + + def up_slow + say 'Upsert is not available in PostgreSQL below 9.5, falling back to slow import of counters' + + # We cannot use bulk INSERT or overarching transactions here because of possible + # uniqueness violations that we need to skip over + Account.unscoped.select('id, statuses_count, following_count, followers_count, created_at, updated_at').find_each do |account| + begin + params = [[nil, account.id], [nil, account.statuses_count], [nil, account.following_count], [nil, account.followers_count], [nil, account.created_at], [nil, account.updated_at]] + exec_insert('INSERT INTO account_stats (account_id, statuses_count, following_count, followers_count, created_at, updated_at) VALUES ($1, $2, $3, $4, $5, $6)', nil, params) + rescue ActiveRecord::RecordNotUnique + next + end + end + end +end diff --git a/db/post_migrate/20181116184611_copy_account_stats_cleanup.rb b/db/post_migrate/20181116184611_copy_account_stats_cleanup.rb new file mode 100644 index 000000000..9267e9b2c --- /dev/null +++ b/db/post_migrate/20181116184611_copy_account_stats_cleanup.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class CopyAccountStatsCleanup < ActiveRecord::Migration[5.2] + disable_ddl_transaction! + + def change + safety_assured do + remove_column :accounts, :statuses_count, :integer, default: 0, null: false + remove_column :accounts, :following_count, :integer, default: 0, null: false + remove_column :accounts, :followers_count, :integer, default: 0, null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 731a84521..b0f14954f 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: 2018_10_26_034033) do +ActiveRecord::Schema.define(version: 2018_11_16_184611) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -56,6 +56,16 @@ ActiveRecord::Schema.define(version: 2018_10_26_034033) do t.index ["target_account_id"], name: "index_account_pins_on_target_account_id" end + create_table "account_stats", force: :cascade do |t| + t.bigint "account_id", null: false + t.bigint "statuses_count", default: 0, null: false + t.bigint "following_count", default: 0, null: false + t.bigint "followers_count", default: 0, null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["account_id"], name: "index_account_stats_on_account_id", unique: true + end + create_table "accounts", force: :cascade do |t| t.string "username", default: "", null: false t.string "domain" @@ -85,9 +95,6 @@ ActiveRecord::Schema.define(version: 2018_10_26_034033) do t.boolean "suspended", default: false, null: false t.boolean "locked", default: false, null: false t.string "header_remote_url", default: "", null: false - t.integer "statuses_count", default: 0, null: false - t.integer "followers_count", default: 0, null: false - t.integer "following_count", default: 0, null: false t.datetime "last_webfingered_at" t.string "inbox_url", default: "", null: false t.string "outbox_url", default: "", null: false @@ -629,6 +636,7 @@ ActiveRecord::Schema.define(version: 2018_10_26_034033) do add_foreign_key "account_moderation_notes", "accounts", column: "target_account_id" add_foreign_key "account_pins", "accounts", column: "target_account_id", on_delete: :cascade add_foreign_key "account_pins", "accounts", on_delete: :cascade + add_foreign_key "account_stats", "accounts", on_delete: :cascade add_foreign_key "accounts", "accounts", column: "moved_to_account_id", on_delete: :nullify add_foreign_key "admin_action_logs", "accounts", on_delete: :cascade add_foreign_key "backups", "users", on_delete: :nullify diff --git a/spec/fabricators/account_stat_fabricator.rb b/spec/fabricators/account_stat_fabricator.rb new file mode 100644 index 000000000..2b06b4790 --- /dev/null +++ b/spec/fabricators/account_stat_fabricator.rb @@ -0,0 +1,6 @@ +Fabricator(:account_stat) do + account nil + statuses_count "" + following_count "" + followers_count "" +end diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 60d13d32e..9133616a2 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -754,24 +754,6 @@ RSpec.describe Account, type: :model do expect(Account.suspended).to match_array([account_1]) end end - - describe 'without_followers' do - it 'returns a relation of accounts without followers' do - account_1 = Fabricate(:account) - account_2 = Fabricate(:account) - Fabricate(:follow, account: account_1, target_account: account_2) - expect(Account.without_followers).to match_array([account_1]) - end - end - - describe 'with_followers' do - it 'returns a relation of accounts with followers' do - account_1 = Fabricate(:account) - account_2 = Fabricate(:account) - Fabricate(:follow, account: account_1, target_account: account_2) - expect(Account.with_followers).to match_array([account_2]) - end - end end context 'when is local' do diff --git a/spec/models/account_stat_spec.rb b/spec/models/account_stat_spec.rb new file mode 100644 index 000000000..a94185109 --- /dev/null +++ b/spec/models/account_stat_spec.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe AccountStat, type: :model do +end diff --git a/spec/models/notification_spec.rb b/spec/models/notification_spec.rb index be5545646..403eb8c33 100644 --- a/spec/models/notification_spec.rb +++ b/spec/models/notification_spec.rb @@ -101,7 +101,7 @@ RSpec.describe Notification, type: :model do before do allow(accounts_with_ids).to receive(:[]).with(stale_account1.id).and_return(account1) allow(accounts_with_ids).to receive(:[]).with(stale_account2.id).and_return(account2) - allow(Account).to receive_message_chain(:where, :each_with_object).and_return(accounts_with_ids) + allow(Account).to receive_message_chain(:where, :includes, :each_with_object).and_return(accounts_with_ids) end let(:cached_items) do diff --git a/spec/models/status_stat_spec.rb b/spec/models/status_stat_spec.rb index 5e9351aff..af1a6f288 100644 --- a/spec/models/status_stat_spec.rb +++ b/spec/models/status_stat_spec.rb @@ -1,5 +1,4 @@ require 'rails_helper' RSpec.describe StatusStat, type: :model do - pending "add some examples to (or delete) #{__FILE__}" end -- cgit From 0eaf6d7693ba1f5568c9b857a306e01250f2f714 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 24 Nov 2018 20:48:50 +0100 Subject: Sort self-replies to the top of descendants (#9320) Fix #6463 --- app/models/concerns/status_threading_concern.rb | 26 ++++++++++++++++++++-- .../concerns/status_threading_concern_spec.rb | 10 +++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) (limited to 'spec/models') diff --git a/app/models/concerns/status_threading_concern.rb b/app/models/concerns/status_threading_concern.rb index fa441469c..b9c800c2a 100644 --- a/app/models/concerns/status_threading_concern.rb +++ b/app/models/concerns/status_threading_concern.rb @@ -8,7 +8,7 @@ module StatusThreadingConcern end def descendants(limit, account = nil, max_child_id = nil, since_child_id = nil, depth = nil) - find_statuses_from_tree_path(descendant_ids(limit, max_child_id, since_child_id, depth), account) + find_statuses_from_tree_path(descendant_ids(limit, max_child_id, since_child_id, depth), account, promote: true) end private @@ -76,7 +76,7 @@ module StatusThreadingConcern descendants_with_self - [self] end - def find_statuses_from_tree_path(ids, account) + def find_statuses_from_tree_path(ids, account, promote: false) statuses = statuses_with_accounts(ids).to_a account_ids = statuses.map(&:account_id).uniq domains = statuses.map(&:account_domain).compact.uniq @@ -86,6 +86,28 @@ module StatusThreadingConcern # Order ancestors/descendants by tree path statuses.sort_by! { |status| ids.index(status.id) } + + # Bring self-replies to the top + if promote + promote_by!(statuses) { |status| status.in_reply_to_account_id == status.account_id } + else + statuses + end + end + + def promote_by!(arr) + insert_at = arr.find_index { |item| !yield(item) } + + return arr if insert_at.nil? + + arr.each_with_index do |item, index| + next if index <= insert_at || !yield(item) + + arr.insert(insert_at, arr.delete_at(index)) + insert_at += 1 + end + + arr end def relations_map_for_account(account, account_ids, domains) diff --git a/spec/models/concerns/status_threading_concern_spec.rb b/spec/models/concerns/status_threading_concern_spec.rb index e5736a307..94c2d5fc2 100644 --- a/spec/models/concerns/status_threading_concern_spec.rb +++ b/spec/models/concerns/status_threading_concern_spec.rb @@ -118,5 +118,15 @@ describe StatusThreadingConcern do viewer.block_domain!('example.com') expect(status.descendants(4, viewer)).to_not include(reply2) end + + it 'promotes self-replies to the top while leaving the rest in order' do + a = Fabricate(:status, account: alice) + d = Fabricate(:status, account: jeff, thread: a) + e = Fabricate(:status, account: bob, thread: d) + c = Fabricate(:status, account: alice, thread: a) + f = Fabricate(:status, account: bob, thread: c) + + expect(a.descendants(20)).to eq [c, d, e, f] + end end end -- cgit From 73faadad28b897cd61cc1d6c8bee47e7d72a51a1 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Mon, 26 Nov 2018 15:53:27 +0100 Subject: Redesign admin accounts index (#9340) * Improve overview of accounts in admin UI - Display suspended status, role, last activity and IP prominently - Default to showing local accounts - Default to not showing suspended accounts * Remove unused strings * Fix tests * Allow filtering accounts by IP mask --- app/controllers/admin/accounts_controller.rb | 2 +- .../admin/account_moderation_notes_helper.rb | 2 +- app/helpers/admin/filter_helper.rb | 2 +- app/helpers/stream_entries_helper.rb | 8 +++-- app/javascript/packs/public.js | 2 +- app/models/account.rb | 1 + app/models/account_filter.rb | 23 ++++++------- app/models/user.rb | 1 - app/views/admin/accounts/_account.html.haml | 23 ++++++------- app/views/admin/accounts/index.html.haml | 38 +++++----------------- app/views/admin/accounts/show.html.haml | 2 +- config/locales/ar.yml | 4 --- config/locales/ca.yml | 4 --- config/locales/co.yml | 4 --- config/locales/cs.yml | 4 --- config/locales/cy.yml | 4 --- config/locales/da.yml | 4 --- config/locales/de.yml | 4 --- config/locales/el.yml | 4 --- config/locales/en.yml | 4 --- config/locales/eo.yml | 4 --- config/locales/es.yml | 4 --- config/locales/eu.yml | 4 --- config/locales/fa.yml | 4 --- config/locales/fi.yml | 4 --- config/locales/fr.yml | 4 --- config/locales/gl.yml | 4 --- config/locales/he.yml | 4 --- config/locales/hu.yml | 4 --- config/locales/id.yml | 4 --- config/locales/io.yml | 4 --- config/locales/it.yml | 4 --- config/locales/ja.yml | 4 --- config/locales/ka.yml | 4 --- config/locales/ko.yml | 4 --- config/locales/nl.yml | 4 --- config/locales/no.yml | 4 --- config/locales/oc.yml | 4 --- config/locales/pl.yml | 4 --- config/locales/pt-BR.yml | 4 --- config/locales/pt.yml | 4 --- config/locales/ru.yml | 4 --- config/locales/sk.yml | 4 --- config/locales/sl.yml | 4 --- config/locales/sr-Latn.yml | 4 --- config/locales/sr.yml | 4 --- config/locales/sv.yml | 4 --- config/locales/th.yml | 4 --- config/locales/tr.yml | 4 --- config/locales/uk.yml | 4 --- config/locales/zh-CN.yml | 4 --- config/locales/zh-HK.yml | 4 --- config/locales/zh-TW.yml | 4 --- spec/controllers/admin/accounts_controller_spec.rb | 4 +-- spec/models/account_filter_spec.rb | 25 +++----------- spec/models/user_spec.rb | 12 ------- 56 files changed, 47 insertions(+), 266 deletions(-) (limited to 'spec/models') diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb index 5d57fe361..f155543ce 100644 --- a/app/controllers/admin/accounts_controller.rb +++ b/app/controllers/admin/accounts_controller.rb @@ -94,8 +94,8 @@ module Admin :local, :remote, :by_domain, + :active, :silenced, - :alphabetic, :suspended, :username, :display_name, diff --git a/app/helpers/admin/account_moderation_notes_helper.rb b/app/helpers/admin/account_moderation_notes_helper.rb index 4d8f0352e..40b2a5289 100644 --- a/app/helpers/admin/account_moderation_notes_helper.rb +++ b/app/helpers/admin/account_moderation_notes_helper.rb @@ -24,7 +24,7 @@ module Admin::AccountModerationNotesHelper def name_tag_classes(account, inline = false) classes = [inline ? 'inline-name-tag' : 'name-tag'] - classes << 'suspended' if account.suspended? + classes << 'suspended' if account.suspended? || (account.local? && account.user.nil?) classes.join(' ') end end diff --git a/app/helpers/admin/filter_helper.rb b/app/helpers/admin/filter_helper.rb index 60e5142e3..9a663051c 100644 --- a/app/helpers/admin/filter_helper.rb +++ b/app/helpers/admin/filter_helper.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Admin::FilterHelper - ACCOUNT_FILTERS = %i(local remote by_domain silenced suspended alphabetic username display_name email ip staff).freeze + ACCOUNT_FILTERS = %i(local remote by_domain active silenced suspended username display_name email ip staff).freeze REPORT_FILTERS = %i(resolved account_id target_account_id).freeze INVITE_FILTER = %i(available expired).freeze CUSTOM_EMOJI_FILTERS = %i(local remote by_domain shortcode).freeze diff --git a/app/helpers/stream_entries_helper.rb b/app/helpers/stream_entries_helper.rb index ac655f622..033d435c4 100644 --- a/app/helpers/stream_entries_helper.rb +++ b/app/helpers/stream_entries_helper.rb @@ -34,12 +34,14 @@ module StreamEntriesHelper end end - def account_badge(account) + def account_badge(account, all: false) if account.bot? content_tag(:div, content_tag(:div, t('accounts.roles.bot'), class: 'account-role bot'), class: 'roles') - elsif Setting.show_staff_badge && account.user_staff? + elsif (Setting.show_staff_badge && account.user_staff?) || all content_tag(:div, class: 'roles') do - if account.user_admin? + if all && !account.user_staff? + content_tag(:div, t('admin.accounts.roles.user'), class: 'account-role') + elsif account.user_admin? content_tag(:div, t('accounts.roles.admin'), class: 'account-role admin') elsif account.user_moderator? content_tag(:div, t('accounts.roles.moderator'), class: 'account-role moderator') diff --git a/app/javascript/packs/public.js b/app/javascript/packs/public.js index 3b02b7c39..36b1fd26b 100644 --- a/app/javascript/packs/public.js +++ b/app/javascript/packs/public.js @@ -63,7 +63,7 @@ function main() { content.textContent = timeAgoString({ formatMessage: ({ id, defaultMessage }, values) => (new IntlMessageFormat(messages[id] || defaultMessage, locale)).format(values), formatDate: (date, options) => (new Intl.DateTimeFormat(locale, options)).format(date), - }, datetime, now, datetime.getFullYear()); + }, datetime, now, now.getFullYear()); }); const reactComponents = document.querySelectorAll('[data-component]'); diff --git a/app/models/account.rb b/app/models/account.rb index 593ee29f7..46d32a36e 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -123,6 +123,7 @@ class Account < ApplicationRecord scope :suspended, -> { where(suspended: true) } scope :without_suspended, -> { where(suspended: false) } scope :recent, -> { reorder(id: :desc) } + scope :bots, -> { where(actor_type: %w(Application Service)) } scope :alphabetic, -> { order(domain: :asc, username: :asc) } scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') } scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) } diff --git a/app/models/account_filter.rb b/app/models/account_filter.rb index 84364bf1b..b10f50db7 100644 --- a/app/models/account_filter.rb +++ b/app/models/account_filter.rb @@ -5,13 +5,14 @@ class AccountFilter def initialize(params) @params = params + set_defaults! end def results - scope = Account.recent + scope = Account.recent.includes(:user) params.each do |key, value| - scope.merge!(scope_for(key, value)) if value.present? + scope.merge!(scope_for(key, value.to_s.strip)) if value.present? end scope @@ -19,6 +20,11 @@ class AccountFilter private + def set_defaults! + params['local'] = '1' if params['remote'].blank? + params['active'] = '1' if params['suspended'].blank? && params['silenced'].blank? + end + def scope_for(key, value) case key.to_s when 'local' @@ -27,10 +33,10 @@ class AccountFilter Account.remote when 'by_domain' Account.where(domain: value) + when 'active' + Account.without_suspended when 'silenced' Account.silenced - when 'alphabetic' - Account.reorder(nil).alphabetic when 'suspended' Account.suspended when 'username' @@ -40,11 +46,7 @@ class AccountFilter when 'email' accounts_with_users.merge User.matches_email(value) when 'ip' - if valid_ip?(value) - accounts_with_users.merge User.with_recent_ip_address(value) - else - Account.default_scoped - end + valid_ip?(value) ? accounts_with_users.where('users.current_sign_in_ip <<= ?', value) : Account.none when 'staff' accounts_with_users.merge User.staff else @@ -57,8 +59,7 @@ class AccountFilter end def valid_ip?(value) - IPAddr.new(value) - true + IPAddr.new(value) && true rescue IPAddr::InvalidAddressError false end diff --git a/app/models/user.rb b/app/models/user.rb index 69fa0688a..453ffa8b0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -83,7 +83,6 @@ class User < ApplicationRecord scope :inactive, -> { where(arel_table[:current_sign_in_at].lt(ACTIVE_DURATION.ago)) } scope :active, -> { confirmed.where(arel_table[:current_sign_in_at].gteq(ACTIVE_DURATION.ago)).joins(:account).where(accounts: { suspended: false }) } scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) } - scope :with_recent_ip_address, ->(value) { where(arel_table[:current_sign_in_ip].eq(value).or(arel_table[:last_sign_in_ip].eq(value))) } before_validation :sanitize_languages diff --git a/app/views/admin/accounts/_account.html.haml b/app/views/admin/accounts/_account.html.haml index c6e63152d..0fadaae1e 100644 --- a/app/views/admin/accounts/_account.html.haml +++ b/app/views/admin/accounts/_account.html.haml @@ -1,18 +1,15 @@ %tr - %td.username - = account.username %td - - unless account.local? - = link_to account.domain, admin_accounts_path(by_domain: account.domain) + = admin_account_link_to(account) %td - - if account.local? - - if account.user.nil? - = t("admin.accounts.moderation.suspended") - - else - = t("admin.accounts.roles.#{account.user.role}") + %div{ style: 'margin: -2px 0' }= account_badge(account, all: true) + %td + - if account.user_current_sign_in_ip + %samp= account.user_current_sign_in_ip - else - = account.protocol.humanize + \- %td - = table_link_to 'circle', t('admin.accounts.web'), web_path("accounts/#{account.id}") - = table_link_to 'globe', t('admin.accounts.public'), TagManager.instance.url_for(account) - = table_link_to 'pencil', t('admin.accounts.edit'), admin_account_path(account.id) + - if account.user_current_sign_in_at + %time.time-ago{ datetime: account.user_current_sign_in_at.iso8601, title: l(account.user_current_sign_in_at) }= l account.user_current_sign_in_at + - else + \- diff --git a/app/views/admin/accounts/index.html.haml b/app/views/admin/accounts/index.html.haml index 4bee73adc..0d31eee36 100644 --- a/app/views/admin/accounts/index.html.haml +++ b/app/views/admin/accounts/index.html.haml @@ -5,41 +5,19 @@ .filter-subset %strong= t('admin.accounts.location.title') %ul - %li= filter_link_to t('admin.accounts.location.all'), local: nil, remote: nil - %li - - if selected? local: '1', remote: nil - = filter_link_to t('admin.accounts.location.local'), {local: nil, remote: nil}, {local: '1', remote: nil} - - else - = filter_link_to t('admin.accounts.location.local'), local: '1', remote: nil - %li - - if selected? remote: '1', local: nil - = filter_link_to t('admin.accounts.location.remote'), {remote: nil, local: nil}, {remote: '1', local: nil} - - else - = filter_link_to t('admin.accounts.location.remote'), remote: '1', local: nil + %li= filter_link_to t('admin.accounts.location.local'), remote: nil + %li= filter_link_to t('admin.accounts.location.remote'), remote: '1' .filter-subset %strong= t('admin.accounts.moderation.title') %ul - %li= filter_link_to t('admin.accounts.moderation.all'), silenced: nil, suspended: nil - %li - - if selected? silenced: '1' - = filter_link_to t('admin.accounts.moderation.silenced'), {silenced: nil}, {silenced: '1'} - - else - = filter_link_to t('admin.accounts.moderation.silenced'), silenced: '1' - %li - - if selected? suspended: '1' - = filter_link_to t('admin.accounts.moderation.suspended'), {suspended: nil}, {suspended: '1'} - - else - = filter_link_to t('admin.accounts.moderation.suspended'), suspended: '1' + %li= filter_link_to t('admin.accounts.moderation.active'), silenced: nil, suspended: nil + %li= filter_link_to t('admin.accounts.moderation.silenced'), silenced: '1', suspended: nil + %li= filter_link_to t('admin.accounts.moderation.suspended'), suspended: '1', silenced: nil .filter-subset %strong= t('admin.accounts.role') %ul %li= filter_link_to t('admin.accounts.moderation.all'), staff: nil %li= filter_link_to t('admin.accounts.roles.staff'), staff: '1' - .filter-subset - %strong= t('admin.accounts.order.title') - %ul - %li= filter_link_to t('admin.accounts.order.most_recent'), alphabetic: nil - %li= filter_link_to t('admin.accounts.order.alphabetic'), alphabetic: '1' = form_tag admin_accounts_url, method: 'GET', class: 'simple_form' do .fields-group @@ -60,9 +38,9 @@ %thead %tr %th= t('admin.accounts.username') - %th= t('admin.accounts.domain') - %th - %th + %th= t('admin.accounts.role') + %th= t('admin.accounts.most_recent_ip') + %th= t('admin.accounts.most_recent_activity') %tbody = render @accounts diff --git a/app/views/admin/accounts/show.html.haml b/app/views/admin/accounts/show.html.haml index 17f1f224d..c1a5fc1bd 100644 --- a/app/views/admin/accounts/show.html.haml +++ b/app/views/admin/accounts/show.html.haml @@ -68,7 +68,7 @@ %time.formatted{ datetime: @account.user_current_sign_in_at.iso8601, title: l(@account.user_current_sign_in_at) } = l @account.user_current_sign_in_at - else - Never + \- - else %tr %th= t('admin.accounts.profile_url') diff --git a/config/locales/ar.yml b/config/locales/ar.yml index 095b1f584..e9d9c03cd 100644 --- a/config/locales/ar.yml +++ b/config/locales/ar.yml @@ -115,10 +115,6 @@ ar: most_recent_ip: أحدث عنوان إيبي no_limits_imposed: مِن دون حدود مشروطة not_subscribed: غير مشترك - order: - alphabetic: أبجديًا - most_recent: الأحدث - title: الترتيب outbox_url: رابط صندوق الصادر perform_full_suspension: تعطيل profile_url: رابط الملف الشخصي diff --git a/config/locales/ca.yml b/config/locales/ca.yml index b74d7a00b..29a75f13b 100644 --- a/config/locales/ca.yml +++ b/config/locales/ca.yml @@ -123,10 +123,6 @@ ca: most_recent_ip: IP més recent no_limits_imposed: Sense límits imposats not_subscribed: No subscrit - order: - alphabetic: Alfabètic - most_recent: Més recent - title: Ordre outbox_url: URL de la bústia de sortida perform_full_suspension: Suspèn profile_url: URL del perfil diff --git a/config/locales/co.yml b/config/locales/co.yml index 9306c6c33..8c68aff69 100644 --- a/config/locales/co.yml +++ b/config/locales/co.yml @@ -123,10 +123,6 @@ co: most_recent_ip: IP più ricente no_limits_imposed: Nisuna limita imposta not_subscribed: Micca abbunatu - order: - alphabetic: Alfabeticu - most_recent: Più ricente - title: Urdine outbox_url: URL di l’outbox perform_full_suspension: Suspende profile_url: URL di u prufile diff --git a/config/locales/cs.yml b/config/locales/cs.yml index 05f65653b..9e749a951 100644 --- a/config/locales/cs.yml +++ b/config/locales/cs.yml @@ -115,10 +115,6 @@ cs: most_recent_ip: Nejnovější IP no_limits_imposed: Nejsou nastavena žádná omezení not_subscribed: Neodebírá - order: - alphabetic: Abecedně - most_recent: Nejnovější - title: Pořadí outbox_url: URL odchozích zpráv perform_full_suspension: Suspendovat profile_url: URL profilu diff --git a/config/locales/cy.yml b/config/locales/cy.yml index a4df28b61..e238abc37 100644 --- a/config/locales/cy.yml +++ b/config/locales/cy.yml @@ -115,10 +115,6 @@ cy: most_recent_ip: IP diweddaraf no_limits_imposed: Dim terfynau wedi'i gosod not_subscribed: Heb danysgrifio - order: - alphabetic: Allfabetig - most_recent: Diweddaraf - title: Trefnu outbox_url: Allflwch URL perform_full_suspension: Atal profile_url: URL proffil diff --git a/config/locales/da.yml b/config/locales/da.yml index d9ce9d55d..fb6cac828 100644 --- a/config/locales/da.yml +++ b/config/locales/da.yml @@ -122,10 +122,6 @@ da: most_recent_activity: Seneste aktivitet most_recent_ip: Senest IP not_subscribed: Ikke abonneret - order: - alphabetic: Alfabetisk - most_recent: Seneste - title: Rækkefølge outbox_url: Link til udgående perform_full_suspension: Udeluk profile_url: Link til profil diff --git a/config/locales/de.yml b/config/locales/de.yml index 587b9dfc9..51b4818bd 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -123,10 +123,6 @@ de: most_recent_ip: Letzte IP-Adresse no_limits_imposed: Keine Limits eingesetzt not_subscribed: Nicht abonniert - order: - alphabetic: Alphabetisch - most_recent: Neueste - title: Sortierung outbox_url: Postausgangs-URL perform_full_suspension: Sperren profile_url: Profil-URL diff --git a/config/locales/el.yml b/config/locales/el.yml index 8f718a849..757133c9b 100644 --- a/config/locales/el.yml +++ b/config/locales/el.yml @@ -123,10 +123,6 @@ el: most_recent_ip: Πιο πρόσφατη IP no_limits_imposed: Χωρίς όρια not_subscribed: Άνευ συνδρομής - order: - alphabetic: Αλφαβητικά - most_recent: Πιο πρόσφατα - title: Ταξινόμηση outbox_url: URL εξερχομένων perform_full_suspension: Κάνε πλήρη αναστολή profile_url: URL προφίλ diff --git a/config/locales/en.yml b/config/locales/en.yml index a2859aa5d..2d27a4ac7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -123,10 +123,6 @@ en: most_recent_ip: Most recent IP no_limits_imposed: No limits imposed not_subscribed: Not subscribed - order: - alphabetic: Alphabetic - most_recent: Most recent - title: Order outbox_url: Outbox URL perform_full_suspension: Suspend profile_url: Profile URL diff --git a/config/locales/eo.yml b/config/locales/eo.yml index 72628a944..eddefab05 100644 --- a/config/locales/eo.yml +++ b/config/locales/eo.yml @@ -112,10 +112,6 @@ eo: most_recent_activity: Lasta ago most_recent_ip: Lasta IP not_subscribed: Ne abonita - order: - alphabetic: Laŭalfabete - most_recent: Plej lastatempa - title: Ordo outbox_url: Elira URL perform_full_suspension: Tute haltigi profile_url: Profila URL diff --git a/config/locales/es.yml b/config/locales/es.yml index f7531161c..4cd1e2a38 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -123,10 +123,6 @@ es: most_recent_ip: IP más reciente no_limits_imposed: Sin límites impuestos not_subscribed: No se está suscrito - order: - alphabetic: Alfabético - most_recent: Más reciente - title: Orden outbox_url: URL de bandeja de salida perform_full_suspension: Suspender profile_url: URL del perfil diff --git a/config/locales/eu.yml b/config/locales/eu.yml index 206b439b0..122b074eb 100644 --- a/config/locales/eu.yml +++ b/config/locales/eu.yml @@ -123,10 +123,6 @@ eu: most_recent_ip: Azken IP-a no_limits_imposed: Ez da mugarik ezarri not_subscribed: Harpidetu gabe - order: - alphabetic: Alfabetikoa - most_recent: Azkena - title: Ordena outbox_url: Irteera ontziaren URL-a perform_full_suspension: Kanporatu profile_url: Profilaren URL-a diff --git a/config/locales/fa.yml b/config/locales/fa.yml index 4209a2c00..769b3a0fd 100644 --- a/config/locales/fa.yml +++ b/config/locales/fa.yml @@ -123,10 +123,6 @@ fa: most_recent_ip: آخرین IP ها no_limits_imposed: بدون محدودیت not_subscribed: عضو نیست - order: - alphabetic: الفبایی - most_recent: تازه‌ترین‌ها - title: ترتیب outbox_url: نشانی صندوق خروجی perform_full_suspension: تعلیق profile_url: نشانی نمایه diff --git a/config/locales/fi.yml b/config/locales/fi.yml index c90490212..77fca8dec 100644 --- a/config/locales/fi.yml +++ b/config/locales/fi.yml @@ -102,10 +102,6 @@ fi: most_recent_activity: Viimeisin toiminta most_recent_ip: Viimeisin IP not_subscribed: Ei tilaaja - order: - alphabetic: Aakkosjärjestys - most_recent: Uusin - title: Järjestys outbox_url: Lähtevän postilaatikon osoite perform_full_suspension: Siirrä kokonaan jäähylle profile_url: Profiilin osoite diff --git a/config/locales/fr.yml b/config/locales/fr.yml index e3128b569..51b4fb1f8 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -123,10 +123,6 @@ fr: most_recent_ip: Adresse IP la plus récente no_limits_imposed: Aucune limite imposée not_subscribed: Non abonné - order: - alphabetic: Alphabétique - most_recent: Plus récent - title: Tri outbox_url: URL de sortie perform_full_suspension: Suspendre profile_url: URL du profil diff --git a/config/locales/gl.yml b/config/locales/gl.yml index b9b9a37ad..726b6e400 100644 --- a/config/locales/gl.yml +++ b/config/locales/gl.yml @@ -123,10 +123,6 @@ gl: most_recent_ip: IP máis recente no_limits_imposed: Sen límites impostos not_subscribed: Non suscrita - order: - alphabetic: Alfabética - most_recent: Máis recente - title: Orde outbox_url: URL caixa de saída perform_full_suspension: Suspender profile_url: URL do perfil diff --git a/config/locales/he.yml b/config/locales/he.yml index 79b1ed822..65ca617b2 100644 --- a/config/locales/he.yml +++ b/config/locales/he.yml @@ -95,10 +95,6 @@ he: most_recent_activity: פעילות עדכנית most_recent_ip: כתובות אחרונות not_subscribed: לא רשום - order: - alphabetic: אלפביתי - most_recent: עדכני - title: סידור outbox_url: כתובת תיבת דואר יוצא perform_full_suspension: ביצוע השעייה מלאה profile_url: כתובת פרופיל diff --git a/config/locales/hu.yml b/config/locales/hu.yml index faa52fabc..cf2c5f1e4 100644 --- a/config/locales/hu.yml +++ b/config/locales/hu.yml @@ -92,10 +92,6 @@ hu: most_recent_activity: Legutóbbi tevékenységek most_recent_ip: Legutóbbi IP-cím not_subscribed: Nincs feliratkozás - order: - alphabetic: Alfabetikus - most_recent: Legutóbbi - title: Rendezés outbox_url: Kimenő üzenetek URL perform_full_suspension: Teljes felfüggesztés profile_url: Profil URL diff --git a/config/locales/id.yml b/config/locales/id.yml index 38b08a257..235bc0bcb 100644 --- a/config/locales/id.yml +++ b/config/locales/id.yml @@ -50,10 +50,6 @@ id: most_recent_activity: Aktivitas terbaru most_recent_ip: IP terbaru not_subscribed: Tidak berlangganan - order: - alphabetic: Alfabetik - most_recent: Terbaru - title: Urutan perform_full_suspension: Lakukan suspen penuh profile_url: URL profil public: Publik diff --git a/config/locales/io.yml b/config/locales/io.yml index 0c1e6520b..342fcbc28 100644 --- a/config/locales/io.yml +++ b/config/locales/io.yml @@ -44,10 +44,6 @@ io: most_recent_activity: Most recent activity most_recent_ip: Most recent IP not_subscribed: Not subscribed - order: - alphabetic: Alphabetic - most_recent: Most recent - title: Order perform_full_suspension: Perform full suspension profile_url: Profile URL public: Public diff --git a/config/locales/it.yml b/config/locales/it.yml index 4fffded5f..dc62b1bea 100644 --- a/config/locales/it.yml +++ b/config/locales/it.yml @@ -123,10 +123,6 @@ it: most_recent_ip: IP più recenti no_limits_imposed: Nessun limite imposto not_subscribed: Non sottoscritto - order: - alphabetic: Alfabetico - most_recent: Più recente - title: Ordine outbox_url: URL outbox perform_full_suspension: Sospendi profile_url: URL profilo diff --git a/config/locales/ja.yml b/config/locales/ja.yml index 90ff66acb..415665944 100644 --- a/config/locales/ja.yml +++ b/config/locales/ja.yml @@ -123,10 +123,6 @@ ja: most_recent_ip: 直近のIP no_limits_imposed: 制限なし not_subscribed: 購読していない - order: - alphabetic: アルファベット順 - most_recent: 直近の活動順 - title: 順序 outbox_url: Outbox URL perform_full_suspension: 完全に活動停止させる profile_url: プロフィールURL diff --git a/config/locales/ka.yml b/config/locales/ka.yml index 1b74405d2..b0d1087c3 100644 --- a/config/locales/ka.yml +++ b/config/locales/ka.yml @@ -112,10 +112,6 @@ ka: most_recent_activity: უახლესი აქტივობა most_recent_ip: უახლესი აი-პი not_subscribed: გამოუწერელი - order: - alphabetic: ანბანური - most_recent: უახლესი - title: წესრიგი outbox_url: აუთბოქსის ურლ perform_full_suspension: მოახდინეთ სრული შეჩერება profile_url: პროფილის ურლ diff --git a/config/locales/ko.yml b/config/locales/ko.yml index e1cb350d1..7c948e8ba 100644 --- a/config/locales/ko.yml +++ b/config/locales/ko.yml @@ -123,10 +123,6 @@ ko: most_recent_ip: 최근 IP no_limits_imposed: 제한 없음 not_subscribed: 구독하지 않음 - order: - alphabetic: 알파벳 순 - most_recent: 최근 순 - title: 순서 outbox_url: 발신함 URL perform_full_suspension: 정지시키기 profile_url: 프로필 URL diff --git a/config/locales/nl.yml b/config/locales/nl.yml index ab5a72a66..3c101fd77 100644 --- a/config/locales/nl.yml +++ b/config/locales/nl.yml @@ -123,10 +123,6 @@ nl: most_recent_ip: Laatst gebruikt IP-adres no_limits_imposed: Geen limieten ingesteld not_subscribed: Niet geabonneerd - order: - alphabetic: Alfabetisch - most_recent: Meest recent - title: Sorteren outbox_url: Outbox-URL perform_full_suspension: Opschorten profile_url: Profiel-URL diff --git a/config/locales/no.yml b/config/locales/no.yml index 61466fa20..5e06564ac 100644 --- a/config/locales/no.yml +++ b/config/locales/no.yml @@ -92,10 +92,6 @@ most_recent_activity: Nyligste aktivitet most_recent_ip: Nyligste IP not_subscribed: Ikke abonnért - order: - alphabetic: Alfabetisk - most_recent: Nyligst - title: Rekkefølge outbox_url: Utboks URL perform_full_suspension: Utfør full utvisning profile_url: Profil-URL diff --git a/config/locales/oc.yml b/config/locales/oc.yml index 428bbfffe..36d5ddda4 100644 --- a/config/locales/oc.yml +++ b/config/locales/oc.yml @@ -123,10 +123,6 @@ oc: most_recent_ip: IP mai recenta no_limits_imposed: Cap de limit impausat not_subscribed: Pas seguidor - order: - alphabetic: Alfabetic - most_recent: Mai recent - title: Ordre outbox_url: URL Outbox perform_full_suspension: Suspendre profile_url: URL del perfil diff --git a/config/locales/pl.yml b/config/locales/pl.yml index 9c893b605..2f79f5f6b 100644 --- a/config/locales/pl.yml +++ b/config/locales/pl.yml @@ -131,10 +131,6 @@ pl: most_recent_ip: Ostatnie IP no_limits_imposed: Nie nałożono ograniczeń not_subscribed: Nie zasubskrybowano - order: - alphabetic: Alfabetycznie - most_recent: Najnowsze - title: Kolejność outbox_url: Adres skrzynki nadawczej perform_full_suspension: Zawieś profile_url: Adres profilu diff --git a/config/locales/pt-BR.yml b/config/locales/pt-BR.yml index 2b9bf747d..1d778e60f 100644 --- a/config/locales/pt-BR.yml +++ b/config/locales/pt-BR.yml @@ -123,10 +123,6 @@ pt-BR: most_recent_ip: IP mais recente no_limits_imposed: Nenhum limite imposto not_subscribed: Não está inscrito - order: - alphabetic: Alfabética - most_recent: Mais recente - title: Ordem outbox_url: URL da caixa de saída perform_full_suspension: Suspender profile_url: URL do perfil diff --git a/config/locales/pt.yml b/config/locales/pt.yml index b68ffbd7f..4c23c9cf4 100644 --- a/config/locales/pt.yml +++ b/config/locales/pt.yml @@ -92,10 +92,6 @@ pt: most_recent_activity: Actividade mais recente most_recent_ip: IP mais recente not_subscribed: Não inscrito - order: - alphabetic: Alfabética - most_recent: Mais recente - title: Ordem outbox_url: URL da caixa de saída perform_full_suspension: Fazer suspensão completa profile_url: URL do perfil diff --git a/config/locales/ru.yml b/config/locales/ru.yml index a3ac754f2..9fa85b7c2 100644 --- a/config/locales/ru.yml +++ b/config/locales/ru.yml @@ -128,10 +128,6 @@ ru: most_recent_activity: Последняя активность most_recent_ip: Последний IP not_subscribed: Не подписаны - order: - alphabetic: По алфавиту - most_recent: По дате - title: Порядок outbox_url: URL исходящих perform_full_suspension: Полная блокировка profile_url: URL профиля diff --git a/config/locales/sk.yml b/config/locales/sk.yml index 2b928169a..cc06b2d6c 100644 --- a/config/locales/sk.yml +++ b/config/locales/sk.yml @@ -113,10 +113,6 @@ sk: most_recent_activity: Posledná aktivita most_recent_ip: Posledná IP not_subscribed: Nezaregistrované - order: - alphabetic: Abecedne - most_recent: Podľa času - title: Zoradiť outbox_url: URL poslaných perform_full_suspension: Suspendovať profile_url: URL profilu diff --git a/config/locales/sl.yml b/config/locales/sl.yml index 0e9d7d3aa..46f83876c 100644 --- a/config/locales/sl.yml +++ b/config/locales/sl.yml @@ -102,10 +102,6 @@ sl: moderation_notes: Opombe moderiranja most_recent_activity: Zadnja aktivnost most_recent_ip: Zadnji IP - order: - alphabetic: Po abecedi - most_recent: Najnovejše - title: Red promote: Spodbujanje remote_interaction: prompt: 'Želite interakcijo s tem trobom:' diff --git a/config/locales/sr-Latn.yml b/config/locales/sr-Latn.yml index 1e32190cc..50802945f 100644 --- a/config/locales/sr-Latn.yml +++ b/config/locales/sr-Latn.yml @@ -92,10 +92,6 @@ sr-Latn: most_recent_activity: Najskorija aktivnost most_recent_ip: Najskorija IP adresa not_subscribed: Nije pretplaćen - order: - alphabetic: Abecedni - most_recent: Najskoriji - title: Redosled outbox_url: Odlazno sanduče perform_full_suspension: Izvrši kompletno isključenje profile_url: Adresa profila diff --git a/config/locales/sr.yml b/config/locales/sr.yml index 1ade87f9e..14354f8a6 100644 --- a/config/locales/sr.yml +++ b/config/locales/sr.yml @@ -113,10 +113,6 @@ sr: most_recent_activity: Најскорија активност most_recent_ip: Најскорија IP адреса not_subscribed: Није претплаћен - order: - alphabetic: Абецедни - most_recent: Најскорији - title: Редослед outbox_url: Одлазно сандуче perform_full_suspension: Изврши комплетно искључење profile_url: Адреса профила diff --git a/config/locales/sv.yml b/config/locales/sv.yml index 465a9b127..55ab9b2ba 100644 --- a/config/locales/sv.yml +++ b/config/locales/sv.yml @@ -103,10 +103,6 @@ sv: most_recent_activity: Senaste aktivitet most_recent_ip: Senaste IP not_subscribed: Inte prenumererat - order: - alphabetic: Alfabetiskt - most_recent: Senaste - title: Ordning outbox_url: Utkorg URL perform_full_suspension: Utför full avstängning profile_url: Profil URL diff --git a/config/locales/th.yml b/config/locales/th.yml index b0b8e9ba0..42d52af26 100644 --- a/config/locales/th.yml +++ b/config/locales/th.yml @@ -49,10 +49,6 @@ th: most_recent_activity: กิจกรรมล่าสุด most_recent_ip: IP ล่าสุด not_subscribed: Not subscribed - order: - alphabetic: ตามตัวอักษร - most_recent: ล่าสุด - title: จัดเรียง perform_full_suspension: Perform full suspension profile_url: Profile URL public: สาธารณะ diff --git a/config/locales/tr.yml b/config/locales/tr.yml index bc0a558e1..14d356eef 100644 --- a/config/locales/tr.yml +++ b/config/locales/tr.yml @@ -48,10 +48,6 @@ tr: most_recent_activity: Son aktivite most_recent_ip: Son IP not_subscribed: Abone edilmedi - order: - alphabetic: Alfabetik - most_recent: En son - title: Sıralama perform_full_suspension: Tamamen uzaklaştır profile_url: Profil linki public: Herkese açık diff --git a/config/locales/uk.yml b/config/locales/uk.yml index e4ea774ec..28dd7f579 100644 --- a/config/locales/uk.yml +++ b/config/locales/uk.yml @@ -108,10 +108,6 @@ uk: most_recent_activity: Остання активність most_recent_ip: Останній IP not_subscribed: Не підписані - order: - alphabetic: За алфавітом - most_recent: За датою - title: Порядок outbox_url: Вихідний URL perform_full_suspension: Повне блокування profile_url: URL профілю diff --git a/config/locales/zh-CN.yml b/config/locales/zh-CN.yml index a32f36e32..744648921 100644 --- a/config/locales/zh-CN.yml +++ b/config/locales/zh-CN.yml @@ -114,10 +114,6 @@ zh-CN: most_recent_activity: 最后一次活跃的时间 most_recent_ip: 最后一次活跃的 IP 地址 not_subscribed: 未订阅 - order: - alphabetic: 按字母 - most_recent: 按时间 - title: 排序 outbox_url: 发件箱(Outbox)URL perform_full_suspension: 永久封禁 profile_url: 个人资料页面 URL diff --git a/config/locales/zh-HK.yml b/config/locales/zh-HK.yml index 7296587a3..abbaa77d6 100644 --- a/config/locales/zh-HK.yml +++ b/config/locales/zh-HK.yml @@ -103,10 +103,6 @@ zh-HK: most_recent_activity: 最新活動 most_recent_ip: 最新 IP 位域 not_subscribed: 未訂閱 - order: - alphabetic: 按字母 - most_recent: 按時間 - title: 排序 outbox_url: 寄件箱(Outbox)URL perform_full_suspension: 完全停權 profile_url: 個人檔案 URL diff --git a/config/locales/zh-TW.yml b/config/locales/zh-TW.yml index 9a7c2b293..b4c15f6f1 100644 --- a/config/locales/zh-TW.yml +++ b/config/locales/zh-TW.yml @@ -108,10 +108,6 @@ zh-TW: most_recent_activity: 最近活動 most_recent_ip: 最近 IP 位址 not_subscribed: 未訂閱 - order: - alphabetic: 按字母 - most_recent: 按時間 - title: 排序 outbox_url: 寄件箱 (Outbox) URL perform_full_suspension: 進行停權 profile_url: 個人檔案 URL diff --git a/spec/controllers/admin/accounts_controller_spec.rb b/spec/controllers/admin/accounts_controller_spec.rb index ae9e058c8..dbcad3c2d 100644 --- a/spec/controllers/admin/accounts_controller_spec.rb +++ b/spec/controllers/admin/accounts_controller_spec.rb @@ -24,8 +24,8 @@ RSpec.describe Admin::AccountsController, type: :controller do expect(h[:local]).to eq '1' expect(h[:remote]).to eq '1' expect(h[:by_domain]).to eq 'domain' + expect(h[:active]).to eq '1' expect(h[:silenced]).to eq '1' - expect(h[:alphabetic]).to eq '1' expect(h[:suspended]).to eq '1' expect(h[:username]).to eq 'username' expect(h[:display_name]).to eq 'display name' @@ -39,8 +39,8 @@ RSpec.describe Admin::AccountsController, type: :controller do local: '1', remote: '1', by_domain: 'domain', + active: '1', silenced: '1', - alphabetic: '1', suspended: '1', username: 'username', display_name: 'display name', diff --git a/spec/models/account_filter_spec.rb b/spec/models/account_filter_spec.rb index 0a0252642..176a0eeac 100644 --- a/spec/models/account_filter_spec.rb +++ b/spec/models/account_filter_spec.rb @@ -2,10 +2,10 @@ require 'rails_helper' describe AccountFilter do describe 'with empty params' do - it 'defaults to recent account list' do + it 'defaults to recent local not-suspended account list' do filter = described_class.new({}) - expect(filter.results).to eq Account.recent + expect(filter.results).to eq Account.local.recent.without_suspended end end @@ -17,23 +17,6 @@ describe AccountFilter do end end - describe 'when an IP address is provided' do - it 'filters with IP when valid' do - filter = described_class.new(ip: '127.0.0.1') - allow(User).to receive(:with_recent_ip_address).and_return(User.none) - - filter.results - expect(User).to have_received(:with_recent_ip_address).with('127.0.0.1') - end - - it 'skips IP when invalid' do - filter = described_class.new(ip: '345.678.901.234') - expect(User).not_to receive(:with_recent_ip_address) - - filter.results - end - end - describe 'with valid params' do it 'combines filters on Account' do filter = described_class.new( @@ -60,13 +43,13 @@ describe AccountFilter do end describe 'that call account methods' do - %i(local remote silenced alphabetic suspended).each do |option| + %i(local remote silenced suspended).each do |option| it "delegates the #{option} option" do allow(Account).to receive(option).and_return(Account.none) filter = described_class.new({ option => true }) filter.results - expect(Account).to have_received(option) + expect(Account).to have_received(option).at_least(1) end end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8c6778edc..c82919597 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -89,18 +89,6 @@ RSpec.describe User, type: :model do expect(User.matches_email('specified')).to match_array([specified]) end end - - describe 'with_recent_ip_address' do - it 'returns a relation of users who is, or was at last time, online with the given IP address' do - specifieds = [ - Fabricate(:user, current_sign_in_ip: '0.0.0.42', last_sign_in_ip: '0.0.0.0'), - Fabricate(:user, current_sign_in_ip: nil, last_sign_in_ip: '0.0.0.42') - ] - Fabricate(:user, current_sign_in_ip: '0.0.0.0', last_sign_in_ip: '0.0.0.0') - - expect(User.with_recent_ip_address('0.0.0.42')).to match_array(specifieds) - end - end end let(:account) { Fabricate(:account, username: 'alice') } -- cgit From 395615d9f3c521824f7e56f6d1bb2d82b8e421b4 Mon Sep 17 00:00:00 2001 From: ThibG Date: Tue, 27 Nov 2018 12:28:01 +0100 Subject: Allow hyphens in the middle of remote user names (#9345) Fixes #9309 This only allows hyphens in the middle of a username, much like dots, although I don't have a compelling reason to do so other than keeping the changes minimal. --- app/models/account.rb | 2 +- spec/models/account_spec.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) (limited to 'spec/models') diff --git a/app/models/account.rb b/app/models/account.rb index 46d32a36e..f25263306 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -46,7 +46,7 @@ # class Account < ApplicationRecord - USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.]+[a-z0-9_]+)?/i + USERNAME_RE = /[a-z0-9_]+([a-z0-9_\.-]+[a-z0-9_]+)?/i MENTION_RE = /(?<=^|[^\/[:word:]])@((#{USERNAME_RE})(?:@[a-z0-9\.\-]+[a-z0-9]+)?)/i include AccountAvatar diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index 9133616a2..f7f78d34c 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -618,9 +618,15 @@ RSpec.describe Account, type: :model do expect(account).not_to model_have_error_on_field(:username) end - it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do + it 'is valid even if the username contains hyphens' do account = Fabricate.build(:account, domain: 'domain', username: 'the-doctor') account.valid? + expect(account).to_not model_have_error_on_field(:username) + end + + it 'is invalid if the username doesn\'t only contains letters, numbers, underscores and hyphens' do + account = Fabricate.build(:account, domain: 'domain', username: 'the doctor') + account.valid? expect(account).to model_have_error_on_field(:username) end -- cgit