Skip to content

Commit e35b237

Browse files
authored
Merge pull request #1873 from codidact/cellio/714-rename-thread
2 parents c6816f9 + 31f711b commit e35b237

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

app/controllers/comments_controller.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,32 @@ def thread_rename
220220
return
221221
end
222222

223+
orig_title = @comment_thread.title
223224
title = helpers.strip_markdown(params[:title], strip_leading_quote: true)
224225
status = @comment_thread.update(title: title)
225226

227+
if status
228+
# Comment is owned by System so regular users can't delete it. Without
229+
# this record, the title would be attributed to the thread creator,
230+
# which can be abused.
231+
log_msg =
232+
Comment.new(post: @post,
233+
content:
234+
"Thread renamed from \\\"#{orig_title}\\\" to \\\"#{title}\\\" by @##{current_user.id}",
235+
user: User.find(-1),
236+
comment_thread: @comment_thread,
237+
has_reference: false)
238+
comment_status = log_msg.save
239+
end
240+
226241
unless status
227242
flash[:danger] = I18n.t('comments.errors.rename_thread_generic')
228243
end
229244

245+
unless comment_status
246+
flash[:danger] = I18n.t('comments.errors.comment_not_posted')
247+
end
248+
230249
redirect_to comment_thread_path(@comment_thread.id)
231250
end
232251

app/models/user.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ def post_privilege?(name, post)
8888
post.user == self || privilege?(name)
8989
end
9090

91+
# Can the user rename a given comment thread?
92+
# @param thread [CommentThread] thread to rename
93+
# @return [Boolean] check result
94+
def can_rename?(thread)
95+
privilege?('flag_curate') ||
96+
Comment.where(user: self, comment_thread_id: thread.id).any?
97+
end
98+
9199
# Can the user archive a given comment thread?
92100
# @param thread [CommentThread] thread to archive
93101
# @return [Boolean] check result

app/views/comment_threads/_expanded.html.erb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<%#
2-
Full thread view with all the details & actions
2+
"Full thread view with all the details & actions
33
44
Variables:
55
? comment_id : Comment ID to show even if it would be hidden otherwise
66
inline : whether the thread is diplayed inline with the parent post
77
show_deleted : whether to display deleted comments for those who can see them
88
thread : CommentThread to display
9-
%>
9+
"%>
1010

11+
<% can_only_rename = current_user&.can_rename?(thread) && !current_user&.privilege?('flag_curate') %>
1112
<% max_shown_comments = 5 %>
1213
<% comment_id ||= defined?(comment_id) ? comment_id : nil %>
1314
<% pingable = thread.pingable %>
@@ -38,6 +39,15 @@
3839
<a href="<%= comment_thread_path(thread.id) %>" class="widget--header-link">show thread</a>
3940
<a href="javascript:void(0)" class="js-collapse-thread widget--header-link">collapse</a>
4041
<% end %>
42+
<% if can_only_rename %>
43+
<a href="javascript:void(0)"
44+
class="widget--header-link"
45+
data-modal=".js--rename-thread-<%= thread.id %>"
46+
role="button"
47+
aria-label="Rename thread">
48+
<i class="fas fa-pen fa-fw"></i> rename
49+
</a>
50+
<% end %>
4151
<% if current_user&.privilege?('flag_curate') %>
4252
<%= render 'comments/thread_tools_link', thread: thread %>
4353
<% end %>
@@ -107,6 +117,10 @@
107117
<% end %>
108118
<% end %>
109119

120+
<% if can_only_rename %>
121+
<%= render 'comments/rename_thread_modal', thread: thread %>
122+
<% end %>
123+
110124
<% if current_user&.privilege?('flag_curate') %>
111125
<%= render 'comments/thread_actions_modal', thread: thread, user: current_user %>
112126
<% end %>

config/locales/strings/en.comments.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ en:
3333
Failed to rename the thread.
3434
title_presence: >
3535
can't be empty after Markdown is removed.
36+
comment_not_posted: >
37+
Failed to create comment.
3638
labels:
3739
create_new_thread: >
3840
Start new comment thread

0 commit comments

Comments
 (0)