chore(api): re-add roleypoly creation gated by root

This commit is contained in:
41666 2020-12-18 13:17:49 -05:00
parent ba52f7229d
commit 2976b35505
3 changed files with 135 additions and 80 deletions

View file

@ -1,80 +1,94 @@
import KSUID from 'ksuid'; import KSUID from 'ksuid';
import { CategoryType, Features, GuildData as GuildDataT } from 'roleypoly/common/types'; import { CategoryType, Features, GuildData as GuildDataT } from 'roleypoly/common/types';
import { respond } from '../utils/api-tools'; import { onlyRootUsers, respond } from '../utils/api-tools';
import { GuildData } from '../utils/kv'; import { GuildData } from '../utils/kv';
// Temporary use. // Temporary use.
export const CreateRoleypolyData = async (request: Request): Promise<Response> => { export const CreateRoleypolyData = onlyRootUsers(
const data: GuildDataT = { async (request: Request): Promise<Response> => {
id: '386659935687147521', const data: GuildDataT = {
message: id: '386659935687147521',
'Hey, this is kind of a demo setup so features/use cases can be shown off.\n\nThanks for using Roleypoly <3', message:
features: Features.Preview, 'Hey, this is kind of a demo setup so features/use cases can be shown off.\n\nThanks for using Roleypoly <3',
categories: [ features: Features.Preview,
{ categories: [
id: KSUID.randomSync().string, {
name: 'Demo Roles', id: KSUID.randomSync().string,
type: CategoryType.Multi, name: 'Demo Roles',
hidden: false, type: CategoryType.Multi,
position: 0, hidden: false,
roles: [ position: 0,
'557825026406088717', roles: [
'557824994269200384', '557825026406088717',
'557824893241131029', '557824994269200384',
'557812915386843170', '557824893241131029',
'557812901717737472', '557812915386843170',
'557812805546541066', '557812901717737472',
], '557812805546541066',
}, ],
{ },
id: KSUID.randomSync().string, {
name: 'Colors', id: KSUID.randomSync().string,
type: CategoryType.Single, name: 'Colors',
hidden: false, type: CategoryType.Single,
position: 1, hidden: false,
roles: ['394060232893923349', '394060145799331851', '394060192846839809'], position: 1,
}, roles: [
{ '394060232893923349',
id: KSUID.randomSync().string, '394060145799331851',
name: 'Test Roles', '394060192846839809',
type: CategoryType.Multi, ],
hidden: false, },
position: 5, {
roles: ['558104828216213505', '558103534453653514', '558297233582194728'], id: KSUID.randomSync().string,
}, name: 'Test Roles',
{ type: CategoryType.Multi,
id: KSUID.randomSync().string, hidden: false,
name: 'Region', position: 5,
type: CategoryType.Multi, roles: [
hidden: false, '558104828216213505',
position: 3, '558103534453653514',
roles: [ '558297233582194728',
'397296181803483136', ],
'397296137066774529', },
'397296218809827329', {
'397296267283267605', id: KSUID.randomSync().string,
], name: 'Region',
}, type: CategoryType.Multi,
{ hidden: false,
id: KSUID.randomSync().string, position: 3,
name: 'Opt-in Channels', roles: [
type: CategoryType.Multi, '397296181803483136',
hidden: false, '397296137066774529',
position: 4, '397296218809827329',
roles: ['414514823959674890', '764230661904007219'], '397296267283267605',
}, ],
{ },
id: KSUID.randomSync().string, {
name: 'Pronouns', id: KSUID.randomSync().string,
type: CategoryType.Multi, name: 'Opt-in Channels',
hidden: false, type: CategoryType.Multi,
position: 2, hidden: false,
roles: ['485916566790340608', '485916566941335583', '485916566311927808'], position: 4,
}, roles: ['414514823959674890', '764230661904007219'],
], },
}; {
id: KSUID.randomSync().string,
name: 'Pronouns',
type: CategoryType.Multi,
hidden: false,
position: 2,
roles: [
'485916566790340608',
'485916566941335583',
'485916566311927808',
],
},
],
};
await GuildData.put(data.id, data); await GuildData.put(data.id, data);
return respond({ ok: true }); return respond({ ok: true });
}; }
);

