1 import { Actor } from '../account/actor.model'
2 import { ActorInfo, Avatar, FollowState, UserNotification as UserNotificationServer, UserNotificationType, VideoInfo } from '@shared/models'
4 export class UserNotification implements UserNotificationServer {
6 type: UserNotificationType
10 channel: ActorInfo & { avatarUrl?: string }
24 account: ActorInfo & { avatarUrl?: string }
51 account?: ActorInfo & { avatarUrl?: string }
56 follower: ActorInfo & { avatarUrl?: string }
58 type: 'account' | 'channel' | 'instance'
72 videoAutoBlacklistUrl?: string
74 videoImportIdentifier?: string
75 videoImportUrl?: string
76 instanceFollowUrl?: string
78 constructor (hash: UserNotificationServer) {
83 // We assume that some fields exist
84 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
86 this.video = hash.video
87 if (this.video) this.setAvatarUrl(this.video.channel)
89 this.videoImport = hash.videoImport
91 this.comment = hash.comment
92 if (this.comment) this.setAvatarUrl(this.comment.account)
94 this.abuse = hash.abuse
96 this.videoBlacklist = hash.videoBlacklist
98 this.account = hash.account
99 if (this.account) this.setAvatarUrl(this.account)
101 this.actorFollow = hash.actorFollow
102 if (this.actorFollow) this.setAvatarUrl(this.actorFollow.follower)
104 this.createdAt = hash.createdAt
105 this.updatedAt = hash.updatedAt
108 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
109 this.videoUrl = this.buildVideoUrl(this.video)
112 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
113 this.videoUrl = this.buildVideoUrl(this.video)
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)
123 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
124 this.abuseUrl = '/admin/moderation/abuses/list'
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)
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 }
136 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
139 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
140 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
143 case UserNotificationType.MY_VIDEO_PUBLISHED:
144 this.videoUrl = this.buildVideoUrl(this.video)
147 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
148 this.videoImportUrl = this.buildVideoImportUrl()
149 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
151 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
154 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
155 this.videoImportUrl = this.buildVideoImportUrl()
156 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
159 case UserNotificationType.NEW_USER_REGISTRATION:
160 this.accountUrl = this.buildAccountUrl(this.account)
163 case UserNotificationType.NEW_FOLLOW:
164 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
167 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
168 this.instanceFollowUrl = '/admin/follows/followers-list'
171 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
172 this.instanceFollowUrl = '/admin/follows/following-list'
181 private buildVideoUrl (video: { uuid: string }) {
182 return '/videos/watch/' + video.uuid
185 private buildAccountUrl (account: { name: string, host: string }) {
186 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
189 private buildVideoImportUrl () {
190 return '/my-account/video-imports'
193 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
194 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
197 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
198 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
201 private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
202 actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)