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-06-27 18:04:09 +09:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-04-23 22:35:26 +09:00
|
|
|
|
|
|
|
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 = {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
properties: {
|
|
|
|
id: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
description: 'The unique identifier for this MessagingMessage.',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'date-time',
|
|
|
|
description: 'The date that the MessagingMessage was created.'
|
|
|
|
},
|
|
|
|
userId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
user: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'User',
|
2019-06-27 18:04:09 +09:00
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
text: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
fileId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
file: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'DriveFile',
|
|
|
|
},
|
|
|
|
recipientId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
recipient: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'User'
|
|
|
|
},
|
2019-05-18 20:36:33 +09:00
|
|
|
groupId: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
group: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: true as const, nullable: true as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
ref: 'UserGroup'
|
|
|
|
},
|
2019-04-23 22:35:26 +09:00
|
|
|
isRead: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
2019-05-18 20:36:33 +09:00
|
|
|
reads: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
items: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-05-18 20:36:33 +09:00
|
|
|
format: 'id'
|
|
|
|
}
|
|
|
|
},
|
2019-04-23 22:35:26 +09:00
|
|
|
},
|
|
|
|
};
|