1 import { ActorInfo, FollowState, UserNotification as UserNotificationServer, UserNotificationType, VideoInfo } from '../../../../../shared'
2 import { Actor } from '@app/shared/actor/actor.model'
4 export class UserNotification implements UserNotificationServer {
6 type: UserNotificationType
10 channel: ActorInfo & { avatarUrl?: string }
24 account: ActorInfo & { avatarUrl?: string }
38 account?: ActorInfo & { avatarUrl?: string }
43 follower: ActorInfo & { avatarUrl?: string }
45 type: 'account' | 'channel' | 'instance'
58 videoAbuseUrl?: string
59 videoAutoBlacklistUrl?: string
61 videoImportIdentifier?: string
62 videoImportUrl?: string
63 instanceFollowUrl?: string
65 constructor (hash: UserNotificationServer) {
70 // We assume that some fields exist
71 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
73 this.video = hash.video
74 if (this.video) this.setAvatarUrl(this.video.channel)
76 this.videoImport = hash.videoImport
78 this.comment = hash.comment
79 if (this.comment) this.setAvatarUrl(this.comment.account)
81 this.videoAbuse = hash.videoAbuse
83 this.videoBlacklist = hash.videoBlacklist
85 this.account = hash.account
86 if (this.account) this.setAvatarUrl(this.account)
88 this.actorFollow = hash.actorFollow
89 if (this.actorFollow) this.setAvatarUrl(this.actorFollow.follower)
91 this.createdAt = hash.createdAt
92 this.updatedAt = hash.updatedAt
95 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
96 this.videoUrl = this.buildVideoUrl(this.video)
99 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
100 this.videoUrl = this.buildVideoUrl(this.video)
103 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
104 case UserNotificationType.COMMENT_MENTION:
105 if (!this.comment) break
106 this.accountUrl = this.buildAccountUrl(this.comment.account)
107 this.commentUrl = [ this.buildVideoUrl(this.comment.video), { threadId: this.comment.threadId } ]
110 case UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS:
111 this.videoAbuseUrl = '/admin/moderation/video-abuses/list'
112 this.videoUrl = this.buildVideoUrl(this.videoAbuse.video)
115 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
116 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
117 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
118 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
120 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
123 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
124 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
127 case UserNotificationType.MY_VIDEO_PUBLISHED:
128 this.videoUrl = this.buildVideoUrl(this.video)
131 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
132 this.videoImportUrl = this.buildVideoImportUrl()
133 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
135 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
138 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
139 this.videoImportUrl = this.buildVideoImportUrl()
140 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
143 case UserNotificationType.NEW_USER_REGISTRATION:
144 this.accountUrl = this.buildAccountUrl(this.account)
147 case UserNotificationType.NEW_FOLLOW:
148 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
151 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
152 this.instanceFollowUrl = '/admin/follows/followers-list'
155 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
156 this.instanceFollowUrl = '/admin/follows/following-list'
165 private buildVideoUrl (video: { uuid: string }) {
166 return '/videos/watch/' + video.uuid
169 private buildAccountUrl (account: { name: string, host: string }) {
170 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
173 private buildVideoImportUrl () {
174 return '/my-account/video-imports'
177 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
178 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
181 private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { path: string } }) {
182 actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)