]> git.openstreetmap.org Git - rails.git/blob - app/models/diary_comment.rb
Merge remote-tracking branch 'upstream/pull/5064'
[rails.git] / app / models / diary_comment.rb
1 # == Schema Information
2 #
3 # Table name: diary_comments
4 #
5 #  id             :bigint(8)        not null, primary key
6 #  diary_entry_id :bigint(8)        not null
7 #  user_id        :bigint(8)        not null
8 #  body           :text             not null
9 #  created_at     :datetime         not null
10 #  updated_at     :datetime         not null
11 #  visible        :boolean          default(TRUE), not null
12 #  body_format    :enum             default("markdown"), not null
13 #
14 # Indexes
15 #
16 #  diary_comment_user_id_created_at_index  (user_id,created_at)
17 #  diary_comments_entry_id_idx             (diary_entry_id,id) UNIQUE
18 #  index_diary_comments_on_user_id_and_id  (user_id,id)
19 #
20 # Foreign Keys
21 #
22 #  diary_comments_diary_entry_id_fkey  (diary_entry_id => diary_entries.id)
23 #  diary_comments_user_id_fkey         (user_id => users.id)
24 #
25
26 class DiaryComment < ApplicationRecord
27   belongs_to :user, :counter_cache => true
28   belongs_to :diary_entry
29
30   scope :visible, -> { where(:visible => true) }
31
32   validates :body, :presence => true, :characters => true
33   validates :diary_entry, :user, :associated => true
34
35   after_save :spam_check
36
37   def body
38     RichText.new(self[:body_format], self[:body])
39   end
40
41   def notification_token(subscriber)
42     sha256 = Digest::SHA256.new
43     sha256 << Rails.application.key_generator.generate_key("openstreetmap/diary_comment")
44     sha256 << id.to_s
45     sha256 << subscriber.to_s
46     Base64.urlsafe_encode64(sha256.digest)[0, 8]
47   end
48
49   private
50
51   def spam_check
52     user.spam_check
53   end
54 end