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