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