2020-04-18 15:59:18 -07:00
|
|
|
class Subscription < ApplicationRecord
|
2015-04-20 18:12:11 -07:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :answer
|
|
|
|
|
|
|
|
class << self
|
2023-03-04 11:42:51 -08:00
|
|
|
def subscribe(recipient, target)
|
2015-04-21 05:22:32 -07:00
|
|
|
existing = Subscription.find_by(user: recipient, answer: target)
|
2023-03-04 11:42:51 -08:00
|
|
|
return true if existing.present?
|
|
|
|
|
|
|
|
Subscription.create!(user: recipient, answer: target)
|
2015-04-20 18:12:11 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def unsubscribe(recipient, target)
|
|
|
|
if recipient.nil? or target.nil?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
subs = Subscription.find_by(user: recipient, answer: target)
|
2023-03-04 11:42:51 -08:00
|
|
|
subs&.destroy
|
2015-04-20 18:12:11 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def destruct(target)
|
|
|
|
if target.nil?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
Subscription.where(answer: target).destroy_all
|
|
|
|
end
|
|
|
|
|
2015-04-21 05:22:32 -07:00
|
|
|
def destruct_by(recipient, target)
|
|
|
|
if recipient.nil? or target.nil?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
subs = Subscription.find_by(user: recipient, answer: target)
|
|
|
|
subs.destroy unless subs.nil?
|
|
|
|
end
|
|
|
|
|
2015-04-20 18:12:11 -07:00
|
|
|
def notify(source, target)
|
|
|
|
if source.nil? or target.nil?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2023-03-04 11:42:51 -08:00
|
|
|
Subscription.where(answer: target).each do |subs|
|
2015-04-20 18:12:11 -07:00
|
|
|
next unless not subs.user == source.user
|
|
|
|
Notification.notify subs.user, source
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def denotify(source, target)
|
|
|
|
if source.nil? or target.nil?
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
Subscription.where(answer: target).each do |subs|
|
|
|
|
Notification.denotify subs.user, source
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|