-
Notifications
You must be signed in to change notification settings - Fork 34
/
AdditionalInlinePostModMenu.user.js
1099 lines (987 loc) · 45.2 KB
/
AdditionalInlinePostModMenu.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name Additional Inline Post Mod Menu
// @description Adds additional mod-only quick links to each post, underneath system-provided links
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author Samuel Liew
// @author Cody Gray
// @version 5.6.13
//
// @match https://*.stackoverflow.com/*
// @match https://*.serverfault.com/*
// @match https://*.superuser.com/*
// @match https://*.askubuntu.com/*
// @match https://*.mathoverflow.net/*
// @match https://*.stackapps.com/*
// @match https://*.stackexchange.com/*
// @match https://stackoverflowteams.com/*
//
// @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://unpkg.com/sweetalert/dist/sweetalert.min.js
// ==/UserScript==
/* eslint-disable no-multi-spaces */
/* global swal:readonly */
/* global $:readonly */
/* global StackExchange:readonly */
/* global fkey:readonly */
/* global isSO:readonly */
/* global isSOMeta:readonly */
/* global isMetaSite:readonly */
/* global selfId:readonly */
/// <reference types="./globals" />
'use strict';
// This is a moderator-only userscript
if (!isModerator()) return;
// Yes, you can declare the variable apikey here and have it picked up by the functions in se-ajax-common.js
const apikey = 'lSrVEbQTXrJ4eb4c3NEMXQ((';
const newlines = '\n\n';
// Add your user ID here (or set the corresponding value in your Local Storage) to promote yourself
// to a "superuser", which enables rarely-used options and decreases the number of confirmations.
const superusers = [584192, 366904, 6451573];
const isSuperuser = superusers.includes(selfId) ||
((localStorage.getItem('SOMU-aipmm.isSuperuser') ?? 'false') === 'true');
// This option defaults to "false". Manually set it to "true" (or set the corresponding value
// in your Local Storage) to allow destroying spam accounts as fast as possible
// without multiple confirmations.
const underSpamAttackMode = (localStorage.getItem('SOMU-aipmm.underSpamAttackMode') ?? 'false') === 'true';
// This option defaults to "true". Manually set it to "false" (or set the corresponding value
// in your Local Storage) to make the size of the inserted quicklinks match the standard ones.
const smallerQuicklinks = (localStorage.getItem('SOMU-aipmm.smallerQuicklinks') ?? 'true') === 'true';
/*
* Reload functions
*/
const reloadPage = () => {
// If in mod queues, do not reload
if (isModDashboardPage) return false;
location.reload();
};
const reloadWhenDone = () => {
// Triggers when all ajax requests have completed
$(document).ajaxStop(function () {
// Stop subsequent calls
$(this).off("ajaxStop");
reloadPage();
});
};
const reloadPost = async pid => {
if (pid && StackExchange?.realtime?.reloadPosts) {
const result = await StackExchange.realtime.reloadPosts(pid);
if (result) return true;
}
return reloadPage();
};
/*
* Post dissociation feature functions
*/
const sendCmDissociateMessage = async (uid, pid, postType = 'question') => {
// Build message
const messageText = `Hello,
I'm writing in reference to the Stack Overflow account:
${location.origin}/users/${uid}
The following ${postType} need to be dissociated from the author's profile:
${location.origin}/${postType === 'question' ? 'q' : 'a'}/${pid}
**This was requested by the post author via custom flag.**
Regards,
A ${StackExchange.options.site.name} moderator`;
// Send CM message
return await sendCmMessage(pid, 'post-dissociation', messageText);
};
function updateModTemplates() {
const template = $('.popup input[name=mod-template]').filter((i, el) => $(el).next().text().includes('post disassociation'));
let addstr = '';
// Build list of posts
const pids = getQueryParam('pid').split('|');
pids.forEach(function (v) {
if (v.length === 0) return;
addstr += `${location.origin}/a/${v}` + newlines;
});
// Build list of meta posts
const metaPids = getQueryParam('metapid').split('|');
metaPids.forEach(function (v) {
if (v.length === 0) return;
addstr += `${metaUrl}/a/${v}` + newlines;
});
if (addstr === '') addstr = newlines;
// Insert to template
template.val(template.val()
.replace(/:\s+{todo}/, ':<br>\n' + addstr + '**Requested via custom flag.**' + newlines) // replace todo with additional information
).trigger('click');
$('.popup-submit').trigger('click');
// Failsafe
$('#templateName').val('post disassociation');
};
function initPostDissociationHelper() {
// Only on main sites
if (isMetaSite) return;
// Run once, whether on AdditionalPostModActions or AdditionalInlinePostModMenu
if (document.body.classList.contains('SOMU-PostDissociationHelper')) return;
else document.body.classList.add('SOMU-PostDissociationHelper');
// If on contact CM page and action = dissocciate
if (location.pathname.includes('/admin/cm-message/create/') && getQueryParam('action') == 'post-dissociation') {
// On any page update
$(document).ajaxComplete(function (event, xhr, settings) {
// If CM templates loaded on contact CM page, and action = dissocciate, update templates
if (settings.url.includes('/admin/contact-cm/template-popup/')) {
// Run once only. Unbind ajaxComplete event
$(event.currentTarget).unbind('ajaxComplete');
// Update CM mod templates
setTimeout(updateModTemplates, 500);
}
});
// click template link
$('#show-templates').trigger('click');
return;
}
// If on mod flag queues, remove close question and convert to comment buttons when flag message contains "di(sa)?ssociate", and add "dissociate" button
if (isModDashboardPage) {
const dissocFlags = $('.revision-comment.active-flag').filter((i, v) => v.innerText.match(/di(sa)?ssociate/));
const dissocPosts = dissocFlags.closest('.js-flagged-post');
dissocPosts.each(function () {
const post = $(this);
const userlink = post.find('.mod-audit-user-info a');
const uid = getUserId(userlink.attr('href'));
// User not found, probably already deleted
if (!uid) return;
const pid = post.attr('data-post-id') || post.attr('data-questionid') || post.attr('data-answerid');
$('.js-post-flag-options', this).prepend(`<a href="${location.origin}/admin/cm-message/create/${uid}?action=post-dissociation&pid=${pid}" class="btn" target="_blank">dissociate</a>`);
$('.close-question-button, .js-convert-to-comment', this).hide();
});
return;
}
};
/*
* Confirm functions
*/
async function promptToRedFlagPost(pid, postType, rudeFlag, isLocked, isDeleted, isFlagDeleted) {
if (typeof pid === 'undefined' || pid === null) { throw new Error('null or undefined pid'); }
const noun = rudeFlag ? 'rude/abusive' : 'spam';
const confirmMsg = !isFlagDeleted
? `Are you sure you want to red-flag nuke this ${postType} (post\xa0ID\xa0${pid}) as ${noun.toUpperCase()}?\n\n(All penalties associated with a ${noun} flag will apply.)`
: `Are you sure you want to change the red flag on this ${postType} (post\xa0ID\xa0${pid}) to ${noun.toUpperCase()}?\n\n(This should only be done to change a spam flag to a rude/abusive flag in order to prevent the post from being chosen as an audit. Do not choose this option if you have already flagged the post as rude/abusive, as you cannot re-raise a flag of the same type, even as a moderator. All penalties associated with a ${noun} flag will still apply.)`;
let needsRefresh = false;
if (confirm(confirmMsg)) {
try {
if (isLocked || isFlagDeleted) {
await unlockPost(pid);
needsRefresh = true;
}
if (isLocked || isFlagDeleted || isDeleted) {
await undeletePost(pid);
needsRefresh = true;
}
await flagPost(pid, rudeFlag);
needsRefresh = true;
}
catch (e) {
alert('An error occurred; please see the console for details on exactly what failed.');
needsRefresh = false; // unconditionally prevent refresh to avoid clearing the console
}
}
return needsRefresh;
};
async function promptToNukePostAndUser(pid, isQuestion, isDeleted, uid, uName, spammer, usercardHtml = null) {
if (typeof uid === 'undefined' || uid === null) { throw new Error('null or undefined uid'); }
const postType = spammer ? 'spam' : 'trolling/abusive';
const userType = spammer ? 'spammer' : 'troll';
const nukePost = pid && !isDeleted;
const userInfo = (await Promise.allSettled([getUserInfoFromApi(uid)]))[0].value;
// Display the confirmation and options dialog.
let swalContentHtml = `<div class="group"><div class="info">`; // begin first info group
if (usercardHtml) {
swalContentHtml += usercardHtml;
}
if (userInfo) {
// To counteract information overload, only display what is not already displayed in the user-card
// (which is always visible on the page on the associated post, and also added inline to this dialog,
// if possible). Therefore, reputation and badge information is not included in this text.
// The user account name and ID are still displayed for double-checking purposes and for linkability
// (since clicking on links in the user-card under the post would dismiss the modal dialog).
const creationDate = seApiDateToDate(userInfo?.creation_date);
const modifiedDate = seApiDateToDate(userInfo?.last_modified_date);
const accessDate = seApiDateToDate(userInfo?.last_access_date);
const hasOtherPosts = isQuestion ? (Number(userInfo?.question_count) > 1 || Number(userInfo?.answer_count) > 0)
: (Number(userInfo?.question_count) > 0 || Number(userInfo?.answer_count) > 1);
swalContentHtml += `
The <i>${userInfo.user_type}</i> user,
"<a href="${userInfo.link}"><strong>${uName}</strong></a>"
(ID <code>${uid}</code>${userInfo?.account_id ? `; <a href="https://stackexchange.com/users/${userInfo.account_id}?tab=accounts">network account</a> ID <code>${userInfo.account_id}</code>` : ''}),
was
<strong>created <span title="${dateToIsoString(creationDate).replaceAll(' ', ' ')}">${dateToRelativeTime(creationDate).replaceAll(' ', ' ')}</span></strong>${modifiedDate ? `, last <strong>modified <span title="${dateToIsoString(modifiedDate).replaceAll(' ', ' ')}">${dateToRelativeTime(modifiedDate).replaceAll(' ', ' ')}</span></strong>,` : ''}
and <strong>last seen <span title="${dateToIsoString(accessDate).replaceAll(' ', ' ')}">${dateToRelativeTime(accessDate).replaceAll(' ', ' ')}</span></strong>.
They have
<a href="${userInfo.link}?tab=questions&sort=newest"><strong>${userInfo.question_count}</strong> non‑deleted question${userInfo.question_count !== 1 ? 's' : ''}</a>
and
<a href="${userInfo.link}?tab=answers&sort=newest"><strong>${userInfo.answer_count}</strong> non‑deleted answer${userInfo.answer_count !== 1 ? 's' : ''}</a>.`;
if (hasOtherPosts > 0) {
swalContentHtml += `<div class="s-notice s-notice__warning" role="status">User has other non-deleted posts: be sure to check these before destroying the account!</div>`;
}
}
// end first info group and begin next group
swalContentHtml += `
</div></div>
<div class="group">
<div class="header">Optional Overrides:</div>`;
swalContentHtml += `
<div class="option">
<input type="checkbox" name="aipmm-bowdlerize-toggle" id="aipmm-bowdlerize-toggle" />
<label for="aipmm-bowdlerize-toggle" title="Enabling this option will clear all fields in the user's profile to remove spam content and reset the display name. (If the account is removed on this site, the original info is still retrieved and recorded in the deleted user record.)">
Bowdlerize profile and push edits to all sites
</label>
</div>`;
if (spammer) {
swalContentHtml += `
<div class="option">
<input type="checkbox" name="aipmm-noaudit-toggle" id="aipmm-noaudit-toggle" ${nukePost ? '' : 'disabled'}/>
<label for="aipmm-noaudit-toggle" title="Enabling this option will nuke the post as "rude/abusive", thus preventing it from being automatically selected as an audit. Otherwise, if this option is not enabled, the post will be nuked as "spam", thus allowing it to be selected as an audit.">
Prevent post from becoming a spam audit
</label>
</div>`;
}
swalContentHtml += `
<div class="option">
<input type="checkbox" name="aipmm-suspendonly-toggle" id="aipmm-suspendonly-toggle" />
<label for="aipmm-suspendonly-toggle" title="Enabling this option will prevent the account from being destroyed. Instead, it will automatically send a message that suspends the user for the maximum duration that is permitted for moderators (365 days). This is intended to be used in situations where you'd prefer to keep the account around (e.g., for follow-up investigations or because staff has requested it).">
Skip destroying user—only suspend for maximum duration of 1 year
</label>
</div>`;
// end second group
swalContentHtml += `</div>`;
// final group
swalContentHtml += `
<div class="group">
<div class="header">Destroy Details:</div>
<textarea autocapitalize="sentences"
autocomplete="on"
autocorrect="on"
placeholder="Optional context and details for why you are destroying the account. (This will be included with the deleted user profile.)"
></textarea>
</div>`;
const swalContent = document.createElement('div');
swalContent.innerHTML = swalContentHtml;
// TODO: Add option to report to Smokey before nuking, with checkbox and nested textbox, a la SIM.
// (For spammer, default message to 'reported by site moderator as spam';
// for troll, default message to 'reported by site moderator for training'.)
swalContent.querySelector('#aipmm-suspendonly-toggle').addEventListener('click', (event) => {
const suspendOnly = event.target.checked;
const modal = event.target.closest('.swal-modal');
if (modal) {
const textarea = modal.querySelector('textarea');
if (textarea) {
textarea.disabled = suspendOnly;
}
const submitBtn = modal.querySelector('.swal-button--confirm.swal-button--danger');
if (submitBtn) {
let label = submitBtn.textContent;
if (suspendOnly) label = label.replace('Destroy', 'Suspend');
else label = label.replace('Suspend', 'Destroy');
submitBtn.textContent = label;
}
}
});
let needsRefresh = false;
const skipAllDialogs = selfId === 584192;
try {
const confirmed = skipAllDialogs || await swal({
title: `Nuke ${nukePost ? `this post as ${postType} and ` : ''} the user "${uName}" as a ${userType}?`,
buttons:
{
confirm:
{
text: `Destroy "${uName}" as ${userType}`,
value: true,
visible: true,
closeModal: false,
className: 's-btn s-btn__filled s-btn__danger',
},
cancel:
{
text: 'Cancel',
value: null,
visible: true,
closeModal: true,
className: 's-btn s-btn__muted',
}
},
dangerMode: true,
closeOnEsc: true,
closeOnClickOutside: true,
backdrop: false,
content: swalContent,
});
if (skipAllDialogs || confirmed) {
const bowdlerize = skipAllDialogs ? false : document.querySelector('#aipmm-bowdlerize-toggle').checked;
const rudeFlag = skipAllDialogs ? false : !spammer || document.querySelector('#aipmm-noaudit-toggle').checked;
const suspendOnly = skipAllDialogs ? false : document.querySelector('#aipmm-suspendonly-toggle').checked;
const details = skipAllDialogs ? '' : document.querySelector('.swal-content textarea').value.trim();
if ((spammer && underSpamAttackMode) ||
isSuperuser ||
confirm(`Are you certain that you want to${nukePost ? ' nuke this post and ' : ' '}${suspendOnly ? 'SUSPEND' : 'DESTROY'} the account "${uName}" as a ${userType}?`)) {
// TODO: If post has already been flag-nuked as spam, but "rudeFlag" is set, change it.
// (This requires undeleting the post, unlocking it, and then re-flagging it.
// But, more importantly, it requires determining how the post has been flagged.)
// For now, if the post has already been deleted, just don't do anything.
// (The option to raise a rude flag instead will have been disabled.)
if (nukePost) {
await flagPost(pid, rudeFlag);
needsRefresh = true;
}
// If we are to suspend the user, then do so first. This ensures that their *current*
// name appears in the mod message, not what it gets reset to after bowdlerization.
// Note that we no longer send a suspension before destroying the account. This is because:
// (1) a recent system change makes it obsolete (accounts destroyed for the reason we use
// are blocked for 365 days, instead of 14 days), and (2) doing so without the workaround
// to prevent the message from showing up in the global mod inbox (padding the message name
// out with spaces to make it extremely long), which is virtually necessary to keep the
// mod inbox usable on Stack Overflow, generates exceptions, causing staff to request that
// we stop using it. Sending the suspension first does marginally improve the UX for users
// who recreate the account (which is stupidly easy to do), in that the reason why their
// old account was destroyed appears in their inbox (although they cannot actually view it,
// only see the preview), but (1) it is not all that important to improve the UX for users
// whose account has been destroyed, (2) this is not a big enough improvement to justify
// irritating staff/devs, and (3) if this is actually desirable (which it probably is), it
// should simply be implemented at the system level when any account that has been destroyed
// or deleted for the reasons that automatically suspend upon re-creation is re-created.
if (suspendOnly) {
await modMessageUser(uid,
'Account disabled for spamming and/or abusive behavior. You\'re no longer welcome to participate here.',
false, // do not email (show message on-site only)
365, // suspend for 365 days (maximum duration)
`suspend ${userType}`,
spammer ? 'for promotional content' : 'for rule violations');
needsRefresh = true;
}
// Before bowdlerizing, which will reset some of the PII fields, retrieve the current PII
// so that it can be recorded in the deletion record (this info is inaccessible or perhaps
// removed entirely for deleted/destroyed accounts, so this step is critical to preserve
// the information for later investigations, if necessary). Of course, we don't want to
// retrieve PII unnecessary, not only for information-privacy reasons, but also for speed
// and rate-limiting concerns. Therefore, we only retrieve the PII if we are actually
// going to destroy the account (i.e., if we are not only suspending).
const pii = !suspendOnly ? await getUserPii(uid) : null;
if (bowdlerize) {
await resetUserProfile(uid);
needsRefresh = true;
}
// If we are to destroy the user, then do so now, after everything else has been done.
// Pass in the user information and PII that we cached in order for it to be recorded.
if (!suspendOnly) {
await destroyUser(uid,
details,
'This user was created to post spam or nonsense and has no other positive participation',
userInfo,
pii);
needsRefresh = true;
}
// If the account was bowdlerized and/or destroyed, show the user profile page
// in a new pop-up window. (Exception: don't do it when destroying a spammer's
// account when the site is under a spam attack, or ever for superusers.)
if ((bowdlerize || !suspendOnly) && (!underSpamAttackMode || !spammer) && !isSuperuser) {
window.open(`${location.origin}/users/${uid}`,
'_blank',
'popup=true');
}
}
}
}
catch (e) {
console.error(e);
alert('An error occurred; please see the console for details on exactly what failed.');
needsRefresh = false; // unconditionally prevent refresh to avoid clearing the console
}
// Try closing swal dialog
try {
swal.stopLoading();
swal.close();
}
catch (e) { }
return needsRefresh;
};
/*
* UI functions
*/
function addPostModMenuLinks() {
// If it doesn't exist yet, append the menu of mod quicklinks to each post
$('.js-post-menu').not('.preview-options').not('.js-init-better-inline-menu').addClass('js-init-better-inline-menu').each(function () {
const post = $(this).closest('.question, .answer');
const postScore = Number(post.find('.js-vote-count').text());
const postStatusEl = post.find('.js-post-notice, .special-status, .question-status');
const postStatus = postStatusEl.text().toLowerCase();
const isQuestion = post.hasClass('question');
const isClosed = postStatusEl.find('b').text().toLowerCase().includes('closed') || postStatus.includes('on hold') || postStatus.includes('duplicate') || postStatus.includes('already has');
const isMigrated = postStatus.includes('migrated to');
const isProtected = postStatusEl.find('b').text().toLowerCase().includes('highly active question');
const isLocked = isMigrated || postStatus.includes('locked');
const isDeleted = post.hasClass('deleted-answer');
const isModDeleted = isDeleted && (postStatus.includes('deleted') && containsDiamondUnicode(postStatus));
const isBotDeleted = isDeleted && (postStatus.includes('deleted') && postStatusEl.html().toLowerCase().includes('/users/-1/community'));
const isFlagDeleted = isBotDeleted && isLocked && postStatus.includes('flagged as spam or offensive content');
const isOldDupe = isQuestion && post.find('.js-post-body blockquote').first().find('strong').text().includes('Possible Duplicate');
const needsRedupe = postStatus.match(/This question already has( an)? answers? here:(\s|\n|\r)+Closed/i) != null;
const hasComments = post.find('.comment, .comments-link.js-show-link:not(.dno)').length > 0;
const pid = post.attr('data-questionid') || post.attr('data-answerid');
const userbox = post.find('.post-layout .user-info:last .user-action-time').filter((i, el) => el.innerText.includes('answered') || el.innerText.includes('asked')).parent();
const userlink = userbox.find('a').first();
const uid = getUserId(userlink.attr('href'));
const userRep = userbox.find('.reputation-score').text();
const username = userbox.find('.user-details a').first().text();
const userAttributes = { uid, username };
const postDate = userbox.find('.relativetime').attr('title');
const postAge = (Date.now() - new Date(postDate)) / 86400000;
const postType = isQuestion ? 'question' : 'answer';
const postAttributes = { isClosed, isProtected, isLocked, isDeleted, isFlagDeleted };
const dissocPostIdParam = pid ? '&' + (!isMetaSite ? `pid=${pid}` : `metapid=${pid}`) : '';
const allowDestroyUser = (postAge < 60 || isSuperuser) && Number(userRep) < 500;
// .js-post-menu is also found on the post revisions page, but we don't want to touch that
if (typeof pid === 'undefined') return;
// Create menu based on post type and state
function makeLabel(text) {
return `<span class="inline-label">${text}: </span>`;
}
function makeItem(action, text, title = '', enabled = true, style = '', dataAttribs = '') {
// Convert data attributes object to string
if (typeof dataAttribs === 'object' && dataAttribs !== null) {
dataAttribs = Object.entries(dataAttribs).map(([key, value]) => `data-${camelToKebab(key)}="${value}"`).join(' ');
}
return `<button type="button" class="s-btn s-btn__link ${style}" data-action="${action}" ${dataAttribs} title="${title}" ${enabled ? '' : 'disabled'}>${text}</button>`;
}
const items = {
'redupeLabel': isSO && isOldDupe && needsRedupe && makeLabel('instant'),
'redupeOldQuestion': isSO && isOldDupe && needsRedupe &&
makeItem('old-redupe', 'close as proper duplicate', '', true, '', { 'redupe-pid': oldDupePid }),
'instantLabel': makeLabel('instant'),
'instantProtect': isQuestion && !isProtected && makeItem('protect', 'protect', 'protect this question to prevent it from being answered by anonymous and low-rep users', !isDeleted),
'instantUnprotect': isQuestion && isProtected && makeItem('unprotect', 'unprotect', 'unprotect this question to allow it to be answered by anonymous and low-rep users', !isDeleted),
'instantSOClose': isQuestion && isSO && makeItem('close-offtopic', 'close', 'close with default off-topic reason', !isClosed && !isDeleted),
'instantSOMetaClose': isQuestion && isSOMeta && makeItem('close-meta', 'close+del', 'close and delete as not a Meta question', !isDeleted),
'instantModDelete': makeItem('mod-delete', !isDeleted ? 'delete' : 'redelete', `re-delete as moderator to prevent undeletion`, true, 'warning'),
'instantSpamFlag': makeItem('spam-flag', 'spam..', `prompt for confirmation to ${!isFlagDeleted ? 'flag-nuke' : 're-flag'} this ${postType} as spam`, !isFlagDeleted, 'warning', postAttributes),
'instantAbusiveFlag': makeItem('abusive-flag', 'abusive..', `prompt for confirmation to ${!isFlagDeleted ? 'flag-nuke' : 're-flag'} this ${postType} as rude/abusive`, true, 'warning', postAttributes),
// Convert answers
'convertLabel': !isQuestion && makeLabel('convert'),
'convertToComment': !isQuestion && makeItem('convert-comment', 'to-comment', 'convert this answer to a comment on the question'),
'convertToEdit': !isQuestion && makeItem('convert-edit', 'to-edit', 'append this answer as an edit to the question'),
// Lock posts
'lockLabel': makeLabel('lock'),
'lockDispute': !isLocked && makeItem('lock-dispute', 'dispute..', `prompt for number of days to apply a content-dispute lock to this ${postType}`),
'lockComments': !isLocked && makeItem('lock-comments', 'cmnts..', `prompt for number of days to apply a comment lock to this ${postType}`),
'lockWiki': !isLocked && makeItem('lock-wiki', 'wiki..', `prompt for confirmation to apply a permanent wiki lock to this ${postType}`),
'lockObsolete': !isLocked && makeItem('lock-obsolete', 'obsolete..', `prompt for confirmation to apply a permanent obsolete lock to this ${postType}`),
'lockHistorical': !isLocked && isQuestion && makeItem('lock-historical', 'hist..', `prompt for confirmation to apply a permanent historical lock to this ${postType}`, (postAge >= 60 && postScore >= 20) || isSuperuser),
'lockUnlock': isLocked && makeItem('unlock', 'unlock', `unlock this ${postType}`),
// Add user-related links only if there is a post author, and is not a Meta site
'userLabel': !isMetaSite && uid && makeLabel('user'),
'userSpammer': !isMetaSite && uid && makeItem('nuke-spammer', 'spammer..', `prompt for options and confirmation to nuke this ${postType} and the user as a spammer (promotional content)`, allowDestroyUser, 'danger', userAttributes),
'userTroll': !isMetaSite && uid && makeItem('nuke-troll', 'troll..', `prompt for options and confirmation to nuke this ${postType} and the user as a troll/abusive`, allowDestroyUser, 'danger', userAttributes),
'userNoLongerWelcome': !isMetaSite && uid && makeItem('no-longer-welcome', 'nlw..', `prompt for confirmation to delete the user as "no longer welcome"`, true, 'danger', userAttributes),
// Add CM message options, if user is not deleted
'cmLabel': uid && makeLabel('cm'),
'cmPostDissociation': uid && makeItem('cm-post-dissociation', 'dissociate post..', isSuperuser ? `send CM dissociation message for this ${postType}` : `compose CM dissociation message in a new window`, true, '', userAttributes),
'cmSuspiciousVotes': uid && makeItem('cm-suspicious-votes', 'suspicious votes..', `compose CM suspicious voting message in a new window`, true, '', userAttributes),
};
// Add menu items to menu
let menuitems = '';
for (const item in items) {
const val = items[item];
if (val) menuitems += val;
}
$(this).append(`<div class="js-better-inline-menu ${smallerQuicklinks ? 'smaller' : ''}" data-pid="${pid}">${menuitems}</div>`);
});
}
function initPostModMenuLinks() {
// Handle clicks on links in the mod quicklinks menu.
// NOTE: We have to include the tag "main" for mobile web because it doesn't contain the wrapping elem "#content".
$('#content, main').on('click', '.js-better-inline-menu button[data-action]', function () {
if (this.disabled) return; // should not need this because s-btn[disabled] already has pointer-events: none
// Get question link if in mod queue
const qlink = $(this).closest('.js-flagged-post').find('.js-body-loader a').first().attr('href');
const reviewlink = $('.question-hyperlink').attr('href');
const menuEl = this.parentNode;
const pid = Number(menuEl.dataset.postId || menuEl.dataset.pid);
const qid = Number($('#question').attr('data-questionid') || getPostId(qlink) || getPostId(reviewlink)) || null;
const uid = Number(this.dataset.uid);
const uName = this.dataset.username;
//console.log(pid, qid);
if (isNaN(pid) || isNaN(qid)) return;
const post = $(this).closest('.answer, .question');
const isQuestion = post.hasClass('question');
const isDeleted = post.hasClass('deleted-answer');
const postType = isQuestion ? 'question' : 'answer';
const action = this.dataset.action;
//console.log(action);
const removePostFromModQueue = pid => {
if (isModDashboardPage) {
post.parents('.js-flagged-post').remove();
return true;
}
return false;
};
const removePostFromModQueueOrReloadPage = pid => {
removePostFromModQueue() || reloadPage();
};
switch (action) {
case 'old-redupe':
const redupePid = Number(this.dataset.redupePid);
if (!redupePid) return;
reopenQuestion(pid).then(function (v) {
closeQuestionAsDuplicate(pid, redupePid).finally(reloadPage);
});
break;
case 'convert-comment':
undeletePost(pid).then(function () {
convertToComment(pid, qid).then(removePostFromModQueueOrReloadPage);
});
break;
case 'convert-edit':
undeletePost(pid).then(function () {
convertToEdit(pid, qid).then(function () {
removePostFromModQueue();
goToPost(qid);
});
});
break;
case 'protect':
protectPost(pid).finally(reloadPage);
break;
case 'unprotect':
unprotectPost(pid).finally(reloadPage);
break;
case 'close-meta':
closeSOMetaQuestionAsOfftopic(pid).then(function () {
deletePost(pid).finally(reloadPage);
});
break;
case 'close-offtopic':
closeQuestionAsOfftopic(pid).finally(function () {
removePostFromModQueue();
goToPost(qid);
});
break;
case 'mod-delete':
redeletePost(pid).finally(removePostFromModQueueOrReloadPage);
break;
case 'spam-flag':
case 'abusive-flag':
promptToRedFlagPost(pid,
postType,
action === 'abusive-flag',
this.dataset.isLocked,
this.dataset.isDeleted,
this.dataset.isFlagDeleted
).then(function (result) {
if (result) removePostFromModQueueOrReloadPage();
});
break;
case 'lock-dispute': {
const input = prompt(`Apply a CONTENT-DISPUTE LOCK to this ${postType} for how many days?`, '3')?.trim();
const days = Number(input);
if (!isNaN(days) && days > 0) lockPost(pid, 20, 24 * days).then(reloadPage);
else if (input && isNaN(days)) StackExchange.helpers.showErrorMessage(menuEl.parentNode, 'Invalid number of days');
break;
}
case 'lock-comments': {
const input = prompt(`Apply a COMMENT LOCK to this ${postType} for how many days?`, '1')?.trim();
const days = Number(input);
if (!isNaN(days) && days > 0) lockPost(pid, 21, 24 * days).then(reloadPage);
else if (input && isNaN(days)) StackExchange.helpers.showErrorMessage(menuEl.parentNode, 'Invalid number of days');
break;
}
case 'lock-wiki':
if (confirm(`Are you sure you want to apply a PERMANENT WIKI LOCK to this ${postType}?`)) {
lockPost(pid, 23, -1).then(reloadPage);
}
break;
case 'lock-obsolete':
if (confirm(`Are you sure you want to apply a PERMANENT OBSOLETE LOCK to this ${postType}?`)) {
lockPost(pid, 28, -1).then(reloadPage);
}
break;
case 'lock-historical':
if (confirm('Are you sure you want to apply a PERMANENT HISTORICAL LOCK to this entire Q&A?')) {
lockPost(pid, 22, -1).then(reloadPage);
}
break;
case 'unlock':
unlockPost(pid).then(reloadPage);
break;
case 'nuke-spammer':
case 'nuke-troll':
promptToNukePostAndUser(
pid,
isQuestion,
isDeleted,
uid,
uName,
action === 'nuke-spammer',
post.find('.post-signature:last .user-info')[0]?.outerHTML
).then(function (result) {
if (result) {
removePostFromModQueueOrReloadPage();
}
});
break;
case 'no-longer-welcome':
if (confirm(`Are you sure you want to DELETE THE USER "${uName}" as "no longer welcome"?\n\n(Note that this post will not be affected, unless it is negatively-scored, in which case it will be implicitly deleted along with the user account.)`)) {
deleteUser(
uid,
'', // no details needed
'This user is no longer welcome to participate on the site'
).then(function () {
removePostFromModQueueOrReloadPage();
});
}
break;
case 'cm-post-dissociation':
if (isSuperuser && confirm(`Are you sure you want to SEND (without review) a CM message to dissociate this ${postType} (ID: ${pid}) by "${uName}"?`)) {
sendCmDissociateMessage(uid, pid, postType).then(function () {
StackExchange.helpers.showSuccessMessage(menuEl.parentNode, 'CM dissociation message sent successfully.');
});
return;
}
else if (!isSuperuser) {
// Open CM message in new window/tab
window.open(`${parentUrl}/admin/cm-message/create/${uid}?action=post-dissociation`);
}
break;
case 'cm-suspicious-votes':
// Open CM message in new window/tab
window.open(`${parentUrl}/admin/cm-message/create/${uid}?action=suspicious-voting`);
break;
default:
}
return;
});
}
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="s-link__danger js-show-deleted-comments-link" role="button"
title="Expand to show all comments on this post, including deleted"
href="${delCommentsBtn.attr('href')}">
Show <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="s-link__danger js-move-comments-link" title="Move all comments to chat, then delete all comments">move to chat</a>
<span class="js-link-separator3"> | </span>
<a data-post-id="${pid}" class="s-link__danger js-purge-comments-link" 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 initPostCommentsModLinks() {
const d = $('body').not('.js-comments-menu-events').addClass('js-comments-menu-events');
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();
});
d.on('click', 'a.js-move-comments-link', function (e) {
e.preventDefault();
const post = $(this).closest('.answer, .question');
const pid = Number(this.dataset.postId) || null;
$(this).remove();
moveCommentsOnPostToChat(pid);
});
d.on('click', 'a.js-purge-comments-link', function (e) {
e.preventDefault();
const post = $(this).closest('.answer, .question');
const pid = Number(this.dataset.postId) || null;
deleteCommentsOnPost(pid);
});
}
// Append styles
addStylesheet(`
/* Better post menu links */
.js-post-menu {
margin-top: 7px !important;
}
.js-post-menu .s-anchors > .flex--item {
margin-top: 0 !important;
}
.js-post-menu .s-anchors > div.flex--item {
margin-left: 0; /* Move margins from container to item... */
margin-right: 0; /* ...to allow hiding individual items. */
}
.js-post-menu .s-anchors > div.flex--item button,
.js-post-menu .s-anchors > div.flex--item a {
text-transform: lowercase;
font-size: 0.97em;
margin-left: calc(var(--su8) / 2);
margin-right: calc(var(--su8) / 2);
}
.js-post-menu .s-anchors.s-anchors__muted .s-btn.s-btn__link,
.js-post-menu .s-anchors.s-anchors__muted a:not(.s-link) {
color: var(--black-500);
}
.js-post-menu .s-anchors.s-anchors__muted .s-btn.s-btn__link:hover,
.js-post-menu .s-anchors.s-anchors__muted a:not(.s-link):hover {
color: var(--black-300);
}
.post-signature {
min-width: 180px;
width: auto; /* allow the usercard to shrink, if possible... */
max-width: 200px; /* ...but never allow to expand larger than default size */
}
/* Post inline menu */
.js-better-inline-menu {
clear: both;
float: left;
min-width: 260px;
margin: 14px 0;
text-align: left;
white-space: nowrap;
}
.js-better-inline-menu.smaller {
font-size: 0.94em;
line-height: 1.05;
}
.js-better-inline-menu * {
font-family: inherit;
font-size: inherit;
letter-spacing: inherit;
}
.js-better-inline-menu .inline-label,
.js-better-inline-menu button.s-btn.s-btn__link {
float: left;
padding: 3px 4px;
}
.js-better-inline-menu .inline-label {
color: var(--black-800);
padding-left: 0;
clear: both;
}
.js-better-inline-menu button.s-btn.s-btn__link {
color: var(--black-500);
text-decoration: none;
}
.js-better-inline-menu button.s-btn.s-btn__link:hover {
color: var(--black-300);
}
.js-better-inline-menu button.s-btn.s-btn__link.warning:hover,
.js-post-menu .s-anchors.s-anchors__muted .s-btn.s-btn__link.js-delete-post:hover {
color: var(--red-600);
}
.js-better-inline-menu button.s-btn.s-btn__link.danger:hover {
background-color: var(--red-500);
color: var(--white);
}
/* Comments form and links */
.js-comment-form-layout > div:nth-child(2),
.js-comments-menu {
display: block !important;
}
.js-comment-form-layout > div:nth-child(2) br {
display: none;
}
.js-edit-comment-cancel {
display: block;
margin-bottom: 5px;
}
a.js-load-deleted-nomination-comments-link,
a.js-show-deleted-comments-link,
a.js-move-comments-link,
a.js-purge-comments-link {
color: var(--red-600); /* slightly darken "danger" color (.bg-danger) */
}
a.js-show-deleted-comments-link,
a.js-move-comments-link,
a.js-purge-comments-link {
padding: 0 3px 2px 3px;
text-decoration: none;
}
.comment-help {
max-width: none;
}
/* Pop-up dialog for destroy user */
.swal-overlay {
background-color: hsl(358deg 67% 6% / 50%); /* Stacks' --_mo-bg */
/*background: 0; */
}
.swal-overlay,
.swal-overlay--show-modal .swal-modal {
transition: ease-in-out 0.1s;
animation: 0;
}
.swal-modal,
.swal-overlay--show-modal .swal-modal {
padding: 18px;
border-radius: 0;
will-change: unset;
}
.swal-modal {
width: 700px;
background-color: var(--white);
border: solid 1px var(--black-300);
box-shadow: var(--bs-sm);
}
body.theme-dark .swal-modal,
.theme-dark__forced .swal-modal,
body.theme-system .theme-dark__forced .swal-modal {
background-color: var(--black-100);
}
@media only screen and (max-width: 750px) {
.swal-modal {
width: 495px;
}
}
.swal-content {
margin: 0;
padding: 0;
color: inherit;
font-size: var(--fs-body2);
width: 100%
}
.swal-title:first-child,
.swal-title:not(:last-child) {
margin: -3px 0 18px 0;
padding: 0;
text-align: left;
font-size: var(--fs-title);
font-weight: 400;
color: var(--red-600); /* Stacks' --_mo-header-fc */
}
.swal-content .group {
display: block;
margin: 18px 0;
text-align: left;
}
.swal-content .header {
font-weight: bold;
}
.swal-content .info {
margin: 18px 0;
font-size: var(--fs-body2);
}
.swal-content code {
padding: 0;
background-color: inherit;
}
.swal-content .user-info {
float: right;
margin-left: 18px;
font-size: 90%;
background-color: var(--theme-secondary-050); /* ALTERNATIVE: var(--highlight-bg); */
border: 1px solid var(--br-md); /* ALTERNATIVE: var(--bc-light); */
}
.swal-content .s-notice {
clear: both;
float: left;
width: 100%;
margin: 9px 0 18px;