fix: domain duplication

This commit is contained in:
diced 2022-08-26 20:35:25 -07:00
parent 2e8bee931c
commit bd055d704b
No known key found for this signature in database
GPG key ID: 370BD1BA142842D1
2 changed files with 14 additions and 28 deletions

View file

@ -24,7 +24,6 @@ export default function Manage() {
const modals = useModals();
const [exports, setExports] = useState([]);
const [domains, setDomains] = useState(user.domains ?? []);
const [file, setFile] = useState<File>(null);
const [fileDataURL, setFileDataURL] = useState(user.avatar ?? null);
@ -116,7 +115,7 @@ export default function Manage() {
embedTitle: user.embedTitle ?? '',
embedColor: user.embedColor,
embedSiteName: user.embedSiteName ?? '',
domains: user.domains ?? [],
domains: user.domains.join(','),
},
});
@ -143,7 +142,7 @@ export default function Manage() {
embedTitle: cleanEmbedTitle === '' ? null : cleanEmbedTitle,
embedColor: cleanEmbedColor === '' ? null : cleanEmbedColor,
embedSiteName: cleanEmbedSiteName === '' ? null : cleanEmbedSiteName,
domains,
domains: values.domains.split(/\s?,\s?/).map(x => x.trim()).filter(x => x !== ''),
};
const newUser = await useFetch('/api/user', 'PATCH', data);
@ -260,18 +259,7 @@ export default function Manage() {
<TextInput id='embedTitle' label='Embed Title' {...form.getInputProps('embedTitle')} />
<ColorInput id='embedColor' label='Embed Color' {...form.getInputProps('embedColor')} />
<TextInput id='embedSiteName' label='Embed Site Name' {...form.getInputProps('embedSiteName')} />
<MultiSelect
id='domains'
label='Domains'
data={domains}
placeholder='Leave blank if you dont want random domain selection.'
creatable
searchable
clearable
getCreateLabel={query => `Add ${query}`}
onCreate={query => setDomains((current) => [...current, query])}
{...form.getInputProps('domains')}
/>
<TextInput id='domains' label='Domains' description='A list of domains separated by commas. These domains will be used to randomly output a domain when uploading. This is optional.' placeholder='https://example.com, https://example2.com' {...form.getInputProps('domains')} />
<Group position='right' mt='md'>
<Button

View file

@ -24,7 +24,7 @@ async function handler(req: NextApiReq, res: NextApiRes) {
},
});
if (existing && user.username !== req.body.username) {
return res.forbid('Username is already taken');
return res.forbid('username is already taken');
}
await prisma.user.update({
where: { id: user.id },
@ -64,27 +64,25 @@ async function handler(req: NextApiReq, res: NextApiRes) {
});
const invalidDomains = [];
const domains = [];
for (const domain of req.body.domains) {
try {
const url = new URL(domain);
url.pathname = '/api/version';
const res = await fetch(url.toString());
if (!res.ok) invalidDomains.push({ domain, reason: 'Got a non OK response' });
else {
const body = await res.json();
if (body?.local !== pkg.version) invalidDomains.push({ domain, reason: 'Version mismatch' });
else await prisma.user.update({
where: { id: user.id },
data: { domains: { push: url.origin } },
});
}
domains.push(url.origin);
} catch (e) {
invalidDomains.push({ domain, reason: e.message });
}
}
if (invalidDomains.length) return res.forbid('Invalid domains', { invalidDomains });
if (invalidDomains.length) return res.forbid('invalid domains', { invalidDomains });
await prisma.user.update({
where: { id: user.id },
data: { domains },
});
return res.json({ domains });
}
const newUser = await prisma.user.findFirst({