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'
15 import { Video } from '../video'
17 export class UserNotification implements UserNotificationServer {
19 type: UserNotificationType
23 channel: ActorInfo & { avatarUrl?: string }
37 account: ActorInfo & { avatarUrl?: string }
61 account?: ActorInfo & { avatarUrl?: string }
66 follower: ActorInfo & { avatarUrl?: string }
68 type: 'account' | 'channel' | 'instance'
93 abuseQueryParams?: { [id: string]: string } = {}
95 videoAutoBlacklistUrl?: string
99 videoImportIdentifier?: string
100 videoImportUrl?: string
102 instanceFollowUrl?: string
104 peertubeVersionLink?: string
107 pluginQueryParams?: { [id: string]: string } = {}
109 constructor (hash: UserNotificationServer, user: AuthUser) {
111 this.type = hash.type
112 this.read = hash.read
114 // We assume that some fields exist
115 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
117 this.video = hash.video
118 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
120 this.videoImport = hash.videoImport
122 this.comment = hash.comment
123 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
125 this.abuse = hash.abuse
127 this.videoBlacklist = hash.videoBlacklist
129 this.account = hash.account
130 if (this.account) this.setAccountAvatarUrl(this.account)
132 this.actorFollow = hash.actorFollow
133 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
135 this.plugin = hash.plugin
136 this.peertube = hash.peertube
138 this.createdAt = hash.createdAt
139 this.updatedAt = hash.updatedAt
142 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
143 this.videoUrl = this.buildVideoUrl(this.video)
146 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
147 this.videoUrl = this.buildVideoUrl(this.video)
150 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
151 case UserNotificationType.COMMENT_MENTION:
152 if (!this.comment) break
153 this.accountUrl = this.buildAccountUrl(this.comment.account)
154 this.commentUrl = this.buildCommentUrl(this.comment)
157 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
158 this.abuseUrl = '/admin/moderation/abuses/list'
159 this.abuseQueryParams.search = '#' + this.abuse.id
161 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
162 else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment)
163 else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account)
166 case UserNotificationType.ABUSE_STATE_CHANGE:
167 this.abuseUrl = '/my-account/abuses'
168 this.abuseQueryParams.search = '#' + this.abuse.id
171 case UserNotificationType.ABUSE_NEW_MESSAGE:
172 this.abuseUrl = user.hasRight(UserRight.MANAGE_ABUSES)
173 ? '/admin/moderation/abuses/list'
174 : '/my-account/abuses'
175 this.abuseQueryParams.search = '#' + this.abuse.id
178 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
179 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
180 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
181 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
183 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
186 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
187 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
190 case UserNotificationType.MY_VIDEO_PUBLISHED:
191 this.videoUrl = this.buildVideoUrl(this.video)
194 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
195 this.videoImportUrl = this.buildVideoImportUrl()
196 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
198 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
201 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
202 this.videoImportUrl = this.buildVideoImportUrl()
203 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
206 case UserNotificationType.NEW_USER_REGISTRATION:
207 this.accountUrl = this.buildAccountUrl(this.account)
210 case UserNotificationType.NEW_FOLLOW:
211 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
214 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
215 this.instanceFollowUrl = '/admin/follows/followers-list'
218 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
219 this.instanceFollowUrl = '/admin/follows/following-list'
222 case UserNotificationType.NEW_PEERTUBE_VERSION:
223 this.peertubeVersionLink = 'https://joinpeertube.org/news'
226 case UserNotificationType.NEW_PLUGIN_VERSION:
227 this.pluginUrl = `/admin/plugins/list-installed`
228 this.pluginQueryParams.pluginType = this.plugin.type + ''
237 private buildVideoUrl (video: { uuid: string }) {
238 return Video.buildWatchUrl(video)
241 private buildAccountUrl (account: { name: string, host: string }) {
242 return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host)
245 private buildVideoImportUrl () {
246 return '/my-library/video-imports'
249 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
250 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
253 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
254 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
257 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) {
258 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || Account.GET_DEFAULT_AVATAR_URL(48)
261 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatars: { width: number, url?: string, path: string }[] }) {
262 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor, 48) || VideoChannel.GET_DEFAULT_AVATAR_URL(48)