MediaWiki:DiscussionsViewer/code.js

// Discussions Posts Viewer // Button from Special:Contribs that shows all Discussions posts and replies // @author: Jr Mime (function { var config = mw.config.get([ 'wgCanonicalSpecialPageName', 'wgCityId', 'wgUserGroups' ]); if ( config.wgCanonicalSpecialPageName !== 'Contributions' || window.dpv || !$('.discussion-details').exists ) {   return;  }  var dpv = {};  /**       importArticle({ type: 'script', article: 'u:dev:MediaWiki:I18n-js/code.js' });      dpv.preload = function (i18n) {           $.when( i18n.loadMessages('Discussions Posts Viewer'), dpv.id ).then(dpv.init);      };    **/  dpv.id = function {    return $.nirvana.getJson('UserProfilePage', 'renderUserIdentityBox', { title: 'User:' + $('.masthead-info hgroup h1').text }); };

dpv.init = function( /**i18n, data**/ ) { /**dpv.i18n = i18n; if (!data || !data[0] || !data[0].user || !data[0].user.id || data[1] !== 'success') { return; }**/   //dpv.id = data[0].user.id; // TODO: move this along with i18n importArticles({     type: "style",      articles: [        "u:dev:MediaWiki:DiscussionsViewer/code.css"      ]    }); $.when(dpv.id).then(function(data) {     dpv.id = data.user.id;    }); $('.selected').attr("data-id", "contribs").after(     $('').append( $('', {         id: 'dpv',          text: 'View posts'        }) ).click(dpv.click)   ); };

dpv.click = function { $.showCustomModal('', ' ', {     id: 'dpv-view-post',      width: 600,      buttons: []    }); dpv.getPosts; };

// Mustache templates dpv.templates = { mainPost: '' + '   ' + ' &bull; contribs' + ' &bull; posts' + ' &bull; in  ' + '  ' + ' ' +      ' ' +      '  ' + ' View in Discussions > ' + '  ',    postReply: '' + ' <img src="" /> ' + ' &bull; contribs</a> &bull; posts</a> ' + ' </a> ' + ' ' +      ' <img class="dpv-post-" srv="" srcset="/scale-to-width-down/420 420w, /scale-to-width-down/520 520w,  600w" sizes="(min-width: 1575px) 640px, (min-width:1064px) 520px, 100vw"> ' + ' View in Discussions ></a> ' + ' '  };

// Render a template using Mustache dpv.render = function(template, args) { return $(     Mustache.render(template, args)    ); };

// Pad numbers if 0-9 with an extra 0 dpv.timePad = function(n) { return (n < 10) ? '0' + n : n; };

dpv.getPosts = function {

$.ajax({     url: 'https://services.wikia.com/discussion/' + wgCityId + '/users/' + dpv.id + '/posts?responseGroup=full&limit=100&viewableOnly=false',      type: 'GET',      xhrFields: {        withCredentials: true      },      crossdomain: true    }).done(function(data) {      $("#dpv-content").empty;      var posts = data._embedded['doc:posts'];

// For each post, put into box as well $.each(posts, function(i, post) {       var option = "none";        // Format date        var date = new Date(post.creationDate.epochSecond * 1000);        date = dpv.timePad(date.getHours) + ':' + dpv.timePad(date.getMinutes) + ', ' + date.getDate + ' ' + wgMonthNames[date.getMonth + 1] + ' ' + date.getFullYear;        // Fix avatar        if (!post.createdBy.avatarUrl) {          post.createdBy.avatarUrl = 'https://images.wikia.com/messaging/images/1/19/Avatar.jpg';        }        // If post is over 196 characters, cut        if (post.rawContent.length >= 196) {          post.rawContent = post.rawContent.substring(0, 195) + "...";        }        // Add Discussions images        var imgURL = "#",            imgClass = "none";        if (post._embedded.contentImages && post._embedded.contentImages[0]) {          imgURL = post._embedded.contentImages[0].url;          imgClass = "image"; }       // If it's a reply if (post.isReply) { // Options if the post is reported, deleted or locked if (post.isReported) option = "report"; if (post.isDeleted) option = "delete"; $('#dpv-view-posts').append(           dpv.render(dpv.templates.postReply, { post: post, date: date, wiki: mw.config.get("wgServer"), locked: option, img: imgURL, imgClass: imgClass })         );        } else // If a post if (!post.isReply) { // Options if the post is reported, deleted or locked if (post.isReported) option = "report"; if (post._embedded.thread[0].isLocked) option = "lock"; if (post.isDeleted) option = "delete"; $('#dpv-view-posts').append(           dpv.render(dpv.templates.mainPost, { post: post, date: date, wiki: mw.config.get("wgServer"), locked: option, img: imgURL, imgClass: imgClass })         );        }       });    });  };

window.dpv = dpv; dpv.init; // mw.hook('dev.i18n').add(dpv.preload); });