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 }
65 account?: ActorInfo & { avatarUrl?: string }
70 follower: ActorInfo & { avatarUrl?: string }
72 type: 'account' | 'channel' | 'instance'
97 abuseQueryParams?: { [id: string]: string } = {}
99 videoAutoBlacklistUrl?: string
103 videoImportIdentifier?: string
104 videoImportUrl?: string
106 instanceFollowUrl?: string
108 peertubeVersionLink?: string
111 pluginQueryParams?: { [id: string]: string } = {}
113 constructor (hash: UserNotificationServer, user: AuthUser) {
115 this.type = hash.type
116 this.read = hash.read
118 // We assume that some fields exist
119 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
121 this.video = hash.video
122 if (this.video) this.setVideoChannelAvatarUrl(this.video.channel)
124 this.videoImport = hash.videoImport
126 this.comment = hash.comment
127 if (this.comment) this.setAccountAvatarUrl(this.comment.account)
129 this.abuse = hash.abuse
131 this.videoBlacklist = hash.videoBlacklist
133 this.account = hash.account
134 if (this.account) this.setAccountAvatarUrl(this.account)
136 this.actorFollow = hash.actorFollow
137 if (this.actorFollow) this.setAccountAvatarUrl(this.actorFollow.follower)
139 this.plugin = hash.plugin
140 this.peertube = hash.peertube
142 this.createdAt = hash.createdAt
143 this.updatedAt = hash.updatedAt
146 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
147 this.videoUrl = this.buildVideoUrl(this.video)
150 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
151 this.videoUrl = this.buildVideoUrl(this.video)
154 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
155 case UserNotificationType.COMMENT_MENTION:
156 if (!this.comment) break
157 this.accountUrl = this.buildAccountUrl(this.comment.account)
158 this.commentUrl = this.buildCommentUrl(this.comment)
161 case UserNotificationType.NEW_ABUSE_FOR_MODERATORS:
162 this.abuseUrl = '/admin/moderation/abuses/list'
163 this.abuseQueryParams.search = '#' + this.abuse.id
165 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
166 else if (this.abuse.comment) this.commentUrl = this.buildCommentUrl(this.abuse.comment)
167 else if (this.abuse.account) this.accountUrl = this.buildAccountUrl(this.abuse.account)
170 case UserNotificationType.ABUSE_STATE_CHANGE:
171 this.abuseUrl = '/my-account/abuses'
172 this.abuseQueryParams.search = '#' + this.abuse.id
175 case UserNotificationType.ABUSE_NEW_MESSAGE:
176 this.abuseUrl = user.hasRight(UserRight.MANAGE_ABUSES)
177 ? '/admin/moderation/abuses/list'
178 : '/my-account/abuses'
179 this.abuseQueryParams.search = '#' + this.abuse.id
182 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
183 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
184 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
185 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
187 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
190 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
191 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
194 case UserNotificationType.MY_VIDEO_PUBLISHED:
195 this.videoUrl = this.buildVideoUrl(this.video)
198 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
199 this.videoImportUrl = this.buildVideoImportUrl()
200 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
202 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
205 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
206 this.videoImportUrl = this.buildVideoImportUrl()
207 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
210 case UserNotificationType.NEW_USER_REGISTRATION:
211 this.accountUrl = this.buildAccountUrl(this.account)
214 case UserNotificationType.NEW_FOLLOW:
215 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
218 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
219 this.instanceFollowUrl = '/admin/follows/followers-list'
222 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
223 this.instanceFollowUrl = '/admin/follows/following-list'
226 case UserNotificationType.NEW_PEERTUBE_VERSION:
227 this.peertubeVersionLink = 'https://joinpeertube.org/news'
230 case UserNotificationType.NEW_PLUGIN_VERSION:
231 this.pluginUrl = `/admin/plugins/list-installed`
232 this.pluginQueryParams.pluginType = this.plugin.type + ''
241 private buildVideoUrl (video: { uuid: string }) {
242 return Video.buildWatchUrl(video)
245 private buildAccountUrl (account: { name: string, host: string }) {
246 return '/a/' + Actor.CREATE_BY_STRING(account.name, account.host)
249 private buildVideoImportUrl () {
250 return '/my-library/video-imports'
253 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
254 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
257 private buildCommentUrl (comment: { video: { uuid: string }, threadId: number }) {
258 return [ this.buildVideoUrl(comment.video), { threadId: comment.threadId } ]
261 private setAccountAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
262 actor.avatarUrl = Account.GET_ACTOR_AVATAR_URL(actor) || Account.GET_DEFAULT_AVATAR_URL()
265 private setVideoChannelAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
266 actor.avatarUrl = VideoChannel.GET_ACTOR_AVATAR_URL(actor) || VideoChannel.GET_DEFAULT_AVATAR_URL()