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'
94 abuseQueryParams?: { [id: string]: string } = {}
96 videoAutoBlacklistUrl?: string
100 videoImportIdentifier?: string
101 videoImportUrl?: string
103 instanceFollowUrl?: string
105 peertubeVersionLink?: string
108 pluginQueryParams?: { [id: string]: string } = {}
110 constructor (hash: UserNotificationServer, user: AuthUser) {
112 this.type = hash.type
113 this.read = hash.read
115 // We assume that some fields exist
116 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
118 this.video = hash.video
119 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
121 this.videoImport = hash.videoImport
123 this.comment = hash.comment
124 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
126 this.abuse = hash.abuse
128 this.videoBlacklist = hash.videoBlacklist
130 this.account = hash.account
131 if (this.account) this.setAccountAvatarUrl(this.account)
133 this.actorFollow = hash.actorFollow
134 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
136 this.plugin = hash.plugin
137 this.peertube = hash.peertube
139 this.createdAt = hash.createdAt
140 this.updatedAt = hash.updatedAt
143 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
144 this.videoUrl = this.buildVideoUrl(this.video)
147 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
148 this.videoUrl = this.buildVideoUrl(this.video)
151 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
152 case UserNotificationType.COMMENT_MENTION:
153 if (!this.comment) break
154 this.accountUrl = this.buildAccountUrl(this.comment.account)
155 this.commentUrl = this.buildCommentUrl(this.comment)
158 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
159 this.abuseUrl = '/admin/moderation/abuses/list'
160 this.abuseQueryParams.search = '#' + this.abuse.id
162 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
163 else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment)
164 else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account)
167 case UserNotificationType.ABUSE_STATE_CHANGE:
168 this.abuseUrl = '/my-account/abuses'
169 this.abuseQueryParams.search = '#' + this.abuse.id
172 case UserNotificationType.ABUSE_NEW_MESSAGE:
173 this.abuseUrl = user.hasRight(UserRight.MANAGE_ABUSES)
174 ? '/admin/moderation/abuses/list'
175 : '/my-account/abuses'
176 this.abuseQueryParams.search = '#' + this.abuse.id
179 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
180 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
181 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
182 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
184 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
187 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
188 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
191 case UserNotificationType.MY_VIDEO_PUBLISHED:
192 this.videoUrl = this.buildVideoUrl(this.video)
195 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
196 this.videoImportUrl = this.buildVideoImportUrl()
197 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
199 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
202 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
203 this.videoImportUrl = this.buildVideoImportUrl()
204 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
207 case UserNotificationType.NEW_USER_REGISTRATION:
208 this.accountUrl = this.buildAccountUrl(this.account)
211 case UserNotificationType.NEW_FOLLOW:
212 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
215 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
216 this.instanceFollowUrl = '/admin/follows/followers-list'
219 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
220 this.instanceFollowUrl = '/admin/follows/following-list'
223 case UserNotificationType.NEW_PEERTUBE_VERSION:
224 this.peertubeVersionLink = 'https://joinpeertube.org/news'
227 case UserNotificationType.NEW_PLUGIN_VERSION:
228 this.pluginUrl = `/admin/plugins/list-installed`
229 this.pluginQueryParams.pluginType = this.plugin.type + ''
232 case UserNotificationType.MY_VIDEO_STUDIO_EDITION_FINISHED:
233 this.videoUrl = this.buildVideoUrl(this.video)
242 private buildVideoUrl (video: { uuid: string }) {
243 return Video.buildWatchUrl(video)
246 private buildAccountUrl (account: { name: string, host: string }) {
247 return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host)
250 private buildVideoImportUrl () {
251 return '/my-library/video-imports'
254 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
255 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
258 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
259 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
262 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) {
263 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || Account.GET_DEFAULT_AVATAR_URL(48)
266 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) {
267 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || VideoChannel.GET_DEFAULT_AVATAR_URL(48)