]> git.openstreetmap.org Git - rails.git/blob - app/models/note.rb
Use filled chat icon for nonzero comment counts in history
[rails.git] / app / models / note.rb
1 # == Schema Information
2 #
3 # Table name: notes
4 #
5 #  id          :bigint           not null, primary key
6 #  latitude    :integer          not null
7 #  longitude   :integer          not null
8 #  tile        :bigint           not null
9 #  updated_at  :datetime         not null
10 #  created_at  :datetime         not null
11 #  status      :enum             not null
12 #  closed_at   :datetime
13 #  description :text             default(""), not null
14 #  user_id     :bigint
15 #  user_ip     :inet
16 #
17 # Indexes
18 #
19 #  index_notes_on_description             (to_tsvector('english'::regconfig, description)) USING gin
20 #  index_notes_on_user_id_and_created_at  (user_id,created_at) WHERE (user_id IS NOT NULL)
21 #  notes_created_at_idx                   (created_at)
22 #  notes_tile_status_idx                  (tile,status)
23 #  notes_updated_at_idx                   (updated_at)
24 #
25 # Foreign Keys
26 #
27 #  notes_user_id_fkey  (user_id => users.id)
28 #
29
30 class Note < ApplicationRecord
31   include GeoRecord
32
33   belongs_to :author, :class_name => "User", :foreign_key => "user_id", :optional => true
34
35   has_many :comments, -> { left_joins(:author).where(:visible => true, :users => { :status => [nil, "active", "confirmed"] }).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id
36   has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id, :inverse_of => :note
37   has_many :subscriptions, :class_name => "NoteSubscription"
38   has_many :subscribers, :through => :subscriptions, :source => :user
39
40   validates :id, :uniqueness => true, :presence => { :on => :update },
41                  :numericality => { :on => :update, :only_integer => true }
42   validates :latitude, :longitude, :numericality => { :only_integer => true }
43   validates :closed_at, :presence => true, :if => proc { :status == "closed" }
44   validates :status, :inclusion => %w[open closed hidden]
45
46   validate :validate_position
47
48   scope :visible, -> { where.not(:status => "hidden") }
49   scope :invisible, -> { where(:status => "hidden") }
50
51   after_initialize :set_defaults
52
53   DEFAULT_FRESHLY_CLOSED_LIMIT = 7.days
54
55   # Sanity check the latitude and longitude and add an error if it's broken
56   def validate_position
57     errors.add(:base, "Note is not in the world") unless in_world?
58   end
59
60   # Close a note
61   def close
62     self.status = "closed"
63     self.closed_at = Time.now.utc
64     save
65   end
66
67   # Reopen a note
68   def reopen
69     self.status = "open"
70     self.closed_at = nil
71     save
72   end
73
74   # Check if a note is visible
75   def visible?
76     status != "hidden"
77   end
78
79   # Check if a note is closed
80   def closed?
81     !closed_at.nil?
82   end
83
84   def freshly_closed?
85     return false unless closed?
86
87     Time.now.utc < freshly_closed_until
88   end
89
90   def freshly_closed_until
91     return nil unless closed?
92
93     closed_at + DEFAULT_FRESHLY_CLOSED_LIMIT
94   end
95
96   # Return the note's description
97   def description
98     RichText.new("text", super)
99   end
100
101   private
102
103   # Fill in default values for new notes
104   def set_defaults
105     self.status = "open" unless attribute_present?(:status)
106   end
107 end