/** * Takes TypeORM entity rows and converts them to plain DTO objects * matching the Angular-side interfaces. */ import { MessageEntity } from '../entities/MessageEntity'; import { UserEntity } from '../entities/UserEntity'; import { RoomEntity } from '../entities/RoomEntity'; import { ReactionEntity } from '../entities/ReactionEntity'; import { BanEntity } from '../entities/BanEntity'; import { AttachmentEntity } from '../entities/AttachmentEntity'; export function rowToMessage(row: MessageEntity) { return { id: row.id, roomId: row.roomId, channelId: row.channelId ?? undefined, senderId: row.senderId, senderName: row.senderName, content: row.content, timestamp: row.timestamp, editedAt: row.editedAt ?? undefined, reactions: JSON.parse(row.reactions || '[]') as unknown[], isDeleted: !!row.isDeleted, replyToId: row.replyToId ?? undefined }; } export function rowToUser(row: UserEntity) { return { id: row.id, oderId: row.oderId ?? '', username: row.username ?? '', displayName: row.displayName ?? '', avatarUrl: row.avatarUrl ?? undefined, status: row.status ?? 'offline', role: row.role ?? 'member', joinedAt: row.joinedAt ?? 0, peerId: row.peerId ?? undefined, isOnline: !!row.isOnline, isAdmin: !!row.isAdmin, isRoomOwner: !!row.isRoomOwner, voiceState: row.voiceState ? JSON.parse(row.voiceState) : undefined, screenShareState: row.screenShareState ? JSON.parse(row.screenShareState) : undefined }; } export function rowToRoom(row: RoomEntity) { return { id: row.id, name: row.name, description: row.description ?? undefined, topic: row.topic ?? undefined, hostId: row.hostId, password: row.password ?? undefined, isPrivate: !!row.isPrivate, createdAt: row.createdAt, userCount: row.userCount, maxUsers: row.maxUsers ?? undefined, icon: row.icon ?? undefined, iconUpdatedAt: row.iconUpdatedAt ?? undefined, permissions: row.permissions ? JSON.parse(row.permissions) : undefined, channels: row.channels ? JSON.parse(row.channels) : undefined }; } export function rowToReaction(row: ReactionEntity) { return { id: row.id, messageId: row.messageId, oderId: row.oderId ?? '', userId: row.userId ?? '', emoji: row.emoji, timestamp: row.timestamp }; } export function rowToAttachment(row: AttachmentEntity) { return { id: row.id, messageId: row.messageId, filename: row.filename, size: row.size, mime: row.mime, isImage: !!row.isImage, uploaderPeerId: row.uploaderPeerId ?? undefined, filePath: row.filePath ?? undefined, savedPath: row.savedPath ?? undefined }; } export function rowToBan(row: BanEntity) { return { oderId: row.oderId, userId: row.userId ?? '', roomId: row.roomId, bannedBy: row.bannedBy, displayName: row.displayName ?? undefined, reason: row.reason ?? undefined, expiresAt: row.expiresAt ?? undefined, timestamp: row.timestamp }; }