fedibird-fe/app/models/account_subscribe.rb

63 lines
1.7 KiB
Ruby
Raw Normal View History

2019-08-30 08:02:41 +00:00
# == Schema Information
#
# Table name: account_subscribes
#
# id :bigint(8) not null, primary key
# account_id :bigint(8)
# target_account_id :bigint(8)
# created_at :datetime not null
# updated_at :datetime not null
# list_id :bigint(8)
# show_reblogs :boolean default(TRUE), not null
# media_only :boolean default(FALSE), not null
2019-08-30 08:02:41 +00:00
#
class AccountSubscribe < ApplicationRecord
2019-12-12 11:02:35 +00:00
include Paginable
include RelationshipCacheable
2019-08-30 08:02:41 +00:00
belongs_to :account
belongs_to :target_account, class_name: 'Account'
belongs_to :list, optional: true
validates :account_id, uniqueness: { scope: [:target_account_id, :list_id] }
scope :recent, -> { reorder(id: :desc) }
scope :subscribed_lists, ->(account) { AccountSubscribe.where(target_account_id: account.id).where.not(list_id: nil).select(:list_id).uniq }
scope :home, -> { where(list_id: nil) }
scope :list, -> { where.not(list_id: nil) }
scope :with_reblog, ->(reblog) { where(show_reblogs: true) if reblog }
2021-08-16 06:46:01 +00:00
scope :with_media, ->(status) { where(media_only: false) unless status&.with_media? }
2019-08-30 08:02:41 +00:00
2019-12-12 11:02:35 +00:00
after_create :increment_cache_counters
after_destroy :decrement_cache_counters
attr_accessor :acct
def acct=(val)
return if val.nil?
target_account = ResolveAccountService.new.call(acct, skip_webfinger: true)
target_account ||= ResolveAccountService.new.call(acct, skip_webfinger: false)
end
def acct
target_account&.acct
end
2019-12-12 11:02:35 +00:00
private
def home?
list_id.nil?
end
2019-12-12 11:02:35 +00:00
def increment_cache_counters
account&.increment_count!(:subscribing_count)
end
def decrement_cache_counters
account&.decrement_count!(:subscribing_count)
end
2019-08-30 08:02:41 +00:00
end