2017-11-18 10:16:48 +11:00
|
|
|
# frozen_string_literal: true
|
2023-02-20 16:58:28 +11:00
|
|
|
|
2017-11-18 10:16:48 +11:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: list_accounts
|
|
|
|
#
|
2023-05-02 22:40:36 +10:00
|
|
|
# id :bigint(8) not null, primary key
|
|
|
|
# list_id :bigint(8) not null
|
|
|
|
# account_id :bigint(8) not null
|
|
|
|
# follow_id :bigint(8)
|
|
|
|
# follow_request_id :bigint(8)
|
2017-11-18 10:16:48 +11:00
|
|
|
#
|
|
|
|
|
|
|
|
class ListAccount < ApplicationRecord
|
2018-01-20 06:56:47 +11:00
|
|
|
belongs_to :list
|
|
|
|
belongs_to :account
|
2019-11-04 23:02:01 +11:00
|
|
|
belongs_to :follow, optional: true
|
2023-05-02 22:40:36 +10:00
|
|
|
belongs_to :follow_request, optional: true
|
2017-11-18 10:16:48 +11:00
|
|
|
|
2017-12-06 09:02:27 +11:00
|
|
|
validates :account_id, uniqueness: { scope: :list_id }
|
2023-05-04 03:19:25 +10:00
|
|
|
validate :validate_relationship
|
2017-12-06 09:02:27 +11:00
|
|
|
|
2024-09-13 18:49:03 +10:00
|
|
|
before_validation :set_follow, unless: :list_owner_account_is_account?
|
2017-11-18 10:16:48 +11:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_follow
|
2023-05-02 22:40:36 +10:00
|
|
|
self.follow = Follow.find_by!(account_id: list.account_id, target_account_id: account.id)
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
self.follow_request = FollowRequest.find_by!(account_id: list.account_id, target_account_id: account.id)
|
2017-11-18 10:16:48 +11:00
|
|
|
end
|
2023-05-04 03:19:25 +10:00
|
|
|
|
|
|
|
def validate_relationship
|
|
|
|
return if list.account_id == account_id
|
|
|
|
|
|
|
|
errors.add(:account_id, 'follow relationship missing') if follow_id.nil? && follow_request_id.nil?
|
|
|
|
errors.add(:follow, 'mismatched accounts') if follow_id.present? && follow.target_account_id != account_id
|
|
|
|
errors.add(:follow_request, 'mismatched accounts') if follow_request_id.present? && follow_request.target_account_id != account_id
|
|
|
|
end
|
2024-09-13 18:49:03 +10:00
|
|
|
|
|
|
|
def list_owner_account_is_account?
|
|
|
|
list.account_id == account_id
|
|
|
|
end
|
2017-11-18 10:16:48 +11:00
|
|
|
end
|