fix(guild): import all template channel types, fetch on desktop (#3473)

This commit is contained in:
Hampus authored and GitHub committed 2026-10-14 18:39:09 +02:00
1 parent 00249f9b4d
commit 99be186fa1
50 files changed
+2008 -111

No files matched your search

@@ -1,12 +1,17 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
import {createTestAccount} from '@app/api/auth/tests/AuthTestUtils';
import {createGuildID} from '@app/api/BrandedTypes';
import {fetchMany} from '@app/api/database/CassandraQueryExecution';
import {getGuildChannels, getRoles} from '@app/api/guild/tests/GuildTestUtils';
import {ChannelsByGuild, ThreadOnlyChannelsByGuild} from '@app/api/Tables';
import {type ApiTestHarness, createApiTestHarness} from '@app/api/test/ApiTestHarness';
import {createBuilder} from '@app/api/test/TestRequestBuilder';
import {ChannelTypes, Permissions} from '@fluxer/constants/src/ChannelConstants';
import {SystemChannelFlags} from '@fluxer/constants/src/GuildConstants';
import {CHANNEL_TOPIC_MAX_LENGTH, VOICE_CHANNEL_USER_LIMIT_MAX} from '@fluxer/constants/src/LimitConstants';
import {ChannelFlags, ForumLayoutTypes, ForumSortOrderTypes} from '@fluxer/constants/src/ThreadConstants';
import type {ChannelResponse} from '@fluxer/schema/src/domains/channel/ChannelSchemas';
import type {GuildResponse} from '@fluxer/schema/src/domains/guild/GuildResponseSchemas';
import {afterAll, beforeAll, beforeEach, describe, expect, test} from 'vitest';
@@ -110,7 +115,7 @@ describe('Guild Template Import', () => {
expect(guild.system_channel_flags).toBe(SystemChannelFlags.SUPPRESS_JOIN_NOTIFICATIONS);
expect(guild.afk_timeout).toBe(60);
});
test('maps supported competitor-only channel types to Fluxer equivalents and ignores unsupported ones', async () => {
test('imports announcement, stage, forum and media channels and ignores unsupported types', async () => {
const account = await createTestAccount(harness);
const guild = await createBuilder<GuildResponse>(harness, account.token)
.post('/guilds')
@@ -155,16 +160,31 @@ describe('Guild Template Import', () => {
},
{
id: '4104',
type: 16,
type: ChannelTypes.GUILD_MEDIA,
name: 'media-feed',
parent_id: '4101',
position: 3,
},
{
id: '4105',
type: ChannelTypes.GUILD_FORUM,
name: 'help',
parent_id: '4101',
position: 4,
rate_limit_per_user: 120,
},
{
id: '4106',
type: 14,
name: 'directory',
parent_id: '4101',
position: 5,
},
],
},
})
.execute();
const channels = await getGuildChannels(harness, account.token, guild.id);
const channels = await listThreadAwareChannels(account.token, guild.id);
const categoryChannel = channels.find((channel) => channel.name === 'Main');
const announcementsChannel = channels.find((channel) => channel.name === 'announcements');
const stageChannel = channels.find((channel) => channel.name === 'town-hall');
@@ -176,7 +196,14 @@ describe('Guild Template Import', () => {
expect(stageChannel).toBeDefined();
expect(stageChannel?.type).toBe(ChannelTypes.GUILD_VOICE);
expect(stageChannel?.parent_id).toBe(categoryChannel?.id ?? null);
expect(mediaChannel).toBeUndefined();
const forumChannel = channels.find((channel) => channel.name === 'help');
expect(mediaChannel?.type).toBe(ChannelTypes.GUILD_MEDIA);
expect(mediaChannel?.parent_id).toBe(categoryChannel?.id ?? null);
expect(mediaChannel?.position).toBe(3);
expect(forumChannel?.type).toBe(ChannelTypes.GUILD_FORUM);
expect(forumChannel?.parent_id).toBe(categoryChannel?.id ?? null);
expect(forumChannel?.rate_limit_per_user).toBe(120);
expect(channels.find((channel) => channel.name === 'directory')).toBeUndefined();
expect(guild.system_channel_id).toBe(announcementsChannel?.id ?? null);
});
test('accepts template roles and channels with empty names', async () => {
@@ -273,7 +300,7 @@ describe('Guild Template Import', () => {
expect(channels.find((channel) => channel.name === 'town-hall')?.user_limit).toBe(VOICE_CHANNEL_USER_LIMIT_MAX);
expect(channels.find((channel) => channel.name === 'lounge')?.voice_connection_limit).toBe(100);
});
test('accepts forum-length topics and shortens them to the channel topic limit', async () => {
test('accepts forum-length topics and shortens them only for non-forum channels', async () => {
const account = await createTestAccount(harness);
const longTopic = `${'a'.repeat(CHANNEL_TOPIC_MAX_LENGTH - 1)}\u{1F600}${'b'.repeat(300)}`;
const guild = await createBuilder<GuildResponse>(harness, account.token)
@@ -288,13 +315,224 @@ describe('Guild Template Import', () => {
}),
})
.execute();
const channels = await getGuildChannels(harness, account.token, guild.id);
const channels = await listThreadAwareChannels(account.token, guild.id);
const general = channels.find((channel) => channel.name === 'general');
expect(general?.topic).toBe('a'.repeat(CHANNEL_TOPIC_MAX_LENGTH - 1));
expect(channels.find((channel) => channel.name === 'projects')).toBeUndefined();
expect(channels.find((channel) => channel.name === 'projects')?.topic).toBe('c'.repeat(1356));
});
test('imports forum settings and drops values that cannot carry over', async () => {
const account = await createTestAccount(harness);
const guild = await createBuilder<GuildResponse>(harness, account.token)
.post('/guilds')
.body({
name: 'Forum Settings Guild',
template: buildMinimalTemplate({
channels: [
{id: 6001, type: ChannelTypes.GUILD_TEXT, name: 'general', position: 0},
{
id: 6002,
type: ChannelTypes.GUILD_FORUM,
name: 'support',
position: 1,
topic: 'Be kind',
available_tags: [
{id: 111, name: 'bug', moderated: false, emoji_id: null, emoji_name: '\u{1F41B}'},
{id: 112, name: 'custom', moderated: false, emoji_id: '123456789', emoji_name: null},
{id: 113, name: 'bug', moderated: true, emoji_id: null, emoji_name: null},
{id: 114, name: 'staff', moderated: true, emoji_id: null, emoji_name: 'not-an-emoji'},
],
default_reaction_emoji: {emoji_id: '987654321', emoji_name: null},
default_sort_order: ForumSortOrderTypes.CREATION_TIME,
default_forum_layout: ForumLayoutTypes.GRID,
default_tag_setting: 'match_all',
default_thread_rate_limit_per_user: 60,
default_auto_archive_duration: 1440,
flags: ChannelFlags.REQUIRE_TAG | ChannelFlags.HIDE_MEDIA_DOWNLOAD_OPTIONS,
},
{
id: 6003,
type: ChannelTypes.GUILD_MEDIA,
name: 'gallery',
position: 2,
available_tags: Array.from({length: 25}, (_, index) => ({name: `tag-${index}`})),
default_reaction_emoji: {emoji_id: null, emoji_name: '\u{1F525}'},
default_forum_layout: ForumLayoutTypes.LIST,
default_sort_order: 7,
flags: ChannelFlags.HIDE_MEDIA_DOWNLOAD_OPTIONS,
},
{
id: 6004,
type: ChannelTypes.GUILD_FORUM,
name: 'announcements-forum',
position: 3,
available_tags: [{name: 'staff', moderated: true}],
flags: ChannelFlags.REQUIRE_TAG,
},
],
}),
})
.execute();
const channels = await listThreadAwareChannels(account.token, guild.id);
const forum = await getThreadAwareChannel(account.token, findChannelId(channels, 'support'));
expect(forum.type).toBe(ChannelTypes.GUILD_FORUM);
expect(forum.topic).toBe('Be kind');
expect(
forum.available_tags?.map(({name, moderated, emoji_id, emoji_name}) => ({name, moderated, emoji_id, emoji_name})),
).toEqual([
{name: 'bug', moderated: false, emoji_id: null, emoji_name: '\u{1F41B}'},
{name: 'custom', moderated: false, emoji_id: null, emoji_name: null},
{name: 'staff', moderated: true, emoji_id: null, emoji_name: null},
]);
expect(new Set(forum.available_tags?.map((tag) => tag.id)).size).toBe(3);
expect(forum.available_tags?.some((tag) => ['111', '112', '113', '114'].includes(tag.id))).toBe(false);
expect(forum.default_reaction_emoji).toBeNull();
expect(forum.default_sort_order).toBe(ForumSortOrderTypes.CREATION_TIME);
expect(forum.default_forum_layout).toBe(ForumLayoutTypes.GRID);
expect(forum.default_tag_setting).toBe('match_all');
expect(forum.default_thread_rate_limit_per_user).toBe(60);
expect(forum.default_auto_archive_duration).toBe(1440);
expect(forum.flags).toBe(ChannelFlags.REQUIRE_TAG);
const media = await getThreadAwareChannel(account.token, findChannelId(channels, 'gallery'));
expect(media.type).toBe(ChannelTypes.GUILD_MEDIA);
expect(media.available_tags).toHaveLength(20);
expect(media.default_reaction_emoji).toEqual({emoji_id: null, emoji_name: '\u{1F525}'});
expect(media.default_forum_layout).toBeUndefined();
expect(media.default_sort_order).toBeNull();
expect(media.flags).toBe(ChannelFlags.HIDE_MEDIA_DOWNLOAD_OPTIONS);
const moderatedOnly = await getThreadAwareChannel(account.token, findChannelId(channels, 'announcements-forum'));
expect(moderatedOnly.flags).toBe(0);
});
test('indexes imported forum and media channels as thread-only channels', async () => {
const account = await createTestAccount(harness);
const guild = await createBuilder<GuildResponse>(harness, account.token)
.post('/guilds')
.body({
name: 'Index Guild',
template: buildMinimalTemplate({
channels: [
{id: 6001, type: ChannelTypes.GUILD_TEXT, name: 'general', position: 0},
{id: 6002, type: ChannelTypes.GUILD_FORUM, name: 'forum', position: 1},
{id: 6003, type: ChannelTypes.GUILD_MEDIA, name: 'media', position: 2},
],
}),
})
.execute();
const channels = await listThreadAwareChannels(account.token, guild.id);
const threadOnlyIds = [findChannelId(channels, 'forum'), findChannelId(channels, 'media')];
const guildId = createGuildID(BigInt(guild.id));
const threadOnlyRows = await fetchMany<{channel_id: bigint}>(
ThreadOnlyChannelsByGuild.selectCql({where: ThreadOnlyChannelsByGuild.where.eq('guild_id')}),
{guild_id: guildId},
);
const regularRows = await fetchMany<{channel_id: bigint}>(
ChannelsByGuild.selectCql({where: ChannelsByGuild.where.eq('guild_id')}),
{guild_id: guildId},
);
expect(threadOnlyRows.map((row) => row.channel_id.toString()).sort()).toEqual([...threadOnlyIds].sort());
expect(regularRows.some((row) => threadOnlyIds.includes(row.channel_id.toString()))).toBe(false);
expect(regularRows.some((row) => row.channel_id.toString() === findChannelId(channels, 'general'))).toBe(true);
});
test('accepts channels shaped like a full template export', async () => {
const account = await createTestAccount(harness);
const exported = {
bitrate: 64000,
user_limit: 0,
nsfw: false,
rate_limit_per_user: 0,
topic: null,
default_auto_archive_duration: null,
permission_overwrites: [{id: 0, type: 0, allow: '0', deny: Permissions.SEND_MESSAGES.toString()}],
available_tags: [],
template: '',
default_reaction_emoji: null,
default_thread_rate_limit_per_user: 0,
default_sort_order: null,
default_forum_layout: 0,
default_tag_setting: null,
icon_emoji: null,
theme_color: null,
};
const guild = await createBuilder<GuildResponse>(harness, account.token)
.post('/guilds')
.body({
name: 'Exported Guild',
template: {
...buildMinimalTemplate({
channels: [
{...exported, id: 1, type: ChannelTypes.GUILD_CATEGORY, name: 'Info', position: 0, parent_id: null},
{...exported, id: 6001, type: ChannelTypes.GUILD_TEXT, name: 'rules', position: 0, parent_id: 1},
{...exported, id: 3, type: ChannelTypes.GUILD_CATEGORY, name: 'Nested', position: 1, parent_id: 1},
{
...exported,
id: 4,
type: ChannelTypes.GUILD_FORUM,
name: 'ideas',
position: 1,
parent_id: 1,
nsfw: null,
},
],
}),
afk_channel_id: null,
preferred_locale: 'en-US',
region: 'us-west',
},
})
.execute();
const channels = await listThreadAwareChannels(account.token, guild.id);
const category = channels.find((channel) => channel.name === 'Info');
const rules = channels.find((channel) => channel.name === 'rules');
expect(rules?.parent_id).toBe(category?.id ?? null);
expect(rules?.permission_overwrites?.find((overwrite) => overwrite.id === guild.id)?.deny).toBe(
Permissions.SEND_MESSAGES.toString(),
);
expect(channels.find((channel) => channel.name === 'Nested')?.parent_id ?? null).toBeNull();
expect(channels.find((channel) => channel.name === 'ideas')?.parent_id).toBe(category?.id ?? null);
expect(guild.system_channel_id).toBe(rules?.id ?? null);
});
test('hides imported forum channels from viewers outside the threads experiment', async () => {
const account = await createTestAccount(harness);
const guild = await createBuilder<GuildResponse>(harness, account.token)
.post('/guilds')
.body({
name: 'Hidden Forum Guild',
template: buildMinimalTemplate({
channels: [
{id: 6001, type: ChannelTypes.GUILD_TEXT, name: 'general', position: 0},
{id: 6002, type: ChannelTypes.GUILD_FORUM, name: 'forum', position: 1},
],
}),
})
.execute();
const legacy = await getGuildChannels(harness, account.token, guild.id);
expect(legacy.some((channel) => channel.name === 'forum')).toBe(false);
expect(legacy.some((channel) => channel.name === 'general')).toBe(true);
});
function listThreadAwareChannels(token: string, guildId: string): Promise<Array<ChannelResponse>> {
return createBuilder<Array<ChannelResponse>>(harness, token)
.get(`/guilds/${guildId}/channels`)
.header(THREAD_FEATURES_HEADER, THREAD_FEATURES_CAPABLE)
.execute();
}
function getThreadAwareChannel(token: string, channelId: string): Promise<ChannelResponse> {
return createBuilder<ChannelResponse>(harness, token)
.get(`/channels/${channelId}`)
.header(THREAD_FEATURES_HEADER, THREAD_FEATURES_CAPABLE)
.execute();
}
});
const THREAD_FEATURES_HEADER = 'X-Fluxer-Features';
const THREAD_FEATURES_CAPABLE = 'channel_threads';
function findChannelId(channels: Array<ChannelResponse>, name: string): string {
const channel = channels.find((candidate) => candidate.name === name);
if (!channel) throw new Error(`Missing channel ${name}`);
return channel.id;
}
const DEFAULT_EVERYONE_PERMISSIONS = Permissions.VIEW_CHANNEL.toString();
function buildMinimalTemplate(overrides: {channels?: Array<object>; roles?: Array<object>}) {