5 UserNotification as UserNotificationServer,
9 } from '@shared/models'
10 import { Account, Actor, VideoChannel } from '@app/shared/shared-main'
11 import { AuthUser } from '@app/core'
13 export class UserNotification implements UserNotificationServer {
15 type: UserNotificationType
19 channel: ActorInfo & { avatarUrl?: string }
33 account: ActorInfo & { avatarUrl?: string }
61 account?: ActorInfo & { avatarUrl?: string }
66 follower: ActorInfo & { avatarUrl?: string }
68 type: 'account' | 'channel' | 'instance'
82 abuseQueryParams?: { [id: string]: string } = {}
83 videoAutoBlacklistUrl?: string
85 videoImportIdentifier?: string
86 videoImportUrl?: string
87 instanceFollowUrl?: string
89 constructor (hash: UserNotificationServer, user: AuthUser) {
94 // We assume that some fields exist
95 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
97 this.video = hash.video
98 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
100 this.videoImport = hash.videoImport
102 this.comment = hash.comment
103 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
105 this.abuse = hash.abuse
107 this.videoBlacklist = hash.videoBlacklist
109 this.account = hash.account
110 if (this.account) this.setAccountAvatarUrl(this.account)
112 this.actorFollow = hash.actorFollow
113 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
115 this.createdAt = hash.createdAt
116 this.updatedAt = hash.updatedAt
119 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
120 this.videoUrl = this.buildVideoUrl(this.video)
123 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
124 this.videoUrl = this.buildVideoUrl(this.video)
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)
134 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
135 this.abuseUrl = '/admin/moderation/abuses/list'
136 this.abuseQueryParams.search = '#' + this.abuse.id
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)
143 case UserNotificationType.ABUSE_STATE_CHANGE:
144 this.abuseUrl = '/my-account/abuses'
145 this.abuseQueryParams.search = '#' + this.abuse.id
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
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 }
160 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
163 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
164 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
167 case UserNotificationType.MY_VIDEO_PUBLISHED:
168 this.videoUrl = this.buildVideoUrl(this.video)
171 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
172 this.videoImportUrl = this.buildVideoImportUrl()
173 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
175 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
178 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
179 this.videoImportUrl = this.buildVideoImportUrl()
180 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
183 case UserNotificationType.NEW_USER_REGISTRATION:
184 this.accountUrl = this.buildAccountUrl(this.account)
187 case UserNotificationType.NEW_FOLLOW:
188 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
191 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
192 this.instanceFollowUrl = '/admin/follows/followers-list'
195 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
196 this.instanceFollowUrl = '/admin/follows/following-list'
205 private buildVideoUrl (video: { uuid: string }) {
206 return '/videos/watch/' + video.uuid
209 private buildAccountUrl (account: { name: string, host: string }) {
210 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
213 private buildVideoImportUrl () {
214 return '/my-library/video-imports'
217 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
218 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
221 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
222 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
225 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
226 actor.avatarUrl = Account.GET_ACTOR_AVATAR_URL(actor)
229 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
230 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor)