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