fedibird-fe/app/models/favourite.rb
Claire 4ac78e2a06
Add feature to automatically delete old toots (#16529)
* Add account statuses cleanup policy model

* Record last inspected toot to delete to speed up successive calls to statuses_to_delete

* Add service to cleanup a given account's statuses within a budget

* Add worker to go through account policies and delete old toots

* Fix last inspected status id logic

All existing statuses older or equal to last inspected status id must be
kept by the current policy. This is an invariant that must be kept so that
resuming deletion from the last inspected status remains sound.

* Add tests

* Refactor scheduler and add tests

* Add user interface

* Add support for discriminating based on boosts/favs

* Add UI support for min_reblogs and min_favs, rework UI

* Address first round of review comments

* Replace Snowflake#id_at_start with with_random parameter

* Add tests

* Add tests for StatusesCleanupController

* Rework settings page

* Adjust load-avoiding mechanisms

* Please CodeClimate
2021-08-09 23:11:50 +02:00

49 lines
1.2 KiB
Ruby

# frozen_string_literal: true
# == Schema Information
#
# Table name: favourites
#
# id :bigint(8) not null, primary key
# created_at :datetime not null
# updated_at :datetime not null
# account_id :bigint(8) not null
# status_id :bigint(8) not null
#
class Favourite < ApplicationRecord
include Paginable
update_index('statuses#status', :status)
belongs_to :account, inverse_of: :favourites
belongs_to :status, inverse_of: :favourites
has_one :notification, as: :activity, dependent: :destroy
validates :status_id, uniqueness: { scope: :account_id }
before_validation do
self.status = status.reblog if status&.reblog?
end
after_create :increment_cache_counters
after_destroy :decrement_cache_counters
after_destroy :invalidate_cleanup_info
private
def increment_cache_counters
status&.increment_count!(:favourites_count)
end
def decrement_cache_counters
return if association(:status).loaded? && status.marked_for_destruction?
status&.decrement_count!(:favourites_count)
end
def invalidate_cleanup_info
return unless status&.account_id == account_id && account.local?
account.statuses_cleanup_policy&.invalidate_last_inspected(status, :unfav)
end
end