Rename streaming playlists routes/directories
[oweals/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
122           if (this.videoImport.video) this.videoUrl = this.buildVideoUrl(this.videoImport.video)
123           break
124
125         case UserNotificationType.MY_VIDEO_IMPORT_ERROR:
126           this.videoImportUrl = this.buildVideoImportUrl()
127           this.videoImportIdentifier = this.buildVideoImportIdentifier(this.videoImport)
128           break
129
130         case UserNotificationType.NEW_USER_REGISTRATION:
131           this.accountUrl = this.buildAccountUrl(this.account)
132           break
133
134         case UserNotificationType.NEW_FOLLOW:
135           this.accountUrl = this.buildAccountUrl(this.actorFollow.follower)
136           break
137       }
138     } catch (err) {
139       console.error(err)
140     }
141   }
142
143   private buildVideoUrl (video: { uuid: string }) {
144     return '/videos/watch/' + video.uuid
145   }
146
147   private buildAccountUrl (account: { name: string, host: string }) {
148     return '/accounts/' + Actor.CREATE_BY_STRING(account.name, account.host)
149   }
150
151   private buildVideoImportUrl () {
152     return '/my-account/video-imports'
153   }
154
155   private buildVideoImportIdentifier (videoImport: { targetUrl?: string, magnetUri?: string, torrentName?: string }) {
156     return videoImport.targetUrl || videoImport.magnetUri || videoImport.torrentName
157   }
158
159   private setAvatarUrl (actor: { avatarUrl?: string, avatar?: { path: string } }) {
160     actor.avatarUrl = Actor.GET_ACTOR_AVATAR_URL(actor)
161   }
162 }