adc248a1ef902209c6ea5d41e791e9375a5a7472
[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 import { ServerConfig } from '../../../../../shared/models'
8
9 export class Video implements VideoServerModel {
10   by: string
11   createdAt: Date
12   updatedAt: Date
13   publishedAt: Date
14   category: VideoConstant<number>
15   licence: VideoConstant<number>
16   language: VideoConstant<number>
17   description: string
18   duration: number
19   durationLabel: string
20   id: number
21   uuid: string
22   isLocal: boolean
23   name: string
24   serverHost: string
25   thumbnailPath: string
26   thumbnailUrl: string
27   previewPath: string
28   previewUrl: string
29   embedPath: string
30   embedUrl: string
31   views: number
32   likes: number
33   dislikes: number
34   nsfw: boolean
35
36   account: {
37     name: string
38     displayName: string
39     url: string
40     host: string
41     avatar: Avatar
42   }
43
44   private static createDurationString (duration: number) {
45     const hours = Math.floor(duration / 3600)
46     const minutes = Math.floor(duration % 3600 / 60)
47     const seconds = duration % 60
48
49     const minutesPadding = minutes >= 10 ? '' : '0'
50     const secondsPadding = seconds >= 10 ? '' : '0'
51     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
52
53     return displayedHours + minutesPadding +
54         minutes.toString() + ':' + secondsPadding + seconds.toString()
55   }
56
57   constructor (hash: VideoServerModel) {
58     const absoluteAPIUrl = getAbsoluteAPIUrl()
59
60     this.createdAt = new Date(hash.createdAt.toString())
61     this.publishedAt = new Date(hash.publishedAt.toString())
62     this.category = hash.category
63     this.licence = hash.licence
64     this.language = hash.language
65     this.description = hash.description
66     this.duration = hash.duration
67     this.durationLabel = Video.createDurationString(hash.duration)
68     this.id = hash.id
69     this.uuid = hash.uuid
70     this.isLocal = hash.isLocal
71     this.name = hash.name
72     this.thumbnailPath = hash.thumbnailPath
73     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
74     this.previewPath = hash.previewPath
75     this.previewUrl = absoluteAPIUrl + hash.previewPath
76     this.embedPath = hash.embedPath
77     this.embedUrl = absoluteAPIUrl + hash.embedPath
78     this.views = hash.views
79     this.likes = hash.likes
80     this.dislikes = hash.dislikes
81     this.nsfw = hash.nsfw
82     this.account = hash.account
83
84     this.by = Account.CREATE_BY_STRING(hash.account.name, hash.account.host)
85   }
86
87   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
88     // Video is not NSFW, skip
89     if (this.nsfw === false) return false
90
91     // Return user setting if logged in
92     if (user) return user.nsfwPolicy !== 'display'
93
94     // Return default instance config
95     return serverConfig.instance.defaultNSFWPolicy !== 'display'
96   }
97 }