Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
5.5 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {createChannelID, createGuildID} from '@app/api/BrandedTypes';
import {GatedJsonValidator} from '@app/api/channel/threads/GatedJsonValidator';
import {viewerActive, viewerFromCtx} from '@app/api/experiment/ChannelThreadsGate';
import {LoginRequired} from '@app/api/middleware/AuthMiddleware';
import {RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';
import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';
import {RateLimitConfigs} from '@app/api/RateLimitConfig';
import type {HonoApp} from '@app/api/types/HonoEnv';
import {Validator} from '@app/api/Validator';
import {THREAD_ONLY_CHANNEL_TYPES} from '@fluxer/constants/src/ThreadConstants';
import {
	ChannelCreateControlRequest,
	ChannelCreateRequest,
	ChannelPositionUpdateRequest,
} from '@fluxer/schema/src/domains/channel/ChannelRequestSchemas';
import {ChannelListResponse, ChannelResponse} from '@fluxer/schema/src/domains/channel/ChannelSchemas';
import {GuildIdParam} from '@fluxer/schema/src/domains/common/CommonParamSchemas';

const THREAD_PARENT_DEFAULT_KEYS = ['default_auto_archive_duration', 'default_thread_rate_limit_per_user'];

function touchesThreadChannelCreate(body: unknown): boolean {
	if (typeof body !== 'object' || body === null) return false;
	const record = body as Record<string, unknown>;
	if (typeof record.type === 'number' && THREAD_ONLY_CHANNEL_TYPES.has(record.type)) return true;
	return THREAD_PARENT_DEFAULT_KEYS.some((key) => record[key] !== undefined);
}

export function GuildChannelController(app: HonoApp) {
	app.get(
		'/guilds/:guild_id/channels',
		RateLimitMiddleware(RateLimitConfigs.GUILD_CHANNELS_LIST),
		LoginRequired,
		Validator('param', GuildIdParam),
		OpenAPI({
			operationId: 'list_guild_channels',
			summary: 'List guild channels',
			responseSchema: ChannelListResponse,
			statusCode: 200,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Guilds'],
			description:
				'List guild channels. Returns all channels in the guild that the user has access to view. Someone visiting the guild gets the channels that @everyone can see, with only the @everyone permission overwrite.',
		}),
		async (ctx) => {
			const userId = ctx.get('user').id;
			const guildId = createGuildID(ctx.req.valid('param').guild_id);
			const requestCache = ctx.get('requestCache');
			const viewer = viewerFromCtx(ctx);
			const visitorChannels = await ctx
				.get('guildVisitService')
				.listChannelsForVisitor({userId, guildId, viewer, requestCache});
			return ctx.json(
				visitorChannels ??
					(await ctx.get('guildService').channels.getChannels({userId, guildId, requestCache, viewer})),
			);
		},
	);
	app.post(
		'/guilds/:guild_id/channels',
		RateLimitMiddleware(RateLimitConfigs.GUILD_CHANNEL_CREATE),
		LoginRequired,
		Validator('param', GuildIdParam),
		GatedJsonValidator(ChannelCreateControlRequest, ChannelCreateRequest, {
			touchesGate: touchesThreadChannelCreate,
			active: (ctx) => viewerActive(viewerFromCtx(ctx), ctx.req.param('guild_id') ?? '0'),
		}),
		OpenAPI({
			operationId: 'create_guild_channel',
			summary: 'Create guild channel',
			requestSchema: ChannelCreateRequest,
			responseSchema: ChannelResponse,
			statusCode: 200,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Guilds'],
			description:
				'Create guild channel. Requires manage_channels permission. Creates a new text, voice, or category channel in the guild.',
		}),
		async (ctx) => {
			const userId = ctx.get('user').id;
			const guildId = createGuildID(ctx.req.valid('param').guild_id);
			const data = ctx.req.valid('json');
			const requestCache = ctx.get('requestCache');
			const auditLogReason = ctx.get('auditLogReason') ?? null;
			return ctx.json(
				await ctx.get('guildService').channels.createChannel(
					{
						userId,
						guildId,
						data,
						requestCache,
						clientFeatures: ctx.get('clientFeatures'),
						viewer: viewerFromCtx(ctx),
					},
					auditLogReason,
				),
			);
		},
	);
	app.patch(
		'/guilds/:guild_id/channels',
		RateLimitMiddleware(RateLimitConfigs.GUILD_CHANNEL_POSITIONS),
		LoginRequired,
		Validator('param', GuildIdParam),
		Validator('json', ChannelPositionUpdateRequest),
		OpenAPI({
			operationId: 'update_guild_channel_positions',
			summary: 'Update channel positions',
			responseSchema: null,
			statusCode: 204,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Guilds'],
			description:
				'Update channel positions. Requires manage_channels permission. Reorders channels and optionally changes parent categories and permission locks.',
		}),
		async (ctx) => {
			const userId = ctx.get('user').id;
			const guildId = createGuildID(ctx.req.valid('param').guild_id);
			const payload = ctx.req.valid('json');
			const requestCache = ctx.get('requestCache');
			const auditLogReason = ctx.get('auditLogReason') ?? null;
			await ctx.get('guildService').channels.updateChannelPositions(
				{
					userId,
					guildId,
					updates: payload.map((item) => ({
						channelId: createChannelID(item.id),
						position: item.position,
						parentId: item.parent_id == null ? item.parent_id : createChannelID(item.parent_id),
						precedingSiblingId:
							item.preceding_sibling_id == null
								? item.preceding_sibling_id
								: createChannelID(item.preceding_sibling_id),
						lockPermissions: item.lock_permissions ?? false,
					})),
					requestCache,
					clientFeatures: ctx.get('clientFeatures'),
					viewer: viewerFromCtx(ctx),
				},
				auditLogReason,
			);
			return ctx.body(null, 204);
		},
	);
}