d37dc2c3ef969d431f962278b7e0d2045ca83109
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { User } from '../'
2 import { Video as VideoServerModel, VideoPrivacy } from '../../../../../shared'
3 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
4 import { VideoConstant } from '../../../../../shared/models/videos/video.model'
5 import { getAbsoluteAPIUrl } from '../misc/utils'
6 import { ServerConfig } from '../../../../../shared/models'
7 import { Actor } from '@app/shared/actor/actor.model'
8 import { peertubeTranslate } from '@app/shared/i18n/i18n-utils'
9
10 export class Video implements VideoServerModel {
11   by: string
12   accountAvatarUrl: string
13   createdAt: Date
14   updatedAt: Date
15   publishedAt: Date
16   category: VideoConstant<number>
17   licence: VideoConstant<number>
18   language: VideoConstant<string>
19   privacy: VideoConstant<VideoPrivacy>
20   description: string
21   duration: number
22   durationLabel: string
23   id: number
24   uuid: string
25   isLocal: boolean
26   name: string
27   serverHost: string
28   thumbnailPath: string
29   thumbnailUrl: string
30   previewPath: string
31   previewUrl: string
32   embedPath: string
33   embedUrl: string
34   views: number
35   likes: number
36   dislikes: number
37   nsfw: boolean
38
39   account: {
40     id: number
41     uuid: string
42     name: string
43     displayName: string
44     url: string
45     host: string
46     avatar: Avatar
47   }
48
49   channel: {
50     id: number
51     uuid: string
52     name: string
53     displayName: string
54     url: string
55     host: string
56     avatar: Avatar
57   }
58
59   private static createDurationString (duration: number) {
60     const hours = Math.floor(duration / 3600)
61     const minutes = Math.floor(duration % 3600 / 60)
62     const seconds = duration % 60
63
64     const minutesPadding = minutes >= 10 ? '' : '0'
65     const secondsPadding = seconds >= 10 ? '' : '0'
66     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
67
68     return displayedHours + minutesPadding +
69         minutes.toString() + ':' + secondsPadding + seconds.toString()
70   }
71
72   constructor (hash: VideoServerModel, translations = {}) {
73     const absoluteAPIUrl = getAbsoluteAPIUrl()
74
75     this.createdAt = new Date(hash.createdAt.toString())
76     this.publishedAt = new Date(hash.publishedAt.toString())
77     this.category = hash.category
78     this.licence = hash.licence
79     this.language = hash.language
80     this.privacy = hash.privacy
81     this.description = hash.description
82     this.duration = hash.duration
83     this.durationLabel = Video.createDurationString(hash.duration)
84     this.id = hash.id
85     this.uuid = hash.uuid
86     this.isLocal = hash.isLocal
87     this.name = hash.name
88     this.thumbnailPath = hash.thumbnailPath
89     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
90     this.previewPath = hash.previewPath
91     this.previewUrl = absoluteAPIUrl + hash.previewPath
92     this.embedPath = hash.embedPath
93     this.embedUrl = absoluteAPIUrl + hash.embedPath
94     this.views = hash.views
95     this.likes = hash.likes
96     this.dislikes = hash.dislikes
97     this.nsfw = hash.nsfw
98     this.account = hash.account
99
100     this.by = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
101     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
102
103     this.category.label = peertubeTranslate(this.category.label, translations)
104     this.licence.label = peertubeTranslate(this.licence.label, translations)
105     this.language.label = peertubeTranslate(this.language.label, translations)
106     this.privacy.label = peertubeTranslate(this.privacy.label, translations)
107   }
108
109   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
110     // Video is not NSFW, skip
111     if (this.nsfw === false) return false
112
113     // Return user setting if logged in
114     if (user) return user.nsfwPolicy !== 'display'
115
116     // Return default instance config
117     return serverConfig.instance.defaultNSFWPolicy !== 'display'
118   }
119 }