]>
Commit | Line | Data |
---|---|---|
1 | import { Actor } from '../account/actor.model' | |
2 | import { ActorInfo, Avatar, FollowState, UserNotification as UserNotificationServer, UserNotificationType, VideoInfo } from '@shared/models' | |
3 | ||
4 | export class UserNotification implements UserNotificationServer { | |
5 | id: number | |
6 | type: UserNotificationType | |
7 | read: boolean | |
8 | ||
9 | video?: VideoInfo & { | |
10 | channel: ActorInfo & { avatarUrl?: string } | |
11 | } | |
12 | ||
13 | videoImport?: { | |
14 | id: number | |
15 | video?: VideoInfo | |
16 | torrentName?: string | |
17 | magnetUri?: string | |
18 | targetUrl?: string | |
19 | } | |
20 | ||
21 | comment?: { | |
22 | id: number | |
23 | threadId: number | |
24 | account: ActorInfo & { avatarUrl?: string } | |
25 | video: VideoInfo | |
26 | } | |
27 | ||
28 | abuse?: { | |
29 | id: number | |
30 | ||
31 | video?: VideoInfo | |
32 | ||
33 | comment?: { | |
34 | threadId: number | |
35 | ||
36 | video: { | |
37 | id: number | |
38 | uuid: string | |
39 | name: string | |
40 | } | |
41 | } | |
42 | ||
43 | account?: ActorInfo | |
44 | } | |
45 | ||
46 | videoBlacklist?: { | |
47 | id: number | |
48 | video: VideoInfo | |
49 | } | |
50 | ||
51 | account?: ActorInfo & { avatarUrl?: string } | |
52 | ||
53 | actorFollow?: { | |
54 | id: number | |
55 | state: FollowState | |
56 | follower: ActorInfo & { avatarUrl?: string } | |
57 | following: { | |
58 | type: 'account' | 'channel' | 'instance' | |
59 | name: string | |
60 | displayName: string | |
61 | host: string | |
62 | } | |
63 | } | |
64 | ||
65 | createdAt: string | |
66 | updatedAt: string | |
67 | ||
68 | // Additional fields | |
69 | videoUrl?: string | |
70 | commentUrl?: any[] | |
71 | abuseUrl?: string | |
72 | videoAutoBlacklistUrl?: string | |
73 | accountUrl?: string | |
74 | videoImportIdentifier?: string | |
75 | videoImportUrl?: string | |
76 | instanceFollowUrl?: string | |
77 | ||
78 | constructor (hash: UserNotificationServer) { | |
79 | this.id = hash.id | |
80 | this.type = hash.type | |
81 | this.read = hash.read | |
82 | ||
83 | // We assume that some fields exist | |
84 | // To prevent a notification popup crash in case of bug, wrap it inside a try/catch | |
85 | try { | |
86 | this.video = hash.video | |
87 | if (this.video) this.setAvatarUrl(this.video.channel) | |
88 | ||
89 | this.videoImport = hash.videoImport | |
90 | ||
91 | this.comment = hash.comment | |
92 | if (this.comment) this.setAvatarUrl(this.comment.account) | |
93 | ||
94 | this.abuse = hash.abuse | |
95 | ||
96 | this.videoBlacklist = hash.videoBlacklist | |
97 | ||
98 | this.account = hash.account | |
99 | if (this.account) this.setAvatarUrl(this.account) | |
100 | ||
101 | this.actorFollow = hash.actorFollow | |
102 | if (this.actorFollow) this.setAvatarUrl(this.actorFollow.follower) | |
103 | ||
104 | this.createdAt = hash.createdAt | |
105 | this.updatedAt = hash.updatedAt | |
106 | ||
107 | switch (this.type) { | |
108 | case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION: | |
109 | this.videoUrl = this.buildVideoUrl(this.video) | |
110 | break | |
111 | ||
112 | case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO: | |
113 | this.videoUrl = this.buildVideoUrl(this.video) | |
114 | break | |
115 | ||
116 | case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO: | |
117 | case UserNotificationType.COMMENT_MENTION: | |
118 | if (!this.comment) break | |
119 | this.accountUrl = this.buildAccountUrl(this.comment.account) | |
120 | this.commentUrl = this.buildCommentUrl(this.comment) | |
121 | break | |
122 | ||
123 | case UserNotificationType.NEW_ABUSE_FOR_MODERATORS: | |
124 | this.abuseUrl = '/admin/moderation/abuses/list' | |
125 | ||
126 | if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video) | |
127 | else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment) | |
128 | else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account) | |
129 | break | |
130 | ||
131 | case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS: | |
132 | this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list' | |
133 | // Backward compatibility where we did not assign videoBlacklist to this type of notification before | |
134 | if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video } | |
135 | ||
136 | this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video) | |
137 | break | |
138 | ||
139 | case UserNotificationType.BLACKLIST_ON_MY_VIDEO: | |
140 | this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video) | |
141 | break | |
142 | ||
143 | case UserNotificationType.MY_VIDEO_PUBLISHED: | |
144 | this.videoUrl = this.buildVideoUrl(this.video) | |
145 | break | |
146 | ||
147 | case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS: | |
148 | this.videoImportUrl = this.buildVideoImportUrl() | |
149 | this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport) | |
150 | ||
151 | if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video) | |
152 | break | |
153 | ||
154 | case UserNotificationType.MY_VIDEO_IMPORT_ERROR: | |
155 | this.videoImportUrl = this.buildVideoImportUrl() | |
156 | this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport) | |
157 | break | |
158 | ||
159 | case UserNotificationType.NEW_USER_REGISTRATION: | |
160 | this.accountUrl = this.buildAccountUrl(this.account) | |
161 | break | |
162 | ||
163 | case UserNotificationType.NEW_FOLLOW: | |
164 | this.accountUrl = this.buildAccountUrl(this.actorFollow.follower) | |
165 | break | |
166 | ||
167 | case UserNotificationType.NEW_INSTANCE_FOLLOWER: | |
168 | this.instanceFollowUrl = '/admin/follows/followers-list' | |
169 | break | |
170 | ||
171 | case UserNotificationType.AUTO_INSTANCE_FOLLOWING: | |
172 | this.instanceFollowUrl = '/admin/follows/following-list' | |
173 | break | |
174 | } | |
175 | } catch (err) { | |
176 | this.type = null | |
177 | console.error(err) | |
178 | } | |
179 | } | |
180 | ||
181 | private buildVideoUrl (video: { uuid: string }) { | |
182 | return '/videos/watch/' + video.uuid | |
183 | } | |
184 | ||
185 | private buildAccountUrl (account: { name: string, host: string }) { | |
186 | return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host) | |
187 | } | |
188 | ||
189 | private buildVideoImportUrl () { | |
190 | return '/my-account/video-imports' | |
191 | } | |
192 | ||
193 | private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) { | |
194 | return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName | |
195 | } | |
196 | ||
197 | private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) { | |
198 | return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ] | |
199 | } | |
200 | ||
201 | private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) { | |
202 | actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor) | |
203 | } | |
204 | } |