MediaWiki:ChatBanMessage/code.js

/** * ChatBanMessage * Allows a moderator to automatically message a user about their chat ban * @Author Mario&LuigiBowser'sInsideStory * @TODO add i18n support (function{   if (mw.config.get('wgCanonicalSpecialPageName') != "Chat" || window.chatBanMessageLoaded) return;    window.chatBanMessageLoaded = true;    var chatBanMessage = window.chatBanMessage || {};    chatBanMessage.title = chatBanMessage.title || "Ban";    chatBanMessage.body = chatBanMessage.body || "You have been banned from the chat for $1 because $2.";    var wallsDisabled;    // Check if message walls are disabled    $.nirvana.getJson('WikiFeaturesSpecial', 'index', function(d) { wallsDisabled = d.features.filter(function (e) {           return e.name === 'wgEnableWallExt' && e.enabled;        }).length === 0; });   mainRoom.socket.bind('ban', function(event){ $('.chat-ban-message-button').remove; // So that a previous button cannot be accidentally clicked var mod = JSON.parse(event.data).attrs.moderatorName; if (mod === mw.config.get('wgUserName')) { // So that the banning moderator is the one who messages the user $('.inline-alert').last.append(               $(' ', { class: 'chat-ban-message-button', text: 'Message user', click: function{ $.get(mw.util.wikiScript('api'), {                           action: 'query',                            format: 'json',                            list: 'logevents',                            letype: 'chatban',                            lelimit: 1,                        }).done(function(data) {                            var user = data.query.logevents[0].title.slice(5),                                expiry = data.query.logevents[0]['2'],                                reason = data.query.logevents[0].comment;                            // Sending the message                            if (wallsDisabled) { // For talk pages                                $.post(mw.util.wikiScript('api'), { action: 'edit', format: 'json', title: 'User talk:' + user, section: 'new', sectiontitle: chatBanMessage.title, text: chatBanMessage.body.replace(/\$1|\$2/g, function(s){                                       return {                                            '$1': expiry,                                            '$2': reason                                        }[s];                                    }) + '\n\n~', token: mw.user.tokens.get('editToken') }).done(function(result){ if (!result.error) { mainRoom.model.chats.add(new models.InlineAlert({ text: 'Message successfully sent.' }));                                   }                                    else { mainRoom.model.chats.add(new models.InlineAlert({ text: 'An error occured while messaging ' + user + ': ' + result.error.info + '.' }));                                   }                                });                            }                            else {                                $.post(mw.util.wikiScript('wikia'), { // For message walls controller: 'WallExternal', method: 'postNewMessage', pagenamespace: 1200, pagetitle: user, messagetitle: chatBanMessage.title, body: chatBanMessage.body.replace(/\$1|\$2/g, function(s){                                       return {                                            '$1': expiry,                                            '$2': reason                                        }[s];                                    }), format: 'json', token: mw.user.tokens.get('editToken') }).done(function(result){ mainRoom.model.chats.add(new models.InlineAlert({ text: 'Message successfully sent.' }));                               });                            }                        });                        $(this).remove; // So that the same button cannot be clicked twice accidentally }               })            );        }    }); });