Skip to content

Commit 2fed2b1

Browse files
committed
notify node owner for new answer
For node of type Post and Tracker, notify the owner of new answers (in existing thread or new thread). Currently only limited to Post and Tracker as they are the only one shown in the dashboard.
1 parent 377636a commit 2fed2b1

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

app/models/comment.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ class Comment < ActiveRecord::Base
3131
scope :under, ->(path) { where("materialized_path LIKE ?", "#{path}_%") }
3232
scope :published, -> { where(state: 'published') }
3333
scope :on_dashboard, -> { published.order(created_at: :desc) }
34+
scope :last_published, -> {
35+
published.
36+
order(created_at: :desc).
37+
limit(1)
38+
}
3439
scope :footer, -> {
3540
# MariaDB tries to scan nodes first, which is a very slow, so we add an index hint
3641
from("comments USE INDEX (index_comments_on_state_and_created_at)").published.
@@ -139,6 +144,15 @@ def notify_parents
139144
end
140145
end
141146

147+
after_create :notify_node_owner
148+
def notify_node_owner
149+
# Only notify for node types shown in dashboard
150+
if Set['Post', 'Tracker'].include?(content_type) and user_id != node.user_id
151+
node_owner = node.user.try(:account)
152+
node_owner.notify_answer_on node_id if node_owner
153+
end
154+
end
155+
142156
### Calculations ###
143157

144158
before_validation :default_score, on: :create

app/models/node.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ def threads
106106
@threads ||= Threads.all(self.id)
107107
end
108108

109+
def last_answer
110+
comments.last_published.first
111+
end
112+
109113
### Readings ###
110114

111115
def self.readings_keys_of(account_id)

app/views/dashboard/_posts.html.haml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,20 @@
88
%tr
99
%th Forum
1010
%th Sujet
11+
%th Date
1112
%th Note
12-
%th Nombre de commentaires
13-
%th Date du message
13+
%th <abbr title="Nombre de réponses">Rép.</abbr>
14+
%th Dernière réponse
1415
- @posts.each do |node|
1516
- post = node.content
17+
- answer = node.last_answer
1618
%tr
1719
%td= post.forum.title
1820
%td= link_to post.title, [post.forum, post]
19-
%td.number= node.score
20-
%td.number= node.comments.count
2121
%td.date= post.created_at.to_s(:posted)
22+
%td.number= node.score
23+
%td.number
24+
- if answer && !answer.read_by?(current_account)
25+
= image_tag "/images/icones/comment.png", alt: "Nouveaux commentaires !", class: "thread-new-comments"
26+
= node.comments.count
27+
%td.date= answer ? answer.created_at.to_s(:posted) : " "

app/views/dashboard/_trackers.html.haml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,22 @@
88
%tr
99
%th Catégorie
1010
%th Sujet
11+
%th Date
1112
%th État
1213
%th Note
13-
%th Nombre de commentaires
14-
%th Date du message
14+
%th <abbr title="Nombre de réponses">Rép.</abbr>
15+
%th Dernière réponse
1516
- @trackers.each do |node|
1617
- tracker = node.content
18+
- answer = node.last_answer
1719
%tr{class: tracker.state}
1820
%td= tracker.category.title
1921
%td= link_to tracker.title, tracker
22+
%td.date= tracker.created_at.to_s(:posted)
2023
%td= tracker.state_name
2124
%td.number= node.score
22-
%td.number= node.comments.count
23-
%td.date= tracker.created_at.to_s(:posted)
25+
%td.number
26+
- if answer && !answer.read_by?(current_account)
27+
= image_tag "/images/icones/comment.png", alt: "Nouveaux commentaires !", class: "thread-new-comments"
28+
= node.comments.count
29+
%td.date= answer ? answer.created_at.to_s(:posted) : " "

0 commit comments

Comments
 (0)