]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/users/user-notification.model.ts
Don't crash on error in notification popup
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user-notification.model.ts
1 import { UserNotification as UserNotificationServer, UserNotificationType, VideoInfo, ActorInfo } from '../../../../../shared'
2 import { Actor } from '@app/shared/actor/actor.model'
3
4 export class UserNotification implements UserNotificationServer {
5 id: number
6 type: UserNotificationType
7 read: boolean
8
9 video?: VideoInfo & {
10 channel: ActorInfo & { avatarUrl?: string }
11 }
12
13 videoImport?: {
14 id: number
15 video?: VideoInfo
16 torrentName?: string
17 magnetUri?: string
18 targetUrl?: string
19 }
20
21 comment?: {
22 id: number
23 threadId: number
24 account: ActorInfo & { avatarUrl?: string }
25 video: VideoInfo
26 }
27
28 videoAbuse?: {
29 id: number
30 video: VideoInfo
31 }
32
33 videoBlacklist?: {
34 id: number
35 video: VideoInfo
36 }
37
38 account?: ActorInfo & { avatarUrl?: string }
39
40 actorFollow?: {
41 id: number
42 follower: ActorInfo & { avatarUrl?: string }
43 following: {
44 type: 'account' | 'channel'
45 name: string
46 displayName: string
47 }
48 }
49
50 createdAt: string
51 updatedAt: string
52
53 // Additional fields
54 videoUrl?: string
55 commentUrl?: any[]
56 videoAbuseUrl?: string
57 accountUrl?: string
58 videoImportIdentifier?: string
59 videoImportUrl?: string
60
61 constructor (hash: UserNotificationServer) {
62 this.id = hash.id
63 this.type = hash.type
64 this.read = hash.read
65
66 // We assume that some fields exist
67 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
68 try {
69 this.video = hash.video
70 if (this.video) this.setAvatarUrl(this.video.channel)
71
72 this.videoImport = hash.videoImport
73
74 this.comment = hash.comment
75 if (this.comment) this.setAvatarUrl(this.comment.account)
76
77 this.videoAbuse = hash.videoAbuse
78
79 this.videoBlacklist = hash.videoBlacklist
80
81 this.account = hash.account
82 if (this.account) this.setAvatarUrl(this.account)
83
84 this.actorFollow = hash.actorFollow
85 if (this.actorFollow) this.setAvatarUrl(this.actorFollow.follower)
86
87 this.createdAt = hash.createdAt
88 this.updatedAt = hash.updatedAt
89
90 switch (this.type) {
91 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
92 this.videoUrl = this.buildVideoUrl(this.video)
93 break
94
95 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
96 this.videoUrl = this.buildVideoUrl(this.video)
97 break
98
99 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
100 case UserNotificationType.COMMENT_MENTION:
101 this.accountUrl = this.buildAccountUrl(this.comment.account)
102 this.commentUrl = [ this.buildVideoUrl(this.comment.video), { threadId: this.comment.threadId } ]
103 break
104
105 case UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS:
106 this.videoAbuseUrl = '/admin/moderation/video-abuses/list'
107 this.videoUrl = this.buildVideoUrl(this.videoAbuse.video)
108 break
109
110 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
111 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
112 break
113
114 case UserNotificationType.MY_VIDEO_PUBLISHED:
115 this.videoUrl = this.buildVideoUrl(this.video)
116 break
117
118 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
119 this.videoImportUrl = this.buildVideoImportUrl()
120 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
121 this.videoUrl = this.buildVideoUrl(this.videoImport.video)
122 break
123
124 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
125 this.videoImportUrl = this.buildVideoImportUrl()
126 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
127 break
128
129 case UserNotificationType.NEW_USER_REGISTRATION:
130 this.accountUrl = this.buildAccountUrl(this.account)
131 break
132
133 case UserNotificationType.NEW_FOLLOW:
134 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
135 break
136 }
137 } catch (err) {
138 console.error(err)
139 }
140 }
141
142 private buildVideoUrl (video: { uuid: string }) {
143 return '/videos/watch/' + video.uuid
144 }
145
146 private buildAccountUrl (account: { name: string, host: string }) {
147 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
148 }
149
150 private buildVideoImportUrl () {
151 return '/my-account/video-imports'
152 }
153
154 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
155 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
156 }
157
158 private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { path: string } }) {
159 actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)
160 }
161 }