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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/*
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 React, { useEffect, useMemo } from "react";
import { useLocation, useParams } from "wouter";
import { PermType } from "../../../lib/types/perm";
import { useDeleteHeaderAllowMutation, useDeleteHeaderBlockMutation, useGetHeaderAllowQuery, useGetHeaderBlockQuery } from "../../../lib/query/admin/http-header-permissions";
import { HeaderPermission } from "../../../lib/types/http-header-permissions";
import { FetchBaseQueryError } from "@reduxjs/toolkit/query";
import { SerializedError } from "@reduxjs/toolkit";
import Loading from "../../../components/loading";
import { Error } from "../../../components/error";
import { useLazyGetAccountQuery } from "../../../lib/query/admin";
import Username from "../../../components/username";
import { useBaseUrl } from "../../../lib/navigation/util";
import BackButton from "../../../components/back-button";
import MutationButton from "../../../components/form/mutation-button";
const testString = `/* To test this properly, set "flavor" to "Golang", as that's the language GoToSocial uses for regular expressions */
/* Amazon crawler User-Agent example */
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML\\, like Gecko) Version/8.0.2 Safari/600.2.5 (Amazonbot/0.1; +https://developer.amazon.com/support/amazonbot)
/* Some other test strings */
Some Test Value
Another Test Value`;
export default function HeaderPermDetail() {
let params = useParams();
if (params.permType !== "blocks" && params.permType !== "allows") {
throw "unrecognized perm type " + params.permType;
}
const permType = useMemo(() => {
return params.permType?.slice(0, -1) as PermType;
}, [params]);
let permID = params.permId as string | undefined;
if (!permID) {
throw "no perm ID";
}
if (permType === "block") {
return <BlockDetail id={permID} />;
} else {
return <AllowDetail id={permID} />;
}
}
function BlockDetail({ id }: { id: string }) {
return (
<PermDeets
permType={"Block"}
{...useGetHeaderBlockQuery(id)}
/>
);
}
function AllowDetail({ id }: { id: string }) {
return (
<PermDeets
permType={"Allow"}
{...useGetHeaderAllowQuery(id)}
/>
);
}
interface PermDeetsProps {
permType: string;
data?: HeaderPermission;
isLoading: boolean;
isFetching: boolean;
isError: boolean;
error?: FetchBaseQueryError | SerializedError;
}
function PermDeets({
permType,
data: perm,
isLoading: isLoadingPerm,
isFetching: isFetchingPerm,
isError: isErrorPerm,
error: errorPerm,
}: PermDeetsProps) {
const [ location ] = useLocation();
const baseUrl = useBaseUrl();
// Once we've loaded the perm, trigger
// getting the account that created it.
const [ getAccount, getAccountRes ] = useLazyGetAccountQuery();
useEffect(() => {
if (!perm) {
return;
}
getAccount(perm.created_by, true);
}, [getAccount, perm]);
// Load the createdByAccount if possible,
// returning a username lozenge with
// a link to the account.
const createdByAccount = useMemo(() => {
const {
data: account,
isLoading: isLoadingAccount,
isFetching: isFetchingAccount,
isError: isErrorAccount,
} = getAccountRes;
// Wait for query to finish, returning
// loading spinner in the meantime.
if (isLoadingAccount || isFetchingAccount || !perm) {
return <Loading />;
} else if (isErrorAccount || account === undefined) {
// Fall back to account ID.
return perm?.created_by;
}
return (
<Username
account={account}
linkTo={`~/settings/moderation/accounts/${account.id}`}
backLocation={`~${baseUrl}${location}`}
/>
);
}, [getAccountRes, perm, baseUrl, location]);
// Now wait til the perm itself is loaded.
if (isLoadingPerm || isFetchingPerm) {
return <Loading />;
} else if (isErrorPerm) {
return <Error error={errorPerm} />;
} else if (perm === undefined) {
throw "perm undefined";
}
const created = new Date(perm.created_at).toDateString();
// Create parameters to link to regex101
// with this regular expression prepopulated.
const testParams = new URLSearchParams();
testParams.set("regex", perm.regex);
testParams.set("flags", "gm");
testParams.set("testString", testString);
const regexLink = `https://regex101.com/?${testParams.toString()}`;
return (
<div className="http-header-permission-details">
<h1><BackButton to={`~${baseUrl}/${permType.toLowerCase()}s`} /> HTTP Header {permType} Detail</h1>
<dl className="info-list">
<div className="info-list-entry">
<dt>ID</dt>
<dd className="monospace">{perm.id}</dd>
</div>
<div className="info-list-entry">
<dt>Created</dt>
<dd><time dateTime={perm.created_at}>{created}</time></dd>
</div>
<div className="info-list-entry">
<dt>Created By</dt>
<dd>{createdByAccount}</dd>
</div>
<div className="info-list-entry">
<dt>Header Name</dt>
<dd className="monospace">{perm.header}</dd>
</div>
<div className="info-list-entry">
<dt>Value Regex</dt>
<dd className="monospace">{perm.regex}</dd>
</div>
<div className="info-list-entry">
<dt>Test This Regex</dt>
<dd>
<a
href={regexLink}
target="_blank"
rel="noreferrer"
>
<i className="fa fa-fw fa-external-link" aria-hidden="true"></i> Link to Regex101 (opens in a new tab)
</a>
</dd>
</div>
</dl>
{ permType === "Block"
? <DeleteBlock id={perm.id} />
: <DeleteAllow id={perm.id} />
}
</div>
);
}
function DeleteBlock({ id }: { id: string }) {
const [ _location, setLocation ] = useLocation();
const baseUrl = useBaseUrl();
const [ removeTrigger, removeResult ] = useDeleteHeaderBlockMutation();
return (
<MutationButton
type="button"
onClick={() => {
removeTrigger(id);
setLocation(`~${baseUrl}/blocks`);
}}
label="Remove this block"
result={removeResult}
className="button danger"
showError={false}
disabled={false}
/>
);
}
function DeleteAllow({ id }: { id: string }) {
const [ _location, setLocation ] = useLocation();
const baseUrl = useBaseUrl();
const [ removeTrigger, removeResult ] = useDeleteHeaderAllowMutation();
return (
<MutationButton
type="button"
onClick={() => {
removeTrigger(id);
setLocation(`~${baseUrl}/allows`);
}}
label="Remove this allow"
result={removeResult}
className="button danger"
showError={false}
disabled={false}
/>
);
}
|