Skip to content

Commit 9c07378

Browse files
committed
Implement safety features for private threads
1 parent 22b823b commit 9c07378

3 files changed

Lines changed: 65 additions & 14 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,13 @@ def create_thread
5656
def create
5757
@comment_thread = CommentThread.find(params[:id])
5858
@post = @comment_thread.post
59-
if @post.comments_disabled && !current_user.is_moderator && !current_user.is_admin
60-
render json: { status: 'failed', message: 'Comments have been disabled on this post.' }, status: :forbidden
61-
return
62-
elsif !@post.can_access?(current_user)
63-
return not_found
59+
unless @post.nil?
60+
if @post.comments_disabled && !current_user.is_moderator && !current_user.is_admin
61+
render json: { status: 'failed', message: 'Comments have been disabled on this post.' }, status: :forbidden
62+
return
63+
elsif !@post.can_access?(current_user)
64+
return not_found
65+
end
6466
end
6567

6668
body = params[:content]
@@ -82,10 +84,12 @@ def create
8284
.where('link LIKE ?', "#{thread_url}%")
8385
next if existing_notification.exists?
8486

85-
title = @post.parent.nil? ? @post.title : @post.parent.title
86-
follower.user.create_notification("There are new comments in a followed thread '#{@comment_thread.title}' " \
87-
"on the post '#{title}'",
88-
helpers.comment_link(@comment))
87+
unless @post.nil?
88+
title = @post.parent.nil? ? @post.title : @post.parent.title
89+
follower.user.create_notification("There are new comments in a followed thread '#{@comment_thread.title}' " \
90+
"on the post '#{title}'",
91+
helpers.comment_link(@comment))
92+
end
8993
end
9094
else
9195
flash[:danger] = @comment.errors.full_messages.join(', ')
@@ -94,6 +98,12 @@ def create
9498
end
9599

96100
def update
101+
if @comment_thread.is_private
102+
flash[:danger] = 'This action is not permitted.'
103+
redirect_to comment_thread_path(@comment_thread.id)
104+
return
105+
end
106+
97107
before = @comment.content
98108
if @comment.update comment_params
99109
unless current_user.id == @comment.user_id
@@ -110,6 +120,12 @@ def update
110120
end
111121

112122
def destroy
123+
if @comment_thread.is_private
124+
flash[:danger] = 'This action is not permitted.'
125+
redirect_to comment_thread_path(@comment_thread.id)
126+
return
127+
end
128+
113129
if @comment.update(deleted: true)
114130
@comment_thread = @comment.comment_thread
115131
unless current_user.id == @comment.user_id
@@ -123,6 +139,12 @@ def destroy
123139
end
124140

125141
def undelete
142+
if @comment_thread.is_private
143+
flash[:danger] = 'This action is not permitted.'
144+
redirect_to comment_thread_path(@comment_thread.id)
145+
return
146+
end
147+
126148
if @comment.update(deleted: false)
127149
@comment_thread = @comment.comment_thread
128150
unless current_user.id == @comment.user_id
@@ -169,11 +191,23 @@ def thread_rename
169191
return
170192
end
171193

194+
if @comment_thread.is_private && !current_user.is_moderator
195+
flash[:danger] = 'This action is not permitted.'
196+
redirect_to comment_thread_path(@comment_thread.id)
197+
return
198+
end
199+
172200
@comment_thread.update title: params[:title]
173201
redirect_to comment_thread_path(@comment_thread.id)
174202
end
175203

176204
def thread_restrict
205+
if @comment_thread.is_private && !current_user.is_moderator
206+
flash[:danger] = 'This action is not permitted.'
207+
redirect_to comment_thread_path(@comment_thread.id)
208+
return
209+
end
210+
177211
case params[:type]
178212
when 'lock'
179213
return not_found unless current_user.privilege?('flag_curate') && !@comment_thread.locked?
@@ -204,6 +238,12 @@ def thread_restrict
204238
end
205239

