]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/users/user-notification.model.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user-notification.model.ts
1 import { ActorInfo, FollowState, UserNotification as UserNotificationServer, UserNotificationType, VideoInfo } 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 state: FollowState
43 follower: ActorInfo & { avatarUrl?: string }
44 following: {
45 type: 'account' | 'channel' | 'instance'
46 name: string
47 displayName: string
48 host: string
49 }
50 }
51
52 createdAt: string
53 updatedAt: string
54
55 // Additional fields
56 videoUrl?: string
57 commentUrl?: any[]
58 videoAbuseUrl?: string
59 videoAutoBlacklistUrl?: string
60 accountUrl?: string
61 videoImportIdentifier?: string
62 videoImportUrl?: string
63 instanceFollowUrl?: string
64
65 constructor (hash: UserNotificationServer) {
66 this.id = hash.id
67 this.type = hash.type
68 this.read = hash.read
69
70 // We assume that some fields exist
71 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
72 try {
73 this.video = hash.video
74 if (this.video) this.setAvatarUrl(this.video.channel)
75
76 this.videoImport = hash.videoImport
77
78 this.comment = hash.comment
79 if (this.comment) this.setAvatarUrl(this.comment.account)
80
81 this.videoAbuse = hash.videoAbuse
82
83 this.videoBlacklist = hash.videoBlacklist
84
85 this.account = hash.account
86 if (this.account) this.setAvatarUrl(this.account)
87
88 this.actorFollow = hash.actorFollow
89 if (this.actorFollow) this.setAvatarUrl(this.actorFollow.follower)
90
91 this.createdAt = hash.createdAt
92 this.updatedAt = hash.updatedAt
93
94 switch (this.type) {
95 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
96 this.videoUrl = this.buildVideoUrl(this.video)
97 break
98
99 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
100 this.videoUrl = this.buildVideoUrl(this.video)
101 break
102
103 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
104 case UserNotificationType.COMMENT_MENTION:
105 this.accountUrl = this.buildAccountUrl(this.comment.account)
106 this.commentUrl = [ this.buildVideoUrl(this.comment.video), { threadId: this.comment.threadId } ]
107 break
108
109 case UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS:
110 this.videoAbuseUrl = '/admin/moderation/video-abuses/list'
111 this.videoUrl = this.buildVideoUrl(this.videoAbuse.video)
112 break
113
114 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
115 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
116 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
117 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
118
119 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
120 break
121
122 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
123 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
124 break
125
126 case UserNotificationType.MY_VIDEO_PUBLISHED:
127 this.videoUrl = this.buildVideoUrl(this.video)
128 break
129
130 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
131 this.videoImportUrl = this.buildVideoImportUrl()
132 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
133
134 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
135 break
136
137 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
138 this.videoImportUrl = this.buildVideoImportUrl()
139 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
140 break
141
142 case UserNotificationType.NEW_USER_REGISTRATION:
143 this.accountUrl = this.buildAccountUrl(this.account)
144 break
145
146 case UserNotificationType.NEW_FOLLOW:
147 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
148 break
149
150 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
151 this.instanceFollowUrl = '/admin/follows/followers-list'
152 break
153
154 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
155 this.instanceFollowUrl = '/admin/follows/following-list'
156 break
157 }
158 } catch (err) {
159 this.type = null
160 console.error(err)
161 }
162 }
163
164 private buildVideoUrl (video: { uuid: string }) {
165 return '/videos/watch/' + video.uuid
166 }
167
168 private buildAccountUrl (account: { name: string, host: string }) {
169 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
170 }
171
172 private buildVideoImportUrl () {
173 return '/my-account/video-imports'
174 }
175
176 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
177 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
178 }
179
180 private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { path: string } }) {
181 actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)
182 }
183 }