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