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