automatically import from legacy, or die trying.

This commit is contained in:
41666 2022-01-30 03:26:14 -05:00
parent d407a015c9
commit a3691fa112
7 changed files with 193 additions and 35 deletions

View file

@ -1,11 +1,19 @@
jest.mock('../utils/discord');
jest.mock('../utils/legacy');
import { Features, GuildData } from '@roleypoly/types';
import { CategoryType, Features, GuildData } from '@roleypoly/types';
import { APIGuild, discordFetch } from '../utils/discord';
import {
fetchLegacyServer,
LegacyGuildData,
transformLegacyGuild,
} from '../utils/legacy';
import { configContext } from '../utils/testHelpers';
import { getGuild, getGuildData, getGuildMember } from './getters';
const mockDiscordFetch = discordFetch as jest.Mock;
const mockFetchLegacyServer = fetchLegacyServer as jest.Mock;
const mockTransformLegacyGuild = transformLegacyGuild as jest.Mock;
beforeEach(() => {
mockDiscordFetch.mockReset();
@ -91,6 +99,89 @@ describe('getGuildData', () => {
accessControl: expect.any(Object),
});
});
describe('automatic legacy import', () => {
beforeEach(() => {
mockFetchLegacyServer.mockReset();
mockTransformLegacyGuild.mockImplementation(
jest.requireActual('../utils/legacy').transformLegacyGuild
);
});
it('attempts to import guild data from the legacy server', async () => {
const [config] = configContext();
const legacyGuildData: LegacyGuildData = {
id: '123',
message: 'Hello world!',
categories: [
{
id: '123',
name: 'test',
position: 0,
roles: ['role-1', 'role-2'],
hidden: false,
type: 'multi',
},
],
};
mockFetchLegacyServer.mockReturnValue(legacyGuildData);
const expectedGuildData: GuildData = {
id: '123',
message: legacyGuildData.message,
auditLogWebhook: null,
accessControl: {
allowList: [],
blockList: [],
blockPending: true,
},
features: Features.LegacyGuild,
categories: [
{
id: expect.any(String),
name: 'test',
position: 0,
roles: ['role-1', 'role-2'],
hidden: false,
type: CategoryType.Multi,
},
],
};
const currentGuildData = await getGuildData(config, '123');
expect(currentGuildData).toMatchObject(expectedGuildData);
const storedGuildData = await config.kv.guildData.get('123');
expect(storedGuildData).toMatchObject(expectedGuildData);
});
it('fails an import and saves new guild data instead', async () => {
const [config] = configContext();
mockFetchLegacyServer.mockReturnValue(null);
const expectedGuildData: GuildData = {
id: '123',
message: '',
auditLogWebhook: null,
accessControl: {
allowList: [],
blockList: [],
blockPending: true,
},
features: Features.None,
categories: [],
};
const currentGuildData = await getGuildData(config, '123');
expect(currentGuildData).toMatchObject(expectedGuildData);
const storedGuildData = await config.kv.guildData.get('123');
expect(storedGuildData).toMatchObject(expectedGuildData);
});
});
});
describe('getGuildMember', () => {

View file

@ -7,6 +7,7 @@ import {
discordFetch,
getHighestRole,
} from '@roleypoly/api/src/utils/discord';
import { fetchLegacyServer, transformLegacyGuild } from '@roleypoly/api/src/utils/legacy';
import { evaluatePermission, permissions } from '@roleypoly/misc-utils/hasPermission';
import {
Features,
@ -86,6 +87,19 @@ export const getGuildData = async (config: Config, id: string): Promise<GuildDat
};
if (!guildData) {
// It's rare for no guild data to exist while also having a guild.
// It's either an actually new guild... or could be imported.
// Let's attempt the import...
const legacyData = await attemptLegacyImport(config, id);
if (legacyData) {
return {
...empty,
...legacyData,
};
}
// So we don't try again, let's set the data.
await config.kv.guildData.put(id, empty);
return empty;
}
@ -95,6 +109,22 @@ export const getGuildData = async (config: Config, id: string): Promise<GuildDat
};
};
export const attemptLegacyImport = async (
config: Config,
id: string
): Promise<GuildData | null> => {
const legacyGuildData = await fetchLegacyServer(config, id);
if (!legacyGuildData) {
// Means there is no legacy data.
return null;
}
const transformed = transformLegacyGuild(legacyGuildData);
await config.kv.guildData.put(id, transformed);
return transformed;
};
export const getGuildMember = async (
config: Config,
serverID: string,