summaryrefslogtreecommitdiff
path: root/web/source/settings/lib/query/admin/domain-permissions/export.ts
blob: 868e3f7a4aef0c145ed39a6a760c02bdb8ac7e2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
	GoToSocial
	Copyright (C) GoToSocial Authors admin@gotosocial.org
	SPDX-License-Identifier: AGPL-3.0-or-later

	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 fileDownload from "js-file-download";
import { unparse as csvUnparse } from "papaparse";

import { gtsApi } from "../../gts-api";
import { RootState } from "../../../../redux/store";
import { FetchBaseQueryError } from "@reduxjs/toolkit/query";
import { DomainPerm, ExportDomainPermsParams } from "../../../types/domain-permission";

interface _exportProcess {
	transformEntry: (_entry: DomainPerm) => any;
	stringify: (_list: any[]) => string;
	extension: string;
	mime: string;
}

/**
 * Derive process functions and metadata
 * from provided export request form.
 * 
 * @param formData 
 * @returns 
 */
function exportProcess(formData: ExportDomainPermsParams): _exportProcess {
	if (formData.exportType == "json") {
		return {
			transformEntry: (entry) => ({
				domain: entry.domain,
				public_comment: entry.public_comment,
				obfuscate: entry.obfuscate
			}),
			stringify: (list) => JSON.stringify(list),
			extension: ".json",
			mime: "application/json"
		};
	}
	
	if (formData.exportType == "csv") {
		return {
			transformEntry: (entry) => [
				entry.domain,               // domain
				"suspend",                  // severity
				false,                      // reject_media
				false,                      // reject_reports
				entry.public_comment ?? "", // public_comment
				entry.obfuscate ?? false    // obfuscate
			],
			stringify: (list) => csvUnparse({
				fields: [
					"#domain",
					"#severity",
					"#reject_media",
					"#reject_reports",
					"#public_comment",
					"#obfuscate",
				],
				data: list
			}),
			extension: ".csv",
			mime: "text/csv"
		};
	}

	// Fall back to plain text export.
	return {
		transformEntry: (entry) => entry.domain,
		stringify: (list) => list.join("\n"),
		extension: ".txt",
		mime: "text/plain"
	};
}

const extended = gtsApi.injectEndpoints({
	endpoints: (build) => ({		
		exportDomainList: build.mutation<string | null, ExportDomainPermsParams>({
			async queryFn(formData, api, _extraOpts, fetchWithBQ) {
				// Fetch domain perms from relevant endpoint.
				// We could have used 'useDomainBlocksQuery'
				// or 'useDomainAllowsQuery' for this, but
				// we want the untransformed array version.
				const permsRes = await fetchWithBQ({ url: `/api/v1/admin/domain_${formData.permType}s` });
				if (permsRes.error) {
					return { error: permsRes.error as FetchBaseQueryError };
				}

				// Process perms into desired export format.
				const process = exportProcess(formData); 
				const transformed = (permsRes.data as DomainPerm[]).map(process.transformEntry);
				const exportAsString = process.stringify(transformed);

				if (formData.action == "export") {
					// Data will just be exported
					// to the domains text field.
					return { data: exportAsString };
				}

				// File export has been requested.
				// Parse filename to something like:
				// `example.org-blocklist-2023-10-09.json`.
				const state = api.getState() as RootState;
				const instanceUrl = state.oauth.instanceUrl?? "unknown";
				const domain = new URL(instanceUrl).host;
				const date = new Date();
				const filename = [
					domain,
					"blocklist",
					date.getFullYear(),
					(date.getMonth() + 1).toString().padStart(2, "0"),
					date.getDate().toString().padStart(2, "0"),
				].join("-");

				fileDownload(
					exportAsString,
					filename + process.extension,
					process.mime
				);

				// js-file-download handles the
				// nitty gritty for us, so we can
				// just return null data.
				return { data: null };
			}
		}),
	})
});

/**
 * Makes a GET to `/api/v1/admin/domain_{perm_type}s`
 * and exports the result in the requested format.
 * 
 * Return type will be string if `action` is "export",
 * else it will be null, since the file downloader handles
 * the rest of the request then.
 */
const useExportDomainListMutation = extended.useExportDomainListMutation;

export { useExportDomainListMutation };