Skip to content

Commit bc6bacb

Browse files
committed
finished splitting up thread_restrict action
1 parent b22c736 commit bc6bacb

8 files changed

Lines changed: 125 additions & 61 deletions

File tree

app/assets/javascripts/comments.js

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -197,28 +197,64 @@ $(() => {
197197
$modal.find('.js-follower-display').html(data);
198198
});
199199

200-
$(document).on('submit', '[class*=js--lock-thread] form', async (evt) => {
201-
evt.preventDefault();
200+
$(document).on('click', '.js-archive-thread', async (ev) => {
201+
ev.preventDefault();
202202

203-
const $tgt = $(evt.target);
203+
const $tgt = $(ev.target);
204204
const threadID = $tgt.data("thread");
205205

206-
const data = await QPixel.lockThread(threadID);
206+
const data = await QPixel.archiveThread(threadID);
207207

208208
QPixel.handleJSONResponse(data, () => {
209-
window.location.reload();
209+
const wrapper = getCommentThreadWrapper($tgt);
210+
openThread(wrapper, threadID);
211+
});
212+
});
213+
214+
$(document).on('click', '.js-delete-thread', async (ev) => {
215+
ev.preventDefault();
216+
217+
const $tgt = $(ev.target);
218+
const threadID = $tgt.data("thread");
219+
220+
const data = await QPixel.deleteThread(threadID);
221+
222+
QPixel.handleJSONResponse(data, () => {
223+
const wrapper = getCommentThreadWrapper($tgt);
224+
openThread(wrapper, threadID);
210225
});
211226
});
212227

213-
$(document).on('click', '.js--restrict-thread, .js--unrestrict-thread', async (evt) => {
228+
$(document).on('click', '.js-lock-thread', async (ev) => {
229+
ev.preventDefault();
230+
231+
const $tgt = $(ev.target);
232+
const threadID = $tgt.data("thread");
233+
const form = $tgt.closest(`form[data-thread=${threadID}]`).get(0);
234+
235+
if (form instanceof HTMLFormElement) {
236+
const { value: duration } = form.elements['duration'] ?? {};
237+
238+
const data = await QPixel.lockThread(threadID, duration ? Math.round(+duration) : void 0);
239+
240+
QPixel.handleJSONResponse(data, () => {
241+
const wrapper = getCommentThreadWrapper($tgt);
242+
openThread(wrapper, threadID);
243+
});
244+
} else {
245+
QPixel.createNotification('danger', 'Failed to find thread to lock');
246+
}
247+
});
248+
249+
// TODO: split into individual handlers once unrestrict_thread is split
250+
$(document).on('click', '.js--unrestrict-thread', async (evt) => {
214251
evt.preventDefault();
215252

216253
const $tgt = $(evt.target);
217254
const threadID = $tgt.data("thread");
218255
const action = $tgt.data("action");
219-
const route = $tgt.hasClass("js--restrict-thread") ? 'restrict' : 'unrestrict';
220256

221-
const resp = await QPixel.fetchJSON(`/comments/thread/${threadID}/${route}`, { type: action });
257+
const resp = await QPixel.fetchJSON(`/comments/thread/${threadID}/unrestrict`, { type: action });
222258

223259
const data = await resp.json();
224260

app/assets/javascripts/qpixel_api.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,30 @@ window.QPixel = {
451451
return QPixel.parseJSONResponse(resp, 'Failed to vote');
452452
},
453453

454+
archiveThread: async (id) => {
455+
const resp = await QPixel.fetchJSON(`/comments/thread/${id}/archive`, {}, {
456+
headers: { 'Accept': 'application/json' },
457+
});
458+
459+
return QPixel.parseJSONResponse(resp, 'Failed to archive thread');
460+
},
461+
462+
deleteThread: async (id) => {
463+
const resp = await QPixel.fetchJSON(`/comments/thread/${id}/delete`, {}, {
464+
headers: { 'Accept': 'application/json' },
465+
});
466+
467+
return QPixel.parseJSONResponse(resp, 'Failed to delete thread');
468+
},
469+
470+
lockThread: async (id, duration) => {
471+
const resp = await QPixel.fetchJSON(`/comments/thread/${id}/lock`, {
472+
duration,
473+
});
474+
475+
return QPixel.parseJSONResponse(resp, 'Failed to lock thread');
476+
},
477+
454478
deleteComment: async (id) => {
455479
const resp = await QPixel.fetchJSON(`/comments/${id}/delete`, {}, {
456480
headers: { 'Accept': 'application/json' },
@@ -495,14 +519,6 @@ window.QPixel = {
495519
return QPixel.parseJSONResponse(resp, 'Failed to unfollow post comments');
496520
},
497521

498-
lockThread: async (id) => {
499-
const resp = await QPixel.fetchJSON(`/comments/thread/${id}/restrict`, {
500-
type: 'lock'
501-
});
502-
503-
return QPixel.parseJSONResponse(resp, 'Failed to lock thread');
504-
},
505-
506522
renameTag: async (categoryId, tagId, name) => {
507523
const resp = await QPixel.fetchJSON(`/categories/${categoryId}/tags/${tagId}/rename`, { name }, {
508524
headers: { 'Accept': 'application/json' }

app/controllers/comments_controller.rb

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,23 @@ class CommentsController < ApplicationController
55

66
before_action :set_comment, only: [:update, :destroy, :undelete, :show]
77
before_action :set_post, only: [:create_thread, :post_follow, :post_unfollow]
8-
before_action :set_thread,
9-
only: [:create, :thread, :thread_content, :thread_rename, :thread_restrict, :thread_unrestrict,
10-
:thread_followers]
11-
8+
before_action :set_thread, only: [:create,
9+
:thread,
10+
:thread_content,
11+
:thread_rename,
12+
:archive_thread,
13+
:delete_thread,
14+
:follow_thread,
15+
:lock_thread,
16+
:thread_unrestrict,
17+
:thread_followers]
1218
before_action :check_post_access, only: [:create_thread, :create]
1319
before_action :check_privilege, only: [:update, :destroy, :undelete]
1420
before_action :check_create_access, only: [:create_thread, :create]
1521
before_action :check_reply_access, only: [:create]
16-
before_action :check_restrict_access, only: [:thread_restrict]
22+
before_action :check_archive_thread_access, only: [:archive_thread]
23+
before_action :check_delete_thread_access, only: [:delete_thread]
24+
before_action :check_lock_thread_access, only: [:lock_thread]
1725
before_action :check_thread_access, only: [:thread, :thread_content, :thread_followers]
1826
before_action :check_unrestrict_access, only: [:thread_unrestrict]
1927
before_action :check_if_target_post_locked, only: [:create, :post_follow]
@@ -268,22 +276,6 @@ def unlock_thread
268276
restrict_thread_response(@comment_thread, status)
269277
end
270278

271-
def thread_restrict
272-
# TODO: remove this wrapper action entirely (callbacks need to be moved, routes assigned, etc)
273-
case params[:type]
274-
when 'lock'
275-
lock_thread
276-
when 'archive'
277-
archive_thread
278-
when 'delete'
279-
delete_thread
280-
when 'follow'
281-
follow_thread
282-
else
283-
not_found!
284-
end
285-
end
286-
287279
def thread_unrestrict
288280
# TODO: remove this wrapper action entirely (callbacks need to be moved, routes assigned, etc)
289281
case params[:type]
@@ -402,18 +394,20 @@ def check_reply_access
402394
end
403395
end
404396

405-
def check_restrict_access
406-
case params[:type]
407-
when 'lock'
408-
not_found! unless current_user.can_lock?(@comment_thread)
409-
when 'archive'
410-
not_found! unless current_user.can_archive?(@comment_thread)
411-
when 'delete'
412-
not_found! unless current_user.can_delete?(@comment_thread)
413-
end
397+
def check_archive_thread_access
398+
not_found! unless current_user.can_archive?(@comment_thread)
399+
end
400+
401+
def check_delete_thread_access
402+
not_found! unless current_user.can_delete?(@comment_thread)
403+
end
404+
405+
def check_lock_thread_access
406+
not_found! unless current_user.can_lock?(@comment_thread)
414407
end
415408

416409
def check_unrestrict_access
410+
# TODO: split into individual checks once unrestrict_thread is split
417411
case params[:type]
418412
when 'lock'
419413
not_found! unless current_user.can_unlock?(@comment_thread)

app/views/comments/_lock_thread_modal.html.erb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"%>
77

88
<div class="modal is-with-backdrop is-small js--lock-thread-<%= thread.id %>">
9-
<%= form_tag restrict_comment_thread_path(thread.id),
9+
<%= form_tag lock_comment_thread_path(thread.id),
1010
class: 'modal--container',
1111
data: { thread: thread.id } do %>
1212
<%= hidden_field_tag :type, 'lock' %>
@@ -25,7 +25,7 @@
2525
<%= number_field_tag :duration, nil, class: 'form-element' %>
2626
</div>
2727
<div class="modal--footer">
28-
<%= submit_tag 'Lock thread', class: 'button is-muted is-filled',
28+
<%= submit_tag 'Lock thread', class: 'button is-muted is-filled js-lock-thread',
2929
data: { thread: thread.id } %>
3030
<button type="button" class="button is-muted" data-modal=".js--lock-thread-<%= thread.id %>">Cancel</button>
3131
</div>

app/views/comments/_thread_actions_modal.html.erb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</a>
1818
<% end %>
1919

20-
<% unless thread.archived || thread.deleted %>
20+
<% unless thread.archived || thread.deleted? %>
2121
<% if thread.locked? %>
2222
<a href="javascript:void(0)" class="js--unrestrict-thread"
2323
data-action="lock"
@@ -35,7 +35,7 @@
3535
<% end %>
3636
<% end %>
3737

38-
<% unless thread.locked? || thread.deleted %>
38+
<% unless thread.locked? || thread.deleted? %>
3939
<% if thread.archived %>
4040
<a href="javascript:void(0)" class="js--unrestrict-thread"
4141
data-action="archive"
@@ -45,8 +45,7 @@
4545
<i class="fas fa-archive fa-fw"></i> restore
4646
</a>
4747
<% else %>
48-
<a href="javascript:void(0)" class="js--restrict-thread"
49-
data-action="archive"
48+
<a href="javascript:void(0)" class="js-archive-thread"
5049
data-thread="<%= thread.id %>"
5150
role="button"
5251
aria-label="Archive thread">
@@ -55,7 +54,7 @@
5554
<% end %>
5655
<% end %>
5756

58-
<% unless thread.locked? || thread.archived %>
57+
<% unless thread.locked? || thread.archived? %>
5958
<% if thread.deleted %>
6059
<a href="javascript:void(0)" class="js--unrestrict-thread"
6160
data-action="delete"
@@ -65,8 +64,7 @@
6564
<i class="fas fa-trash fa-fw"></i> undelete
6665
</a>
6766
<% else %>
68-
<a href="javascript:void(0)" class="js--restrict-thread"
69-
data-action="delete"
67+
<a href="javascript:void(0)" class="js-delete-thread"
7068
data-thread="<%= thread.id %>"
7169
role="button"
7270
aria-label="Delete thread">

config/routes.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,12 @@
239239
get 'thread/:id/pingable', to: 'comments#pingable', as: :thread_pingable
240240
post 'thread/:id/new', to: 'comments#create', as: :create_comment
241241
post 'thread/:id/rename', to: 'comments#thread_rename', as: :rename_comment_thread
242-
post 'thread/:id/restrict', to: 'comments#thread_restrict', as: :restrict_comment_thread
242+
243+
post 'thread/:id/archive', to: 'comments#archive_thread', as: :archive_comment_thread
244+
post 'thread/:id/delete', to: 'comments#delete_thread', as: :delete_comment_thread
245+
post 'thread/:id/follow', to: 'comments#follow_thread', as: :follow_comment_thread
246+
post 'thread/:id/lock', to: 'comments#lock_thread', as: :lock_comment_thread
247+
243248
post 'thread/:id/unrestrict', to: 'comments#thread_unrestrict', as: :unrestrict_comment_thread
244249
get 'thread/:id/followers', to: 'comments#thread_followers', as: :comment_thread_followers
245250
get 'post/:post_id', to: 'comments#post', as: :post_comments

global.d.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,27 @@ interface QPixel {
540540
onSuccess: (data: Extract<T, QPixelSuccessResponseJSON>) => void,
541541
onFinally?: (data: T) => void) => boolean
542542

543+
/**
544+
* Attempts to archive a comment thread
545+
* @param id id of the thread to archive
546+
* @returns result of the operation
547+
*/
548+
archiveThread?: (id: string) => Promise<QPixelResponseJSON>
549+
543550
/**
544551
* Attempts to delete a comment
545552
* @param id id of the comment to delete
546553
* @returns result of the operation
547554
*/
548555
deleteComment?: (id: string) => Promise<QPixelResponseJSON>
549556

557+
/**
558+
* Attempts to delete a comment thread
559+
* @param id id of the thread to delete
560+
* @returns result of the operation
561+
*/
562+
deleteThread?: (id: string) => Promise<QPixelResponseJSON>
563+
550564
/**
551565
* Attempts to start following comments on a given post
552566
* @param postId id of the post to follow comments on
@@ -584,9 +598,10 @@ interface QPixel {
584598
/**
585599
* Attempts to lock a comment thread
586600
* @param id id of the comment thread to lock
601+
* @param duration how long should the thread be locked for, in days
587602
* @returns result of the operation
588603
*/
589-
lockThread?: (id: string) => Promise<QPixelResponseJSON>
604+
lockThread?: (id: string, duration?: number) => Promise<QPixelResponseJSON>
590605

591606
/**
592607
* Attempts to rename a tag

test/comments_test_helpers.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module CommentsControllerTestHelpers
66
# Attempts to archive a given comment thread
77
# @param thread [CommentThread] thread to archive
88
def try_archive_thread(thread)
9-
post :thread_restrict, params: { id: thread.id, type: 'archive' }
9+
post :archive_thread, params: { id: thread.id }
1010
end
1111

1212
# Attempts to create a comment thread on a given post
@@ -50,7 +50,7 @@ def try_create_comment(thread,
5050
# Attempts to delete a given comment thread
5151
# @param thread [CommentThread] thread to delete
5252
def try_delete_thread(thread)
53-
post :thread_restrict, params: { id: thread.id, type: 'delete' }
53+
post :delete_thread, params: { id: thread.id }
5454
end
5555

5656
# Attempts to undelete a given comment thread
@@ -62,7 +62,7 @@ def try_undelete_thread(thread)
6262
# Attempts to follow a given comment thread
6363
# @param thread [CommentThread] thread to follow
6464
def try_follow_thread(thread)
65-
post :thread_restrict, params: { id: thread.id, type: 'follow' }
65+
post :follow_thread, params: { id: thread.id }
6666
end
6767

6868
# Attempts to unfollow a given comment thread
@@ -87,7 +87,7 @@ def try_post_unfollow(test_post)
8787
# @param thread [CommentThread] thread to lock
8888
# @param duration [Integer] lock duration, in days
8989
def try_lock_thread(thread, duration: nil)
90-
post :thread_restrict, params: { duration: duration, id: thread.id, type: 'lock' }
90+
post :lock_thread, params: { duration: duration, id: thread.id }
9191
end
9292

9393
# Attempts to unlock a given comment thread

0 commit comments

Comments
 (0)