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