Skip to content

Commit

Permalink
refactor: Normalize uri -> domain
Browse files Browse the repository at this point in the history
  • Loading branch information
null2264 committed Nov 1, 2023
1 parent 7fbf1f7 commit cd584ed
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/soapbox/reducers/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,19 @@ const handleAuthFetch = (state: typeof initialState) => {
};
};

const getHost = (instance: { uri: string }) => {
const getHost = (instance: { uri: string | undefined, domain: string }) => {
try {
return new URL(instance.uri).host;
return new URL(instance.uri ?? `https://${instance.domain}`).host;
} catch {
try {
return new URL(`https://${instance.uri}`).host;
return new URL(`https://${instance.uri ?? instance.domain}`).host;
} catch {
return null;
}
}
};

const persistInstance = (instance: { uri: string }) => {
const persistInstance = (instance: { uri: string | undefined, domain: string }) => {
const host = getHost(instance);

if (host) {
Expand Down
2 changes: 2 additions & 0 deletions src/soapbox/schemas/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ const naiveInstanceSchema = coerceObject({
thumbnail: z.string().catch(''),
title: z.string().catch(''),
urls: urlsSchema,
uri: z.string().url().catch(''),
usage: usageSchema,
version: z.string().catch(''),
});
Expand Down Expand Up @@ -192,6 +193,7 @@ const instanceSchema = z.preprocess((data: any) => {
email: email,
},
description: short_description || description,
domain: instance.uri.split('://')[1],
pleroma: {
...pleroma,
metadata: {
Expand Down

0 comments on commit cd584ed

Please sign in to comment.