View file

@ -1,4 +1,5 @@
import { BotJoin } from './handlers/bot-join'; import { BotJoin } from './handlers/bot-join';
import { CreateRoleypolyData } from './handlers/create-roleypoly-data';
import { GetPickerData } from './handlers/get-picker-data'; import { GetPickerData } from './handlers/get-picker-data';
import { GetSession } from './handlers/get-session'; import { GetSession } from './handlers/get-session';
import { GetSlug } from './handlers/get-slug'; import { GetSlug } from './handlers/get-slug';
@ -6,20 +7,28 @@ import { LoginBounce } from './handlers/login-bounce';
import { LoginCallback } from './handlers/login-callback'; import { LoginCallback } from './handlers/login-callback';
import { RevokeSession } from './handlers/revoke-session'; import { RevokeSession } from './handlers/revoke-session';
import { Router } from './router'; import { Router } from './router';
import { respond } from './utils/api-tools';
import { uiPublicURI } from './utils/config';
const router = new Router(); const router = new Router();
router.addFallback('root', () => { // OAuth
return new Response('hello!!');
});
router.add('GET', 'bot-join', BotJoin); router.add('GET', 'bot-join', BotJoin);
router.add('GET', 'login-bounce', LoginBounce); router.add('GET', 'login-bounce', LoginBounce);
router.add('GET', 'login-callback', LoginCallback); router.add('GET', 'login-callback', LoginCallback);
router.add('POST', 'revoke-session', RevokeSession);
// Session
router.add('GET', 'get-session', GetSession); router.add('GET', 'get-session', GetSession);
router.add('POST', 'revoke-session', RevokeSession);
// Main biz logic
router.add('GET', 'get-slug', GetSlug); router.add('GET', 'get-slug', GetSlug);
router.add('GET', 'get-picker-data', GetPickerData); router.add('GET', 'get-picker-data', GetPickerData);
// Root users only
router.add('GET', 'x-create-roleypoly-data', CreateRoleypolyData);
// Tester Routes
router.add('GET', 'x-headers', (request) => { router.add('GET', 'x-headers', (request) => {
const headers: { [x: string]: string } = {}; const headers: { [x: string]: string } = {};
@ -29,7 +38,21 @@ router.add('GET', 'x-headers', (request) => {
return new Response(JSON.stringify(headers)); return new Response(JSON.stringify(headers));
}); });
// router.add('GET', 'x-create-roleypoly-data', CreateRoleypolyData);
// Root Zen <3
router.addFallback('root', () => {
return respond({
__warning: '🦊',
this: 'is',
a: 'fox-based',
web: 'application',
please: 'be',
mindful: 'of',
your: 'surroundings',
warning__: '🦊',
meta: uiPublicURI,
});
});
addEventListener('fetch', (event: FetchEvent) => { addEventListener('fetch', (event: FetchEvent) => {
event.respondWith(router.handle(event.request)); event.respondWith(router.handle(event.request));

View file

@ -4,7 +4,7 @@ import {
permissions as Permissions, permissions as Permissions,
} from '../../common/utils/hasPermission'; } from '../../common/utils/hasPermission';
import { Handler } from '../router'; import { Handler } from '../router';
import { uiPublicURI } from './config'; import { rootUsers, uiPublicURI } from './config';
import { Sessions, WrappedKVNamespace } from './kv'; import { Sessions, WrappedKVNamespace } from './kv';
export const formData = (obj: Record<string, any>): string => { export const formData = (obj: Record<string, any>): string => {
@ -147,3 +147,21 @@ export const withSession = (
return await wrappedHandler(session)(request); return await wrappedHandler(session)(request);
}; };
export const isRoot = (userID: string): boolean => rootUsers.includes(userID);
export const onlyRootUsers = (handler: Handler): Handler =>
withSession((session) => (request: Request) => {
if (isRoot(session.user.id)) {
return handler(request);
}
return respond(
{
error: 'not_found',
},
{
status: 404,
}
);
});