Jump to content

User:Firefly/more-block-info.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Firefly (talk | contribs) at 16:12, 17 December 2021 (+datetime). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
function getWarningboxLocked(username, logid, datetime, lockActor, lockComment) {
    console.log(logid);
    return `<div class="warningbox mw-warning-with-logexcerpt mw-content-ltr" dir="ltr" lang="en" > <p> This account is currently globally locked. The latest <span class="plainlinks" ><a class="external text" href="https://meta.wikimedia.org/wiki/Special:CentralAuth/${username}" >global account change log</a ></span > entry is provided below for reference: </p> <ul class="mw-logevent-loglines"> <li> <a href="https://meta.wikimedia.org/w/index.php?title=Special:Log&logid=${logid}" >${datetime}</a > <a href="https://meta.wikimedia.org/wiki/User:${lockActor}" class="mw-userlink userlink" title="User:${lockActor}" ><bdi>${lockActor}</bdi></a > <span class="mw-usertoollinks mw-changeslist-links" ><span ><a href="https://meta.wikimedia.org/wiki/User_talk:${lockActor}" class="mw-usertoollinks-talk userlink" title="User talk:${lockActor}" >talk</a ></span > <span ><a href="https://meta.wikimedia.org/wiki/Special:Contributions/${lockActor}" class="mw-usertoollinks-contribs userlink" title="Special:Contributions/${lockActor}" >contribs</a ></span ></span > changed status for global account <a href="https://meta.wikimedia.org/wiki/Special:CentralAuth/${username}" class="mw-userlink userlink user-blocked-indef" ><bdi>${username}</bdi></a > <span class="mw-usertoollinks mw-changeslist-links" ><span ><a href="/wiki/User_talk:${username}" class="mw-usertoollinks-talk userlink user-blocked-indef" >talk</a ></span > <span ><a href="/wiki/Special:Contributions/${username}" class="mw-usertoollinks-contribs userlink user-blocked-indef" >contribs</a ></span ></span > : set locked; unset (none) <span class="comment">(${lockComment})</span> </li> </ul> <a href="https://meta.wikimedia.org/wiki/Special:CentralAuth/${username}">View full log</a> </div>`;
}

// Shamelessly nicked from [[User:GeneralNotability/mark-locked.js]]
async function moreBlockDisplay_isLocked(user) {
    const api = new mw.Api();
    try {
        const response = await api.get({
            action: "query",
            list: "globalallusers",
            agulimit: "1",
            agufrom: user,
            aguto: user,
            aguprop: "lockinfo",
        });
        if (response.query.globalallusers.length === 0) {
            // If the length is 0, then we couldn't find the global user
            return false;
        }
        // If the 'locked' field is present, then the user is locked
        return "locked" in response.query.globalallusers[0];
    } catch (error) {
        return false;
    }
}

async function moreBlockDisplay_getLockLogEntry(user) {
    const api = new mw.Api();
    try {
        const response = await $.post("https://meta.wikimedia.org/w/api.php", {
            action: "query",
            list: "logevents",
            lelimit: "1",
            letitle: `User:${user}@global`,
            leaction: "globalauth/setstatus",
            origin: "*",
            format: "json",
        });

        let logEvent = response.query.logevents[0];
        let retVal = {
            logid: logEvent.logid,
            lockActor: logEvent.user,
            lockComment: logEvent.comment,
        };
        return retVal;
    } catch (error) {
        return undefined;
    }
}

$.when($.ready, mw.loader.using("mediawiki.util")).then(async function() {
    if (mw.config.get("wgPageName").includes("Special:Contributions/")) {
        const relevantUserName = mw.config.get("wgRelevantUserName");
        const userLocked = await moreBlockDisplay_isLocked(relevantUserName);
        if (userLocked) {
            const lockLogEntry = await moreBlockDisplay_getLockLogEntry(
                relevantUserName
            );
            const lockedBox = getWarningboxLocked(
                relevantUserName,
                lockLogEntry.logid,
                lockLogEntry.lockActor,
                lockLogEntry.lockComment
            );
            $("a[title='m:Global locks']").parent().remove(); // Remove "this account is globally locked" box
            $(".mw-contributions-blocked-notice").after(lockedBox);
        }
    }
});