<commit_before>var actions = {
    ban: {
        url: "mod/toggle_ban",
        btnStyle: "danger",
        callback: function(data, elem) {
            elem.text(`${data.banned ? "Unban" : "Ban"}`);
        },
        buttonText: function(data) {
            return data.banned ? "Unban" : "Ban";
        }
    },
    mod: {
        url: "admin/toggle_mod",
        btnStyle: "info",
        adminOnly: true,
        callback: function(data, elem) {
            elem.text(`${data.moderator ? "Remove" : "Give"} Moderator`);
        },
        buttonText: function(data) {
            return `${data.moderator ? "Remove" : "Give"} Moderator`
        }
    }
}
var renderAction = function(actionName, data) {
    var action = actions[actionName];
    return `<a href="javascript:void(0)" class="btn btn-${action.btnStyle} user-action-btn" data-admin-only=${action.adminOnly === true} data-user-action="${actionName}">${action.buttonText(data)}</a>`;
}

var renderUserActions = function(user) {
    return `<div class="actions-ctn" data-user-id="${user._id}">
        <a href="/admin/users/similar/${user._id}" class="btn btn-warning">View Similar</a>
        ${renderAction("ban", user)}
        ${renderAction("mod", user)}
    </div>`
}

$("body").on("click", ".user-action-btn", function() {
    function handleError(data) {
        var error = "An unknown error occurred."
        if(data && typeof data.error !== 'undefined' && data.error.message) error = data.error.message;
        alert("Couldn't perform action on user: " + error);
    }
    var userID = $(this).parent().data("user-id");
    var action = actions[$(this).data("user-action")];
    var originalText = $(this).html();
    $(this).addClass("disabled");
    $(this).html(`<i class="fa fa-circle-o-notch fa-spin"></i> ${originalText}`);
    var elem = $(this);
    $.get(`/api/${action.url}/`, {id: userID}).success(function(data) {
        if(!data.success) return handleError(data);
        action.callback(data, elem);<commit_msg>Don't allow users to modify others unless they're allowed to (client-side)<commit_after>var actions = {
    ban: {
        url: "mod/toggle_ban",
        btnStyle: "danger",
        callback: function(data, elem) {
            elem.text(`${data.banned ? "Unban" : "Ban"}`);
        },
        buttonText: function(data) {
            return data.banned ? "Unban" : "Ban";
        }
    },
    mod: {
        url: "admin/toggle_mod",
        btnStyle: "info",
        adminOnly: true,
        callback: function(data, elem) {
            elem.text(`${data.moderator ? "Remove" : "Give"} Moderator`);
        },
        buttonText: function(data) {
            return `${data.moderator ? "Remove" : "Give"} Moderator`
        }
    }
}
var renderAction = function(actionName, data) {
    var action = actions[actionName];
    return `<a href="javascript:void(0)" class="btn btn-${action.btnStyle} user-action-btn" data-admin-only=${action.adminOnly === true} data-user-action="${actionName}">${action.buttonText(data)}</a>`;
}

var renderUserActions = function(user) {
    var currentUserID = $("body").data("user-id");
    var currentIsAdmin = $("body").data("user-is-admin");
    var currentIsMod = $("body").data("user-is-mod");
    var canTouchUser = (currentIsMod && !(user.moderator || user.administrator)) || (currentIsAdmin && !user.administrator);
    if(currentUserID == user._id || !canTouchUser) return "";
    return `<div class="actions-ctn" data-user-id="${user._id}">
        <a href="/admin/users/similar/${user._id}" class="btn btn-warning">View Similar</a>
        ${renderAction("ban", user)}
        ${renderAction("mod", user)}
    </div>`
}

$("body").on("click", ".user-action-btn", function() {
    function handleError(data) {
        var error = "An unknown error occurred."
        if(data && typeof data.error !== 'undefined' && data.error.message) error = data.error.message;
        alert("Couldn't perform action on user: " + error);
    }
    var userID = $(this).parent().data("user-id");
    var action = actions[$(this).data("user-action")];
    var originalText = $(this).html();
    $(this).addClass("disabled");
    $(this).html(`<i class="fa fa-circle-o-notch fa-spin"></i> ${originalText}`);
    var elem = $(this);
    $.get(`/api/${action.url}/`, {id: userID}).success(function(data) {
        if(!data.success) return handleError(data);
        action.callback(data, elem);
{'fraction_non_alphanumeric': 0.19325639284906088, 'fraction_numerical': 0.00045259108395564606, 'mean_word_length': 2.992773261065944, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2720973', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1386, 'n_words': 275}