[bugfix/frontend] Export/import CSV correctly (#2294)

* [bugfix/frontend] Export/import CSV correctly

* export mastodon style
This commit is contained in:
tobi 2023-10-24 18:24:18 +02:00 committed by GitHub
parent cc313f58cb
commit edac3bc361
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 14 deletions

View file

@ -56,12 +56,12 @@ function exportProcess(formData: ExportDomainPermsParams): _exportProcess {
if (formData.exportType == "csv") { if (formData.exportType == "csv") {
return { return {
transformEntry: (entry) => [ transformEntry: (entry) => [
entry.domain, // #domain entry.domain, // domain
"suspend", // #severity "suspend", // severity
false, // #reject_media false, // reject_media
false, // #reject_reports false, // reject_reports
entry.public_comment, // #public_comment entry.public_comment ?? "", // public_comment
entry.obfuscate ?? false // #obfuscate entry.obfuscate ?? false // obfuscate
], ],
stringify: (list) => csvUnparse({ stringify: (list) => csvUnparse({
fields: [ fields: [

View file

@ -27,7 +27,7 @@ import { isValidDomainPermission, hasBetterScope } from "../../../util/domain-pe
import { gtsApi } from "../../gts-api"; import { gtsApi } from "../../gts-api";
import { import {
isDomainPerms, validateDomainPerms,
type DomainPerm, type DomainPerm,
} from "../../../types/domain-permission"; } from "../../../types/domain-permission";
@ -43,19 +43,39 @@ function parseDomainList(list: string): DomainPerm[] {
if (list.startsWith("[")) { if (list.startsWith("[")) {
// Assume JSON array. // Assume JSON array.
const data = JSON.parse(list); const data = JSON.parse(list);
if (!isDomainPerms(data)) {
throw "parsed JSON was not array of DomainPermission"; const validateRes = validateDomainPerms(data);
if (!validateRes.success) {
throw `parsed JSON was not array of DomainPermission: ${JSON.stringify(validateRes.errors)}`;
} }
return data; return data;
} else if (list.startsWith("#domain") || list.startsWith("domain,severity")) { } else if (list.startsWith("#domain") || list.startsWith("domain,severity")) {
// Assume Mastodon-style CSV. // Assume Mastodon-style CSV.
const csvParseCfg: CSVParseConfig = { const csvParseCfg: CSVParseConfig = {
// Key by header.
header: true, header: true,
// Remove leading '#' if present. // Remove leading '#' from headers if present.
transformHeader: (header) => header.startsWith("#") ? header.slice(1) : header, transformHeader: (header) => header.startsWith("#") ? header.slice(1) : header,
// Massage weird boolean values.
transform: (value, _field) => {
if (value == "False" || value == "True") {
return value.toLowerCase();
} else {
return value;
}
},
skipEmptyLines: true, skipEmptyLines: true,
dynamicTyping: true // Only dynamic type boolean values,
// leave the rest as strings.
dynamicTyping: {
"domain": false,
"severity": false,
"reject_media": true,
"reject_reports": true,
"public_comment": false,
"obfuscate": true,
}
}; };
const { data, errors } = csvParse(list, csvParseCfg); const { data, errors } = csvParse(list, csvParseCfg);
@ -67,8 +87,9 @@ function parseDomainList(list: string): DomainPerm[] {
throw error; throw error;
} }
if (!isDomainPerms(data)) { const validateRes = validateDomainPerms(data);
throw "parsed CSV was not array of DomainPermission"; if (!validateRes.success) {
throw `parsed CSV was not array of DomainPermission: ${JSON.stringify(validateRes.errors)}`;
} }
return data; return data;

View file

@ -19,7 +19,7 @@
import typia from "typia"; import typia from "typia";
export const isDomainPerms = typia.createIs<DomainPerm[]>(); export const validateDomainPerms = typia.createValidate<DomainPerm[]>();
export type PermType = "block" | "allow"; export type PermType = "block" | "allow";