Skip to content

Commit d3c3198

Browse files
committed
Allow sending private mod messages
1 parent 9c07378 commit d3c3198

7 files changed

Lines changed: 88 additions & 7 deletions

File tree

app/controllers/users_controller.rb

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ class UsersController < ApplicationController
1111
before_action :verify_moderator, only: [:mod, :destroy, :soft_delete, :role_toggle, :full_log,
1212
:annotate, :annotations, :mod_privileges,
1313
:mod_privilege_action, :mod_delete, :mod_reset_profile,
14-
:mod_clear_profile, :mod_escalation, :mod_escalate]
14+
:mod_clear_profile, :mod_escalation, :mod_escalate,
15+
:mod_contact, :mod_message]
1516
before_action :verify_global_moderator, only: [:mod_destroy]
1617
before_action :set_user, only: [:show, :mod, :destroy, :soft_delete, :posts, :role_toggle,
1718
:full_log, :activity,
1819
:annotate, :annotations, :mod_privileges, :mod_privilege_action,
1920
:vote_summary, :avatar, :mod_delete, :mod_destroy,
2021
:mod_reset_profile, :mod_clear_profile, :mod_escalation,
21-
:mod_escalate]
22+
:mod_escalate, :mod_contact, :mod_message]
2223
before_action :check_deleted, only: [:show, :posts, :activity]
2324

2425
def index
@@ -157,6 +158,38 @@ def mod_escalate
157158
redirect_to mod_user_path(@user)
158159
end
159160

161+
def mod_contact
162+
render layout: 'without_sidebar'
163+
end
164+
165+
def mod_message
166+
title = params[:title]
167+
unless title.present?
168+
title = "Private Moderator Message"
169+
end
170+
171+
body = params[:body]
172+
173+
@comment_thread = CommentThread.new(title: title, post: nil, is_private: true)
174+
@comment = Comment.new(post: nil, content: body, user: current_user, comment_thread: @comment_thread)
175+
176+
success = ActiveRecord::Base.transaction do
177+
@comment_thread.save!
178+
@comment.save!
179+
ThreadFollower.create! comment_thread: @comment_thread, user: @user
180+
end
181+
182+
if success
183+
@user.create_notification("You have received a moderator message: #{@comment_thread.title}",
184+
helpers.comment_link(@comment))
185+
redirect_to comment_thread_path(@comment_thread.id)
186+
else
187+
flash[:danger] = "Could not create comment thread: #{(@comment_thread.errors.full_messages \
188+
+ @comment.errors.full_messages).join(', ')}"
189+
render :mod_contact, layout: 'without_sidebar'
190+
end
191+
end
192+
160193
def full_log
161194
@posts = Post.where(user: @user).count
162195
@comments = Comment.where(user: @user).count

app/models/comment_thread.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def can_access?(user)
3939
# automatically followed on new answer comment threads. Comment author follower creation is done
4040
# on the Comment model.
4141
def create_follower
42-
if post.user.preference('auto_follow_comment_threads') == 'true'
42+
if post.present? && post.user.preference('auto_follow_comment_threads') == 'true'
4343
ThreadFollower.create comment_thread: self, user: post.user
4444
end
4545
end

app/views/comments/_comment.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
</div>
3232
<% end %>
3333
<div class="comment--info">
34-
<% if @comment_thread.is_private %>
34+
<% if comment.comment_thread.is_private %>
3535
<% if comment.user == current_user || current_user.is_moderator || !comment.user.is_moderator %>
3636
<%= user_link comment.user %>
3737
<% if comment.user.is_moderator %>(visible only to you)<% end %>
@@ -48,10 +48,10 @@
4848
<%= link_to comment_link(comment), class: 'js-comment-permalink' do %>
4949
<span class="js-text">copy link</span>
5050
<% end %>
51-
<% if with_post_link %>
51+
<% if with_post_link && comment.post.present? %>
5252
<%= link_to 'post', generic_share_link(comment.post) %>
5353
<% end %>
54-
<% if user_signed_in? && !@comment_thread.is_private && (comment.user == current_user || current_user.is_moderator) && params[:inline] != 'true' %>
54+
<% if user_signed_in? && !comment.comment_thread.is_private && (comment.user == current_user || current_user.is_moderator) && params[:inline] != 'true' %>
5555
<a href="#" class="js-comment-edit">edit</a>
5656
<% if comment.deleted %>
5757
<a href="#" class="is-red js-comment-undelete">undelete</a>

