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'
10 UserNotification as UserNotificationServer,
14 } from '@shared/models'
16 export class UserNotification implements UserNotificationServer {
18 type: UserNotificationType
22 channel: ActorInfo & { avatarUrl?: string }
36 account: ActorInfo & { avatarUrl?: string }
64 account?: ActorInfo & { avatarUrl?: string }
69 follower: ActorInfo & { avatarUrl?: string }
71 type: 'account' | 'channel' | 'instance'
96 abuseQueryParams?: { [id: string]: string } = {}
98 videoAutoBlacklistUrl?: string
102 videoImportIdentifier?: string
103 videoImportUrl?: string
105 instanceFollowUrl?: string
107 peertubeVersionLink?: string
110 pluginQueryParams?: { [id: string]: string } = {}
112 constructor (hash: UserNotificationServer, user: AuthUser) {
114 this.type = hash.type
115 this.read = hash.read
117 // We assume that some fields exist
118 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
120 this.video = hash.video
121 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
123 this.videoImport = hash.videoImport
125 this.comment = hash.comment
126 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
128 this.abuse = hash.abuse
130 this.videoBlacklist = hash.videoBlacklist
132 this.account = hash.account
133 if (this.account) this.setAccountAvatarUrl(this.account)
135 this.actorFollow = hash.actorFollow
136 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
138 this.plugin = hash.plugin
139 this.peertube = hash.peertube
141 this.createdAt = hash.createdAt
142 this.updatedAt = hash.updatedAt
145 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
146 this.videoUrl = this.buildVideoUrl(this.video)
149 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
150 this.videoUrl = this.buildVideoUrl(this.video)
153 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
154 case UserNotificationType.COMMENT_MENTION:
155 if (!this.comment) break
156 this.accountUrl = this.buildAccountUrl(this.comment.account)
157 this.commentUrl = this.buildCommentUrl(this.comment)
160 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
161 this.abuseUrl = '/admin/moderation/abuses/list'
162 this.abuseQueryParams.search = '#' + this.abuse.id
164 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
165 else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment)
166 else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account)
169 case UserNotificationType.ABUSE_STATE_CHANGE:
170 this.abuseUrl = '/my-account/abuses'
171 this.abuseQueryParams.search = '#' + this.abuse.id
174 case UserNotificationType.ABUSE_NEW_MESSAGE:
175 this.abuseUrl = user.hasRight(UserRight.MANAGE_ABUSES)
176 ? '/admin/moderation/abuses/list'
177 : '/my-account/abuses'
178 this.abuseQueryParams.search = '#' + this.abuse.id
181 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
182 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
183 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
184 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
186 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
189 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
190 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
193 case UserNotificationType.MY_VIDEO_PUBLISHED:
194 this.videoUrl = this.buildVideoUrl(this.video)
197 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
198 this.videoImportUrl = this.buildVideoImportUrl()
199 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
201 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
204 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
205 this.videoImportUrl = this.buildVideoImportUrl()
206 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
209 case UserNotificationType.NEW_USER_REGISTRATION:
210 this.accountUrl = this.buildAccountUrl(this.account)
213 case UserNotificationType.NEW_FOLLOW:
214 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
217 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
218 this.instanceFollowUrl = '/admin/follows/followers-list'
221 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
222 this.instanceFollowUrl = '/admin/follows/following-list'
225 case UserNotificationType.NEW_PEERTUBE_VERSION:
226 this.peertubeVersionLink = 'https://joinpeertube.org/news'
229 case UserNotificationType.NEW_PLUGIN_VERSION:
230 this.pluginUrl = `/admin/plugins/list-installed`
231 this.pluginQueryParams.pluginType = this.plugin.type + ''
240 private buildVideoUrl (video: { uuid: string }) {
241 return '/videos/watch/' + video.uuid
244 private buildAccountUrl (account: { name: string, host: string }) {
245 return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host)
248 private buildVideoImportUrl () {
249 return '/my-library/video-imports'
252 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
253 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
256 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
257 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
260 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
261 actor.avatarUrl = Account.GET_ACTOR_AVATAR_URL(actor) || Account.GET_DEFAULT_AVATAR_URL()
264 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
265 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor) || VideoChannel.GET_DEFAULT_AVATAR_URL()