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'
9 UserNotification as UserNotificationServer,
13 } from '@shared/models'
15 export class UserNotification implements UserNotificationServer {
17 type: UserNotificationType
21 channel: ActorInfo & { avatarUrl?: string }
35 account: ActorInfo & { avatarUrl?: string }
63 account?: ActorInfo & { avatarUrl?: string }
68 follower: ActorInfo & { avatarUrl?: string }
70 type: 'account' | 'channel' | 'instance'
84 abuseQueryParams?: { [id: string]: string } = {}
85 videoAutoBlacklistUrl?: string
87 videoImportIdentifier?: string
88 videoImportUrl?: string
89 instanceFollowUrl?: string
91 constructor (hash: UserNotificationServer, user: AuthUser) {
96 // We assume that some fields exist
97 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
99 this.video = hash.video
100 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
102 this.videoImport = hash.videoImport
104 this.comment = hash.comment
105 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
107 this.abuse = hash.abuse
109 this.videoBlacklist = hash.videoBlacklist
111 this.account = hash.account
112 if (this.account) this.setAccountAvatarUrl(this.account)
114 this.actorFollow = hash.actorFollow
115 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
117 this.createdAt = hash.createdAt
118 this.updatedAt = hash.updatedAt
121 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
122 this.videoUrl = this.buildVideoUrl(this.video)
125 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
126 this.videoUrl = this.buildVideoUrl(this.video)
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)
136 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
137 this.abuseUrl = '/admin/moderation/abuses/list'
138 this.abuseQueryParams.search = '#' + this.abuse.id
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)
145 case UserNotificationType.ABUSE_STATE_CHANGE:
146 this.abuseUrl = '/my-account/abuses'
147 this.abuseQueryParams.search = '#' + this.abuse.id
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
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 }
162 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
165 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
166 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
169 case UserNotificationType.MY_VIDEO_PUBLISHED:
170 this.videoUrl = this.buildVideoUrl(this.video)
173 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
174 this.videoImportUrl = this.buildVideoImportUrl()
175 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
177 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
180 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
181 this.videoImportUrl = this.buildVideoImportUrl()
182 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
185 case UserNotificationType.NEW_USER_REGISTRATION:
186 this.accountUrl = this.buildAccountUrl(this.account)
189 case UserNotificationType.NEW_FOLLOW:
190 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
193 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
194 this.instanceFollowUrl = '/admin/follows/followers-list'
197 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
198 this.instanceFollowUrl = '/admin/follows/following-list'
207 private buildVideoUrl (video: { uuid: string }) {
208 return '/videos/watch/' + video.uuid
211 private buildAccountUrl (account: { name: string, host: string }) {
212 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
215 private buildVideoImportUrl () {
216 return '/my-library/video-imports'
219 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
220 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
223 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
224 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
227 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
228 actor.avatarUrl = Account.GET_ACTOR_AVATAR_URL(actor)
231 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
232 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor)