FoundKey/packages/backend/src/models/repositories/messaging-message.ts
Johann150 6ce4b3fe2f
Some checks failed
ci/woodpecker/push/lint-backend Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/lint-client Pipeline failed
ci/woodpecker/push/test Pipeline failed
fix some lints
Many of these were fixed automatically with eslint --fix.

Some of them (e.g. adding return types to functions) were done manually.
2022-08-11 00:09:29 +02:00

40 lines
1.4 KiB
TypeScript

import { db } from '@/db/postgre.js';
import { Packed } from '@/misc/schema.js';
import { MessagingMessage } from '@/models/entities/messaging-message.js';
import { User } from '@/models/entities/user.js';
import { Users, DriveFiles, UserGroups } from '../index.js';
export const MessagingMessageRepository = db.getRepository(MessagingMessage).extend({
async pack(
src: MessagingMessage['id'] | MessagingMessage,
me?: { id: User['id'] } | null | undefined,
options?: {
populateRecipient?: boolean,
populateGroup?: boolean,
},
): Promise<Packed<'MessagingMessage'>> {
const opts = options || {
populateRecipient: true,
populateGroup: true,
};
const message = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
return {
id: message.id,
createdAt: message.createdAt.toISOString(),
text: message.text,
userId: message.userId,
user: await Users.pack(message.user || message.userId, me),
recipientId: message.recipientId,
recipient: message.recipientId && opts.populateRecipient ? await Users.pack(message.recipient || message.recipientId, me) : undefined,
groupId: message.groupId,
group: message.groupId && opts.populateGroup ? await UserGroups.pack(message.group || message.groupId) : undefined,
fileId: message.fileId,
file: message.fileId ? await DriveFiles.pack(message.fileId) : null,
isRead: message.isRead,
reads: message.reads,
};
},
});