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