summaryrefslogtreecommitdiff
path: root/src/models/repositories/messaging-message.ts
blob: d70e95bc122da7b0f8b34c1f0b9bc38751a6bb1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { EntityRepository, Repository } from 'typeorm';
import { MessagingMessage } from '../entities/messaging-message';
import { Users, DriveFiles, UserGroups } from '..';
import { ensure } from '../../prelude/ensure';
import { SchemaType } from '../../misc/schema';

export type PackedMessagingMessage = SchemaType<typeof packedMessagingMessageSchema>;

@EntityRepository(MessagingMessage)
export class MessagingMessageRepository extends Repository<MessagingMessage> {
	public validateText(text: string): boolean {
		return text.trim().length <= 1000 && text.trim() != '';
	}

	public async pack(
		src: MessagingMessage['id'] | MessagingMessage,
		me?: any,
		options?: {
			populateRecipient?: boolean,
			populateGroup?: boolean,
		}
	): Promise<PackedMessagingMessage> {
		const opts = options || {
			populateRecipient: true,
			populateGroup: true,
		};

		const message = typeof src === 'object' ? src : await this.findOne(src).then(ensure);

		return {
			id: message.id,
			createdAt: message.createdAt.toISOString(),
			text: message.text,
			userId: message.userId,
			user: await Users.pack(message.user || message.userId, me),
			recipientId: message.recipientId,
			recipient: message.recipientId && opts.populateRecipient ? await Users.pack(message.recipient || message.recipientId, me) : undefined,
			groupId: message.groupId,
			group: message.groupId && opts.populateGroup ? await UserGroups.pack(message.group || message.groupId) : undefined,
			fileId: message.fileId,
			file: message.fileId ? await DriveFiles.pack(message.fileId) : null,
			isRead: message.isRead,
			reads: message.reads,
		};
	}
}

export const packedMessagingMessageSchema = {
	type: 'object' as const,
	optional: false as const, nullable: false as const,
	properties: {
		id: {
			type: 'string' as const,
			optional: false as const, nullable: false as const,
			format: 'id',
			description: 'The unique identifier for this MessagingMessage.',
			example: 'xxxxxxxxxx',
		},
		createdAt: {
			type: 'string' as const,
			optional: false as const, nullable: false as const,
			format: 'date-time',
			description: 'The date that the MessagingMessage was created.'
		},
		userId: {
			type: 'string' as const,
			optional: false as const, nullable: false as const,
			format: 'id',
		},
		user: {
			type: 'object' as const,
			ref: 'User',
			optional: true as const, nullable: false as const,
		},
		text: {
			type: 'string' as const,
			optional: false as const, nullable: true as const,
		},
		fileId: {
			type: 'string' as const,
			optional: true as const, nullable: true as const,
			format: 'id',
		},
		file: {
			type: 'object' as const,
			optional: true as const, nullable: true as const,
			ref: 'DriveFile',
		},
		recipientId: {
			type: 'string' as const,
			optional: false as const, nullable: true as const,
			format: 'id',
		},
		recipient: {
			type: 'object' as const,
			optional: true as const, nullable: true as const,
			ref: 'User'
		},
		groupId: {
			type: 'string' as const,
			optional: false as const, nullable: true as const,
			format: 'id',
		},
		group: {
			type: 'object' as const,
			optional: true as const, nullable: true as const,
			ref: 'UserGroup'
		},
		isRead: {
			type: 'boolean' as const,
			optional: true as const, nullable: false as const,
		},
		reads: {
			type: 'array' as const,
			optional: true as const, nullable: false as const,
			items: {
				type: 'string' as const,
				optional: false as const, nullable: false as const,
				format: 'id'
			}
		},
	},
};