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