mirror of
https://github.com/roleypoly/roleypoly.git
synced 2025-06-16 09:39:09 +00:00
feat(api): add /sync-from-legacy route (#192)
* feat(api): add /sync-from-legacy route * chore: remove extraneous dockerfile * chore: remove extraneous dockerfile build * chore: remove extraneous dockerfile build matrix
This commit is contained in:
parent
a983492154
commit
bfc96b0750
13 changed files with 209 additions and 6 deletions
|
@ -12,3 +12,4 @@ export const uiPublicURI = safeURI(env('UI_PUBLIC_URI'));
|
|||
export const apiPublicURI = safeURI(env('API_PUBLIC_URI'));
|
||||
export const rootUsers = list(env('ROOT_USERS'));
|
||||
export const allowedCallbackHosts = list(env('ALLOWED_CALLBACK_HOSTS'));
|
||||
export const importSharedKey = env('BOT_IMPORT_TOKEN');
|
||||
|
|
54
packages/api/utils/import-from-legacy.ts
Normal file
54
packages/api/utils/import-from-legacy.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { sortBy } from '@roleypoly/misc-utils/sortBy';
|
||||
import { CategoryType, Features, GuildData } from '@roleypoly/types';
|
||||
import KSUID from 'ksuid';
|
||||
import { importSharedKey } from './config';
|
||||
|
||||
export type LegacyCategory = {
|
||||
id: string;
|
||||
name: string;
|
||||
roles: string[];
|
||||
hidden: boolean;
|
||||
type: 'single' | 'multi';
|
||||
position: number;
|
||||
};
|
||||
|
||||
export type LegacyGuildData = {
|
||||
id: string;
|
||||
categories: LegacyCategory[];
|
||||
message: string;
|
||||
};
|
||||
|
||||
export const fetchLegacyServer = async (id: string): Promise<LegacyGuildData | null> => {
|
||||
const guildDataResponse = await fetch(
|
||||
`https://beta.roleypoly.com/x/import-to-next/${id}`,
|
||||
{
|
||||
headers: {
|
||||
authorization: `Shared ${importSharedKey}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
if (guildDataResponse.status === 404) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (guildDataResponse.status !== 200) {
|
||||
throw new Error('Guild data fetch failed');
|
||||
}
|
||||
|
||||
return await guildDataResponse.json();
|
||||
};
|
||||
|
||||
export const transformLegacyGuild = (guild: LegacyGuildData): GuildData => {
|
||||
return {
|
||||
id: guild.id,
|
||||
message: guild.message,
|
||||
features: Features.LegacyGuild,
|
||||
categories: sortBy(guild.categories, 'position').map((category, idx) => ({
|
||||
...category,
|
||||
id: KSUID.randomSync().string,
|
||||
position: idx, // Reset positions by index. May have side-effects but oh well.
|
||||
type: category.type === 'multi' ? CategoryType.Multi : CategoryType.Single,
|
||||
})),
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue