-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathActivityModel.ts
More file actions
191 lines (163 loc) · 3.82 KB
/
Copy pathActivityModel.ts
File metadata and controls
191 lines (163 loc) · 3.82 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
import type { IPreview, IRawActivity, IRichObject } from './types.ts'
import moment from '@nextcloud/moment'
export default class ActivityModel {
_activity: IRawActivity
/**
* Create the activity object
*
* @param rawActivity the activity object from the ocs response
*/
constructor(rawActivity: IRawActivity) {
if (typeof rawActivity !== 'object') {
throw new Error('Received activity data is not an object.')
}
// Sanity checks
if (typeof rawActivity.activity_id !== 'number') {
throw new Error('The activity_id argument is not a number')
}
if (typeof rawActivity.type !== 'string' || rawActivity.type.trim() === '') {
throw new Error('The type argument is not a valid string')
}
if (typeof rawActivity.subject !== 'string' || rawActivity.subject.trim() === '') {
throw new Error('The subject argument is not a valid string')
}
if (typeof rawActivity.icon !== 'string' || rawActivity.icon.trim() === '') {
throw new Error('The icon argument is not a valid string')
}
if (typeof rawActivity.datetime !== 'string' || rawActivity.datetime.trim() === '') {
throw new Error('The datetime argument is not a valid string')
}
// store state
this._activity = rawActivity
}
/**
* get the activity id
*/
get id(): number {
return this._activity.activity_id
}
/**
* Get the app causing the activity
*/
get app(): string {
return this._activity.app
}
/**
* Get the activity type
*/
get type(): string {
return this._activity.type
}
/**
* Get the user ID of the user causing the activity or affected by the activity
*/
get user(): string {
return this._activity.user
}
/**
* Get the activity subject
*/
get subject(): string {
return this._activity.subject
}
/**
* Get the activity subject_rich template
*/
get subjectRichTemplate(): string {
return this._activity.subject_rich[0]
}
/**
* Get the activity subject_rich objects
*/
get subjectRichObjects(): Record<string, IRichObject> {
if (Array.isArray(this._activity.subject_rich[1])) {
return {}
}
return this._activity.subject_rich[1] as Record<string, IRichObject>
}
/**
* Get the activity message
*/
get message(): string {
return this._activity.message
}
/**
* Get the activity message_rich template
*/
get messageRichTemplate(): string {
return this._activity.message_rich[0]
}
/**
* Get the activity message_rich objects
*/
get messageRichObjects(): Record<string, IRichObject> {
if (!Array.isArray(this._activity.message_rich[1])) {
return {}
}
return this._activity.message_rich[1] as Record<string, IRichObject>
}
/**
* Get the object_type
*/
get objectType(): string {
return this._activity.object_type
}
/**
* Get the activity object_id
*/
get objectId(): number {
return this._activity.object_id
}
/**
* Get the activity object_name
*/
get objectName(): string {
return this._activity.object_name
}
/**
* Get the activity link
*/
get link(): string {
return this._activity.link
}
/**
* Get the activity icon
*/
get icon(): string {
return this._activity.icon
}
/**
* Get the activity datetime
*/
get datetime(): string {
return this._activity.datetime
}
/**
* Get the activity formatted date from the current date
*/
get dateFromNow(): string {
return moment(this._activity.datetime).fromNow()
}
/**
* Get the activity formatted datetime
*/
get formattedDate(): string {
return moment(this._activity.datetime).format('LLL')
}
/**
* Get the activity timestamp
*/
get timestamp(): number {
return moment(this._activity.datetime).valueOf()
}
/**
* Get previews of affected files
*/
get previews(): IPreview[] {
return this._activity.previews ?? []
}
}