206240
def thread_unrestrict
241+
if @comment_thread.is_private && !current_user.is_moderator
242+
flash[:danger] = 'This action is not permitted.'
243+
redirect_to comment_thread_path(@comment_thread.id)
244+
return
245+
end
246+
207247
case params[:type]
208248
when 'lock'
209249
return not_found unless current_user.privilege?('flag_curate') && @comment_thread.locked?
@@ -278,7 +318,7 @@ def check_if_parent_post_locked
278318
end
279319

280320
def check_if_target_post_locked
281-
check_if_locked(Post.find(params[:post_id]))
321+
params[:post_id].present? && check_if_locked(Post.find(params[:post_id]))
282322
end
283323

284324
def check_for_pings(thread, content)
@@ -302,12 +342,14 @@ def apply_pings(pings)
302342
end
303343

304344
def comment_rate_limited
345+
return false if @comment_thread&.is_private
346+
305347
recent_comments = Comment.where(created_at: 24.hours.ago..DateTime.now, user: current_user).where \
306348
.not(post: Post.includes(:parent).where(parents_posts: { user_id: current_user.id })) \
307349
.where.not(post: Post.where(user_id: current_user.id)).count
308350
max_comments_per_day = SiteSetting[current_user.privilege?('unrestricted') ? 'RL_Comments' : 'RL_NewUserComments']
309351

310-
if (!@post.user_id == current_user.id || @post&.parent&.user_id == current_user.id) \
352+
if (!@post&.user_id == current_user.id || @post&.parent&.user_id == current_user.id) \
311353
&& recent_comments >= max_comments_per_day
312354
comment_limit_msg = "You have used your daily comment limit of #{recent_comments} comments." \
313355
' Come back tomorrow to continue commenting. Comments on own posts and on answers' \

app/views/comments/_comment.html.erb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,16 @@
3131
</div>
3232
<% end %>
3333
<div class="comment--info">
34-
<%= user_link comment.user %>
34+
<% if @comment_thread.is_private %>
35+
<% if comment.user == current_user || current_user.is_moderator || !comment.user.is_moderator %>
36+
<%= user_link comment.user %>
37+
<% if comment.user.is_moderator %>(visible only to you)<% end %>
38+
<% else %>
39+
<strong>moderator team</strong>
40+
<% end %>
41+
<% else %>
42+
<%= user_link comment.user %>
43+
<% end %>
3544
wrote
3645
<span title="<%= comment.created_at.iso8601 %>"><%= time_ago_in_words(comment.created_at) %> ago:</span>
3746
</div>
@@ -42,7 +51,7 @@
4251
<% if with_post_link %>
4352
<%= link_to 'post', generic_share_link(comment.post) %>
4453
<% end %>
45-
<% if user_signed_in? && (comment.user == current_user || current_user.is_moderator) && params[:inline] != 'true' %>
54+
<% if user_signed_in? && !@comment_thread.is_private && (comment.user == current_user || current_user.is_moderator) && params[:inline] != 'true' %>
4655
<a href="#" class="js-comment-edit">edit</a>
4756
<% if comment.deleted %>
4857
<a href="#" class="is-red js-comment-undelete">undelete</a>

app/views/comments/thread.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<% if current_user&.privilege? 'flag_curate' %>
3939
<a href="#" class="widget--header-link" data-drop=".js--tools-thread-<%= @comment_thread.id %>"><i class="fa fa-cog fa-fw"></i>tools</a>
4040
<% end %>
41-
<% unless current_user.nil? %>
41+
<% unless current_user.nil? || @comment_thread.is_private %>
4242
<% if @comment_thread.followed_by? current_user %>
4343
<a href="#" class="widget--header-link js--unrestrict-thread" data-action="follow"
4444
data-thread="<%= @comment_thread.id %>" title="You are following this thread and will be notified of every response. You can unfollow at any time.">

0 commit comments

Comments
 (0)