provide specific engine boundaries for nodejs and yarn
[oweals/peertube.git] / client / src / app / shared / users / user-notification.model.ts
1 import { ActorInfo, FollowState, UserNotification as UserNotificationServer, UserNotificationType, VideoInfo, Avatar } 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           if (!this.comment) break
106           this.accountUrl = this.buildAccountUrl(this.comment.account)
107           this.commentUrl = [ this.buildVideoUrl(this.comment.video), { threadId: this.comment.threadId } ]
108           break
109
110         case UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS:
111           this.videoAbuseUrl = '/admin/moderation/video-abuses/list'
112           this.videoUrl = this.buildVideoUrl(this.videoAbuse.video)
113           break
114
115         case UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS:
116           this.videoAutoBlacklistUrl = '/admin/moderation/video-auto-blacklist/list'
117           // Backward compatibility where we did not assign videoBlacklist to this type of notification before
118           if (!this.videoBlacklist) this.videoBlacklist = { id: null, video: this.video }
119
120           this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
121           break
122
123         case UserNotificationType.BLACKLIST_ON_MY_VIDEO:
124           this.videoUrl = this.buildVideoUrl(this.videoBlacklist.video)
125           break
126
127         case UserNotificationType.MY_VIDEO_PUBLISHED:
128           this.videoUrl = this.buildVideoUrl(this.video)
129           break
130
131         case UserNotificationType.MY_VIDEO_IMPORT_SUCCESS:
132           this.videoImportUrl = this.buildVideoImportUrl()
133           this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
134
135           if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
136           break
137
138         case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
139           this.videoImportUrl = this.buildVideoImportUrl()
140           this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
141           break
142
143         case UserNotificationType.NEW_USER_REGISTRATION:
144           this.accountUrl = this.buildAccountUrl(this.account)
145           break
146
147         case UserNotificationType.NEW_FOLLOW:
148           this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
149           break
150
151         case UserNotificationType.NEW_INSTANCE_FOLLOWER:
152           this.instanceFollowUrl = '/admin/follows/followers-list'
153           break
154
155         case UserNotificationType.AUTO_INSTANCE_FOLLOWING:
156           this.instanceFollowUrl = '/admin/follows/following-list'
157           break
158       }
159     } catch (err) {
160       this.type = null
161       console.error(err)
162     }
163   }
164
165   private buildVideoUrl (video: { uuid: string }) {
166     return '/videos/watch/' + video.uuid
167   }
168
169   private buildAccountUrl (account: { name: string, host: string }) {
170     return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
171   }
172
173   private buildVideoImportUrl () {
174     return '/my-account/video-imports'
175   }
176
177   private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
178     return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
179   }
180
181   private setAvatarUrl (actor: { avatarUrl?: string, avatar?: Avatar }) {
182     actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)
183   }
184 }