Skip to content

Commit 49f2c76

Browse files
committed
add /comments/latest route
This page will be useful for moderators to retrieve latest comments. The limit is based on current date to limit number of comments retrieved from the database. Only comments from the last 7 days will be available. As it's the first page to index all comments with criteria only made on created_at property, I added an index to the comments sql table. As LinuxFr has between 1500 to 2000 comments each 15 days, I've decided to paginate to 50 comments. As the number of comments in the atom feed is linked to this pagination setting, I wanted it to be big enough so RSS readers can take all comments with around 4 requests by day.
1 parent 377636a commit 49f2c76

File tree

9 files changed

+74
-4
lines changed

9 files changed

+74
-4
lines changed

app/assets/stylesheets/parts/content.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* contenu du site (hors commentaire) */
22
article,
33
ul.threads > li.comment,
4+
ul.comments_list > li.comment,
45
.markdown_cheatsheet,
56
#contents > form,
67
form#new_post,
@@ -30,6 +31,10 @@ body#wiki_pages-changes #contents {
3031
font-size: 1em;
3132
}
3233

34+
#contents > ul.comments_list {
35+
padding: 0;
36+
}
37+
3338
article {
3439
position: relative;
3540
header {

app/controllers/comments_controller.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# encoding: UTF-8
22
class CommentsController < ApplicationController
3-
before_action :authenticate_account!, except: [:index, :show]
4-
before_action :find_node, except: [:templeet]
5-
before_action :find_comment, except: [:index, :new, :answer, :create, :templeet]
3+
before_action :authenticate_account!, except: [:index, :show, :latest]
4+
before_action :find_node, except: [:templeet, :latest]
5+
before_action :find_comment, except: [:index, :new, :answer, :create, :templeet, :latest]
66

77
def index
88
@comments = @node.comments.published.order('id DESC')
@@ -12,6 +12,16 @@ def index
1212
end
1313
end
1414

15+
def latest
16+
@comments = Comment.latest.
17+
page(params[:page]).
18+
order(created_at: :desc)
19+
respond_to do |wants|
20+
wants.html
21+
wants.atom
22+
end
23+
end
24+
1525
def show
1626
enforce_view_permission(@comment)
1727
end

app/models/comment.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ class Comment < ActiveRecord::Base
3939
limit(12).
4040
select([:id, :node_id, :title])
4141
}
42+
scope :latest, -> {
43+
where(["created_at >= ?", Date.current - 7.day])
44+
}
45+
46+
paginates_per 50
4247

4348
validates :title, presence: { message: "Le titre est obligatoire" },
4449
length: { maximum: 100, message: "Le titre est trop long" }
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
atom_feed(:root_url => comments_latest_url) do |feed|
2+
feed.title("LinuxFr.org : les derniers commentaires")
3+
feed.updated(@comments.first.try :created_at)
4+
feed.icon("/favicon.png")
5+
6+
@comments.each do |comment|
7+
feed.entry(comment, :url => "#{url_for_content comment.node.content}#comment-#{comment.id}") do |entry|
8+
in_response_to = content_tag(:p,
9+
content_tag(:em,
10+
"En réponse #{translate_to_content_type comment.content_type} #{link_to comment.node.content.title, path_for_content(comment.node.content)}.".html_safe
11+
)
12+
)
13+
if comment.deleted?
14+
entry.title("Commentaire supprimé")
15+
entry.content("#{content_tag(:p, "Commentaire supprimé")} #{in_response_to}", :type => 'html')
16+
else
17+
entry.title(comment.title)
18+
entry.content("#{comment.body} #{in_response_to}", :type => 'html')
19+
end
20+
entry.author do |author|
21+
author.name(comment.user_name)
22+
end
23+
end
24+
end
25+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
= h1 "Les derniers commentaires"
2+
- feed "Flux Atom des derniers commentaires"
3+
%nav.toolbox
4+
.follow_feed
5+
= link_to "Flux Atom des derniers commentaires", "latest.atom"
6+
-# order is not managed, but order_navbar is required due to float property on follow_feed div
7+
.order_navbar
8+
&nbsp;
9+
%main#contents(role="main")
10+
%ul.comments_list
11+
- @comments.each do |comment|
12+
%li.comment(id="comment-#{comment.id}")
13+
= render comment
14+
%nav.toolbox
15+
= paginate(@comments, inner_window:10)

app/views/moderation/news/index.html.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
%h2
2929
= link_to "Étiquettes publiques", moderation_tags_path
3030

31+
%h2
32+
= link_to "Derniers commentaires", comments_latest_path
33+
3134
%h2
3235
= link_to "Sondages", moderation_polls_path
3336
- if @polls.empty?

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
# Nodes
5959
get "/tableau-de-bord" => "dashboard#index", as: :dashboard
6060
get "/tableau-de-bord/reponses" => "dashboard#answers"
61+
get "/comments/latest" => "comments#latest"
6162
get "/comments/:id(,:d)(.html)" => "comments#templeet"
6263
resources :nodes, only: [] do
6364
resources :comments do
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddIndexOnCreatedAtToComments < ActiveRecord::Migration[5.2]
2+
def change
3+
add_index :comments, [:created_at], order: {created_at: :desc}
4+
end
5+
end

db/schema.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2021_01_31_231806) do
13+
ActiveRecord::Schema.define(version: 2021_09_13_191843) do
1414

1515
create_table "accounts", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci", force: :cascade do |t|
1616
t.integer "user_id"
@@ -93,6 +93,7 @@
9393
t.text "wiki_body", limit: 16777215
9494
t.datetime "created_at"
9595
t.datetime "updated_at"
96+
t.index ["created_at"], name: "index_comments_on_created_at"
9697
t.index ["node_id"], name: "index_comments_on_node_id"
9798
t.index ["state", "created_at"], name: "index_comments_on_state_and_created_at"
9899
t.index ["state", "materialized_path"], name: "index_comments_on_state_and_materialized_path", length: { materialized_path: 120 }

0 commit comments

Comments
 (0)