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:
41666 2021-03-22 16:54:33 -04:00 committed by GitHub
parent a983492154
commit bfc96b0750
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 209 additions and 6 deletions

View file

@ -0,0 +1,60 @@
import {
Features,
GuildData as GuildDataT,
UserGuildPermissions,
} from '@roleypoly/types';
import { isRoot, respond, withSession } from '../utils/api-tools';
import { fetchLegacyServer, transformLegacyGuild } from '../utils/import-from-legacy';
import { GuildData } from '../utils/kv';
const noPermissions = () =>
respond({ error: 'no permissions to this guild' }, { status: 403 });
const notFound = () => respond({ error: 'guild not found' }, { status: 404 });
const alreadyImported = () =>
respond({ error: 'guild already imported' }, { status: 400 });
export const SyncFromLegacy = withSession(
(session) => async (request: Request): Promise<Response> => {
const url = new URL(request.url);
const [, , guildID] = url.pathname.split('/');
// Allow root users to trigger this too, just in case.
if (!isRoot(session.user.id)) {
const guild = session.guilds.find((guild) => guild.id === guildID);
if (!guild) {
return notFound();
}
if (
guild?.permissionLevel !== UserGuildPermissions.Manager ||
guild?.permissionLevel !== UserGuildPermissions.Admin
) {
return noPermissions();
}
}
const shouldForce = url.searchParams.get('force') === 'yes';
// Not using getGuildData as we want null feedback, not a zeroed out object.
const checkGuild = await GuildData.get<GuildDataT>(guildID);
// Don't force, and guild exists in our side, but LegacyGuild flag is set,
// fail this request.
if (
!shouldForce &&
checkGuild &&
(checkGuild.features & Features.LegacyGuild) === Features.LegacyGuild
) {
return alreadyImported();
}
const legacyGuild = await fetchLegacyServer(guildID);
if (!legacyGuild) {
return notFound();
}
const newGuildData = transformLegacyGuild(legacyGuild);
await GuildData.put(guildID, newGuildData);
return respond({ ok: true });
}
);

View file

@ -6,6 +6,7 @@ import { GetSlug } from './handlers/get-slug';
import { LoginBounce } from './handlers/login-bounce';
import { LoginCallback } from './handlers/login-callback';
import { RevokeSession } from './handlers/revoke-session';
import { SyncFromLegacy } from './handlers/sync-from-legacy';
import { UpdateRoles } from './handlers/update-roles';
import { Router } from './router';
import { respond } from './utils/api-tools';
@ -26,6 +27,7 @@ router.add('POST', 'revoke-session', RevokeSession);
router.add('GET', 'get-slug', GetSlug);
router.add('GET', 'get-picker-data', GetPickerData);
router.add('PATCH', 'update-roles', UpdateRoles);
router.add('POST', 'sync-from-legacy', SyncFromLegacy);
// Root users only
router.add('GET', 'x-create-roleypoly-data', CreateRoleypolyData);

View file

@ -4,7 +4,7 @@
"scripts": {
"build": "yarn workspace @roleypoly/worker-emulator build --basePath `pwd`",
"lint:types": "tsc --noEmit",
"start": "yarn workspace @roleypoly/worker-emulator start --basePath `pwd`"
"start": "cfw-emulator"
},
"devDependencies": {
"@cloudflare/workers-types": "^2.2.1",

View file

@ -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');

View 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,
})),
};
};