1
0
Fork 0
forked from gitea/nas

Add emoji reaction option on status cleanup

This commit is contained in:
KMY 2023-04-20 17:40:25 +09:00
parent 3a1f268be2
commit 1a8e8f02dd
8 changed files with 41 additions and 6 deletions

View file

@ -18,6 +18,8 @@
# min_reblogs :integer
# created_at :datetime not null
# updated_at :datetime not null
# min_emojis :integer
# keep_self_emoji :boolean default(TRUE), not null
#
class AccountStatusesCleanupPolicy < ApplicationRecord
include Redisable
@ -33,8 +35,8 @@ class AccountStatusesCleanupPolicy < ApplicationRecord
2.years.seconds,
].freeze
EXCEPTION_BOOLS = %w(keep_direct keep_pinned keep_polls keep_media keep_self_fav keep_self_bookmark).freeze
EXCEPTION_THRESHOLDS = %w(min_favs min_reblogs).freeze
EXCEPTION_BOOLS = %w(keep_direct keep_pinned keep_polls keep_media keep_self_fav keep_self_bookmark keep_self_emoji).freeze
EXCEPTION_THRESHOLDS = %w(min_favs min_reblogs min_emojis).freeze
# Depending on the cleanup policy, the query to discover the next
# statuses to delete my get expensive if the account has a lot of old
@ -52,6 +54,7 @@ class AccountStatusesCleanupPolicy < ApplicationRecord
validates :min_status_age, inclusion: { in: ALLOWED_MIN_STATUS_AGE }
validates :min_favs, numericality: { greater_than_or_equal_to: 1, allow_nil: true }
validates :min_reblogs, numericality: { greater_than_or_equal_to: 1, allow_nil: true }
validates :min_emojis, numericality: { greater_than_or_equal_to: 1, allow_nil: true }
validate :validate_local_account
before_save :update_last_inspected
@ -60,13 +63,14 @@ class AccountStatusesCleanupPolicy < ApplicationRecord
scope = account.statuses
scope.merge!(old_enough_scope(max_id))
scope = scope.where(Status.arel_table[:id].gteq(min_id)) if min_id.present?
scope.merge!(without_popular_scope) unless min_favs.nil? && min_reblogs.nil?
scope.merge!(without_popular_scope) unless min_favs.nil? && min_reblogs.nil? && min_emojis.nil?
scope.merge!(without_direct_scope) if keep_direct?
scope.merge!(without_pinned_scope) if keep_pinned?
scope.merge!(without_poll_scope) if keep_polls?
scope.merge!(without_media_scope) if keep_media?
scope.merge!(without_self_fav_scope) if keep_self_fav?
scope.merge!(without_self_bookmark_scope) if keep_self_bookmark?
scope.merge!(without_self_emoji_scope) if keep_self_emoji?
scope.reorder(id: :asc).limit(limit)
end
@ -107,6 +111,8 @@ class AccountStatusesCleanupPolicy < ApplicationRecord
return unless keep_self_bookmark?
when :unfav
return unless keep_self_fav?
when :unemoji
return unless keep_self_emoji?
when :unpin
return unless keep_pinned?
end
@ -148,6 +154,10 @@ class AccountStatusesCleanupPolicy < ApplicationRecord
Status.where('NOT EXISTS (SELECT * FROM favourites fav WHERE fav.account_id = statuses.account_id AND fav.status_id = statuses.id)')
end
def without_self_emoji_scope
Status.where('NOT EXISTS (SELECT * FROM emoji_reactions emj WHERE emj.account_id = statuses.account_id AND emj.status_id = statuses.id)')
end
def without_self_bookmark_scope
Status.where('NOT EXISTS (SELECT * FROM bookmarks bookmark WHERE bookmark.account_id = statuses.account_id AND bookmark.status_id = statuses.id)')
end
@ -168,6 +178,7 @@ class AccountStatusesCleanupPolicy < ApplicationRecord
scope = Status.left_joins(:status_stat)
scope = scope.where('COALESCE(status_stats.reblogs_count, 0) < ?', min_reblogs) unless min_reblogs.nil?
scope = scope.where('COALESCE(status_stats.favourites_count, 0) < ?', min_favs) unless min_favs.nil?
scope = scope.where('COALESCE(status_stats.emoji_reactions_count, 0) < ?', min_emojis) unless min_emojis.nil?
scope
end
end