]>
Commit | Line | Data |
---|---|---|
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 | registration?: { | |
87 | id: number | |
88 | username: string | |
89 | } | |
90 | ||
91 | createdAt: string | |
92 | updatedAt: string | |
93 | ||
94 | // Additional fields | |
95 | videoUrl?: string | |
96 | commentUrl?: any[] | |
97 | ||
98 | abuseUrl?: string | |
99 | abuseQueryParams?: { [id: string]: string } = {} | |
100 | ||
101 | videoAutoBlacklistUrl?: string | |
102 | ||
103 | accountUrl?: string | |
104 | ||
105 | registrationsUrl?: string | |
106 | ||
107 | videoImportIdentifier?: string | |
108 | videoImportUrl?: string | |
109 | ||
110 | instanceFollowUrl?: string | |
111 | ||
112 | peertubeVersionLink?: string | |
113 | ||
114 | pluginUrl?: string | |
115 | pluginQueryParams?: { [id: string]: string } = {} | |
116 | ||
117 | constructor (hash: UserNotificationServer, user: AuthUser) { | |
118 | this.id = hash.id | |
119 | this.type = hash.type | |
120 | this.read = hash.read | |
121 | ||
122 | // We assume that some fields exist | |
123 | // To prevent a notification popup crash in case of bug, wrap it inside a try/catch | |
124 | try { | |
125 | this.video = hash.video | |
126 | if (this.video) this.setVideoChannelAvatarUrl(this.video.channel) | |
127 | ||
128 | this.videoImport = hash.videoImport | |
129 | ||
130 | this.comment = hash.comment | |
131 | if (this.comment) this.setAccountAvatarUrl(this.comment.account) | |
132 | ||
133 | this.abuse = hash.abuse | |
134 | ||
135 | this.videoBlacklist = hash.videoBlacklist | |
136 | ||
137 | this.account = hash.account | |
138 | if (this.account) this.setAccountAvatarUrl(this.account) | |
139 | ||
140 | this.actorFollow = hash.actorFollow | |
141 | if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower) | |
142 | ||
143 | this.plugin = hash.plugin | |
144 | this.peertube = hash.peertube | |
145 | this.registration = hash.registration | |
146 | ||
147 | this.createdAt = hash.createdAt | |
148 | this.updatedAt = hash.updatedAt | |
149 | ||
150 | switch (this.type) { | |
151 | case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION: | |
152 | this.videoUrl = this.buildVideoUrl(this.video) | |
153 | break | |
154 | ||
155 | case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO: | |
156 | this.videoUrl = this.buildVideoUrl(this.video) | |
157 | break | |
158 | ||
159 | case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO: | |
160 | case UserNotificationType.COMMENT_MENTION: | |
161 | if (!this.comment) break | |
162 | this.accountUrl = this.buildAccountUrl(this.comment.account) | |
163 | this.commentUrl = this.buildCommentUrl(this.comment) | |
164 | break | |
165 | ||
166 | case UserNotificationType.NEW_ABUSE_FOR_MODERATORS: | |
167 | this.abuseUrl = '/admin/moderation/abuses/list' | |
168 | this.abuseQueryParams.search = '#' + this.abuse.id | |
169 | ||
170 | if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video) | |
171 | else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment) | |
172 | else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account) | |
173 | break | |
174 | ||
175 | case UserNotificationType.ABUSE_STATE_CHANGE: | |
176 | this.abuseUrl = '/my-account/abuses' | |
177 | this.abuseQueryParams.search = '#' + this.abuse.id | |
178 | break | |
179 | ||
180 | case UserNotificationType.ABUSE_NEW_MESSAGE: | |
181 | this.abuseUrl = user.hasRight(UserRight.MANAGE_ABUSES) | |
182 | ? '/admin/moderation/abuses/list' | |
183 | : '/my-account/abuses' | |
184 | this.abuseQueryParams.search = '#' + this.abuse.id | |
185 | break | |
186 | ||
187 | case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS: | |
188 | this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list' | |
189 | // Backward compatibility where we did not assign videoBlacklist to this type of notification before | |
190 | if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video } | |
191 | ||
192 | this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video) | |
193 | break | |
194 | ||
195 | case UserNotificationType.BLACKLIST_ON_MY_VIDEO: | |
196 | this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video) | |
197 | break | |
198 | ||
199 | case UserNotificationType.MY_VIDEO_PUBLISHED: | |
200 | this.videoUrl = this.buildVideoUrl(this.video) | |
201 | break | |
202 | ||
203 | case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS: | |
204 | this.videoImportUrl = this.buildVideoImportUrl() | |
205 | this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport) | |
206 | ||
207 | if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video) | |
208 | break | |
209 | ||
210 | case UserNotificationType.MY_VIDEO_IMPORT_ERROR: | |
211 | this.videoImportUrl = this.buildVideoImportUrl() | |
212 | this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport) | |
213 | break | |
214 | ||
215 | case UserNotificationType.NEW_USER_REGISTRATION: | |
216 | this.accountUrl = this.buildAccountUrl(this.account) | |
217 | break | |
218 | ||
219 | case UserNotificationType.NEW_USER_REGISTRATION_REQUEST: | |
220 | this.registrationsUrl = '/admin/moderation/registrations/list' | |
221 | break | |
222 | ||
223 | case UserNotificationType.NEW_FOLLOW: | |
224 | this.accountUrl = this.buildAccountUrl(this.actorFollow.follower) | |
225 | break | |
226 | ||
227 | case UserNotificationType.NEW_INSTANCE_FOLLOWER: | |
228 | this.instanceFollowUrl = '/admin/follows/followers-list' | |
229 | break | |
230 | ||
231 | case UserNotificationType.AUTO_INSTANCE_FOLLOWING: | |
232 | this.instanceFollowUrl = '/admin/follows/following-list' | |
233 | break | |
234 | ||
235 | case UserNotificationType.NEW_PEERTUBE_VERSION: | |
236 | this.peertubeVersionLink = 'https://joinpeertube.org/news' | |
237 | break | |
238 | ||
239 | case UserNotificationType.NEW_PLUGIN_VERSION: | |
240 | this.pluginUrl = `/admin/plugins/list-installed` | |
241 | this.pluginQueryParams.pluginType = this.plugin.type + '' | |
242 | break | |
243 | ||
244 | case UserNotificationType.MY_VIDEO_STUDIO_EDITION_FINISHED: | |
245 | this.videoUrl = this.buildVideoUrl(this.video) | |
246 | break | |
247 | } | |
248 | } catch (err) { | |
249 | this.type = null | |
250 | logger.error(err) | |
251 | } | |
252 | } | |
253 | ||
254 | private buildVideoUrl (video: { uuid: string }) { | |
255 | return Video.buildWatchUrl(video) | |
256 | } | |
257 | ||
258 | private buildAccountUrl (account: { name: string, host: string }) { | |
259 | return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host) | |
260 | } | |
261 | ||
262 | private buildVideoImportUrl () { | |
263 | return '/my-library/video-imports' | |
264 | } | |
265 | ||
266 | private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) { | |
267 | return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName | |
268 | } | |
269 | ||
270 | private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) { | |
271 | return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ] | |
272 | } | |
273 | ||
274 | private setAccountAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) { | |
275 | actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || Account.GET_DEFAULT_AVATAR_URL(48) | |
276 | } | |
277 | ||
278 | private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) { | |
279 | actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || VideoChannel.GET_DEFAULT_AVATAR_URL(48) | |
280 | } | |
281 | } |