Debian Codegraph @

extensions/Thanks/modules/ext.thanks.flowthank.js

raw symbols SCIP
1 ( function () {
2 const $thankedLabel = $( '<span>' ).addClass( 'mw-thanks-flow-thanked mw-ui-quiet' );
3
4 const attrName = 'data-flow-id';
5 const cookieName = 'flow-thanked';
6
7 function findPostAuthorFromThankLink( $thankLink ) {
8 // We can't use 'closest' directly because .flow-author is a cousin
9 // of $thankLink rather than its ancestor
10 return $( $thankLink.findWithParent( '< .flow-post .flow-author a.mw-userlink' )[ 0 ] ).text().trim();
11 }
12
13 function reloadThankedState() {
14 $( 'a.mw-thanks-flow-thank-link' ).each( ( idx, el ) => {
15 const $thankLink = $( el );
16 if ( mw.thanks.thanked.contains( $thankLink.closest( '.flow-post' ).attr( attrName ), cookieName ) ) {
17 const author = findPostAuthorFromThankLink( $thankLink );
18 mw.thanks.getUserGender( author )
19 .done( ( recipientGender ) => {
20 $thankLink.before(
21 $thankedLabel
22 .clone()
23 .append(
24 mw.msg( 'thanks-button-thanked', mw.user, recipientGender )
25 )
26 );
27 $thankLink.remove();
28 } );
29 }
30 } );
31 }
32
33 function sendFlowThanks( $thankLink ) {
34 ( new mw.Api() ).postWithToken( 'csrf', {
35 action: 'flowthank',
36 postid: $thankLink.closest( '.flow-post' ).attr( attrName )
37 } )
38 .then(
39 // Success
40 () => {
41 const author = findPostAuthorFromThankLink( $thankLink );
42 // Get the user who was thanked (for gender purposes)
43 return mw.thanks.getUserGender( author );
44 },
45 // Failure
46 ( errorCode ) => {
47 switch ( errorCode ) {
48 case 'ratelimited':
49 OO.ui.alert( mw.msg( 'thanks-error-ratelimited', mw.user ) );
50 break;
51 default:
52 OO.ui.alert( mw.msg( 'thanks-error-undefined', errorCode ) );
53 }
54 }
55 )
56 .then( ( recipientGender ) => {
57 const $thankUserLabel = $thankedLabel.clone();
58 $thankUserLabel.append(
59 mw.msg( 'thanks-button-thanked', mw.user, recipientGender )
60 );
61 mw.thanks.thanked.push( $thankLink.closest( '.flow-post' ).attr( attrName ), cookieName );
62 $thankLink.before( $thankUserLabel );
63 $thankLink.remove();
64 } );
65 }
66
67 if ( $.isReady ) {
68 // This condition is required for soft-reloads
69 // to also trigger the reloadThankedState
70 reloadThankedState();
71 } else {
72 $( reloadThankedState );
73 }
74
75 // .on() is needed to make the button work for dynamically loaded posts
76 $( '.flow-board' ).on( 'click', 'a.mw-thanks-flow-thank-link', function ( e ) {
77 const $thankLink = $( this );
78 e.preventDefault();
79 sendFlowThanks( $thankLink );
80 } );
81
82 }() );