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'
5 import { logger } from '@root-helpers/logger'
11 UserNotification as UserNotificationServer,
15 } from '@shared/models'
16 import { Video } from '../video'
18 export class UserNotification implements UserNotificationServer {
20 type: UserNotificationType
24 channel: ActorInfo & { avatarUrl?: string }
38 account: ActorInfo & { avatarUrl?: string }
62 account?: ActorInfo & { avatarUrl?: string }
67 follower: ActorInfo & { avatarUrl?: string }
69 type: 'account' | 'channel' | 'instance'
99 abuseQueryParams?: { [id: string]: string } = {}
101 videoAutoBlacklistUrl?: string
105 registrationsUrl?: string
107 videoImportIdentifier?: string
108 videoImportUrl?: string
110 instanceFollowUrl?: string
112 peertubeVersionLink?: string
115 pluginQueryParams?: { [id: string]: string } = {}
117 constructor (hash: UserNotificationServer, user: AuthUser) {
119 this.type = hash.type
120 this.read = hash.read
122 // We assume that some fields exist
123 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
125 this.video = hash.video
126 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
128 this.videoImport = hash.videoImport
130 this.comment = hash.comment
131 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
133 this.abuse = hash.abuse
135 this.videoBlacklist = hash.videoBlacklist
137 this.account = hash.account
138 if (this.account) this.setAccountAvatarUrl(this.account)
140 this.actorFollow = hash.actorFollow
141 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
143 this.plugin = hash.plugin
144 this.peertube = hash.peertube
145 this.registration = hash.registration
147 this.createdAt = hash.createdAt
148 this.updatedAt = hash.updatedAt
151 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
152 this.videoUrl = this.buildVideoUrl(this.video)
155 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
156 this.videoUrl = this.buildVideoUrl(this.video)
159 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
160 case UserNotificationType.COMMENT_MENTION:
161 if (!this.comment) break
162 this.accountUrl = this.buildAccountUrl(this.comment.account)
163 this.commentUrl = this.buildCommentUrl(this.comment)
166 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
167 this.abuseUrl = '/admin/moderation/abuses/list'
168 this.abuseQueryParams.search = '#' + this.abuse.id
170 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
171 else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment)
172 else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account)
175 case UserNotificationType.ABUSE_STATE_CHANGE:
176 this.abuseUrl = '/my-account/abuses'
177 this.abuseQueryParams.search = '#' + this.abuse.id
180 case UserNotificationType.ABUSE_NEW_MESSAGE:
181 this.abuseUrl = user.hasRight(UserRight.MANAGE_ABUSES)
182 ? '/admin/moderation/abuses/list'
183 : '/my-account/abuses'
184 this.abuseQueryParams.search = '#' + this.abuse.id
187 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
188 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
189 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
190 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
192 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
195 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
196 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
199 case UserNotificationType.MY_VIDEO_PUBLISHED:
200 this.videoUrl = this.buildVideoUrl(this.video)
203 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
204 this.videoImportUrl = this.buildVideoImportUrl()
205 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
207 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
210 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
211 this.videoImportUrl = this.buildVideoImportUrl()
212 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
215 case UserNotificationType.NEW_USER_REGISTRATION:
216 this.accountUrl = this.buildAccountUrl(this.account)
219 case UserNotificationType.NEW_USER_REGISTRATION_REQUEST:
220 this.registrationsUrl = '/admin/moderation/registrations/list'
223 case UserNotificationType.NEW_FOLLOW:
224 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
227 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
228 this.instanceFollowUrl = '/admin/follows/followers-list'
231 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
232 this.instanceFollowUrl = '/admin/follows/following-list'
235 case UserNotificationType.NEW_PEERTUBE_VERSION:
236 this.peertubeVersionLink = 'https://joinpeertube.org/news'
239 case UserNotificationType.NEW_PLUGIN_VERSION:
240 this.pluginUrl = `/admin/plugins/list-installed`
241 this.pluginQueryParams.pluginType = this.plugin.type + ''
244 case UserNotificationType.MY_VIDEO_STUDIO_EDITION_FINISHED:
245 this.videoUrl = this.buildVideoUrl(this.video)
254 private buildVideoUrl (video: { uuid: string }) {
255 return Video.buildWatchUrl(video)
258 private buildAccountUrl (account: { name: string, host: string }) {
259 return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host)
262 private buildVideoImportUrl () {
263 return '/my-library/video-imports'
266 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
267 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
270 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
271 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
274 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) {
275 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || Account.GET_DEFAULT_AVATAR_URL(48)
278 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) {
279 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || VideoChannel.GET_DEFAULT_AVATAR_URL(48)