-
Notifications
You must be signed in to change notification settings - Fork 34
/
TooManyCommentsFlagQueueHelper.user.js
418 lines (353 loc) · 14.5 KB
/
TooManyCommentsFlagQueueHelper.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// ==UserScript==
// @name Too Many Comments Flag Queue Helper
// @description Inserts quicklinks to "Move comments to chat + delete" and "Delete all comments"
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @version 6.0.13
//
// @match https://*.stackoverflow.com/admin/dashboard?flagtype=posttoomanycommentsauto*
// @match https://*.serverfault.com/admin/dashboard?flagtype=posttoomanycommentsauto*
// @match https://*.superuser.com/admin/dashboard?flagtype=posttoomanycommentsauto*
// @match https://*.askubuntu.com/admin/dashboard?flagtype=posttoomanycommentsauto*
// @match https://*.mathoverflow.net/admin/dashboard?flagtype=posttoomanycommentsauto*
// @match https://*.stackapps.com/admin/dashboard?flagtype=posttoomanycommentsauto*
// @match https://*.stackexchange.com/admin/dashboard?flagtype=posttoomanycommentsauto*
// @match https://stackoverflowteams.com/c/*/admin/dashboard?flagtype=posttoomanycommentsauto*
//
// @exclude https://api.stackexchange.com/*
// @exclude https://data.stackexchange.com/*
// @exclude https://contests.stackoverflow.com/*
// @exclude https://winterbash*.stackexchange.com/*
// @exclude *chat.*
// @exclude *blog.*
// @exclude */tour
//
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/se-ajax-common.js
// @require https://raw.githubusercontent.com/samliew/SO-mod-userscripts/master/lib/common.js
// @require https://raw.githubusercontent.com/samliew/ajax-progress/master/jquery.ajaxProgress.js
// ==/UserScript==
/* globals StackExchange, fkey */
/// <reference types="./globals" />
'use strict';
// This is a moderator-only userscript
if (!isModerator()) return;
const superusers = [584192];
const isSuperuser = superusers.includes(selfId);
const delCommentThreshold = 40;
let ajaxTimeout;
// Locks individual post
// Type: 20 - content dispute
// 21 - offtopic comments
function lockPost(pid, type, hours = 24) {
return new Promise(function (resolve, reject) {
if (typeof pid === 'undefined' || pid === null) { reject(); return; }
if (typeof type === 'undefined' || type === null) { reject(); return; }
$.post({
url: `${location.origin}/admin/posts/${pid}/lock`,
data: {
'fkey': fkey,
'mod-actions': 'lock',
'noticetype': type,
'duration': hours
}
})
.done(resolve)
.fail(reject);
});
}
// Undelete individual comment
function undeleteComment(pid, cid) {
if (typeof pid === 'undefined' || pid == null) return;
if (typeof cid === 'undefined' || cid == null) return;
$.post({
url: `${location.origin}/admin/posts/${pid}/comments/${cid}/undelete`,
data: { 'fkey': fkey }
});
}
// Undelete comments
function undeleteComments(pid, cids) {
if (typeof pid === 'undefined' || pid == null) return;
if (typeof cids === 'undefined' || cids.length === 0) return;
cids.forEach(v => undeleteComment(pid, v));
}
// Delete individual comment
function deleteComment(cid) {
return new Promise(function (resolve, reject) {
if (typeof cid === 'undefined' || cid == null) { reject(); return; }
$.post({
url: `${location.origin}/posts/comments/${cid}/vote/10`,
data: { 'fkey': fkey }
})
.done(function (data) {
$('#comment-' + pid).remove();
resolve();
})
.fail(reject);
});
}
// Delete all comments on post
function deleteCommentsOnPost(pid) {
return new Promise(function (resolve, reject) {
if (typeof pid === 'undefined' || pid == null) { reject(); return; }
$.post({
url: `${location.origin}/admin/posts/${pid}/delete-comments`,
data: {
'fkey': fkey,
'mod-actions': 'delete-comments'
}
})
.done(function (data) {
$('#comments-' + pid).remove();
$('#comments-link-' + pid).html('<b>Comments deleted.</b>');
resolve();
})
.fail(reject);
});
}
// Move all comments on post to chat
function moveCommentsOnPostToChat(pid) {
return new Promise(function (resolve, reject) {
if (typeof pid === 'undefined' || pid == null) { reject(); return; }
$.post({
url: `${location.origin}/admin/posts/${pid}/move-comments-to-chat`,
data: {
'fkey': fkey,
'deleteMovedComments': 'true'
}
})
.done(function (data) {
$('#comments-' + pid).remove();
$('#comments-link-' + pid).html(`<b>${data.info}</b>`);
resolve();
})
.fail(reject);
});
}
// Mark all flags on post as helpful
function dismissAllHelpful(pid, comment = "") {
return new Promise(function (resolve, reject) {
if (typeof pid === 'undefined' || pid == null) { reject(); return; }
$.post({
url: `${location.origin}/messages/delete-moderator-messages/${pid}/${StackExchange.moderator.renderTimeTicks}?valid=true`,
data: {
'fkey': fkey,
'comment': comment
}
})
.done(function (data) {
$('#flagged-' + pid).remove();
resolve();
})
.fail(reject);
});
}
function addPostCommentsModLinks() {
$('div[id^="comments-link-"]').addClass('js-comments-menu');
// Append link to post sidebar if it doesn't exist yet
const allCommentMenus = $('.js-comments-menu');
// Init those that are not processed yet
allCommentMenus.not('.js-comments-menu-init').addClass('js-comments-menu-init').each(function () {
const post = $(this).closest('.answer, .question');
const pid = Number(post.attr('data-answerid') || post.attr('data-questionid')) || null;
this.dataset.postId = pid;
// If there are deleted comments, move from sidebar to bottom
const delCommentsBtn = post.find('.js-fetch-deleted-comments');
if (delCommentsBtn.length == 1) {
const numDeletedComments = (delCommentsBtn.attr('title') || delCommentsBtn.attr('aria-label')).match(/\d+/)[0];
$(this).append(`<span class="js-link-separator2"> | </span> <a class="js-show-deleted-comments-link fc-red-600" title="expand to show all comments on this post (including deleted)" href="${delCommentsBtn.attr('href')}" role="button">load <b>${numDeletedComments}</b> deleted comment${numDeletedComments > 1 ? 's' : ''}</a>`);
delCommentsBtn.hide();
}
// Add move to chat and purge links
$(this).children('.mod-action-links').remove(); // in case added by another US
$(this).append(`<div class="mod-action-links dno" style="float:right; padding-right:10px">
<a data-post-id="${pid}" class="js-move-comments-link fc-red-600" title="move all comments to chat + delete all">move to chat</a>
<a data-post-id="${pid}" class="js-purge-comments-link fc-red-600" title="delete all comments">purge all</a>
</div>`);
});
// Show move/purge links depending on comments
allCommentMenus.each(function () {
const hasComments = $(this).prev().find('.comment').length > 0;
$(this).find('.mod-action-links').toggle(hasComments);
});
}
function initPostCommentsModLinksEvents() {
const d = $('body').addClass('js-comments-menu-events')
.off('click handle', 'a.js-show-deleted-comments-link')
.off('click handle', 'a.js-move-comments-link')
.off('click handle', 'a.js-purge-comments-link');
d.on('click', 'a.js-show-deleted-comments-link', function (e) {
e.preventDefault();
const post = $(this).closest('.answer, .question');
post.find('.js-fetch-deleted-comments').trigger('click');
$(this).prev('.js-link-separator2').addBack().remove();
});
// On move comments to chat link click
d.on('click handle', 'a.js-move-comments-link', function (e) {
e.preventDefault();
const post = $(this).closest('.answer, .question');
const pid = Number(this.dataset.postId) || null;
const flaggedPost = $(this).closest('.js-flagged-post');
const possibleDupeCommentIds = $(`#comments-${pid} .comment`).not('.deleted-comment')
.filter(function (i, el) {
const cmmtText = $(el).find('.comment-copy').text().toLowerCase();
return cmmtText.indexOf('possible duplicate of ') === 0;
})
.map((i, el) => el.dataset.commentId).get();
$(this).remove();
moveCommentsOnPostToChat(pid)
.then(function (v) {
lockPost(pid, 21);
undeleteComments(pid, possibleDupeCommentIds);
flaggedPost.addClass('comments-handled');
});
});
// On purge all comments link click
d.on('click handle', 'a.js-purge-comments-link', function (e) {
e.preventDefault();
const post = $(this).closest('.answer, .question');
const pid = Number(this.dataset.postId) || null;
const flaggedPost = $(this).closest('.js-flagged-post');
const possibleDupeCommentIds = $(`#comments-${pid} .comment`).not('.deleted-comment')
.filter(function (i, el) {
const cmmtText = $(el).find('.comment-copy').text().toLowerCase();
return cmmtText.indexOf('possible duplicate of ') === 0 || cmmtText.indexOf('let us continue this discussion ') === 0;
})
.map((i, el) => el.dataset.commentId).get();
deleteCommentsOnPost(pid)
.then(function (v) {
undeleteComments(pid, possibleDupeCommentIds);
flaggedPost.addClass('comments-handled');
});
});
}
function doPageLoad() {
// Expand unhandled posts
const unhandledPosts = $('.js-flagged-post').filter((i, el) => $('.mod-post-actions', el).text().indexOf('ModMovedCommentsToChat') === -1);
const handledPosts = $('.js-flagged-post').not(unhandledPosts).addClass('comments-handled');
setTimeout((unhandledPosts) => $('.js-body-loader:last .js-expand-body', unhandledPosts).trigger('click'), 1000, unhandledPosts);
// On "done" button click
$('.js-flagged-post').on('click', '.js-resolve-now', function () {
const $post = $(this).closest('.js-flagged-post');
const pid = $post[0].dataset.postId;
// Hide post immediately so we can move on
$post.hide();
});
const actionBtns = $('<div id="actionBtns"></div>');
$('.js-flagged-post').first().parent().prepend(actionBtns);
// Start from bottom link
$('<button class="s-btn s-btn__filled s-btn__xs">Review from bottom</button>')
.on('click', function () {
window.scrollTo(0, 999999);
}).appendTo(actionBtns);
if (isSuperuser) {
// Move all comments on page to chat
$('<button class="s-btn s-btn__danger s-btn__filled s-btn__xs">Move ALL to chat</button>')
.on('click', function () {
$(this).remove();
const moveLinks = $('.js-move-comments-link:visible');
$('body').showAjaxProgress(moveLinks.length, { position: 'fixed' });
moveLinks.trigger('handle');
}).appendTo(actionBtns);
// Dismiss all
$('<button class="s-btn s-btn__danger s-btn__filled s-btn__xs">Dismiss ALL</button>')
.on('click', function () {
const dismissLinks = $('.comments-handled .js-resolve-now:visible');
dismissLinks.trigger('click');
}).appendTo(actionBtns);
}
}
function listenToPageUpdates() {
initPostCommentsModLinksEvents();
// On any page update
$(document).ajaxStop(function (event, xhr, settings) {
// Closed questions
$('.question-status').each(function () {
$(this).closest('.js-flagged-post').addClass('already-closed comments-handled');
});
// Always expand comments if post is expanded, if comments have not been expanded yet
$('.comments.js-comments-container').not('.js-del-loaded').each(function () {
const postId = this.id.match(/\d+/)[0];
// So we only load deleted comments once
$(this).addClass('js-del-loaded').removeClass('dno');
// Remove default comment expander
$(this).next().find('.js-show-link.comments-link').prev().addBack().remove();
// Get all including deleted comments
let commentsUrl = `/posts/${postId}/comments?includeDeleted=true&_=${Date.now()}`;
$('#comments-' + postId).children('ul.comments-list').load(commentsUrl, function () {
// Display post stats near action buttons, so we don't have to scroll back up
const el = $(this);
const post = $(this).closest('.js-flagged-post');
const postFlags = post.find('.js-post-flags');
const postCreated = post.find('.post-signature .relativetime').last().get(0).outerHTML;
const numAnswers = post.find('.js-post-header span.s-badge').last().text();
const isAccepted = post.find('.js-post-header .s-badge__answered').length > 0;
const numAnswersText = numAnswers ? `<div>post type: question (${numAnswers} answer${pluralize(numAnswers)})</div>` : `<div>post type: answer ${isAccepted ? '(accepted)' : ''}</div>`;
const closeReasonElem = post.find('.question-status:not(.bounty)');
const closeReason = closeReasonElem.length ? (closeReasonElem[0].children[0].childNodes[2] ? closeReasonElem[0].children[0].childNodes[2].nodeValue.replace(/\s*\b(as|by)\b\s*/g, '') : '') : '';
const closeReasonText = closeReasonElem.length && closeReason.length ? `<div>closed: ${closeReason}</div>` : '';
const cmmts = el.find('.comment-body');
const cmmtsDel = el.find('.deleted-comment');
const percDel = Math.ceil(cmmtsDel.length / cmmts.length * 100);
const cmmtUsers = el.find('.comment-body').find('.comment-user:first').map((i, el) => el.href).get().filter((v, i, self) => self.indexOf(v) === i); // unique users
const infoDiv = $(`
<div class="post-comment-stats">
<h3>Post info:</h3>
<div>created: ${postCreated}</div>
${numAnswersText}
${closeReasonText}
<div>${cmmts.length} comments, ${cmmtsDel.length} deleted (<span class="${percDel >= delCommentThreshold ? 'red' : ''}">${percDel}%</span>)</div>
<div>${cmmtUsers.length} users</div>
</div>`).appendTo(postFlags);
//console.log(closeReasonElem, closeReason);
if (percDel >= delCommentThreshold) {
post.addClass('too-many-deleted');
}
});
});
setTimeout(addPostCommentsModLinks, 100);
});
}
// Append styles
addStylesheet(`
.js-post-body,
.post-taglist,
.js-post-flag-options input,
.js-delete-post,
.mod-message {
display: none !important;
}
.tagged-ignored {
opacity: 1 !important;
}
.post-comment-stats {
padding: 15px 0;
text-align: right;
}
.post-comment-stats .red {
color: var(--red-500);
font-weight: bold;
}
.red-mod-link {
color: var(--red-500);
}
#actionBtns {
margin: 25px 24px 20px;
}
#actionBtns button {
margin-top: 10px;
margin-right: 10px;
}
/* New mod interface */
.js-post-header .js-loaded-body {
margin-left: -44px !important;
margin-right: -3px !important;
}
.visited-post {
opacity: 1;
}
`); // end stylesheet
// On script run
(function init() {
doPageLoad();
listenToPageUpdates();
})();