From 9e75aa30cd229c45fd95e00a93416221c83a4fd7 Mon Sep 17 00:00:00 2001 From: abcang Date: Sun, 19 Aug 2018 22:52:38 +0900 Subject: Unuse ActiveRecord::Base#cache_key (#8185) * Unuse ActiveRecord::Base#cache_key * Enable cache_versioning * Call cache_collection --- app/models/status.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'app/models') diff --git a/app/models/status.rb b/app/models/status.rb index 533d12354..6ba7b7a50 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -24,8 +24,6 @@ # class Status < ApplicationRecord - self.cache_versioning = false - include Paginable include Streamable include Cacheable -- cgit From b34d6238cbd2be08e5df90b1dd99f266948fab8b Mon Sep 17 00:00:00 2001 From: ThibG Date: Mon, 20 Aug 2018 18:46:04 +0200 Subject: Add API endpoint to list featured accounts (fixes #8315) (#8317) --- app/controllers/api/v1/endorsements_controller.rb | 72 +++++++++++++++++++++++ app/models/account_pin.rb | 1 + config/routes.rb | 15 ++--- 3 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 app/controllers/api/v1/endorsements_controller.rb (limited to 'app/models') diff --git a/app/controllers/api/v1/endorsements_controller.rb b/app/controllers/api/v1/endorsements_controller.rb new file mode 100644 index 000000000..0f04b488f --- /dev/null +++ b/app/controllers/api/v1/endorsements_controller.rb @@ -0,0 +1,72 @@ +# frozen_string_literal: true + +class Api::V1::EndorsementsController < Api::BaseController + before_action -> { doorkeeper_authorize! :read, :'read:accounts' } + before_action :require_user! + after_action :insert_pagination_headers + + respond_to :json + + def index + @accounts = load_accounts + render json: @accounts, each_serializer: REST::AccountSerializer + end + + private + + def load_accounts + if unlimited? + endorsed_accounts.all + else + endorsed_accounts.paginate_by_max_id( + limit_param(DEFAULT_ACCOUNTS_LIMIT), + params[:max_id], + params[:since_id] + ) + end + end + + def endorsed_accounts + current_account.endorsed_accounts + end + + def insert_pagination_headers + set_pagination_headers(next_path, prev_path) + end + + def next_path + return if unlimited? + + if records_continue? + api_v1_endorsements_url pagination_params(max_id: pagination_max_id) + end + end + + def prev_path + return if unlimited? + + unless @accounts.empty? + api_v1_endorsements_url pagination_params(since_id: pagination_since_id) + end + end + + def pagination_max_id + @accounts.last.id + end + + def pagination_since_id + @accounts.first.id + end + + def records_continue? + @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT) + end + + def pagination_params(core_params) + params.slice(:limit).permit(:limit).merge(core_params) + end + + def unlimited? + params[:limit] == '0' + end +end diff --git a/app/models/account_pin.rb b/app/models/account_pin.rb index 9a21c3405..b51d3d4cd 100644 --- a/app/models/account_pin.rb +++ b/app/models/account_pin.rb @@ -11,6 +11,7 @@ # class AccountPin < ApplicationRecord + include Paginable include RelationshipCacheable belongs_to :account diff --git a/config/routes.rb b/config/routes.rb index a8716aae5..da7cb8061 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -261,13 +261,14 @@ Rails.application.routes.draw do get '/search', to: 'search#index', as: :search - resources :follows, only: [:create] - resources :media, only: [:create, :update] - resources :blocks, only: [:index] - resources :mutes, only: [:index] - resources :favourites, only: [:index] - resources :reports, only: [:index, :create] - resources :filters, only: [:index, :create, :show, :update, :destroy] + resources :follows, only: [:create] + resources :media, only: [:create, :update] + resources :blocks, only: [:index] + resources :mutes, only: [:index] + resources :favourites, only: [:index] + resources :reports, only: [:index, :create] + resources :filters, only: [:index, :create, :show, :update, :destroy] + resources :endorsements, only: [:index] namespace :apps do get :verify_credentials, to: 'credentials#show' -- cgit From 83746b63646088396232a348f4024b101f39c78a Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 21 Aug 2018 02:49:51 +0200 Subject: Remove annoying Notification#cache_ids scope warning (#8333) --- app/models/notification.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/notification.rb b/app/models/notification.rb index 4f6ec8e8e..b9bec0808 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -39,8 +39,6 @@ class Notification < ApplicationRecord validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] } validates :activity_type, inclusion: { in: TYPE_CLASS_MAP.values } - scope :cache_ids, -> { select(:id, :updated_at, :activity_type, :activity_id) } - scope :browserable, ->(exclude_types = []) { types = TYPE_CLASS_MAP.values - activity_types_from_types(exclude_types + [:follow_request]) where(activity_type: types) @@ -68,6 +66,10 @@ class Notification < ApplicationRecord end class << self + def cache_ids + select(:id, :updated_at, :activity_type, :activity_id) + end + def reload_stale_associations!(cached_items) account_ids = (cached_items.map(&:from_account_id) + cached_items.map { |item| item.target_status&.account_id }.compact).uniq -- cgit From d98de8ada743886c3cd48b2ad942d46b805af7a9 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 21 Aug 2018 12:25:50 +0200 Subject: Get rid of all batch order warnings (#8334) --- app/models/form/status_batch.rb | 4 ++-- app/services/after_block_domain_from_account_service.rb | 4 ++-- app/services/backup_service.rb | 4 ++-- app/services/block_domain_service.rb | 6 +++--- app/services/remove_status_service.rb | 4 ++-- app/workers/refollow_worker.rb | 2 +- app/workers/scheduler/backup_cleanup_scheduler.rb | 2 +- app/workers/scheduler/email_scheduler.rb | 2 +- app/workers/scheduler/user_cleanup_scheduler.rb | 2 +- lib/tasks/mastodon.rake | 8 ++++---- 10 files changed, 19 insertions(+), 19 deletions(-) (limited to 'app/models') diff --git a/app/models/form/status_batch.rb b/app/models/form/status_batch.rb index 4f08a3049..8f5fd1fa2 100644 --- a/app/models/form/status_batch.rb +++ b/app/models/form/status_batch.rb @@ -23,7 +23,7 @@ class Form::StatusBatch media_attached_status_ids = MediaAttachment.where(status_id: status_ids).pluck(:status_id) ApplicationRecord.transaction do - Status.where(id: media_attached_status_ids).find_each do |status| + Status.where(id: media_attached_status_ids).reorder(nil).find_each do |status| status.update!(sensitive: sensitive) log_action :update, status end @@ -35,7 +35,7 @@ class Form::StatusBatch end def delete_statuses - Status.where(id: status_ids).find_each do |status| + Status.where(id: status_ids).reorder(nil).find_each do |status| RemovalWorker.perform_async(status.id) log_action :destroy, status end diff --git a/app/services/after_block_domain_from_account_service.rb b/app/services/after_block_domain_from_account_service.rb index 0f1a8505d..56cc819fb 100644 --- a/app/services/after_block_domain_from_account_service.rb +++ b/app/services/after_block_domain_from_account_service.rb @@ -15,13 +15,13 @@ class AfterBlockDomainFromAccountService < BaseService private def reject_existing_followers! - @account.passive_relationships.where(account: Account.where(domain: @domain)).includes(:account).find_each do |follow| + @account.passive_relationships.where(account: Account.where(domain: @domain)).includes(:account).reorder(nil).find_each do |follow| reject_follow!(follow) end end def reject_pending_follow_requests! - FollowRequest.where(target_account: @account).where(account: Account.where(domain: @domain)).includes(:account).find_each do |follow_request| + FollowRequest.where(target_account: @account).where(account: Account.where(domain: @domain)).includes(:account).reorder(nil).find_each do |follow_request| reject_follow!(follow_request) end end diff --git a/app/services/backup_service.rb b/app/services/backup_service.rb index 8492c1117..da7db6462 100644 --- a/app/services/backup_service.rb +++ b/app/services/backup_service.rb @@ -18,7 +18,7 @@ class BackupService < BaseService def build_json! @collection = serialize(collection_presenter, ActivityPub::CollectionSerializer) - account.statuses.with_includes.find_in_batches do |statuses| + account.statuses.with_includes.reorder(nil).find_in_batches do |statuses| statuses.each do |status| item = serialize(status, ActivityPub::ActivitySerializer) item.delete(:'@context') @@ -60,7 +60,7 @@ class BackupService < BaseService end def dump_media_attachments!(tar) - MediaAttachment.attached.where(account: account).find_in_batches do |media_attachments| + MediaAttachment.attached.where(account: account).reorder(nil).find_in_batches do |media_attachments| media_attachments.each do |m| download_to_tar(tar, m.file, m.file.path) end diff --git a/app/services/block_domain_service.rb b/app/services/block_domain_service.rb index d082de40b..a1fe93665 100644 --- a/app/services/block_domain_service.rb +++ b/app/services/block_domain_service.rb @@ -43,14 +43,14 @@ class BlockDomainService < BaseService end def suspend_accounts! - blocked_domain_accounts.where(suspended: false).find_each do |account| + blocked_domain_accounts.where(suspended: false).reorder(nil).find_each do |account| UnsubscribeService.new.call(account) if account.subscribed? SuspendAccountService.new.call(account) end end def clear_account_images! - blocked_domain_accounts.find_each do |account| + blocked_domain_accounts.reorder(nil).find_each do |account| account.avatar.destroy if account.avatar.exists? account.header.destroy if account.header.exists? account.save @@ -58,7 +58,7 @@ class BlockDomainService < BaseService end def clear_account_attachments! - media_from_blocked_domain.find_each do |attachment| + media_from_blocked_domain.reorder(nil).find_each do |attachment| @affected_status_ids << attachment.status_id if attachment.status_id.present? attachment.file.destroy if attachment.file.exists? diff --git a/app/services/remove_status_service.rb b/app/services/remove_status_service.rb index fb889140b..1a53093b8 100644 --- a/app/services/remove_status_service.rb +++ b/app/services/remove_status_service.rb @@ -43,13 +43,13 @@ class RemoveStatusService < BaseService end def remove_from_followers - @account.followers_for_local_distribution.find_each do |follower| + @account.followers_for_local_distribution.reorder(nil).find_each do |follower| FeedManager.instance.unpush_from_home(follower, @status) end end def remove_from_lists - @account.lists_for_local_distribution.select(:id, :account_id).find_each do |list| + @account.lists_for_local_distribution.select(:id, :account_id).reorder(nil).find_each do |list| FeedManager.instance.unpush_from_list(list, @status) end end diff --git a/app/workers/refollow_worker.rb b/app/workers/refollow_worker.rb index 66bcd27c3..12f2bf671 100644 --- a/app/workers/refollow_worker.rb +++ b/app/workers/refollow_worker.rb @@ -9,7 +9,7 @@ class RefollowWorker target_account = Account.find(target_account_id) return unless target_account.protocol == :activitypub - target_account.followers.where(domain: nil).find_each do |follower| + target_account.followers.where(domain: nil).reorder(nil).find_each do |follower| # Locally unfollow remote account follower.unfollow!(target_account) diff --git a/app/workers/scheduler/backup_cleanup_scheduler.rb b/app/workers/scheduler/backup_cleanup_scheduler.rb index cfdf6f4af..023a77307 100644 --- a/app/workers/scheduler/backup_cleanup_scheduler.rb +++ b/app/workers/scheduler/backup_cleanup_scheduler.rb @@ -6,7 +6,7 @@ class Scheduler::BackupCleanupScheduler sidekiq_options unique: :until_executed def perform - old_backups.find_each(&:destroy!) + old_backups.reorder(nil).find_each(&:destroy!) end private diff --git a/app/workers/scheduler/email_scheduler.rb b/app/workers/scheduler/email_scheduler.rb index 1a5a1c826..24117e424 100644 --- a/app/workers/scheduler/email_scheduler.rb +++ b/app/workers/scheduler/email_scheduler.rb @@ -6,7 +6,7 @@ class Scheduler::EmailScheduler sidekiq_options unique: :until_executed def perform - eligible_users.find_each do |user| + eligible_users.reorder(nil).find_each do |user| next unless user.allows_digest_emails? DigestMailerWorker.perform_async(user.id) end diff --git a/app/workers/scheduler/user_cleanup_scheduler.rb b/app/workers/scheduler/user_cleanup_scheduler.rb index dde195bff..626fb1652 100644 --- a/app/workers/scheduler/user_cleanup_scheduler.rb +++ b/app/workers/scheduler/user_cleanup_scheduler.rb @@ -6,7 +6,7 @@ class Scheduler::UserCleanupScheduler sidekiq_options unique: :until_executed def perform - User.where('confirmed_at is NULL AND confirmation_sent_at <= ?', 2.days.ago).find_in_batches do |batch| + User.where('confirmed_at is NULL AND confirmation_sent_at <= ?', 2.days.ago).reorder(nil).find_in_batches do |batch| Account.where(id: batch.map(&:account_id)).delete_all User.where(id: batch.map(&:id)).delete_all end diff --git a/lib/tasks/mastodon.rake b/lib/tasks/mastodon.rake index f693c8b5a..191ce634c 100644 --- a/lib/tasks/mastodon.rake +++ b/lib/tasks/mastodon.rake @@ -503,7 +503,7 @@ namespace :mastodon do desc 'Remove media attachments attributed to silenced accounts' task remove_silenced: :environment do nb_media_attachments = 0 - MediaAttachment.where(account: Account.silenced).select(:id).find_in_batches do |media_attachments| + MediaAttachment.where(account: Account.silenced).select(:id).reorder(nil).find_in_batches do |media_attachments| nb_media_attachments += media_attachments.length Maintenance::DestroyMediaWorker.push_bulk(media_attachments.map(&:id)) end @@ -515,7 +515,7 @@ namespace :mastodon do time_ago = ENV.fetch('NUM_DAYS') { 7 }.to_i.days.ago nb_media_attachments = 0 - MediaAttachment.where.not(remote_url: '').where.not(file_file_name: nil).where('created_at < ?', time_ago).select(:id).find_in_batches do |media_attachments| + MediaAttachment.where.not(remote_url: '').where.not(file_file_name: nil).where('created_at < ?', time_ago).select(:id).reorder(nil).find_in_batches do |media_attachments| nb_media_attachments += media_attachments.length Maintenance::UncacheMediaWorker.push_bulk(media_attachments.map(&:id)) end @@ -535,7 +535,7 @@ namespace :mastodon do accounts = accounts.where(domain: ENV['DOMAIN']) if ENV['DOMAIN'].present? nb_accounts = 0 - accounts.select(:id).find_in_batches do |accounts_batch| + accounts.select(:id).reorder(nil).find_in_batches do |accounts_batch| nb_accounts += accounts_batch.length Maintenance::RedownloadAccountMediaWorker.push_bulk(accounts_batch.map(&:id)) end @@ -570,7 +570,7 @@ namespace :mastodon do desc 'Generates home timelines for users who logged in in the past two weeks' task build: :environment do - User.active.select(:id, :account_id).find_in_batches do |users| + User.active.select(:id, :account_id).reorder(nil).find_in_batches do |users| RegenerationWorker.push_bulk(users.map(&:account_id)) end end -- cgit