<commit_before>/*
 * Copyright (C) 2012-2017  Online-Go.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import {get, put} from "requests";
import data from "data";
import {ignore, errorAlerter} from "misc";
import ITC from "ITC";

let ignores = {};
let block_state = {};

export function setIgnore(player_id: number, tf: boolean) {
    if (tf) {
        ignoreUser(player_id);
    } else {
        unIgnoreUser(player_id);
    }

    if (player_id > 0) {
        if (!(player_id in block_state)) {
            block_state[player_id] = {};
        }
        block_state[player_id].block_chat = tf;
        put("players/" + player_id + "/block", {block_chat: tf ? 1 : 0})
        .then(() => {
            ITC.send("update-blocks", true);
        })
        .catch(errorAlerter);
    }
}
export function setGameBlock(player_id: number, tf: boolean) {
    if (player_id > 0) {
        if (!(player_id in block_state)) {
            block_state[player_id] = {};
        }
        block_state[player_id].block_games = tf;
        put("players/" + player_id + "/block", {block_games: tf ? 1 : 0})
        .then(() => {
            ITC.send("update-blocks", true);
        })
        .catch(errorAlerter);
    }
}

export function getBlocks(player_id: number) {
    return block_state[player_id] || {
        block_chat: false,
        block_games: false,
    };
}

export function player_is_ignored(user_id) {
    return user_id in ignores;
}

function update_blocks() {
    let user = data.get("user");

    if (!user.anonymous) {
        get("me/blocks")
        .then((entries) => {
            block_state = {};
            let new_ignores = {};
            for (let entry of entries) {
                block_state[entry.blocked] = entry;
                if (entry.block_chat) {
                    new_ignores[entry.blocked] = true;
                }
            }

            for (let uid in new_ignores) {
                if (!(uid in ignores)) {
                    ignoreUser(uid);
                }
            }
            for (let uid in ignores) {
                if (!(uid in new_ignores)) {
                    unIgnoreUser(uid);
                }
            }
        })
        .catch(ignore);
    }
}


function ignoreUser(uid) {
    ignores[uid] = true;
    $("<style type='text/css'> .chat-user-" + uid + " { display: none !important; } </style>").appendTo("head");
}
function unIgnoreUser(uid) {
    delete ignores[uid];
    $("<style type='text/css'> .chat-user-" + uid + " { display: block !important; } </style>").appendTo("head");<commit_msg>Don't let folks block mods in chat<commit_after>/*
 * Copyright (C) 2012-2017  Online-Go.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import {get, put} from "requests";
import data from "data";
import {ignore, errorAlerter} from "misc";
import ITC from "ITC";
import player_cache from 'player_cache';

let ignores = {};
let block_state = {};

export function setIgnore(player_id: number, tf: boolean) {
    if (tf) {
        ignoreUser(player_id);
    } else {
        unIgnoreUser(player_id);
    }

    if (player_id > 0) {
        if (!(player_id in block_state)) {
            block_state[player_id] = {};
        }
        block_state[player_id].block_chat = tf;
        put("players/" + player_id + "/block", {block_chat: tf ? 1 : 0})
        .then(() => {
            ITC.send("update-blocks", true);
        })
        .catch(errorAlerter);
    }
}
export function setGameBlock(player_id: number, tf: boolean) {
    if (player_id > 0) {
        if (!(player_id in block_state)) {
            block_state[player_id] = {};
        }
        block_state[player_id].block_games = tf;
        put("players/" + player_id + "/block", {block_games: tf ? 1 : 0})
        .then(() => {
            ITC.send("update-blocks", true);
        })
        .catch(errorAlerter);
    }
}

export function getBlocks(player_id: number) {
    return block_state[player_id] || {
        block_chat: false,
        block_games: false,
    };
}

export function player_is_ignored(user_id) {
    return user_id in ignores;
}

function update_blocks() {
    let user = data.get("user");

    if (!user.anonymous) {
        get("me/blocks")
        .then((entries) => {
            block_state = {};
            let new_ignores = {};
            for (let entry of entries) {
                block_state[entry.blocked] = entry;
                if (entry.block_chat) {
                    new_ignores[entry.blocked] = true;
                }
            }

            for (let uid in new_ignores) {
                if (!(uid in ignores)) {
                    ignoreUser(uid, true);
                }
            }
            for (let uid in ignores) {
                if (!(uid in new_ignores)) {
                    unIgnoreUser(uid);
                }
            }
        })
        .catch(ignore);
    }
}


function ignoreUser(uid, dont_fetch = false) {
    if (dont_fetch) {
        ignores[uid] = true;
        $("<style type='text/css'> .chat-user-" + uid + " { display: none !important; } </style>").appendTo("head");
    }
    else {
        player_cache.fetch(uid, ['ui_class']).then((obj) => {
            if (obj.ui_class.indexOf('moderator') < 0) {
                ignores[uid] = true;
                $("<style type='text/css'> .chat-user-" + uid + " { display: none !important; } </style>").appendTo("head");
            } else {
                console.error("Can't ignore a moderator.");
            }
        });
    }
}
function unIgnoreUser(uid) {
    delete ignores[uid];
    $("<style type='text/css'> .chat-user-" + uid + " { display: block !important; } </style>").appendTo("head");
{'fraction_non_alphanumeric': 0.13123283711519004, 'fraction_numerical': 0.0044804162451221275, 'mean_word_length': 2.1115107913669067, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3301027', 'n_tokens_mistral': 2120, 'n_tokens_neox': 2025, 'n_words': 581}