]>
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 | UserNotification as UserNotificationServer, | |
10 | UserNotificationType, | |
11 | UserRight, | |
12 | VideoInfo | |
13 | } from '@shared/models' | |
14 | ||
15 | export class UserNotification implements UserNotificationServer { | |
16 | id: number | |
17 | type: UserNotificationType | |
18 | read: boolean | |
19 | ||
20 | video?: VideoInfo & { | |
21 | channel: ActorInfo & { avatarUrl?: string } | |
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 | |
35 | account: ActorInfo & { avatarUrl?: string } | |
36 | video: VideoInfo | |
37 | } | |
38 | ||
39 | abuse?: { | |
40 | id: number | |
41 | state: AbuseState | |
42 | ||
43 | video?: VideoInfo | |
44 | ||
45 | comment?: { | |
46 | threadId: number | |
47 | ||
48 | video: { | |
49 | id: number | |
50 | uuid: string | |
51 | name: string | |
52 | } | |
53 | } | |
54 | ||
55 | account?: ActorInfo | |
56 | } | |
57 | ||
58 | videoBlacklist?: { | |
59 | id: number | |
60 | video: VideoInfo | |
61 | } | |
62 | ||
63 | account?: ActorInfo & { avatarUrl?: string } | |
64 | ||
65 | actorFollow?: { | |
66 | id: number | |
67 | state: FollowState | |
68 | follower: ActorInfo & { avatarUrl?: string } | |
69 | following: { | |
70 | type: 'account' | 'channel' | 'instance' | |
71 | name: string | |
72 | displayName: string | |
73 | host: string | |
74 | } | |
75 | } | |
76 | ||
77 | createdAt: string | |
78 | updatedAt: string | |
79 | ||
80 | // Additional fields | |
81 | videoUrl?: string | |
82 | commentUrl?: any[] | |
83 | abuseUrl?: string | |
84 | abuseQueryParams?: { [id: string]: string } = {} | |
85 | videoAutoBlacklistUrl?: string | |
86 | accountUrl?: string | |
87 | videoImportIdentifier?: string | |
88 | videoImportUrl?: string | |
89 | instanceFollowUrl?: string | |
90 | ||
91 | constructor (hash: UserNotificationServer, user: AuthUser) { | |
92 | this.id = hash.id | |
93 | this.type = hash.type | |
94 | this.read = hash.read | |
95 | ||
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 | |
100 | if (this.video) this.setVideoChannelAvatarUrl(this.video.channel) | |
101 | ||
102 | this.videoImport = hash.videoImport | |
103 | ||
104 | this.comment = hash.comment | |
105 | if (this.comment) this.setAccountAvatarUrl(this.comment.account) | |
106 | ||
107 | this.abuse = hash.abuse | |
108 | ||
109 | this.videoBlacklist = hash.videoBlacklist | |
110 | ||
111 | this.account = hash.account | |
112 | if (this.account) this.setAccountAvatarUrl(this.account) | |
113 | ||
114 | this.actorFollow = hash.actorFollow | |
115 | if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower) | |
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: | |
131 | if (!this.comment) break | |
132 | this.accountUrl = this.buildAccountUrl(this.comment.account) | |
133 | this.commentUrl = this.buildCommentUrl(this.comment) | |
134 | break | |
135 | ||
136 | case UserNotificationType.NEW_ABUSE_FOR_MODERATORS: | |
137 | this.abuseUrl = '/admin/moderation/abuses/list' | |
138 | this.abuseQueryParams.search = '#' + this.abuse.id | |
139 | ||
140 | if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video) | |
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) | |
143 | break | |
144 | ||
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 | ||
157 | case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS: | |
158 | this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list' | |
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 | ||
162 | this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video) | |
163 | break | |
164 | ||
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) | |
176 | ||
177 | if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video) | |
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 | |
192 | ||
193 | case UserNotificationType.NEW_INSTANCE_FOLLOWER: | |
194 | this.instanceFollowUrl = '/admin/follows/followers-list' | |
195 | break | |
196 | ||
197 | case UserNotificationType.AUTO_INSTANCE_FOLLOWING: | |
198 | this.instanceFollowUrl = '/admin/follows/following-list' | |
199 | break | |
200 | } | |
201 | } catch (err) { | |
202 | this.type = null | |
203 | console.error(err) | |
204 | } | |
205 | } | |
206 | ||
207 | private buildVideoUrl (video: { uuid: string }) { | |
208 | return '/videos/watch/' + video.uuid | |
209 | } | |
210 | ||
211 | private buildAccountUrl (account: { name: string, host: string }) { | |
212 | return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host) | |
213 | } | |
214 | ||
215 | private buildVideoImportUrl () { | |
216 | return '/my-library/video-imports' | |
217 | } | |
218 | ||
219 | private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) { | |
220 | return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName | |
221 | } | |
222 | ||
223 | private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) { | |
224 | return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ] | |
225 | } | |
226 | ||
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) | |
233 | } | |
234 | } |