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