]> git.openstreetmap.org Git - rails.git/blob - app/models/user_notifications.rb
Localisation updates from https://translatewiki.net.
[rails.git] / app / models / user_notifications.rb
1 # frozen_string_literal: true
2
3 class UserNotifications
4   class Notification
5     def self.from(notification)
6       event_type_name = event_type_name_of(notification)
7       klass = "UserNotifications::#{event_type_name}Notification".constantize
8       klass.new(notification)
9     end
10
11     # Takes "ChangesetCommentNotifier::Notification", returns "ChangesetComment"
12     def self.event_type_name_of(notification)
13       notification.class.name.sub("Notifier::Notification", "")
14     end
15
16     def initialize(notification)
17       @notification = notification
18     end
19
20     delegate :record, :to => :@notification
21
22     def to_partial_path
23       event_type_name = self.class.event_type_name_of(@notification)
24       "notifications/#{event_type_name.underscore}"
25     end
26
27     def timestamp
28       record.created_at
29     end
30   end
31
32   class ChangesetCommentNotification < Notification
33     delegate :changeset, :to => :record
34     delegate :id, :to => :changeset, :prefix => true
35
36     def changeset_summary
37       changeset.comment
38     end
39
40     def commenter
41       record.author
42     end
43
44     def comment_id
45       record.id
46     end
47
48     def comment_body
49       record.body
50     end
51   end
52
53   class DiaryCommentNotification < Notification
54     delegate :body, :to => :record
55     delegate :diary_entry, :to => :record
56     delegate :title, :to => :diary_entry, :prefix => true
57
58     def commenter
59       record.user
60     end
61
62     def comment_id
63       record.id
64     end
65
66     def comment_body
67       record.body
68     end
69
70     def diary_author
71       diary_entry.user
72     end
73   end
74
75   class GpxImportFailureNotification < Notification
76     def timestamp
77       @notification.created_at
78     end
79
80     def trace_filename
81       @notification.params[:trace_name]
82     end
83
84     def trace_description
85       @notification.params[:trace_description]
86     end
87
88     def trace_tags
89       @notification.params[:trace_tags]
90     end
91
92     def trace_possible_points
93       nil
94     end
95
96     def trace_points
97       nil
98     end
99
100     def error
101       @notification.params[:error]
102     end
103   end
104
105   class GpxImportSuccessNotification < Notification
106     delegate :timestamp, :to => :record
107
108     def trace_filename
109       record.name
110     end
111
112     def trace_description
113       record.description
114     end
115
116     def trace_tags
117       record.tags.map(&:tag)
118     end
119
120     def trace_possible_points
121       @notification.params[:possible_points]
122     end
123
124     def trace_points
125       record.size
126     end
127
128     delegate :user, :to => :record
129   end
130
131   class NewFollowerNotification < Notification
132     delegate :follower, :to => :record
133   end
134
135   class NoteCommentNotification < Notification
136     delegate :note, :to => :record
137     delegate :event, :to => :record
138     delegate :id, :to => :note, :prefix => true
139
140     def comment_body
141       record.body
142     end
143
144     def comment_id
145       record.id
146     end
147
148     def commenter
149       record.author
150     end
151
152     def note_text
153       note.description
154     end
155   end
156
157   include Enumerable
158
159   LISTABLE_NOTIFICATIONS = %w[
160     ChangesetCommentNotifier::Notification
161     DiaryCommentNotifier::Notification
162     GpxImportFailureNotifier::Notification
163     GpxImportSuccessNotifier::Notification
164     NewFollowerNotifier::Notification
165     NoteCommentNotifier::Notification
166   ].freeze
167
168   def self.wrap(notification_records)
169     notification_records.map { |record| Notification.from(record) }
170   end
171
172   def initialize(user)
173     @user = user
174   end
175
176   def notification_records(&)
177     @user
178       .notifications
179       .where(:type => LISTABLE_NOTIFICATIONS)
180   end
181
182   def empty?
183     none?
184   end
185 end