/* GoToSocial Copyright (C) 2021-2022 GoToSocial Authors admin@gotosocial.org 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 . */ "use strict"; const Promise = require("bluebird"); const React = require("react"); const fileDownload = require("js-file-download"); function sortBlocks(blocks) { return blocks.sort((a, b) => { // alphabetical sort return a.domain.localeCompare(b.domain); }); } function deduplicateBlocks(blocks) { let a = new Map(); blocks.forEach((block) => { a.set(block.id, block); }); return Array.from(a.values()); } module.exports = function Blocks({oauth}) { const [blocks, setBlocks] = React.useState([]); const [info, setInfo] = React.useState("Fetching blocks"); const [errorMsg, setError] = React.useState(""); const [checked, setChecked] = React.useState(new Set()); React.useEffect(() => { Promise.try(() => { return oauth.apiRequest("/api/v1/admin/domain_blocks", undefined, undefined, "GET"); }).then((json) => { setInfo(""); setError(""); setBlocks(sortBlocks(json)); }).catch((e) => { setError(e.message); setInfo(""); }); }, []); let blockList = blocks.map((block) => { function update(e) { let newChecked = new Set(checked.values()); if (e.target.checked) { newChecked.add(block.id); } else { newChecked.delete(block.id); } setChecked(newChecked); } return (
{block.domain}
{(new Date(block.created_at)).toLocaleString()}
); }); function clearChecked() { setChecked(new Set()); } function undoChecked() { let amount = checked.size; if(confirm(`Are you sure you want to remove ${amount} block(s)?`)) { setInfo(""); Promise.map(Array.from(checked.values()), (block) => { console.log("deleting", block); return oauth.apiRequest(`/api/v1/admin/domain_blocks/${block}`, "DELETE"); }).then((res) => { console.log(res); setInfo(`Deleted ${amount} blocks: ${res.map((a) => a.domain).join(", ")}`); }).catch((e) => { setError(e); }); let newBlocks = blocks.filter((block) => { if (checked.size > 0 && checked.has(block.id)) { checked.delete(block.id); return false; } else { return true; } }); setBlocks(newBlocks); clearChecked(); } } return (

Blocks

{errorMsg}
{info}

Blocks:

uncheck all
{blockList}
); }; function BulkBlocking({oauth, blocks, setBlocks}) { const [bulk, setBulk] = React.useState(""); const [blockMap, setBlockMap] = React.useState(new Map()); const [output, setOutput] = React.useState(); React.useEffect(() => { let newBlockMap = new Map(); blocks.forEach((block) => { newBlockMap.set(block.domain, block); }); setBlockMap(newBlockMap); }, [blocks]); const fileRef = React.useRef(); function error(e) { setOutput(
{e}
); throw e; } function fileUpload() { let reader = new FileReader(); reader.addEventListener("load", (e) => { try { // TODO: use validatem? let json = JSON.parse(e.target.result); json.forEach((block) => { console.log("block:", block); }); } catch(e) { error(e.message); } }); reader.readAsText(fileRef.current.files[0]); } React.useEffect(() => { if (fileRef && fileRef.current) { fileRef.current.addEventListener("change", fileUpload); } return function cleanup() { fileRef.current.removeEventListener("change", fileUpload); }; }); function textImport() { Promise.try(() => { if (bulk[0] == "[") { // assume it's json return JSON.parse(bulk); } else { return bulk.split("\n").map((val) => { return { domain: val.trim() }; }); } }).then((domains) => { console.log(domains); let before = domains.length; setOutput(`Importing ${before} domain(s)`); domains = domains.filter(({domain}) => { return (domain != "" && !blockMap.has(domain)); }); setOutput({output}
{`Deduplicated ${before - domains.length}/${before} with existing blocks, adding ${domains.length} block(s)`}
); if (domains.length > 0) { let data = new FormData(); data.append("domains", new Blob([JSON.stringify(domains)], {type: "application/json"}), "import.json"); return oauth.apiRequest("/api/v1/admin/domain_blocks?import=true", "POST", data, "form"); } }).then((json) => { console.log("bulk import result:", json); setBlocks(sortBlocks(deduplicateBlocks([...json, ...blocks]))); }).catch((e) => { error(e.message); }); } function textExport() { setBulk(blocks.reduce((str, val) => { if (typeof str == "object") { return str.domain; } else { return str + "\n" + val.domain; } })); } function jsonExport() { Promise.try(() => { return oauth.apiRequest("/api/v1/admin/domain_blocks?export=true", "GET"); }).then((json) => { fileDownload(JSON.stringify(json), "block-export.json"); }).catch((e) => { error(e); }); } function textAreaUpdate(e) { setBulk(e.target.value); } return (

Bulk import/export

{output}
); } function AddBlock({oauth, blocks, setBlocks}) { const [domain, setDomain] = React.useState(""); const [type, setType] = React.useState("suspend"); const [obfuscated, setObfuscated] = React.useState(false); const [privateDescription, setPrivateDescription] = React.useState(""); const [publicDescription, setPublicDescription] = React.useState(""); function addBlock() { console.log(`${type}ing`, domain); Promise.try(() => { return oauth.apiRequest("/api/v1/admin/domain_blocks", "POST", { domain: domain, obfuscate: obfuscated, private_comment: privateDescription, public_comment: publicDescription }, "json"); }).then((json) => { setDomain(""); setPrivateDescription(""); setPublicDescription(""); setBlocks([json, ...blocks]); }); } function onDomainChange(e) { setDomain(e.target.value); } function onTypeChange(e) { setType(e.target.value); } function onKeyDown(e) { if (e.key == "Enter") { addBlock(); } } return (

Add Block:



setObfuscated(e.target.checked)}/>
); } // function Blocklist() { // return ( //
//

Blocklists

//
// ); // }