]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-main/users/user-notification.model.ts
Use 3 tables to represent abuses
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-main / users / user-notification.model.ts
CommitLineData
67ed6552
C
1import { Actor } from '../account/actor.model'
2import { ActorInfo, Avatar, FollowState, UserNotification as UserNotificationServer, UserNotificationType, VideoInfo } from '@shared/models'
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
d95d1559 28 abuse?: {
2f1548fd 29 id: number
d95d1559
C
30
31 video?: VideoInfo
32
33 comment?: {
34 threadId: number
35
36 video: {
37 uuid: string
38 }
39 }
40
41 account?: ActorInfo
2f1548fd
C
42 }
43
44 videoBlacklist?: {
45 id: number
46 video: VideoInfo
47 }
48
457bb213 49 account?: ActorInfo & { avatarUrl?: string }
2f1548fd
C
50
51 actorFollow?: {
52 id: number
846751c9 53 state: FollowState
457bb213 54 follower: ActorInfo & { avatarUrl?: string }
2f1548fd 55 following: {
e1b49ee5 56 type: 'account' | 'channel' | 'instance'
2f1548fd
C
57 name: string
58 displayName: string
e1b49ee5 59 host: string
2f1548fd
C
60 }
61 }
62
63 createdAt: string
64 updatedAt: string
65
66 // Additional fields
67 videoUrl?: string
68 commentUrl?: any[]
d95d1559 69 abuseUrl?: string
7ccddd7b 70 videoAutoBlacklistUrl?: string
2f1548fd
C
71 accountUrl?: string
72 videoImportIdentifier?: string
73 videoImportUrl?: string
8ce1ba6e 74 instanceFollowUrl?: string
2f1548fd
C
75
76 constructor (hash: UserNotificationServer) {
77 this.id = hash.id
78 this.type = hash.type
79 this.read = hash.read
80
344d8be5
C
81 // We assume that some fields exist
82 // To prevent a notification popup crash in case of bug, wrap it inside a try/catch
83 try {
84 this.video = hash.video
85 if (this.video) this.setAvatarUrl(this.video.channel)
86
87 this.videoImport = hash.videoImport
88
89 this.comment = hash.comment
90 if (this.comment) this.setAvatarUrl(this.comment.account)
91
d95d1559 92 this.abuse = hash.abuse
344d8be5
C
93
94 this.videoBlacklist = hash.videoBlacklist
95
96 this.account = hash.account
97 if (this.account) this.setAvatarUrl(this.account)
98
99 this.actorFollow = hash.actorFollow
100 if (this.actorFollow) this.setAvatarUrl(this.actorFollow.follower)
101
102 this.createdAt = hash.createdAt
103 this.updatedAt = hash.updatedAt
104
105 switch (this.type) {
106 case UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION:
107 this.videoUrl = this.buildVideoUrl(this.video)
108 break
109
110 case UserNotificationType.UNBLACKLIST_ON_MY_VIDEO:
111 this.videoUrl = this.buildVideoUrl(this.video)
112 break
113
114 case UserNotificationType.NEW_COMMENT_ON_MY_VIDEO:
115 case UserNotificationType.COMMENT_MENTION:
c5c09c1e 116 if (!this.comment) break
344d8be5
C
117 this.accountUrl = this.buildAccountUrl(this.comment.account)
118 this.commentUrl = [ this.buildVideoUrl(this.comment.video), { threadId: this.comment.threadId } ]
119 break
120
121 case UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS:
d95d1559
C
122 this.abuseUrl = '/admin/moderation/abuses/list'
123
124 if (this.abuse.video) this.videoUrl = this.buildVideoUrl(this.abuse.video)
344d8be5
C
125 break
126
7ccddd7b
JM
127 case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
128 this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
43d35494
C
129 // Backward compatibility where we did not assign videoBlacklist to this type of notification before
130 if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
131
8424c402 132 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
7ccddd7b
JM
133 break
134
344d8be5
C
135 case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
136 this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
137 break
138
139 case UserNotificationType.MY_VIDEO_PUBLISHED:
140 this.videoUrl = this.buildVideoUrl(this.video)
141 break
142
143 case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
144 this.videoImportUrl = this.buildVideoImportUrl()
145 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
6d28a505
C
146
147 if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
344d8be5
C
148 break
149
150 case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
151 this.videoImportUrl = this.buildVideoImportUrl()
152 this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
153 break
154
155 case UserNotificationType.NEW_USER_REGISTRATION:
156 this.accountUrl = this.buildAccountUrl(this.account)
157 break
158
159 case UserNotificationType.NEW_FOLLOW:
160 this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
161 break
8ce1ba6e
C
162
163 case UserNotificationType.NEW_INSTANCE_FOLLOWER:
164 this.instanceFollowUrl = '/admin/follows/followers-list'
165 break
e1b49ee5
C
166
167 case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
168 this.instanceFollowUrl = '/admin/follows/following-list'
169 break
344d8be5
C
170 }
171 } catch (err) {
44b88f18 172 this.type = null
344d8be5 173 console.error(err)
2f1548fd
C
174 }
175 }
176
177 private buildVideoUrl (video: { uuid: string }) {
178 return '/videos/watch/' + video.uuid
179 }
180
38967f7b
C
181 private buildAccountUrl (account: { name: string, host: string }) {
182 return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
2f1548fd
C
183 }
184
185 private buildVideoImportUrl () {
186 return '/my-account/video-imports'
187 }
188
189 private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
190 return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
191 }
192
d95d1559 193 private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { url?: string, path: string } }) {
457bb213
C
194 actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)
195 }
2f1548fd 196}