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