Debian Codegraph @

extensions/DiscussionTools/modules/thanks.js

raw symbols SCIP
1 const cacheKey = 'dt-thanks';
2
3 /**
4 * Thank a comment item
5 *
6 * @param {CommentItem} commentItem Comment item
7 * @return {jQuery.Promise} Resolves when thanks successfully sent, rejects on error
8 */
9 function thankComment( commentItem ) {
10 // TODO: Add recipient gender for messages
11 const recipientGender = 'unknown';
12 return OO.ui.confirm( mw.msg( 'thanks-confirmation2', mw.user ), {
13 actions: [
14 {
15 action: 'accept',
16 label: mw.msg( 'thanks-button-thank', mw.user, recipientGender ),
17 flags: [ 'primary', 'progressive' ]
18 },
19 {
20 action: 'cancel',
21 label: mw.msg( 'cancel' ),
22 flags: 'safe'
23 }
24 ]
25 } ).then( ( confirmed ) => {
26 if ( !confirmed ) {
27 return $.Deferred().reject().promise();
28 }
29
30 const api = require( './controller.js' ).getApi();
31
32 return api.postWithToken( 'csrf', {
33 action: 'discussiontoolsthank',
34 // We don't need to store the correct transcluded comment page
35 // for a thank, any page the comment appears on will do.
36 page: mw.config.get( 'wgRelevantPageName' ),
37 commentid: commentItem.id
38 } ).then( () => {
39 mw.notify( mw.msg( 'thanks-thanked-notice', commentItem.author, recipientGender, mw.user ), { type: 'success' } );
40 cacheThanked( commentItem );
41 }, ( code, data ) => {
42 mw.notify( api.getErrorMessage( data ), { type: 'error' } );
43 return $.Deferred().reject().promise();
44 } );
45 } );
46 }
47
48 function isThanked( threadItem ) {
49 const cache = mw.storage.getObject( cacheKey ) || {};
50 return cache[ threadItem.id ];
51 }
52
53 function cacheThanked( threadItem ) {
54 const cache = mw.storage.getObject( cacheKey ) || {};
55 cache[ threadItem.id ] = true;
56 mw.storage.setObject( cacheKey, cache );
57 }
58
59 mw.hook( 'discussionToolsOverflowMenuOnChoose' ).add( ( id, menuItem, threadItem ) => {
60 // TODO: Add recipient gender for messages
61 const recipientGender = 'unknown';
62 if ( id === 'thank' ) {
63 thankComment( threadItem ).then( () => {
64 menuItem.setLabel( mw.msg( 'thanks-button-thanked', mw.user, recipientGender ) );
65 menuItem.setDisabled( true );
66 } );
67 }
68 } );
69
70 mw.hook( 'discussionToolsOverflowMenuOnAddItem' ).add( ( id, menuItem, threadItem ) => {
71 // TODO: Add recipient gender for messages
72 const recipientGender = 'unknown';
73 if ( id === 'thank' && isThanked( threadItem ) ) {
74 menuItem.setLabel( mw.msg( 'thanks-button-thanked', mw.user, recipientGender ) );
75 menuItem.setDisabled( true );
76 }
77 } );