]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/users/user-notification.model.ts
tslint update
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / users / user-notification.model.ts
CommitLineData
457bb213
C
1import { UserNotification as UserNotificationServer, UserNotificationType, VideoInfo, ActorInfo } from '../../../../../shared'
2import { Actor } from '@app/shared/actor/actor.model'
2f1548fd
C
3
4export class UserNotification implements UserNotificationServer {
5 id: number
6 type: UserNotificationType
7 read: boolean
8
9 video?: VideoInfo & {
457bb213 10 channel: ActorInfo & { avatarUrl?: string }
2f1548fd
C
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
457bb213 24 account: ActorInfo & { avatarUrl?: string }
2f1548fd
C
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
457bb213 38 account?: ActorInfo & { avatarUrl?: string }
2f1548fd
C
39
40 actorFollow?: {
41 id: number
457bb213 42 follower: ActorInfo & { avatarUrl?: string }
2f1548fd
C
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
7ccddd7b 57 videoAutoBlacklistUrl?: string
2f1548fd
C
58 accountUrl?: string
59 videoImportIdentifier?: string
60 videoImportUrl?: string
61
62 constructor (hash: UserNotificationServer) {
63 this.id = hash.id
64 this.type = hash.type
65 this.read = hash.read
66
344d8be5
C
67 // We assume that some fields exist
68 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
69 try {
70 this.video = hash.video
71 if (this.video) this.setAvatarUrl(this.video.channel)
72
73 this.videoImport = hash.videoImport
74
75 this.comment = hash.comment
76 if (this.comment) this.setAvatarUrl(this.comment.account)
77
78 this.videoAbuse = hash.videoAbuse
79
80 this.videoBlacklist = hash.videoBlacklist
81
82 this.account = hash.account
83 if (this.account) this.setAvatarUrl(this.account)
84
85 this.actorFollow = hash.actorFollow
86 if (this.actorFollow) this.setAvatarUrl(this.actorFollow.follower)
87
88 this.createdAt = hash.createdAt
89 this.updatedAt = hash.updatedAt
90
91 switch (this.type) {
92 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
93 this.videoUrl = this.buildVideoUrl(this.video)
94 break
95
96 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
97 this.videoUrl = this.buildVideoUrl(this.video)
98 break
99
100 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
101 case UserNotificationType.COMMENT_MENTION:
102 this.accountUrl = this.buildAccountUrl(this.comment.account)
103 this.commentUrl = [ this.buildVideoUrl(this.comment.video), { threadId: this.comment.threadId } ]
104 break
105
106 case UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS:
107 this.videoAbuseUrl = '/admin/moderation/video-abuses/list'
108 this.videoUrl = this.buildVideoUrl(this.videoAbuse.video)
109 break
110
7ccddd7b
JM
111 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
112 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
113 this.videoUrl = this.buildVideoUrl(this.video)
114 break
115
344d8be5
C
116 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
117 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
118 break
119
120 case UserNotificationType.MY_VIDEO_PUBLISHED:
121 this.videoUrl = this.buildVideoUrl(this.video)
122 break
123
124 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
125 this.videoImportUrl = this.buildVideoImportUrl()
126 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
6d28a505
C
127
128 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
344d8be5
C
129 break
130
131 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
132 this.videoImportUrl = this.buildVideoImportUrl()
133 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
134 break
135
136 case UserNotificationType.NEW_USER_REGISTRATION:
137 this.accountUrl = this.buildAccountUrl(this.account)
138 break
139
140 case UserNotificationType.NEW_FOLLOW:
141 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
142 break
143 }
144 } catch (err) {
145 console.error(err)
2f1548fd
C
146 }
147 }
148
149 private buildVideoUrl (video: { uuid: string }) {
150 return '/videos/watch/' + video.uuid
151 }
152
38967f7b
C
153 private buildAccountUrl (account: { name: string, host: string }) {
154 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
2f1548fd
C
155 }
156
157 private buildVideoImportUrl () {
158 return '/my-account/video-imports'
159 }
160
161 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
162 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
163 }
164
457bb213
C
165 private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { path: string } }) {
166 actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)
167 }
2f1548fd 168}