app/views/mod_warning/new.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<h1>Warn or Suspend User</h1>
1414

1515
<div class="notice is-warning">
16-
<p>Use the warning tool only against users who have violated the site rules. Prefer other measurements, such as friendly asking the user to stop certain behaviors in a comment.</p>
16+
<p>Use the warning tool only against users who have violated the site rules. Prefer other measurements, such as friendly asking the user to stop certain behaviors in a comment or <%= link_to 'contacting them privately', mod_contact_path(@user) %>.</p>
1717
</div>
1818

1919
<%= form_for @warning, url: create_mod_warning_path(@user.id), method: :post do |f| %>

app/views/shared/_user_mod_sidebar.html.erb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
<% end %>
4141

4242
<div class="menu--heading">Warnings and Suspensions</div>
43+
<%= link_to mod_contact_path(user),
44+
class: "menu--item #{current_page?(mod_contact_path(user)) ? 'is-active' : ''}" do %>
45+
<i class="fas fa-envelope fa-fw"></i>
46+
Contact Privately
47+
<% end %>
4348
<%= link_to mod_warning_log_path(user),
4449
class: "menu--item #{current_page?(mod_warning_log_path(user)) ? 'is-active' : ''}" do %>
4550
<i class="fas fa-user-lock fa-fw"></i>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<% content_for :head do %>
2+
<%= render 'posts/markdown_script' %>
3+
<% end %>
4+
5+
<% content_for :title, "Moderator Tools: #{rtl_safe_username(@user)}" %>
6+
7+
<%= render 'users/tabs', user: @user %>
8+
9+
<div class="grid">
10+
<%= render 'shared/user_mod_sidebar', user: @user %>
11+
12+
<div class="grid--cell is-9">
13+
<h1>Contact Privately</h1>
14+
15+
<p>As a moderator, you may contact users privately. If, however, means of public and private contact have not been effective, a <%= link_to 'formal warning', new_mod_warning_path(@user) %> might be in order.</p>
16+
17+
<p>These messages will only be visible to the contacted user and to the moderation team. Your user name will be hidden for the contacted user; we generally recommend not identifying yourself.</p>
18+
19+
<%= form_tag mod_message_path do %>
20+
<%= label_tag :title, 'Subject', class: 'form-element' %>
21+
<%= text_field_tag :title, '', class: 'form-element', data: { character_count: ".js-character-count-thread-title" } %>
22+
23+
<span class="has-float-right has-font-size-caption js-character-count-thread-title"
24+
data-max="255" data-min="0" data-display-at="0.75">
25+
<i class="fas fa-ellipsis-h js-character-count__icon"></i>
26+
<span class="js-character-count__count">0 / 255</span>
27+
</span>
28+
29+
<%= label_tag :body, 'Message', class: 'form-element' %>
30+
<%= text_area_tag :body, '', class: 'form-element is-large js-comment-field', required: true,
31+
data: { thread: '-1', character_count: ".js-character-count-modmsg-body" } %>
32+
<span class="has-float-right has-font-size-caption js-character-count-modmsg-body"
33+
data-max="1000" data-min="15">
34+
<i class="fas fa-ellipsis-h js-character-count__icon"></i>
35+
<span class="js-character-count__count">0 / 1000</span>
36+
</span>
37+
38+
<%= submit_tag 'Create thread', class: 'button is-filled', id: "create_modmsg_button", disabled: true %>
39+
<% end %>
40+
</div>
41+
</div>

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@
197197
get '/:id/avatar/:size', to: 'users#avatar', as: :user_auto_avatar
198198
get '/:id/mod/escalate', to: 'users#mod_escalation', as: :user_escalation
199199
post '/:id/mod/escalate', to: 'users#mod_escalate', as: :user_escalate
200+
get '/:id/mod/contact', to: 'users#mod_contact', as: :mod_contact
201+
post '/:id/mod/contact', to: 'users#mod_message', as: :mod_message
200202
end
201203

202204
post 'notifications/:id/read', to: 'notifications#read', as: :read_notifications

0 commit comments

Comments
 (0)