diff --git a/packages/api/src/guilds/getters.spec.ts b/packages/api/src/guilds/getters.spec.ts index 8dc7ff2..2789643 100644 --- a/packages/api/src/guilds/getters.spec.ts +++ b/packages/api/src/guilds/getters.spec.ts @@ -1,19 +1,11 @@ jest.mock('../utils/discord'); -jest.mock('../utils/legacy'); import { CategoryType, Features, Guild, GuildData, RoleSafety } 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, getPickableRoles } from './getters'; const mockDiscordFetch = discordFetch as jest.Mock; -const mockFetchLegacyServer = fetchLegacyServer as jest.Mock; -const mockTransformLegacyGuild = transformLegacyGuild as jest.Mock; beforeEach(() => { mockDiscordFetch.mockReset(); @@ -99,111 +91,6 @@ 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); - }); - - it('fails an import and prevents re-fetch', async () => { - const [config] = configContext(); - - mockFetchLegacyServer.mockReturnValue(null); - - await getGuildData(config, '123'); - await getGuildData(config, '123'); - expect(mockFetchLegacyServer).toHaveBeenCalledTimes(1); - }); - - it('errors gracefully', async () => { - const [config] = configContext(); - - mockFetchLegacyServer.mockImplementationOnce(() => { - throw new Error('test'); - }); - - await getGuildData(config, '123'); - await getGuildData(config, '123'); - expect(mockFetchLegacyServer).toHaveBeenCalledTimes(1); - }); - }); }); describe('getGuildMember', () => { diff --git a/packages/api/src/guilds/getters.ts b/packages/api/src/guilds/getters.ts index 2f68782..1f197f5 100644 --- a/packages/api/src/guilds/getters.ts +++ b/packages/api/src/guilds/getters.ts @@ -7,7 +7,6 @@ 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 { Category, @@ -98,27 +97,6 @@ export const getGuildData = async (config: Config, id: string): Promise => { - try { - 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; - } catch (e) { - console.error('attemptLegacyImport errored:', e); - return null; - } -}; - export const getGuildMember = async ( config: Config, serverID: string, diff --git a/packages/api/src/utils/legacy.ts b/packages/api/src/utils/legacy.ts deleted file mode 100644 index cb175fa..0000000 --- a/packages/api/src/utils/legacy.ts +++ /dev/null @@ -1,69 +0,0 @@ -import { Config } from '@roleypoly/api/src/utils/config'; -import { getID } from '@roleypoly/api/src/utils/id'; -import { sortBy } from '@roleypoly/misc-utils/sortBy'; -import { CategoryType, Features, GuildData } from '@roleypoly/types'; - -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 ( - config: Config, - id: string -): Promise => { - if (!config.importSharedKey) { - return null; - } - - const guildDataResponse = await fetch( - `https://beta.roleypoly.com/x/import-to-next/${id}`, - { - headers: { - authorization: `Shared ${config.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, - auditLogWebhook: null, - accessControl: { - allowList: [], - blockList: [], - blockPending: true, - }, - categories: sortBy(Object.values(guild.categories), 'position').map( - (category, idx) => ({ - ...category, - id: getID(), - position: idx, // Reset positions by index. May have side-effects but oh well. - type: category.type === 'multi' ? CategoryType.Multi : CategoryType.Single, - }) - ), - }; -};