]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/users/user-notification.model.ts
Support short uuid for GET video/playlist
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / users / user-notification.model.ts
1 import { AuthUser } from '@app/core'
2 import { Account } from '@app/shared/shared-main/account/account.model'
3 import { Actor } from '@app/shared/shared-main/account/actor.model'
4 import { VideoChannel } from '@app/shared/shared-main/video-channel/video-channel.model'
5 import {
6 AbuseState,
7 ActorInfo,
8 FollowState,
9 PluginType,
10 UserNotification as UserNotificationServer,
11 UserNotificationType,
12 UserRight,
13 VideoInfo
14 } from '@shared/models'
15 import { Video } from '../video'
16
17 export class UserNotification implements UserNotificationServer {
18 id: number
19 type: UserNotificationType
20 read: boolean
21
22 video?: VideoInfo & {
23 channel: ActorInfo & { avatarUrl?: string }
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
37 account: ActorInfo & { avatarUrl?: string }
38 video: VideoInfo
39 }
40
41 abuse?: {
42 id: number
43 state: AbuseState
44
45 video?: VideoInfo
46
47 comment?: {
48 threadId: number
49
50 video: {
51 id: number
52 uuid: string
53 name: string
54 }
55 }
56
57 account?: ActorInfo
58 }
59
60 videoBlacklist?: {
61 id: number
62 video: VideoInfo
63 }
64
65 account?: ActorInfo & { avatarUrl?: string }
66
67 actorFollow?: {
68 id: number
69 state: FollowState
70 follower: ActorInfo & { avatarUrl?: string }
71 following: {
72 type: 'account' | 'channel' | 'instance'
73 name: string
74 displayName: string
75 host: string
76 }
77 }
78
79 plugin?: {
80 name: string
81 type: PluginType
82 latestVersion: string
83 }
84
85 peertube?: {
86 latestVersion: string
87 }
88
89 createdAt: string
90 updatedAt: string
91
92 // Additional fields
93 videoUrl?: string
94 commentUrl?: any[]
95
96 abuseUrl?: string
97 abuseQueryParams?: { [id: string]: string } = {}
98
99 videoAutoBlacklistUrl?: string
100
101 accountUrl?: string
102
103 videoImportIdentifier?: string
104 videoImportUrl?: string
105
106 instanceFollowUrl?: string
107
108 peertubeVersionLink?: string
109
110 pluginUrl?: string
111 pluginQueryParams?: { [id: string]: string } = {}
112
113 constructor (hash: UserNotificationServer, user: AuthUser) {
114 this.id = hash.id
115 this.type = hash.type
116 this.read = hash.read
117
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
122 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
123
124 this.videoImport = hash.videoImport
125
126 this.comment = hash.comment
127 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
128
129 this.abuse = hash.abuse
130
131 this.videoBlacklist = hash.videoBlacklist
132
133 this.account = hash.account
134 if (this.account) this.setAccountAvatarUrl(this.account)
135
136 this.actorFollow = hash.actorFollow
137 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
138
139 this.plugin = hash.plugin
140 this.peertube = hash.peertube
141
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:
156 if (!this.comment) break
157 this.accountUrl = this.buildAccountUrl(this.comment.account)
158 this.commentUrl = this.buildCommentUrl(this.comment)
159 break
160
161 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
162 this.abuseUrl = '/admin/moderation/abuses/list'
163 this.abuseQueryParams.search = '#' + this.abuse.id
164
165 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
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)
168 break
169
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
182 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
183 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
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
187 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
188 break
189
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)
201
202 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
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
217
218 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
219 this.instanceFollowUrl = '/admin/follows/followers-list'
220 break
221
222 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
223 this.instanceFollowUrl = '/admin/follows/following-list'
224 break
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
234 }
235 } catch (err) {
236 this.type = null
237 console.error(err)
238 }
239 }
240
241 private buildVideoUrl (video: { uuid: string }) {
242 return Video.buildWatchUrl(video)
243 }
244
245 private buildAccountUrl (account: { name: string, host: string }) {
246 return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host)
247 }
248
249 private buildVideoImportUrl () {
250 return '/my-library/video-imports'
251 }
252
253 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
254 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
255 }
256
257 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
258 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
259 }
260
261 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
262 actor.avatarUrl = Account.GET_ACTOR_AVATAR_URL(actor) || Account.GET_DEFAULT_AVATAR_URL()
263 }
264
265 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
266 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor) || VideoChannel.GET_DEFAULT_AVATAR_URL()
267 }
268 }