]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/users/user-notification.model.ts
Merge branch 'release/5.0.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / users / user-notification.model.ts
CommitLineData
69524f6e
C
1import { AuthUser } from '@app/core'
2import { Account } from '@app/shared/shared-main/account/account.model'
3import { Actor } from '@app/shared/shared-main/account/actor.model'
4import { VideoChannel } from '@app/shared/shared-main/video-channel/video-channel.model'
42b40636 5import { logger } from '@root-helpers/logger'
d573926e
C
6import {
7 AbuseState,
8 ActorInfo,
9 FollowState,
32a18cbf 10 PluginType,
d573926e
C
11 UserNotification as UserNotificationServer,
12 UserNotificationType,
69524f6e
C
13 UserRight,
14 VideoInfo
d573926e 15} from '@shared/models'
d4a8e7a6 16import { Video } from '../video'
2f1548fd
C
17
18export class UserNotification implements UserNotificationServer {
19 id: number
20 type: UserNotificationType
21 read: boolean
22
23 video?: VideoInfo & {
457bb213 24 channel: ActorInfo & { avatarUrl?: string }
2f1548fd
C
25 }
26
27 videoImport?: {
28 id: number
29 video?: VideoInfo
30 torrentName?: string
31 magnetUri?: string
32 targetUrl?: string
33 }
34
35 comment?: {
36 id: number
37 threadId: number
457bb213 38 account: ActorInfo & { avatarUrl?: string }
2f1548fd
C
39 video: VideoInfo
40 }
41
d95d1559 42 abuse?: {
2f1548fd 43 id: number
d573926e 44 state: AbuseState
d95d1559
C
45
46 video?: VideoInfo
47
48 comment?: {
49 threadId: number
50
29837f88 51 video: VideoInfo
d95d1559
C
52 }
53
54 account?: ActorInfo
2f1548fd
C
55 }
56
57 videoBlacklist?: {
58 id: number
59 video: VideoInfo
60 }
61
457bb213 62 account?: ActorInfo & { avatarUrl?: string }
2f1548fd
C
63
64 actorFollow?: {
65 id: number
846751c9 66 state: FollowState
457bb213 67 follower: ActorInfo & { avatarUrl?: string }
2f1548fd 68 following: {
e1b49ee5 69 type: 'account' | 'channel' | 'instance'
2f1548fd
C
70 name: string
71 displayName: string
e1b49ee5 72 host: string
2f1548fd
C
73 }
74 }
75
32a18cbf
C
76 plugin?: {
77 name: string
78 type: PluginType
79 latestVersion: string
80 }
81
82 peertube?: {
83 latestVersion: string
84 }
85
9589907c
C
86 registration?: {
87 id: number
88 username: string
89 }
90
2f1548fd
C
91 createdAt: string
92 updatedAt: string
93
94 // Additional fields
95 videoUrl?: string
96 commentUrl?: any[]
32a18cbf 97
d95d1559 98 abuseUrl?: string
d573926e 99 abuseQueryParams?: { [id: string]: string } = {}
32a18cbf 100
7ccddd7b 101 videoAutoBlacklistUrl?: string
32a18cbf 102
2f1548fd 103 accountUrl?: string
32a18cbf 104
9589907c
C
105 registrationsUrl?: string
106
2f1548fd
C
107 videoImportIdentifier?: string
108 videoImportUrl?: string
32a18cbf 109
8ce1ba6e 110 instanceFollowUrl?: string
2f1548fd 111
32a18cbf
C
112 peertubeVersionLink?: string
113
114 pluginUrl?: string
115 pluginQueryParams?: { [id: string]: string } = {}
116
d573926e 117 constructor (hash: UserNotificationServer, user: AuthUser) {
2f1548fd
C
118 this.id = hash.id
119 this.type = hash.type
120 this.read = hash.read
121
344d8be5
C
122 // We assume that some fields exist
123 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
124 try {
125 this.video = hash.video
c418d483 126 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
344d8be5
C
127
128 this.videoImport = hash.videoImport
129
130 this.comment = hash.comment
c418d483 131 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
344d8be5 132
d95d1559 133 this.abuse = hash.abuse
344d8be5
C
134
135 this.videoBlacklist = hash.videoBlacklist
136
137 this.account = hash.account
c418d483 138 if (this.account) this.setAccountAvatarUrl(this.account)
344d8be5
C
139
140 this.actorFollow = hash.actorFollow
c418d483 141 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
344d8be5 142
32a18cbf
C
143 this.plugin = hash.plugin
144 this.peertube = hash.peertube
9589907c 145 this.registration = hash.registration
32a18cbf 146
344d8be5
C
147 this.createdAt = hash.createdAt
148 this.updatedAt = hash.updatedAt
149
150 switch (this.type) {
151 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
152 this.videoUrl = this.buildVideoUrl(this.video)
153 break
154
155 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
156 this.videoUrl = this.buildVideoUrl(this.video)
157 break
158
159 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
160 case UserNotificationType.COMMENT_MENTION:
c5c09c1e 161 if (!this.comment) break
344d8be5 162 this.accountUrl = this.buildAccountUrl(this.comment.account)
cfde28ba 163 this.commentUrl = this.buildCommentUrl(this.comment)
344d8be5
C
164 break
165
310b5219 166 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
d95d1559 167 this.abuseUrl = '/admin/moderation/abuses/list'
d573926e 168 this.abuseQueryParams.search = '#' + this.abuse.id
d95d1559
C
169
170 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
cfde28ba
C
171 else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment)
172 else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account)
344d8be5
C
173 break
174
d573926e
C
175 case UserNotificationType.ABUSE_STATE_CHANGE:
176 this.abuseUrl = '/my-account/abuses'
177 this.abuseQueryParams.search = '#' + this.abuse.id
178 break
179
180 case UserNotificationType.ABUSE_NEW_MESSAGE:
181 this.abuseUrl = user.hasRight(UserRight.MANAGE_ABUSES)
182 ? '/admin/moderation/abuses/list'
183 : '/my-account/abuses'
184 this.abuseQueryParams.search = '#' + this.abuse.id
185 break
186
7ccddd7b
JM
187 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
188 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
43d35494
C
189 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
190 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
191
8424c402 192 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
7ccddd7b
JM
193 break
194
344d8be5
C
195 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
196 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
197 break
198
199 case UserNotificationType.MY_VIDEO_PUBLISHED:
200 this.videoUrl = this.buildVideoUrl(this.video)
201 break
202
203 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
204 this.videoImportUrl = this.buildVideoImportUrl()
205 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
6d28a505
C
206
207 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
344d8be5
C
208 break
209
210 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
211 this.videoImportUrl = this.buildVideoImportUrl()
212 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
213 break
214
215 case UserNotificationType.NEW_USER_REGISTRATION:
216 this.accountUrl = this.buildAccountUrl(this.account)
217 break
218
9589907c
C
219 case UserNotificationType.NEW_USER_REGISTRATION_REQUEST:
220 this.registrationsUrl = '/admin/moderation/registrations/list'
221 break
222
344d8be5
C
223 case UserNotificationType.NEW_FOLLOW:
224 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
225 break
8ce1ba6e
C
226
227 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
228 this.instanceFollowUrl = '/admin/follows/followers-list'
229 break
e1b49ee5
C
230
231 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
232 this.instanceFollowUrl = '/admin/follows/following-list'
233 break
32a18cbf
C
234
235 case UserNotificationType.NEW_PEERTUBE_VERSION:
236 this.peertubeVersionLink = 'https://joinpeertube.org/news'
237 break
238
239 case UserNotificationType.NEW_PLUGIN_VERSION:
240 this.pluginUrl = `/admin/plugins/list-installed`
241 this.pluginQueryParams.pluginType = this.plugin.type + ''
242 break
1808a1f8 243
92e66e04 244 case UserNotificationType.MY_VIDEO_STUDIO_EDITION_FINISHED:
1808a1f8
C
245 this.videoUrl = this.buildVideoUrl(this.video)
246 break
344d8be5
C
247 }
248 } catch (err) {
44b88f18 249 this.type = null
42b40636 250 logger.error(err)
2f1548fd
C
251 }
252 }
253
254 private buildVideoUrl (video: { uuid: string }) {
d4a8e7a6 255 return Video.buildWatchUrl(video)
2f1548fd
C
256 }
257
38967f7b 258 private buildAccountUrl (account: { name: string, host: string }) {
71887396 259 return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host)
2f1548fd
C
260 }
261
262 private buildVideoImportUrl () {
17119e4a 263 return '/my-library/video-imports'
2f1548fd
C
264 }
265
266 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
267 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
268 }
269
cfde28ba
C
270 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
271 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
272 }
273
d0800f76 274 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) {
275 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || Account.GET_DEFAULT_AVATAR_URL(48)
c418d483 276 }
277
d0800f76 278 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) {
279 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || VideoChannel.GET_DEFAULT_AVATAR_URL(48)
457bb213 280 }
2f1548fd 281}