]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/shared-main/users/user-notification.model.ts
Reorganize left menu and account menu
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / users / user-notification.model.ts
1 import {
2 AbuseState,
3 ActorInfo,
4 FollowState,
5 UserNotification as UserNotificationServer,
6 UserNotificationType,
7 VideoInfo,
8 UserRight
9 } from '@shared/models'
10 import { Actor } from '../account/actor.model'
11 import { AuthUser } from '@app/core'
12
13 export class UserNotification implements UserNotificationServer {
14 id: number
15 type: UserNotificationType
16 read: boolean
17
18 video?: VideoInfo & {
19 channel: ActorInfo & { avatarUrl?: string }
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
33 account: ActorInfo & { avatarUrl?: string }
34 video: VideoInfo
35 }
36
37 abuse?: {
38 id: number
39 state: AbuseState
40
41 video?: VideoInfo
42
43 comment?: {
44 threadId: number
45
46 video: {
47 id: number
48 uuid: string
49 name: string
50 }
51 }
52
53 account?: ActorInfo
54 }
55
56 videoBlacklist?: {
57 id: number
58 video: VideoInfo
59 }
60
61 account?: ActorInfo & { avatarUrl?: string }
62
63 actorFollow?: {
64 id: number
65 state: FollowState
66 follower: ActorInfo & { avatarUrl?: string }
67 following: {
68 type: 'account' | 'channel' | 'instance'
69 name: string
70 displayName: string
71 host: string
72 }
73 }
74
75 createdAt: string
76 updatedAt: string
77
78 // Additional fields
79 videoUrl?: string
80 commentUrl?: any[]
81 abuseUrl?: string
82 abuseQueryParams?: { [id: string]: string } = {}
83 videoAutoBlacklistUrl?: string
84 accountUrl?: string
85 videoImportIdentifier?: string
86 videoImportUrl?: string
87 instanceFollowUrl?: string
88
89 constructor (hash: UserNotificationServer, user: AuthUser) {
90 this.id = hash.id
91 this.type = hash.type
92 this.read = hash.read
93
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
105 this.abuse = hash.abuse
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:
129 if (!this.comment) break
130 this.accountUrl = this.buildAccountUrl(this.comment.account)
131 this.commentUrl = this.buildCommentUrl(this.comment)
132 break
133
134 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
135 this.abuseUrl = '/admin/moderation/abuses/list'
136 this.abuseQueryParams.search = '#' + this.abuse.id
137
138 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
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)
141 break
142
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
155 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
156 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
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
160 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
161 break
162
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)
174
175 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
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
190
191 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
192 this.instanceFollowUrl = '/admin/follows/followers-list'
193 break
194
195 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
196 this.instanceFollowUrl = '/admin/follows/following-list'
197 break
198 }
199 } catch (err) {
200 this.type = null
201 console.error(err)
202 }
203 }
204
205 private buildVideoUrl (video: { uuid: string }) {
206 return '/videos/watch/' + video.uuid
207 }
208
209 private buildAccountUrl (account: { name: string, host: string }) {
210 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
211 }
212
213 private buildVideoImportUrl () {
214 return '/my-library/video-imports'
215 }
216
217 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
218 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
219 }
220
221 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
222 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
223 }
224
225 private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
226 actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)
227 }
228 }