feat(guild-messaging): support message metadata for reply mentions and attachments

This commit is contained in:
nav
2026-05-12 10:28:02 +00:00
parent ceaece754e
commit d3fdc3dd1e
6 changed files with 124 additions and 4 deletions

View File

@@ -1,10 +1,18 @@
import 'reflect-metadata';
import { ValidationPipe } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
app.useGlobalPipes(
new ValidationPipe({
whitelist: true,
forbidNonWhitelisted: true,
transform: true,
}),
);
const port = process.env.PORT ? Number(process.env.PORT) : 7002;
await app.listen(port);
console.log(`Fabric.Backend.Guild listening on :${port}`);

View File

@@ -0,0 +1,59 @@
import {
ArrayMaxSize,
IsArray,
IsOptional,
IsString,
MaxLength,
ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
class AttachmentDto {
@IsString()
@MaxLength(2048)
url!: string;
@IsOptional()
@IsString()
@MaxLength(255)
name?: string;
@IsOptional()
@IsString()
@MaxLength(100)
mimeType?: string;
}
export class CreateMessageDto {
@IsString()
@MaxLength(4000)
content!: string;
@IsOptional()
@IsString()
@MaxLength(80)
clientMessageId?: string;
@IsOptional()
@IsString()
@MaxLength(80)
replyToMessageId?: string;
@IsOptional()
@IsArray()
@ArrayMaxSize(50)
@IsString({ each: true })
mentions?: string[];
@IsOptional()
@IsArray()
@ArrayMaxSize(10)
@ValidateNested({ each: true })
@Type(() => AttachmentDto)
attachments?: AttachmentDto[];
@IsOptional()
@IsString()
@MaxLength(64)
authorUserId?: string;
}

View File

@@ -1,9 +1,15 @@
import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common';
import { CreateMessageDto } from './dto.create-message.dto';
type Message = {
messageId: string;
seq: number;
content: string;
authorUserId: string;
replyToMessageId: string | null;
mentions: string[];
attachments: Array<{ url: string; name?: string; mimeType?: string }>;
createdAt: string;
};
@Controller('channels/:id/messages')
@@ -12,14 +18,19 @@ export class MessagingController {
private messagesByChannel = new Map<string, Message[]>();
@Post()
create(@Param('id') channelId: string, @Body() body: { content?: string; messageId?: string }) {
create(@Param('id') channelId: string, @Body() body: CreateMessageDto) {
const next = (this.seqByChannel.get(channelId) ?? 0) + 1;
this.seqByChannel.set(channelId, next);
const message: Message = {
messageId: body.messageId ?? `m-${channelId}-${next}`,
messageId: body.clientMessageId ?? `m-${channelId}-${next}`,
seq: next,
content: body.content ?? '',
content: body.content,
authorUserId: body.authorUserId ?? 'anonymous',
replyToMessageId: body.replyToMessageId ?? null,
mentions: body.mentions ?? [],
attachments: body.attachments ?? [],
createdAt: new Date().toISOString(),
};
const arr = this.messagesByChannel.get(channelId) ?? [];