Simplify client syndications
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { Account } from '@app/shared/account/account.model'
2 import { User } from '../'
3 import { Video as VideoServerModel } from '../../../../../shared'
4 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
5 import { VideoConstant } from '../../../../../shared/models/videos/video.model'
6 import { getAbsoluteAPIUrl } from '../misc/utils'
7
8 export class Video implements VideoServerModel {
9   by: string
10   createdAt: Date
11   updatedAt: Date
12   publishedAt: Date
13   category: VideoConstant<number>
14   licence: VideoConstant<number>
15   language: VideoConstant<number>
16   description: string
17   duration: number
18   durationLabel: string
19   id: number
20   uuid: string
21   isLocal: boolean
22   name: string
23   serverHost: string
24   thumbnailPath: string
25   thumbnailUrl: string
26   previewPath: string
27   previewUrl: string
28   embedPath: string
29   embedUrl: string
30   views: number
31   likes: number
32   dislikes: number
33   nsfw: boolean
34
35   account: {
36     name: string
37     displayName: string
38     url: string
39     host: string
40     avatar: Avatar
41   }
42
43   private static createDurationString (duration: number) {
44     const hours = Math.floor(duration / 3600)
45     const minutes = Math.floor(duration % 3600 / 60)
46     const seconds = duration % 60
47
48     const minutesPadding = minutes >= 10 ? '' : '0'
49     const secondsPadding = seconds >= 10 ? '' : '0'
50     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
51
52     return displayedHours + minutesPadding +
53         minutes.toString() + ':' + secondsPadding + seconds.toString()
54   }
55
56   constructor (hash: VideoServerModel) {
57     const absoluteAPIUrl = getAbsoluteAPIUrl()
58
59     this.createdAt = new Date(hash.createdAt.toString())
60     this.publishedAt = new Date(hash.publishedAt.toString())
61     this.category = hash.category
62     this.licence = hash.licence
63     this.language = hash.language
64     this.description = hash.description
65     this.duration = hash.duration
66     this.durationLabel = Video.createDurationString(hash.duration)
67     this.id = hash.id
68     this.uuid = hash.uuid
69     this.isLocal = hash.isLocal
70     this.name = hash.name
71     this.thumbnailPath = hash.thumbnailPath
72     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
73     this.previewPath = hash.previewPath
74     this.previewUrl = absoluteAPIUrl + hash.previewPath
75     this.embedPath = hash.embedPath
76     this.embedUrl = absoluteAPIUrl + hash.embedPath
77     this.views = hash.views
78     this.likes = hash.likes
79     this.dislikes = hash.dislikes
80     this.nsfw = hash.nsfw
81     this.account = hash.account
82
83     this.by = Account.CREATE_BY_STRING(hash.account.name, hash.account.host)
84   }
85
86   isVideoNSFWForUser (user: User) {
87     // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
88     return (this.nsfw && (!user || user.displayNSFW === false))
89   }
90 }