]>
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, | |
9 | UserNotification as UserNotificationServer, | |
10 | UserNotificationType, | |
69524f6e C |
11 | UserRight, |
12 | VideoInfo | |
d573926e | 13 | } from '@shared/models' |
2f1548fd C |
14 | |
15 | export class UserNotification implements UserNotificationServer { | |
16 | id: number | |
17 | type: UserNotificationType | |
18 | read: boolean | |
19 | ||
20 | video?: VideoInfo & { | |
457bb213 | 21 | channel: ActorInfo & { avatarUrl?: string } |
2f1548fd C |
22 | } |
23 | ||
24 | videoImport?: { | |
25 | id: number | |
26 | video?: VideoInfo | |
27 | torrentName?: string | |
28 | magnetUri?: string | |
29 | targetUrl?: string | |
30 | } | |
31 | ||
32 | comment?: { | |
33 | id: number | |
34 | threadId: number | |
457bb213 | 35 | account: ActorInfo & { avatarUrl?: string } |
2f1548fd C |
36 | video: VideoInfo |
37 | } | |
38 | ||
d95d1559 | 39 | abuse?: { |
2f1548fd | 40 | id: number |
d573926e | 41 | state: AbuseState |
d95d1559 C |
42 | |
43 | video?: VideoInfo | |
44 | ||
45 | comment?: { | |
46 | threadId: number | |
47 | ||
48 | video: { | |
cfde28ba | 49 | id: number |
d95d1559 | 50 | uuid: string |
cfde28ba | 51 | name: string |
d95d1559 C |
52 | } |
53 | } | |
54 | ||
55 | account?: ActorInfo | |
2f1548fd C |
56 | } |
57 | ||
58 | videoBlacklist?: { | |
59 | id: number | |
60 | video: VideoInfo | |
61 | } | |
62 | ||
457bb213 | 63 | account?: ActorInfo & { avatarUrl?: string } |
2f1548fd C |
64 | |
65 | actorFollow?: { | |
66 | id: number | |
846751c9 | 67 | state: FollowState |
457bb213 | 68 | follower: ActorInfo & { avatarUrl?: string } |
2f1548fd | 69 | following: { |
e1b49ee5 | 70 | type: 'account' | 'channel' | 'instance' |
2f1548fd C |
71 | name: string |
72 | displayName: string | |
e1b49ee5 | 73 | host: string |
2f1548fd C |
74 | } |
75 | } | |
76 | ||
77 | createdAt: string | |
78 | updatedAt: string | |
79 | ||
80 | // Additional fields | |
81 | videoUrl?: string | |
82 | commentUrl?: any[] | |
d95d1559 | 83 | abuseUrl?: string |
d573926e | 84 | abuseQueryParams?: { [id: string]: string } = {} |
7ccddd7b | 85 | videoAutoBlacklistUrl?: string |
2f1548fd C |
86 | accountUrl?: string |
87 | videoImportIdentifier?: string | |
88 | videoImportUrl?: string | |
8ce1ba6e | 89 | instanceFollowUrl?: string |
2f1548fd | 90 | |
d573926e | 91 | constructor (hash: UserNotificationServer, user: AuthUser) { |
2f1548fd C |
92 | this.id = hash.id |
93 | this.type = hash.type | |
94 | this.read = hash.read | |
95 | ||
344d8be5 C |
96 | // We assume that some fields exist |
97 | // To prevent a notification popup crash in case of bug, wrap it inside a try/catch | |
98 | try { | |
99 | this.video = hash.video | |
c418d483 | 100 | if (this.video) this.setVideoChannelAvatarUrl(this.video.channel) |
344d8be5 C |
101 | |
102 | this.videoImport = hash.videoImport | |
103 | ||
104 | this.comment = hash.comment | |
c418d483 | 105 | if (this.comment) this.setAccountAvatarUrl(this.comment.account) |
344d8be5 | 106 | |
d95d1559 | 107 | this.abuse = hash.abuse |
344d8be5 C |
108 | |
109 | this.videoBlacklist = hash.videoBlacklist | |
110 | ||
111 | this.account = hash.account | |
c418d483 | 112 | if (this.account) this.setAccountAvatarUrl(this.account) |
344d8be5 C |
113 | |
114 | this.actorFollow = hash.actorFollow | |
c418d483 | 115 | if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower) |
344d8be5 C |
116 | |
117 | this.createdAt = hash.createdAt | |
118 | this.updatedAt = hash.updatedAt | |
119 | ||
120 | switch (this.type) { | |
121 | case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION: | |
122 | this.videoUrl = this.buildVideoUrl(this.video) | |
123 | break | |
124 | ||
125 | case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO: | |
126 | this.videoUrl = this.buildVideoUrl(this.video) | |
127 | break | |
128 | ||
129 | case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO: | |
130 | case UserNotificationType.COMMENT_MENTION: | |
c5c09c1e | 131 | if (!this.comment) break |
344d8be5 | 132 | this.accountUrl = this.buildAccountUrl(this.comment.account) |
cfde28ba | 133 | this.commentUrl = this.buildCommentUrl(this.comment) |
344d8be5 C |
134 | break |
135 | ||
310b5219 | 136 | case UserNotificationType.NEW_ABUSE_FOR_MODERATORS: |
d95d1559 | 137 | this.abuseUrl = '/admin/moderation/abuses/list' |
d573926e | 138 | this.abuseQueryParams.search = '#' + this.abuse.id |
d95d1559 C |
139 | |
140 | if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video) | |
cfde28ba C |
141 | else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment) |
142 | else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account) | |
344d8be5 C |
143 | break |
144 | ||
d573926e C |
145 | case UserNotificationType.ABUSE_STATE_CHANGE: |
146 | this.abuseUrl = '/my-account/abuses' | |
147 | this.abuseQueryParams.search = '#' + this.abuse.id | |
148 | break | |
149 | ||
150 | case UserNotificationType.ABUSE_NEW_MESSAGE: | |
151 | this.abuseUrl = user.hasRight(UserRight.MANAGE_ABUSES) | |
152 | ? '/admin/moderation/abuses/list' | |
153 | : '/my-account/abuses' | |
154 | this.abuseQueryParams.search = '#' + this.abuse.id | |
155 | break | |
156 | ||
7ccddd7b JM |
157 | case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS: |
158 | this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list' | |
43d35494 C |
159 | // Backward compatibility where we did not assign videoBlacklist to this type of notification before |
160 | if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video } | |
161 | ||
8424c402 | 162 | this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video) |
7ccddd7b JM |
163 | break |
164 | ||
344d8be5 C |
165 | case UserNotificationType.BLACKLIST_ON_MY_VIDEO: |
166 | this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video) | |
167 | break | |
168 | ||
169 | case UserNotificationType.MY_VIDEO_PUBLISHED: | |
170 | this.videoUrl = this.buildVideoUrl(this.video) | |
171 | break | |
172 | ||
173 | case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS: | |
174 | this.videoImportUrl = this.buildVideoImportUrl() | |
175 | this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport) | |
6d28a505 C |
176 | |
177 | if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video) | |
344d8be5 C |
178 | break |
179 | ||
180 | case UserNotificationType.MY_VIDEO_IMPORT_ERROR: | |
181 | this.videoImportUrl = this.buildVideoImportUrl() | |
182 | this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport) | |
183 | break | |
184 | ||
185 | case UserNotificationType.NEW_USER_REGISTRATION: | |
186 | this.accountUrl = this.buildAccountUrl(this.account) | |
187 | break | |
188 | ||
189 | case UserNotificationType.NEW_FOLLOW: | |
190 | this.accountUrl = this.buildAccountUrl(this.actorFollow.follower) | |
191 | break | |
8ce1ba6e C |
192 | |
193 | case UserNotificationType.NEW_INSTANCE_FOLLOWER: | |
194 | this.instanceFollowUrl = '/admin/follows/followers-list' | |
195 | break | |
e1b49ee5 C |
196 | |
197 | case UserNotificationType.AUTO_INSTANCE_FOLLOWING: | |
198 | this.instanceFollowUrl = '/admin/follows/following-list' | |
199 | break | |
344d8be5 C |
200 | } |
201 | } catch (err) { | |
44b88f18 | 202 | this.type = null |
344d8be5 | 203 | console.error(err) |
2f1548fd C |
204 | } |
205 | } | |
206 | ||
207 | private buildVideoUrl (video: { uuid: string }) { | |
208 | return '/videos/watch/' + video.uuid | |
209 | } | |
210 | ||
38967f7b C |
211 | private buildAccountUrl (account: { name: string, host: string }) { |
212 | return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host) | |
2f1548fd C |
213 | } |
214 | ||
215 | private buildVideoImportUrl () { | |
17119e4a | 216 | return '/my-library/video-imports' |
2f1548fd C |
217 | } |
218 | ||
219 | private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) { | |
220 | return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName | |
221 | } | |
222 | ||
cfde28ba C |
223 | private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) { |
224 | return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ] | |
225 | } | |
226 | ||
c418d483 | 227 | private setAccountAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) { |
228 | actor.avatarUrl = Account.GET_ACTOR_AVATAR_URL(actor) | |
229 | } | |
230 | ||
231 | private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) { | |
232 | actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor) | |
457bb213 | 233 | } |
2f1548fd | 234 | } |