initial commit
This commit is contained in:
116
fluxer_api/src/channel/controllers/CallController.ts
Normal file
116
fluxer_api/src/channel/controllers/CallController.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {HonoApp} from '~/App';
|
||||
import {createChannelID, createUserID} from '~/BrandedTypes';
|
||||
import {DefaultUserOnly, LoginRequired} from '~/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '~/middleware/RateLimitMiddleware';
|
||||
import {RateLimitConfigs} from '~/RateLimitConfig';
|
||||
import {createStringType, Int64Type, z} from '~/Schema';
|
||||
import {Validator} from '~/Validator';
|
||||
|
||||
export const CallController = (app: HonoApp) => {
|
||||
app.get(
|
||||
'/channels/:channel_id/call',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_CALL_GET),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const channelService = ctx.get('channelService');
|
||||
|
||||
const {ringable, silent} = await channelService.checkCallEligibility({userId, channelId});
|
||||
|
||||
return ctx.json({ringable, silent});
|
||||
},
|
||||
);
|
||||
|
||||
app.patch(
|
||||
'/channels/:channel_id/call',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_CALL_UPDATE),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
Validator('json', z.object({region: createStringType(1, 64).optional()})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const {region} = ctx.req.valid('json');
|
||||
const channelService = ctx.get('channelService');
|
||||
|
||||
await channelService.updateCall({userId, channelId, region});
|
||||
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/channels/:channel_id/call/ring',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_CALL_RING),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
Validator('json', z.object({recipients: z.array(Int64Type).optional()})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const {recipients} = ctx.req.valid('json');
|
||||
const channelService = ctx.get('channelService');
|
||||
const requestCache = ctx.get('requestCache');
|
||||
|
||||
const recipientIds = recipients ? recipients.map(createUserID) : undefined;
|
||||
|
||||
await channelService.ringCallRecipients({
|
||||
userId,
|
||||
channelId,
|
||||
recipients: recipientIds,
|
||||
requestCache,
|
||||
});
|
||||
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/channels/:channel_id/call/stop-ringing',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_CALL_STOP_RINGING),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
Validator('json', z.object({recipients: z.array(Int64Type).optional()})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const {recipients} = ctx.req.valid('json');
|
||||
const channelService = ctx.get('channelService');
|
||||
|
||||
const recipientIds = recipients ? recipients.map(createUserID) : undefined;
|
||||
|
||||
await channelService.stopRingingCallRecipients({
|
||||
userId,
|
||||
channelId,
|
||||
recipients: recipientIds,
|
||||
});
|
||||
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
};
|
||||
232
fluxer_api/src/channel/controllers/ChannelController.ts
Normal file
232
fluxer_api/src/channel/controllers/ChannelController.ts
Normal file
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {Context} from 'hono';
|
||||
import type {HonoApp, HonoEnv} from '~/App';
|
||||
import {createChannelID, createUserID} from '~/BrandedTypes';
|
||||
import {ChannelTypes} from '~/Constants';
|
||||
import {ChannelUpdateRequest, mapChannelToResponse} from '~/channel/ChannelModel';
|
||||
import {DefaultUserOnly, LoginRequired} from '~/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '~/middleware/RateLimitMiddleware';
|
||||
import {RateLimitConfigs} from '~/RateLimitConfig';
|
||||
import {Int64Type, QueryBooleanType, z} from '~/Schema';
|
||||
import {Validator} from '~/Validator';
|
||||
|
||||
export const ChannelController = (app: HonoApp) => {
|
||||
app.get(
|
||||
'/channels/:channel_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_GET),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
const channel = await ctx.get('channelService').getChannel({userId, channelId});
|
||||
return ctx.json(
|
||||
await mapChannelToResponse({
|
||||
channel,
|
||||
currentUserId: userId,
|
||||
userCacheService: ctx.get('userCacheService'),
|
||||
requestCache,
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/channels/:channel_id/rtc-regions',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_GET),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
|
||||
const regions = await ctx.get('channelService').getAvailableRtcRegions({
|
||||
userId,
|
||||
channelId,
|
||||
});
|
||||
|
||||
return ctx.json(
|
||||
regions.map((region) => ({
|
||||
id: region.id,
|
||||
name: region.name,
|
||||
emoji: region.emoji,
|
||||
})),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.patch(
|
||||
'/channels/:channel_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_UPDATE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
Validator('json', ChannelUpdateRequest, {
|
||||
pre: async (raw: unknown, ctx: Context<HonoEnv>) => {
|
||||
const userId = ctx.get('user').id;
|
||||
// @ts-expect-error not well typed
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const existing = await ctx.get('channelService').getChannel({
|
||||
userId,
|
||||
channelId,
|
||||
});
|
||||
const body = (typeof raw === 'object' && raw !== null ? raw : {}) as Record<string, unknown>;
|
||||
return {...body, type: existing.type};
|
||||
},
|
||||
}),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const data = ctx.req.valid('json');
|
||||
const requestCache = ctx.get('requestCache');
|
||||
const channel = await ctx.get('channelService').editChannel({userId, channelId, data, requestCache});
|
||||
return ctx.json(
|
||||
await mapChannelToResponse({
|
||||
channel,
|
||||
currentUserId: userId,
|
||||
userCacheService: ctx.get('userCacheService'),
|
||||
requestCache,
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_DELETE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
Validator('query', z.object({silent: QueryBooleanType})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const {silent} = ctx.req.valid('query');
|
||||
const requestCache = ctx.get('requestCache');
|
||||
|
||||
const channel = await ctx.get('channelService').getChannel({userId, channelId});
|
||||
if (channel.type === ChannelTypes.GROUP_DM) {
|
||||
await ctx.get('channelService').removeRecipientFromChannel({
|
||||
userId,
|
||||
channelId,
|
||||
recipientId: userId,
|
||||
requestCache,
|
||||
silent,
|
||||
});
|
||||
} else {
|
||||
await ctx.get('channelService').deleteChannel({userId, channelId, requestCache});
|
||||
}
|
||||
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.put(
|
||||
'/channels/:channel_id/recipients/:user_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_UPDATE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, user_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const recipientId = createUserID(ctx.req.valid('param').user_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
|
||||
await ctx.get('channelService').groupDms.addRecipientToChannel({
|
||||
userId,
|
||||
channelId,
|
||||
recipientId,
|
||||
requestCache,
|
||||
});
|
||||
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/recipients/:user_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_UPDATE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, user_id: Int64Type})),
|
||||
Validator('query', z.object({silent: QueryBooleanType})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const recipientId = createUserID(ctx.req.valid('param').user_id);
|
||||
const {silent} = ctx.req.valid('query');
|
||||
const requestCache = ctx.get('requestCache');
|
||||
await ctx
|
||||
.get('channelService')
|
||||
.removeRecipientFromChannel({userId, channelId, recipientId, requestCache, silent});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.put(
|
||||
'/channels/:channel_id/permissions/:overwrite_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_UPDATE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, overwrite_id: Int64Type})),
|
||||
Validator(
|
||||
'json',
|
||||
z.object({
|
||||
type: z.union([z.literal(0), z.literal(1)]),
|
||||
allow: Int64Type.nullish(),
|
||||
deny: Int64Type.nullish(),
|
||||
}),
|
||||
),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const overwriteId = ctx.req.valid('param').overwrite_id;
|
||||
const data = ctx.req.valid('json');
|
||||
const requestCache = ctx.get('requestCache');
|
||||
|
||||
await ctx.get('channelService').setChannelPermissionOverwrite({
|
||||
userId,
|
||||
channelId,
|
||||
overwriteId,
|
||||
overwrite: {
|
||||
type: data.type,
|
||||
allow_: data.allow ? data.allow : 0n,
|
||||
deny_: data.deny ? data.deny : 0n,
|
||||
},
|
||||
requestCache,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/permissions/:overwrite_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_UPDATE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, overwrite_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const overwriteId = ctx.req.valid('param').overwrite_id;
|
||||
const requestCache = ctx.get('requestCache');
|
||||
await ctx.get('channelService').deleteChannelPermissionOverwrite({userId, channelId, overwriteId, requestCache});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
};
|
||||
500
fluxer_api/src/channel/controllers/MessageController.ts
Normal file
500
fluxer_api/src/channel/controllers/MessageController.ts
Normal file
@@ -0,0 +1,500 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {Context} from 'hono';
|
||||
import type {HonoApp, HonoEnv} from '~/App';
|
||||
import {AttachmentDecayService} from '~/attachment/AttachmentDecayService';
|
||||
import type {ChannelID, UserID} from '~/BrandedTypes';
|
||||
import {createAttachmentID, createChannelID, createMessageID} from '~/BrandedTypes';
|
||||
import {MAX_ATTACHMENTS_PER_MESSAGE} from '~/Constants';
|
||||
import {
|
||||
type AttachmentRequestData,
|
||||
type ClientAttachmentReferenceRequest,
|
||||
type ClientAttachmentRequest,
|
||||
mergeUploadWithClientData,
|
||||
type UploadedAttachment,
|
||||
} from '~/channel/AttachmentDTOs';
|
||||
import {MessageRequest, MessageUpdateRequest, mapMessageToResponse} from '~/channel/ChannelModel';
|
||||
import {collectMessageAttachments, isPersonalNotesChannel} from '~/channel/services/message/MessageHelpers';
|
||||
import {InputValidationError, UnclaimedAccountRestrictedError} from '~/Errors';
|
||||
import {DefaultUserOnly, LoginRequired} from '~/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '~/middleware/RateLimitMiddleware';
|
||||
import {RateLimitConfigs} from '~/RateLimitConfig';
|
||||
import {createQueryIntegerType, Int32Type, Int64Type, z} from '~/Schema';
|
||||
import {Validator} from '~/Validator';
|
||||
|
||||
const DEFAULT_ATTACHMENT_UPLOAD_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
||||
|
||||
export interface ParseMultipartMessageDataOptions {
|
||||
uploadExpiresAt?: Date;
|
||||
onPayloadParsed?: (payload: unknown) => void;
|
||||
}
|
||||
|
||||
export async function parseMultipartMessageData(
|
||||
ctx: Context<HonoEnv>,
|
||||
userId: UserID,
|
||||
channelId: ChannelID,
|
||||
schema: z.ZodTypeAny,
|
||||
options?: ParseMultipartMessageDataOptions,
|
||||
): Promise<MessageRequest | MessageUpdateRequest> {
|
||||
let body: Record<string, string | File | Array<string | File>>;
|
||||
try {
|
||||
body = await ctx.req.parseBody();
|
||||
} catch (_error) {
|
||||
throw InputValidationError.create(
|
||||
'multipart_form',
|
||||
'Failed to parse multipart form data. Please check that all field names and filenames are properly formatted.',
|
||||
);
|
||||
}
|
||||
|
||||
if (!body.payload_json || typeof body.payload_json !== 'string') {
|
||||
throw InputValidationError.create('payload_json', 'payload_json field is required for multipart messages');
|
||||
}
|
||||
|
||||
let jsonData: unknown;
|
||||
try {
|
||||
jsonData = JSON.parse(body.payload_json);
|
||||
} catch (_error) {
|
||||
throw InputValidationError.create('payload_json', 'Invalid JSON in payload_json');
|
||||
}
|
||||
|
||||
options?.onPayloadParsed?.(jsonData);
|
||||
const validationResult = schema.safeParse(jsonData);
|
||||
if (!validationResult.success) {
|
||||
throw InputValidationError.create('message_data', 'Invalid message data');
|
||||
}
|
||||
|
||||
const data = validationResult.data as Partial<MessageRequest> &
|
||||
Partial<MessageUpdateRequest> & {
|
||||
attachments?: Array<AttachmentRequestData>;
|
||||
};
|
||||
|
||||
const filesWithIndices: Array<{file: File; index: number}> = [];
|
||||
const seenIndices = new Set<number>();
|
||||
const fieldNamePattern = /^files\[(\d+)\]$/;
|
||||
|
||||
Object.keys(body).forEach((key) => {
|
||||
if (key.startsWith('files[')) {
|
||||
const match = fieldNamePattern.exec(key);
|
||||
if (!match) {
|
||||
throw InputValidationError.create(
|
||||
'files',
|
||||
`Invalid file field name: ${key}. Expected format: files[N] where N is a number`,
|
||||
);
|
||||
}
|
||||
|
||||
const index = parseInt(match[1], 10);
|
||||
|
||||
if (index >= MAX_ATTACHMENTS_PER_MESSAGE) {
|
||||
throw InputValidationError.create(
|
||||
'files',
|
||||
`File index ${index} exceeds maximum allowed index of ${MAX_ATTACHMENTS_PER_MESSAGE - 1}`,
|
||||
);
|
||||
}
|
||||
|
||||
if (seenIndices.has(index)) {
|
||||
throw InputValidationError.create('files', `Duplicate file index: ${index}`);
|
||||
}
|
||||
|
||||
const file = body[key];
|
||||
if (file instanceof File) {
|
||||
filesWithIndices.push({file, index});
|
||||
seenIndices.add(index);
|
||||
} else if (Array.isArray(file)) {
|
||||
const validFiles = file.filter((f) => f instanceof File);
|
||||
if (validFiles.length > 0) {
|
||||
throw InputValidationError.create('files', `Multiple files for index ${index} not allowed`);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (filesWithIndices.length > MAX_ATTACHMENTS_PER_MESSAGE) {
|
||||
throw InputValidationError.create('files', `Too many files. Maximum ${MAX_ATTACHMENTS_PER_MESSAGE} files allowed`);
|
||||
}
|
||||
|
||||
if (filesWithIndices.length > 0) {
|
||||
if (!data.attachments || !Array.isArray(data.attachments) || data.attachments.length === 0) {
|
||||
throw InputValidationError.create('attachments', 'Attachments metadata array is required when uploading files');
|
||||
}
|
||||
|
||||
type AttachmentMetadata = ClientAttachmentRequest | ClientAttachmentReferenceRequest;
|
||||
const attachmentMetadata = data.attachments as Array<AttachmentMetadata>;
|
||||
|
||||
const newAttachments = attachmentMetadata.filter(
|
||||
(a): a is ClientAttachmentRequest => 'filename' in a && a.filename !== undefined,
|
||||
);
|
||||
const existingAttachments = attachmentMetadata.filter(
|
||||
(a): a is ClientAttachmentReferenceRequest => !('filename' in a) || a.filename === undefined,
|
||||
);
|
||||
|
||||
const metadataIds = new Set(newAttachments.map((a) => a.id));
|
||||
const fileIds = new Set(filesWithIndices.map((f) => f.index));
|
||||
|
||||
for (const fileId of fileIds) {
|
||||
if (!metadataIds.has(fileId)) {
|
||||
throw InputValidationError.create('attachments', `No metadata provided for file with ID ${fileId}`);
|
||||
}
|
||||
}
|
||||
|
||||
for (const att of newAttachments) {
|
||||
if (!fileIds.has(att.id)) {
|
||||
throw InputValidationError.create('attachments', `No file uploaded for attachment metadata with ID ${att.id}`);
|
||||
}
|
||||
}
|
||||
|
||||
const uploadExpiresAt = options?.uploadExpiresAt ?? new Date(Date.now() + DEFAULT_ATTACHMENT_UPLOAD_TTL_MS);
|
||||
|
||||
const uploadedAttachments: Array<UploadedAttachment> = await ctx.get('channelService').uploadFormDataAttachments({
|
||||
userId,
|
||||
channelId,
|
||||
files: filesWithIndices,
|
||||
attachmentMetadata: newAttachments,
|
||||
expiresAt: uploadExpiresAt,
|
||||
});
|
||||
|
||||
const uploadedMap = new Map(uploadedAttachments.map((u) => [u.id, u]));
|
||||
|
||||
const processedNewAttachments = newAttachments.map((clientData) => {
|
||||
const uploaded = uploadedMap.get(clientData.id);
|
||||
if (!uploaded) {
|
||||
throw InputValidationError.create('attachments', `No file uploaded for attachment with ID ${clientData.id}`);
|
||||
}
|
||||
|
||||
if (clientData.filename !== uploaded.filename) {
|
||||
throw InputValidationError.create(
|
||||
'attachments',
|
||||
`Filename mismatch for attachment ${clientData.id}: metadata specifies "${clientData.filename}" but this doesn't match`,
|
||||
);
|
||||
}
|
||||
|
||||
return mergeUploadWithClientData(uploaded, clientData);
|
||||
});
|
||||
|
||||
data.attachments = [...existingAttachments, ...processedNewAttachments];
|
||||
} else if (
|
||||
data.attachments?.some((a: unknown) => {
|
||||
const attachment = a as ClientAttachmentRequest | ClientAttachmentReferenceRequest;
|
||||
return 'filename' in attachment && attachment.filename;
|
||||
})
|
||||
) {
|
||||
throw InputValidationError.create(
|
||||
'attachments',
|
||||
'Attachment metadata with filename provided but no files uploaded',
|
||||
);
|
||||
}
|
||||
|
||||
return data as MessageRequest | MessageUpdateRequest;
|
||||
}
|
||||
|
||||
export const MessageController = (app: HonoApp) => {
|
||||
const decayService = new AttachmentDecayService();
|
||||
|
||||
app.get(
|
||||
'/channels/:channel_id/messages',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGES_GET),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
Validator(
|
||||
'query',
|
||||
z.object({
|
||||
limit: createQueryIntegerType({defaultValue: 50, minValue: 1, maxValue: 100}),
|
||||
before: z.optional(Int64Type),
|
||||
after: z.optional(Int64Type),
|
||||
around: z.optional(Int64Type),
|
||||
}),
|
||||
),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const {limit, before, after, around} = ctx.req.valid('query');
|
||||
const requestCache = ctx.get('requestCache');
|
||||
const messages = await ctx.get('channelService').getMessages({
|
||||
userId,
|
||||
channelId,
|
||||
limit,
|
||||
before: before ? createMessageID(before) : undefined,
|
||||
after: after ? createMessageID(after) : undefined,
|
||||
around: around ? createMessageID(around) : undefined,
|
||||
});
|
||||
const allAttachments = messages.flatMap((message) => collectMessageAttachments(message));
|
||||
const attachmentDecayMap =
|
||||
allAttachments.length > 0
|
||||
? await decayService.fetchMetadata(allAttachments.map((att) => ({attachmentId: att.id})))
|
||||
: undefined;
|
||||
return ctx.json(
|
||||
await Promise.all(
|
||||
messages.map((message) =>
|
||||
mapMessageToResponse({
|
||||
message,
|
||||
currentUserId: userId,
|
||||
userCacheService: ctx.get('userCacheService'),
|
||||
requestCache,
|
||||
mediaService: ctx.get('mediaService'),
|
||||
attachmentDecayMap,
|
||||
getReactions: (channelId, messageId) =>
|
||||
ctx.get('channelService').getMessageReactions({userId, channelId, messageId}),
|
||||
setHasReaction: (channelId, messageId, hasReaction) =>
|
||||
ctx.get('channelService').setHasReaction(channelId, messageId, hasReaction),
|
||||
getReferencedMessage: (channelId, messageId) =>
|
||||
ctx.get('channelRepository').getMessage(channelId, messageId),
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/channels/:channel_id/messages/:message_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGE_GET),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, message_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
const message = await ctx.get('channelService').getMessage({userId, channelId, messageId});
|
||||
const messageAttachments = collectMessageAttachments(message);
|
||||
const attachmentDecayMap =
|
||||
messageAttachments.length > 0
|
||||
? await decayService.fetchMetadata(messageAttachments.map((att) => ({attachmentId: att.id})))
|
||||
: undefined;
|
||||
return ctx.json(
|
||||
await mapMessageToResponse({
|
||||
message,
|
||||
currentUserId: userId,
|
||||
userCacheService: ctx.get('userCacheService'),
|
||||
requestCache,
|
||||
mediaService: ctx.get('mediaService'),
|
||||
attachmentDecayMap,
|
||||
getReactions: (channelId, messageId) =>
|
||||
ctx.get('channelService').getMessageReactions({userId, channelId, messageId}),
|
||||
setHasReaction: (channelId, messageId, hasReaction) =>
|
||||
ctx.get('channelService').setHasReaction(channelId, messageId, hasReaction),
|
||||
getReferencedMessage: (channelId, messageId) => ctx.get('channelRepository').getMessage(channelId, messageId),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/channels/:channel_id/messages',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGE_CREATE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const user = ctx.get('user');
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
|
||||
if (!user.passwordHash && !isPersonalNotesChannel({userId: user.id, channelId})) {
|
||||
throw new UnclaimedAccountRestrictedError('send messages');
|
||||
}
|
||||
|
||||
const contentType = ctx.req.header('content-type');
|
||||
const validatedData = contentType?.includes('multipart/form-data')
|
||||
? ((await parseMultipartMessageData(ctx, user.id, channelId, MessageRequest)) as MessageRequest)
|
||||
: await (async () => {
|
||||
const data: unknown = await ctx.req.json();
|
||||
const validationResult = MessageRequest.safeParse(data);
|
||||
if (!validationResult.success) {
|
||||
throw InputValidationError.create('message_data', 'Invalid message data');
|
||||
}
|
||||
return validationResult.data;
|
||||
})();
|
||||
const message = await ctx
|
||||
.get('channelService')
|
||||
.sendMessage({user, channelId, data: validatedData as MessageRequest, requestCache});
|
||||
const messageAttachments = collectMessageAttachments(message);
|
||||
const attachmentDecayMap =
|
||||
messageAttachments.length > 0
|
||||
? await decayService.fetchMetadata(messageAttachments.map((att) => ({attachmentId: att.id})))
|
||||
: undefined;
|
||||
return ctx.json(
|
||||
await mapMessageToResponse({
|
||||
message,
|
||||
currentUserId: user.id,
|
||||
nonce: validatedData.nonce,
|
||||
userCacheService: ctx.get('userCacheService'),
|
||||
requestCache,
|
||||
mediaService: ctx.get('mediaService'),
|
||||
attachmentDecayMap,
|
||||
getReferencedMessage: (channelId, messageId) => ctx.get('channelRepository').getMessage(channelId, messageId),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.patch(
|
||||
'/channels/:channel_id/messages/:message_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGE_UPDATE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, message_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
|
||||
const contentType = ctx.req.header('content-type');
|
||||
const validatedData = contentType?.includes('multipart/form-data')
|
||||
? ((await parseMultipartMessageData(ctx, userId, channelId, MessageUpdateRequest)) as MessageUpdateRequest)
|
||||
: await (async () => {
|
||||
const data: unknown = await ctx.req.json();
|
||||
const validationResult = MessageUpdateRequest.safeParse(data);
|
||||
if (!validationResult.success) {
|
||||
throw InputValidationError.create('message_data', 'Invalid message data');
|
||||
}
|
||||
return validationResult.data;
|
||||
})();
|
||||
const message = await ctx.get('channelService').editMessage({
|
||||
userId,
|
||||
channelId,
|
||||
messageId,
|
||||
data: validatedData as MessageUpdateRequest,
|
||||
requestCache,
|
||||
});
|
||||
const messageAttachments = collectMessageAttachments(message);
|
||||
const attachmentDecayMap =
|
||||
messageAttachments.length > 0
|
||||
? await decayService.fetchMetadata(messageAttachments.map((att) => ({attachmentId: att.id})))
|
||||
: undefined;
|
||||
return ctx.json(
|
||||
await mapMessageToResponse({
|
||||
message,
|
||||
currentUserId: userId,
|
||||
userCacheService: ctx.get('userCacheService'),
|
||||
requestCache,
|
||||
mediaService: ctx.get('mediaService'),
|
||||
attachmentDecayMap,
|
||||
getReferencedMessage: (channelId, messageId) => ctx.get('channelRepository').getMessage(channelId, messageId),
|
||||
}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/messages/ack',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_READ_STATE_DELETE),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
await ctx.get('channelService').deleteReadState({userId, channelId});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/messages/:message_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGE_DELETE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, message_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
await ctx.get('channelService').deleteMessage({userId, channelId, messageId, requestCache});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/messages/:message_id/attachments/:attachment_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGE_DELETE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, message_id: Int64Type, attachment_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id, attachment_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const attachmentId = createAttachmentID(attachment_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
await ctx.get('channelService').deleteAttachment({
|
||||
userId,
|
||||
channelId,
|
||||
messageId: messageId,
|
||||
attachmentId,
|
||||
requestCache,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/channels/:channel_id/messages/bulk-delete',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGE_BULK_DELETE),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
Validator('json', z.object({message_ids: z.array(Int64Type)})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const messageIds = ctx.req.valid('json').message_ids.map(createMessageID);
|
||||
await ctx.get('channelService').bulkDeleteMessages({userId, channelId, messageIds});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/channels/:channel_id/typing',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_TYPING),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
await ctx.get('channelService').startTyping({userId, channelId});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/channels/:channel_id/messages/:message_id/ack',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGE_ACK),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', z.object({channel_id: Int64Type, message_id: Int64Type})),
|
||||
Validator('json', z.object({mention_count: Int32Type.optional(), manual: z.optional(z.boolean())})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const {mention_count: mentionCount, manual} = ctx.req.valid('json');
|
||||
await ctx.get('channelService').ackMessage({
|
||||
userId,
|
||||
channelId,
|
||||
messageId,
|
||||
mentionCount: mentionCount ?? 0,
|
||||
manual,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {HonoApp} from '~/App';
|
||||
import {createChannelID, createMessageID, createUserID} from '~/BrandedTypes';
|
||||
import {isPersonalNotesChannel} from '~/channel/services/message/MessageHelpers';
|
||||
import {UnclaimedAccountRestrictedError} from '~/Errors';
|
||||
import {LoginRequired} from '~/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '~/middleware/RateLimitMiddleware';
|
||||
import {RateLimitConfigs} from '~/RateLimitConfig';
|
||||
import {createStringType, Int64Type, z} from '~/Schema';
|
||||
import {Validator} from '~/Validator';
|
||||
export const MessageInteractionController = (app: HonoApp) => {
|
||||
app.get(
|
||||
'/channels/:channel_id/messages/pins',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_PINS),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
Validator(
|
||||
'query',
|
||||
z.object({
|
||||
limit: z.coerce.number().int().min(1).max(50).optional(),
|
||||
before: z.coerce.date().optional(),
|
||||
}),
|
||||
),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
const {limit, before} = ctx.req.valid('query');
|
||||
return ctx.json(
|
||||
await ctx
|
||||
.get('channelService')
|
||||
.getChannelPins({userId, channelId, requestCache, limit, beforeTimestamp: before}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/channels/:channel_id/pins/ack',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_PINS),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const {channel} = await ctx.get('channelService').getChannelAuthenticated({userId, channelId});
|
||||
const timestamp = channel.lastPinTimestamp;
|
||||
if (timestamp != null) {
|
||||
await ctx.get('channelService').ackPins({userId, channelId, timestamp});
|
||||
}
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.put(
|
||||
'/channels/:channel_id/pins/:message_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_PINS),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, message_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
await ctx.get('channelService').pinMessage({
|
||||
userId,
|
||||
channelId,
|
||||
messageId,
|
||||
requestCache,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/pins/:message_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_PINS),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, message_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const requestCache = ctx.get('requestCache');
|
||||
await ctx.get('channelService').unpinMessage({
|
||||
userId,
|
||||
channelId,
|
||||
messageId,
|
||||
requestCache,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/channels/:channel_id/messages/:message_id/reactions/:emoji',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_REACTIONS),
|
||||
LoginRequired,
|
||||
Validator(
|
||||
'param',
|
||||
z.object({
|
||||
channel_id: Int64Type,
|
||||
message_id: Int64Type,
|
||||
emoji: createStringType(1, 64),
|
||||
}),
|
||||
),
|
||||
Validator(
|
||||
'query',
|
||||
z.object({
|
||||
limit: z.coerce.number().int().min(1).max(100).optional(),
|
||||
after: Int64Type.optional(),
|
||||
}),
|
||||
),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id, emoji} = ctx.req.valid('param');
|
||||
const {limit, after} = ctx.req.valid('query');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const afterUserId = after ? createUserID(after) : undefined;
|
||||
return ctx.json(
|
||||
await ctx
|
||||
.get('channelService')
|
||||
.getUsersForReaction({userId, channelId, messageId, emoji, limit, after: afterUserId}),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
app.put(
|
||||
'/channels/:channel_id/messages/:message_id/reactions/:emoji/@me',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_REACTIONS),
|
||||
LoginRequired,
|
||||
Validator(
|
||||
'param',
|
||||
z.object({
|
||||
channel_id: Int64Type,
|
||||
message_id: Int64Type,
|
||||
emoji: createStringType(1, 64),
|
||||
}),
|
||||
),
|
||||
Validator('query', z.object({session_id: z.optional(createStringType(1, 64))})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id, emoji} = ctx.req.valid('param');
|
||||
const user = ctx.get('user');
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const sessionId = ctx.req.valid('query').session_id;
|
||||
const requestCache = ctx.get('requestCache');
|
||||
|
||||
if (!user.passwordHash && !isPersonalNotesChannel({userId: user.id, channelId})) {
|
||||
throw new UnclaimedAccountRestrictedError('add reactions');
|
||||
}
|
||||
|
||||
await ctx.get('channelService').addReaction({
|
||||
userId: user.id,
|
||||
sessionId,
|
||||
channelId,
|
||||
messageId,
|
||||
emoji,
|
||||
requestCache,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/messages/:message_id/reactions/:emoji/@me',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_REACTIONS),
|
||||
LoginRequired,
|
||||
Validator(
|
||||
'param',
|
||||
z.object({
|
||||
channel_id: Int64Type,
|
||||
message_id: Int64Type,
|
||||
emoji: createStringType(1, 64),
|
||||
}),
|
||||
),
|
||||
Validator('query', z.object({session_id: z.optional(createStringType(1, 64))})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id, emoji} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const sessionId = ctx.req.valid('query').session_id;
|
||||
const requestCache = ctx.get('requestCache');
|
||||
await ctx.get('channelService').removeOwnReaction({
|
||||
userId,
|
||||
sessionId,
|
||||
channelId,
|
||||
messageId,
|
||||
emoji,
|
||||
requestCache,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/messages/:message_id/reactions/:emoji/:target_id',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_REACTIONS),
|
||||
LoginRequired,
|
||||
Validator(
|
||||
'param',
|
||||
z.object({
|
||||
channel_id: Int64Type,
|
||||
message_id: Int64Type,
|
||||
emoji: createStringType(1, 64),
|
||||
target_id: Int64Type,
|
||||
}),
|
||||
),
|
||||
Validator('query', z.object({session_id: z.optional(createStringType(1, 64))})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id, emoji, target_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const targetId = createUserID(target_id);
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
const sessionId = ctx.req.valid('query').session_id;
|
||||
const requestCache = ctx.get('requestCache');
|
||||
await ctx.get('channelService').removeReaction({
|
||||
userId,
|
||||
sessionId,
|
||||
channelId,
|
||||
messageId,
|
||||
emoji,
|
||||
targetId,
|
||||
requestCache,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/messages/:message_id/reactions/:emoji',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_REACTIONS),
|
||||
LoginRequired,
|
||||
Validator(
|
||||
'param',
|
||||
z.object({
|
||||
channel_id: Int64Type,
|
||||
message_id: Int64Type,
|
||||
emoji: createStringType(1, 64),
|
||||
}),
|
||||
),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id, emoji} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
await ctx.get('channelService').removeAllReactionsForEmoji({userId, channelId, messageId, emoji});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.delete(
|
||||
'/channels/:channel_id/messages/:message_id/reactions',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_REACTIONS),
|
||||
LoginRequired,
|
||||
Validator('param', z.object({channel_id: Int64Type, message_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const {channel_id, message_id} = ctx.req.valid('param');
|
||||
const userId = ctx.get('user').id;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const messageId = createMessageID(message_id);
|
||||
await ctx.get('channelService').removeAllReactions({userId, channelId, messageId});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {HonoApp} from '~/App';
|
||||
import {createChannelID} from '~/BrandedTypes';
|
||||
import {DefaultUserOnly, LoginRequired} from '~/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '~/middleware/RateLimitMiddleware';
|
||||
import {RateLimitConfigs} from '~/RateLimitConfig';
|
||||
import {Int64Type, z} from '~/Schema';
|
||||
import {Validator} from '~/Validator';
|
||||
import {parseScheduledMessageInput} from './ScheduledMessageParsing';
|
||||
|
||||
export const ScheduledMessageController = (app: HonoApp) => {
|
||||
app.post(
|
||||
'/channels/:channel_id/messages/schedule',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_MESSAGE_CREATE),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', z.object({channel_id: Int64Type})),
|
||||
async (ctx) => {
|
||||
const user = ctx.get('user');
|
||||
const channelId = createChannelID(ctx.req.valid('param').channel_id);
|
||||
const scheduledMessageService = ctx.get('scheduledMessageService');
|
||||
|
||||
const {message, scheduledLocalAt, timezone} = await parseScheduledMessageInput({
|
||||
ctx,
|
||||
userId: user.id,
|
||||
channelId,
|
||||
});
|
||||
|
||||
const scheduledMessage = await scheduledMessageService.createScheduledMessage({
|
||||
user,
|
||||
channelId,
|
||||
data: message,
|
||||
scheduledLocalAt,
|
||||
timezone,
|
||||
});
|
||||
|
||||
return ctx.json(scheduledMessage.toResponse(), 201);
|
||||
},
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {Context} from 'hono';
|
||||
import type {HonoEnv} from '~/App';
|
||||
import type {ChannelID, UserID} from '~/BrandedTypes';
|
||||
import type {MessageRequest} from '~/channel/ChannelModel';
|
||||
import {MessageRequest as MessageRequestSchema} from '~/channel/ChannelModel';
|
||||
import {InputValidationError} from '~/Errors';
|
||||
import {createStringType, type z} from '~/Schema';
|
||||
import {parseMultipartMessageData} from './MessageController';
|
||||
|
||||
export const SCHEDULED_ATTACHMENT_TTL_MS = 32 * 24 * 60 * 60 * 1000;
|
||||
|
||||
export const ScheduledMessageSchema = MessageRequestSchema.extend({
|
||||
scheduled_local_at: createStringType(1, 64),
|
||||
timezone: createStringType(1, 128),
|
||||
});
|
||||
|
||||
export type ScheduledMessageSchemaType = z.infer<typeof ScheduledMessageSchema>;
|
||||
|
||||
export function extractScheduleFields(data: ScheduledMessageSchemaType): {
|
||||
scheduled_local_at: string;
|
||||
timezone: string;
|
||||
message: MessageRequest;
|
||||
} {
|
||||
const {scheduled_local_at, timezone, ...messageData} = data;
|
||||
return {
|
||||
scheduled_local_at,
|
||||
timezone,
|
||||
message: messageData as MessageRequest,
|
||||
};
|
||||
}
|
||||
|
||||
export async function parseScheduledMessageInput({
|
||||
ctx,
|
||||
userId,
|
||||
channelId,
|
||||
}: {
|
||||
ctx: Context<HonoEnv>;
|
||||
userId: UserID;
|
||||
channelId: ChannelID;
|
||||
}): Promise<{message: MessageRequest; scheduledLocalAt: string; timezone: string}> {
|
||||
const contentType = ctx.req.header('content-type') ?? '';
|
||||
const isMultipart = contentType.includes('multipart/form-data');
|
||||
|
||||
if (isMultipart) {
|
||||
let parsedPayload: unknown = null;
|
||||
const message = (await parseMultipartMessageData(ctx, userId, channelId, MessageRequestSchema, {
|
||||
uploadExpiresAt: new Date(Date.now() + SCHEDULED_ATTACHMENT_TTL_MS),
|
||||
onPayloadParsed(payload) {
|
||||
parsedPayload = payload;
|
||||
},
|
||||
})) as MessageRequest;
|
||||
|
||||
if (!parsedPayload) {
|
||||
throw InputValidationError.create('scheduled_message', 'Failed to parse multipart payload');
|
||||
}
|
||||
|
||||
const validation = ScheduledMessageSchema.safeParse(parsedPayload);
|
||||
if (!validation.success) {
|
||||
throw InputValidationError.create('scheduled_message', 'Invalid scheduled message payload');
|
||||
}
|
||||
|
||||
const {scheduled_local_at, timezone} = extractScheduleFields(validation.data);
|
||||
return {message, scheduledLocalAt: scheduled_local_at, timezone};
|
||||
}
|
||||
|
||||
const body: unknown = await ctx.req.json();
|
||||
const validation = ScheduledMessageSchema.safeParse(body);
|
||||
if (!validation.success) {
|
||||
throw InputValidationError.create('scheduled_message', 'Invalid scheduled message payload');
|
||||
}
|
||||
|
||||
const {scheduled_local_at, timezone, message} = extractScheduleFields(validation.data);
|
||||
return {message, scheduledLocalAt: scheduled_local_at, timezone};
|
||||
}
|
||||
162
fluxer_api/src/channel/controllers/StreamController.ts
Normal file
162
fluxer_api/src/channel/controllers/StreamController.ts
Normal file
@@ -0,0 +1,162 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {HonoApp} from '~/App';
|
||||
import {createChannelID} from '~/BrandedTypes';
|
||||
import {APIErrorCodes} from '~/constants/API';
|
||||
import {Permissions} from '~/constants/Channel';
|
||||
import {BadRequestError, MissingPermissionsError} from '~/Errors';
|
||||
import {DefaultUserOnly, LoginRequired} from '~/middleware/AuthMiddleware';
|
||||
import {RateLimitMiddleware} from '~/middleware/RateLimitMiddleware';
|
||||
import {RateLimitConfigs} from '~/RateLimitConfig';
|
||||
import {createStringType, Int64Type, z} from '~/Schema';
|
||||
import {Validator} from '~/Validator';
|
||||
|
||||
const streamKeyParam = z.object({stream_key: createStringType(1, 256)});
|
||||
|
||||
const parseStreamKey = (
|
||||
streamKey: string,
|
||||
): {scope: 'guild' | 'dm'; guildId?: string; channelId: string; connectionId: string} | null => {
|
||||
const parts = streamKey.split(':');
|
||||
if (parts.length !== 3) return null;
|
||||
const [scopeRaw, channelId, connectionId] = parts;
|
||||
if (!channelId || !connectionId) return null;
|
||||
if (scopeRaw === 'dm') {
|
||||
return {scope: 'dm', channelId, connectionId};
|
||||
}
|
||||
if (!/^[0-9]+$/.test(scopeRaw) || !/^[0-9]+$/.test(channelId)) return null;
|
||||
return {scope: 'guild', guildId: scopeRaw, channelId, connectionId};
|
||||
};
|
||||
|
||||
export const StreamController = (app: HonoApp) => {
|
||||
app.patch(
|
||||
'/streams/:stream_key/stream',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_STREAM_UPDATE),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('json', z.object({region: createStringType(1, 64).optional()})),
|
||||
Validator('param', streamKeyParam),
|
||||
async (ctx) => {
|
||||
const {region} = ctx.req.valid('json');
|
||||
const streamKey = ctx.req.valid('param').stream_key;
|
||||
await ctx.get('cacheService').set(`stream_region:${streamKey}`, {region, updatedAt: Date.now()}, 60 * 60 * 24);
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
|
||||
app.get(
|
||||
'/streams/:stream_key/preview',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_STREAM_PREVIEW_GET),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator('param', streamKeyParam),
|
||||
async (ctx) => {
|
||||
const streamKey = ctx.req.valid('param').stream_key;
|
||||
if (!parseStreamKey(streamKey)) {
|
||||
throw new BadRequestError({code: APIErrorCodes.INVALID_REQUEST, message: 'Invalid stream key format'});
|
||||
}
|
||||
const preview = await ctx.get('streamPreviewService').getPreview(streamKey);
|
||||
if (!preview) {
|
||||
return ctx.body(null, 404);
|
||||
}
|
||||
const payload: ArrayBuffer = preview.buffer.slice().buffer;
|
||||
const headers = {
|
||||
'Content-Type': preview.contentType || 'image/jpeg',
|
||||
'Cache-Control': 'no-store, private',
|
||||
Pragma: 'no-cache',
|
||||
};
|
||||
return ctx.newResponse(payload, 200, headers);
|
||||
},
|
||||
);
|
||||
|
||||
app.post(
|
||||
'/streams/:stream_key/preview',
|
||||
RateLimitMiddleware(RateLimitConfigs.CHANNEL_STREAM_PREVIEW_POST),
|
||||
LoginRequired,
|
||||
DefaultUserOnly,
|
||||
Validator(
|
||||
'json',
|
||||
z.object({
|
||||
channel_id: Int64Type,
|
||||
thumbnail: createStringType(1, 2_000_000),
|
||||
content_type: createStringType(1, 64).optional(),
|
||||
}),
|
||||
),
|
||||
Validator('param', streamKeyParam),
|
||||
async (ctx) => {
|
||||
const user = ctx.get('user');
|
||||
const {thumbnail, channel_id, content_type} = ctx.req.valid('json');
|
||||
const streamKey = ctx.req.valid('param').stream_key;
|
||||
const channelId = createChannelID(channel_id);
|
||||
const userId = user.id;
|
||||
|
||||
const parsedKey = parseStreamKey(streamKey);
|
||||
if (!parsedKey) {
|
||||
throw new BadRequestError({code: APIErrorCodes.INVALID_REQUEST, message: 'Invalid stream key format'});
|
||||
}
|
||||
|
||||
const channel = await ctx.get('channelService').getChannel({userId, channelId});
|
||||
|
||||
if (channel.guildId) {
|
||||
const hasConnect = await ctx.get('gatewayService').checkPermission({
|
||||
guildId: channel.guildId,
|
||||
channelId,
|
||||
userId,
|
||||
permission: Permissions.CONNECT,
|
||||
});
|
||||
if (!hasConnect) throw new MissingPermissionsError();
|
||||
}
|
||||
|
||||
if (channel.guildId && parsedKey.scope !== 'guild') {
|
||||
throw new BadRequestError({code: APIErrorCodes.INVALID_REQUEST, message: 'Stream key scope mismatch'});
|
||||
}
|
||||
if (!channel.guildId && parsedKey.scope !== 'dm') {
|
||||
throw new BadRequestError({code: APIErrorCodes.INVALID_REQUEST, message: 'Stream key scope mismatch'});
|
||||
}
|
||||
if (parsedKey.channelId !== channelId.toString()) {
|
||||
throw new BadRequestError({code: APIErrorCodes.INVALID_REQUEST, message: 'Stream key channel mismatch'});
|
||||
}
|
||||
|
||||
let body: Uint8Array;
|
||||
try {
|
||||
body = Uint8Array.from(Buffer.from(thumbnail, 'base64'));
|
||||
} catch {
|
||||
throw new BadRequestError({
|
||||
code: APIErrorCodes.INVALID_REQUEST,
|
||||
message: 'Invalid thumbnail payload',
|
||||
});
|
||||
}
|
||||
if (body.byteLength === 0) {
|
||||
throw new BadRequestError({
|
||||
code: APIErrorCodes.INVALID_REQUEST,
|
||||
message: 'Empty thumbnail payload',
|
||||
});
|
||||
}
|
||||
|
||||
await ctx.get('streamPreviewService').uploadPreview({
|
||||
streamKey,
|
||||
channelId,
|
||||
userId,
|
||||
body,
|
||||
contentType: content_type,
|
||||
});
|
||||
return ctx.body(null, 204);
|
||||
},
|
||||
);
|
||||
};
|
||||
35
fluxer_api/src/channel/controllers/index.ts
Normal file
35
fluxer_api/src/channel/controllers/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright (C) 2026 Fluxer Contributors
|
||||
*
|
||||
* This file is part of Fluxer.
|
||||
*
|
||||
* Fluxer is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Fluxer is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with Fluxer. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import type {HonoApp} from '~/App';
|
||||
import {CallController} from './CallController';
|
||||
import {ChannelController} from './ChannelController';
|
||||
import {MessageController} from './MessageController';
|
||||
import {MessageInteractionController} from './MessageInteractionController';
|
||||
import {ScheduledMessageController} from './ScheduledMessageController';
|
||||
import {StreamController} from './StreamController';
|
||||
|
||||
export const registerChannelControllers = (app: HonoApp) => {
|
||||
ChannelController(app);
|
||||
MessageInteractionController(app);
|
||||
MessageController(app);
|
||||
ScheduledMessageController(app);
|
||||
CallController(app);
|
||||
StreamController(app);
|
||||
};
|
||||
Reference in New Issue
Block a user