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 }
49 account?: ActorInfo & { avatarUrl?: string }
54 follower: ActorInfo & { avatarUrl?: string }
56 type: 'account' | 'channel' | 'instance'
70 videoAutoBlacklistUrl?: string
72 videoImportIdentifier?: string
73 videoImportUrl?: string
74 instanceFollowUrl?: string
76 constructor (hash: UserNotificationServer) {
81 // We assume that some fields exist
82 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
84 this.video = hash.video
85 if (this.video) this.setAvatarUrl(this.video.channel)
87 this.videoImport = hash.videoImport
89 this.comment = hash.comment
90 if (this.comment) this.setAvatarUrl(this.comment.account)
92 this.abuse = hash.abuse
94 this.videoBlacklist = hash.videoBlacklist
96 this.account = hash.account
97 if (this.account) this.setAvatarUrl(this.account)
99 this.actorFollow = hash.actorFollow
100 if (this.actorFollow) this.setAvatarUrl(this.actorFollow.follower)
102 this.createdAt = hash.createdAt
103 this.updatedAt = hash.updatedAt
106 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
107 this.videoUrl = this.buildVideoUrl(this.video)
110 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
111 this.videoUrl = this.buildVideoUrl(this.video)
114 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
115 case UserNotificationType.COMMENT_MENTION:
116 if (!this.comment) break
117 this.accountUrl = this.buildAccountUrl(this.comment.account)
118 this.commentUrl = [ this.buildVideoUrl(this.comment.video), { threadId: this.comment.threadId } ]
121 case UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS:
122 this.abuseUrl = '/admin/moderation/abuses/list'
124 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
127 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
128 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
129 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
130 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
132 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
135 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
136 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
139 case UserNotificationType.MY_VIDEO_PUBLISHED:
140 this.videoUrl = this.buildVideoUrl(this.video)
143 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
144 this.videoImportUrl = this.buildVideoImportUrl()
145 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
147 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
150 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
151 this.videoImportUrl = this.buildVideoImportUrl()
152 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
155 case UserNotificationType.NEW_USER_REGISTRATION:
156 this.accountUrl = this.buildAccountUrl(this.account)
159 case UserNotificationType.NEW_FOLLOW:
160 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
163 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
164 this.instanceFollowUrl = '/admin/follows/followers-list'
167 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
168 this.instanceFollowUrl = '/admin/follows/following-list'
177 private buildVideoUrl (video: { uuid: string }) {
178 return '/videos/watch/' + video.uuid
181 private buildAccountUrl (account: { name: string, host: string }) {
182 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
185 private buildVideoImportUrl () {
186 return '/my-account/video-imports'
189 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
190 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
193 private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
194 actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)