]>
Commit | Line | Data |
---|---|---|
69524f6e C |
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' | |
d573926e C |
5 | import { |
6 | AbuseState, | |
7 | ActorInfo, | |
8 | FollowState, | |
32a18cbf | 9 | PluginType, |
d573926e C |
10 | UserNotification as UserNotificationServer, |
11 | UserNotificationType, | |
69524f6e C |
12 | UserRight, |
13 | VideoInfo | |
d573926e | 14 | } from '@shared/models' |
d4a8e7a6 | 15 | import { Video } from '../video' |
2f1548fd C |
16 | |
17 | export class UserNotification implements UserNotificationServer { | |
18 | id: number | |
19 | type: UserNotificationType | |
20 | read: boolean | |
21 | ||
22 | video?: VideoInfo & { | |
457bb213 | 23 | channel: ActorInfo & { avatarUrl?: string } |
2f1548fd C |
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 | |
457bb213 | 37 | account: ActorInfo & { avatarUrl?: string } |
2f1548fd C |
38 | video: VideoInfo |
39 | } | |
40 | ||
d95d1559 | 41 | abuse?: { |
2f1548fd | 42 | id: number |
d573926e | 43 | state: AbuseState |
d95d1559 C |
44 | |
45 | video?: VideoInfo | |
46 | ||
47 | comment?: { | |
48 | threadId: number | |
49 | ||
29837f88 | 50 | video: VideoInfo |
d95d1559 C |
51 | } |
52 | ||
53 | account?: ActorInfo | |
2f1548fd C |
54 | } |
55 | ||
56 | videoBlacklist?: { | |
57 | id: number | |
58 | video: VideoInfo | |
59 | } | |
60 | ||
457bb213 | 61 | account?: ActorInfo & { avatarUrl?: string } |
2f1548fd C |
62 | |
63 | actorFollow?: { | |
64 | id: number | |
846751c9 | 65 | state: FollowState |
457bb213 | 66 | follower: ActorInfo & { avatarUrl?: string } |
2f1548fd | 67 | following: { |
e1b49ee5 | 68 | type: 'account' | 'channel' | 'instance' |
2f1548fd C |
69 | name: string |
70 | displayName: string | |
e1b49ee5 | 71 | host: string |
2f1548fd C |
72 | } |
73 | } | |
74 | ||
32a18cbf C |
75 | plugin?: { |
76 | name: string | |
77 | type: PluginType | |
78 | latestVersion: string | |
79 | } | |
80 | ||
81 | peertube?: { | |
82 | latestVersion: string | |
83 | } | |
84 | ||
2f1548fd C |
85 | createdAt: string |
86 | updatedAt: string | |
87 | ||
88 | // Additional fields | |
89 | videoUrl?: string | |
90 | commentUrl?: any[] | |
32a18cbf | 91 | |
d95d1559 | 92 | abuseUrl?: string |
d573926e | 93 | abuseQueryParams?: { [id: string]: string } = {} |
32a18cbf | 94 | |
7ccddd7b | 95 | videoAutoBlacklistUrl?: string |
32a18cbf | 96 | |
2f1548fd | 97 | accountUrl?: string |
32a18cbf | 98 | |
2f1548fd C |
99 | videoImportIdentifier?: string |
100 | videoImportUrl?: string | |
32a18cbf | 101 | |
8ce1ba6e | 102 | instanceFollowUrl?: string |
2f1548fd | 103 | |
32a18cbf C |
104 | peertubeVersionLink?: string |
105 | ||
106 | pluginUrl?: string | |
107 | pluginQueryParams?: { [id: string]: string } = {} | |
108 | ||
d573926e | 109 | constructor (hash: UserNotificationServer, user: AuthUser) { |
2f1548fd C |
110 | this.id = hash.id |
111 | this.type = hash.type | |
112 | this.read = hash.read | |
113 | ||
344d8be5 C |
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 | |
c418d483 | 118 | if (this.video) this.setVideoChannelAvatarUrl(this.video.channel) |
344d8be5 C |
119 | |
120 | this.videoImport = hash.videoImport | |
121 | ||
122 | this.comment = hash.comment | |
c418d483 | 123 | if (this.comment) this.setAccountAvatarUrl(this.comment.account) |
344d8be5 | 124 | |
d95d1559 | 125 | this.abuse = hash.abuse |
344d8be5 C |
126 | |
127 | this.videoBlacklist = hash.videoBlacklist | |
128 | ||
129 | this.account = hash.account | |
c418d483 | 130 | if (this.account) this.setAccountAvatarUrl(this.account) |
344d8be5 C |
131 | |
132 | this.actorFollow = hash.actorFollow | |
c418d483 | 133 | if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower) |
344d8be5 | 134 | |
32a18cbf C |
135 | this.plugin = hash.plugin |
136 | this.peertube = hash.peertube | |
137 | ||
344d8be5 C |
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: | |
c5c09c1e | 152 | if (!this.comment) break |
344d8be5 | 153 | this.accountUrl = this.buildAccountUrl(this.comment.account) |
cfde28ba | 154 | this.commentUrl = this.buildCommentUrl(this.comment) |
344d8be5 C |
155 | break |
156 | ||
310b5219 | 157 | case UserNotificationType.NEW_ABUSE_FOR_MODERATORS: |
d95d1559 | 158 | this.abuseUrl = '/admin/moderation/abuses/list' |
d573926e | 159 | this.abuseQueryParams.search = '#' + this.abuse.id |
d95d1559 C |
160 | |
161 | if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video) | |
cfde28ba C |
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) | |
344d8be5 C |
164 | break |
165 | ||
d573926e C |
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 | ||
7ccddd7b JM |
178 | case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS: |
179 | this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list' | |
43d35494 C |
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 | ||
8424c402 | 183 | this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video) |
7ccddd7b JM |
184 | break |
185 | ||
344d8be5 C |
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) | |
6d28a505 C |
197 | |
198 | if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video) | |
344d8be5 C |
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 | |
8ce1ba6e C |
213 | |
214 | case UserNotificationType.NEW_INSTANCE_FOLLOWER: | |
215 | this.instanceFollowUrl = '/admin/follows/followers-list' | |
216 | break | |
e1b49ee5 C |
217 | |
218 | case UserNotificationType.AUTO_INSTANCE_FOLLOWING: | |
219 | this.instanceFollowUrl = '/admin/follows/following-list' | |
220 | break | |
32a18cbf C |
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 | |
344d8be5 C |
230 | } |
231 | } catch (err) { | |
44b88f18 | 232 | this.type = null |
344d8be5 | 233 | console.error(err) |
2f1548fd C |
234 | } |
235 | } | |
236 | ||
237 | private buildVideoUrl (video: { uuid: string }) { | |
d4a8e7a6 | 238 | return Video.buildWatchUrl(video) |
2f1548fd C |
239 | } |
240 | ||
38967f7b | 241 | private buildAccountUrl (account: { name: string, host: string }) { |
71887396 | 242 | return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host) |
2f1548fd C |
243 | } |
244 | ||
245 | private buildVideoImportUrl () { | |
17119e4a | 246 | return '/my-library/video-imports' |
2f1548fd C |
247 | } |
248 | ||
249 | private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) { | |
250 | return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName | |
251 | } | |
252 | ||
cfde28ba C |
253 | private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) { |
254 | return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ] | |
255 | } | |
256 | ||
d0800f76 | 257 | private setAccountAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) { |
258 | actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || Account.GET_DEFAULT_AVATAR_URL(48) | |
c418d483 | 259 | } |
260 | ||
d0800f76 | 261 | private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) { |
262 | actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || VideoChannel.GET_DEFAULT_AVATAR_URL(48) | |
457bb213 | 263 | } |
2f1548fd | 264 | } |