-
Notifications
You must be signed in to change notification settings - Fork 718
Expand file tree
/
Copy pathsubscription.rb
More file actions
73 lines (63 loc) · 3.09 KB
/
subscription.rb
File metadata and controls
73 lines (63 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Subscription < ApplicationRecord
VALID_SUBSCRIBABLES = %w(Work User Series).freeze
belongs_to :user
belongs_to :subscribable, polymorphic: true
validates_presence_of :user
validates :subscribable_type, inclusion: { in: VALID_SUBSCRIBABLES }
# Without the condition, you get a 500 error instead of a validation error
# if there's an invalid subscribable type
validates :subscribable, presence: true,
if: proc { |s| VALID_SUBSCRIBABLES.include?(s.subscribable_type) }
# Get the subscriptions associated with this work
# currently: users subscribed to work, users subscribed to creator of work
scope :for_work, lambda {|work|
where(["(subscribable_id = ? AND subscribable_type = 'Work')
OR (subscribable_id IN (?) AND subscribable_type = 'User')
OR (subscribable_id IN (?) AND subscribable_type = 'Series')",
work.id,
work.pseuds.pluck(:user_id),
work.serial_works.pluck(:series_id)]).
group(:user_id)
}
# The name of the object to which the user is subscribed
def name
if subscribable.respond_to?(:login)
subscribable.login
elsif subscribable.respond_to?(:name)
subscribable.name
elsif subscribable.respond_to?(:title)
subscribable.title
else
I18n.t("subscriptions.deleted")
end
end
def creator_no_longer_associated_with?(creation)
# If we're subscribed to a work or series, it doesn't matter who the creator is, we should send the notification
return false unless subscribable_type == "User"
# If any of the creation's pseud's users matches the subscribable, then the
# creator still matches, so we return "true"
return false if creation.pseuds.any? { |p| p.user == subscribable }
# We reach this case if e.g. someone is subscribed to a user, but they
# orphan the work before the subscription notification would be sent
true
end
# Guard against scenarios that may break anonymity or other things.
# Emails should only contain works or chapters.
# Emails should only contain posted works or chapters.
# Emails should never contain chapters of draft works.
# Emails should never contain hidden works or chapters of hidden works.
# Emails should not contain orphaned works or chapters if the subscription is to a creator no longer associated with the work
# Emails for user subs should never contain anon works or chapters.
# Emails for work subs should never contain anything but chapters.
# TODO: AO3-1250: Anon series subscription improvements
def valid_notification_entry?(creation)
return false unless creation.is_a?(Chapter) || creation.is_a?(Work)
return false unless creation.try(:posted)
return false if creation.is_a?(Chapter) && !creation.work.try(:posted)
return false if creation.try(:hidden_by_admin) || (creation.is_a?(Chapter) && creation.work.try(:hidden_by_admin))
return false if creator_no_longer_associated_with?(creation)
return false if subscribable_type == "User" && creation.anonymous?
return false if subscribable_type == "Work" && !creation.is_a?(Chapter)
true
end
end