2019-04-07 07:50:36 -05:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2021-08-19 08:04:15 -05:00
|
|
|
import { MessagingMessage } from '@/models/entities/messaging-message';
|
2021-08-19 07:55:45 -05:00
|
|
|
import { Users, DriveFiles, UserGroups } from '../index';
|
2021-09-22 08:35:55 -05:00
|
|
|
import { Packed } from '@/misc/schema';
|
2021-08-19 08:04:15 -05:00
|
|
|
import { User } from '@/models/entities/user';
|
2019-04-23 08:35:26 -05:00
|
|
|
|
2019-04-07 07:50:36 -05:00
|
|
|
@EntityRepository(MessagingMessage)
|
|
|
|
export class MessagingMessageRepository extends Repository<MessagingMessage> {
|
2019-06-14 10:07:41 -05:00
|
|
|
public validateText(text: string): boolean {
|
2019-04-07 07:50:36 -05:00
|
|
|
return text.trim().length <= 1000 && text.trim() != '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public async pack(
|
|
|
|
src: MessagingMessage['id'] | MessagingMessage,
|
2021-03-23 21:05:37 -05:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-07 07:50:36 -05:00
|
|
|
options?: {
|
2019-05-18 06:36:33 -05:00
|
|
|
populateRecipient?: boolean,
|
|
|
|
populateGroup?: boolean,
|
2019-04-07 07:50:36 -05:00
|
|
|
}
|
2021-09-22 08:35:55 -05:00
|
|
|
): Promise<Packed<'MessagingMessage'>> {
|
2019-04-07 07:50:36 -05:00
|
|
|
const opts = options || {
|
2019-05-18 06:36:33 -05:00
|
|
|
populateRecipient: true,
|
|
|
|
populateGroup: true,
|
2019-04-07 07:50:36 -05:00
|
|
|
};
|
|
|
|
|
2021-02-13 00:33:38 -06:00
|
|
|
const message = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
2019-04-07 07:50:36 -05:00
|
|
|
|
|
|
|
return {
|
|
|
|
id: message.id,
|
2019-04-23 08:35:26 -05:00
|
|
|
createdAt: message.createdAt.toISOString(),
|
2019-04-07 07:50:36 -05:00
|
|
|
text: message.text,
|
|
|
|
userId: message.userId,
|
|
|
|
user: await Users.pack(message.user || message.userId, me),
|
|
|
|
recipientId: message.recipientId,
|
2019-05-18 06:36:33 -05:00
|
|
|
recipient: message.recipientId && opts.populateRecipient ? await Users.pack(message.recipient || message.recipientId, me) : undefined,
|
2019-05-19 06:52:00 -05:00
|
|
|
groupId: message.groupId,
|
2019-05-18 06:36:33 -05:00
|
|
|
group: message.groupId && opts.populateGroup ? await UserGroups.pack(message.group || message.groupId) : undefined,
|
2019-04-07 07:50:36 -05:00
|
|
|
fileId: message.fileId,
|
|
|
|
file: message.fileId ? await DriveFiles.pack(message.fileId) : null,
|
2019-05-18 06:36:33 -05:00
|
|
|
isRead: message.isRead,
|
|
|
|
reads: message.reads,
|
2019-04-07 07:50:36 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 08:35:26 -05:00
|
|
|
|
|
|
|
export const packedMessagingMessageSchema = {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
userId: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
user: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
2021-09-11 11:12:23 -05:00
|
|
|
ref: 'User' as const,
|
2019-06-27 04:04:09 -05:00
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
},
|
|
|
|
text: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
},
|
|
|
|
fileId: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
file: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2021-09-11 11:12:23 -05:00
|
|
|
ref: 'DriveFile' as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
},
|
|
|
|
recipientId: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
recipient: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2021-09-11 11:12:23 -05:00
|
|
|
ref: 'User' as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
},
|
2019-05-18 06:36:33 -05:00
|
|
|
groupId: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-05-18 06:36:33 -05:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
group: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2021-09-11 11:12:23 -05:00
|
|
|
ref: 'UserGroup' as const,
|
2019-05-18 06:36:33 -05:00
|
|
|
},
|
2019-04-23 08:35:26 -05:00
|
|
|
isRead: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 08:35:26 -05:00
|
|
|
},
|
2019-05-18 06:36:33 -05:00
|
|
|
reads: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-05-18 06:36:33 -05:00
|
|
|
items: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2021-12-09 08:58:30 -06:00
|
|
|
format: 'id',
|
|
|
|
},
|
2019-05-18 06:36:33 -05:00
|
|
|
},
|
2019-04-23 08:35:26 -05:00
|
|
|
},
|
|
|
|
};
|