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