c936a8207468f5a9e16d344189af49907c915056
[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 { durationToString, 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   byVideoChannel: string
12   byAccount: string
13
14   accountAvatarUrl: string
15   videoChannelAvatarUrl: string
16
17   createdAt: Date
18   updatedAt: Date
19   publishedAt: Date
20   originallyPublishedAt: Date | string
21   category: VideoConstant<number>
22   licence: VideoConstant<number>
23   language: VideoConstant<string>
24   privacy: VideoConstant<VideoPrivacy>
25   description: string
26   duration: number
27   durationLabel: string
28   id: number
29   uuid: string
30   isLocal: boolean
31   name: string
32   serverHost: string
33   thumbnailPath: string
34   thumbnailUrl: string
35   previewPath: string
36   previewUrl: string
37   embedPath: string
38   embedUrl: string
39   views: number
40   likes: number
41   dislikes: number
42   nsfw: boolean
43
44   waitTranscoding?: boolean
45   state?: VideoConstant<VideoState>
46   scheduledUpdate?: VideoScheduleUpdate
47   blacklisted?: boolean
48   blacklistedReason?: string
49
50   account: {
51     id: number
52     uuid: string
53     name: string
54     displayName: string
55     url: string
56     host: string
57     avatar?: Avatar
58   }
59
60   channel: {
61     id: number
62     uuid: string
63     name: string
64     displayName: string
65     url: string
66     host: string
67     avatar?: Avatar
68   }
69
70   userHistory?: {
71     currentTime: number
72   }
73
74   static buildClientUrl (videoUUID: string) {
75     return '/videos/watch/' + videoUUID
76   }
77
78   constructor (hash: VideoServerModel, translations = {}) {
79     const absoluteAPIUrl = getAbsoluteAPIUrl()
80
81     this.createdAt = new Date(hash.createdAt.toString())
82     this.publishedAt = new Date(hash.publishedAt.toString())
83     this.category = hash.category
84     this.licence = hash.licence
85     this.language = hash.language
86     this.privacy = hash.privacy
87     this.waitTranscoding = hash.waitTranscoding
88     this.state = hash.state
89     this.description = hash.description
90     this.duration = hash.duration
91     this.durationLabel = durationToString(hash.duration)
92     this.id = hash.id
93     this.uuid = hash.uuid
94     this.isLocal = hash.isLocal
95     this.name = hash.name
96     this.thumbnailPath = hash.thumbnailPath
97     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
98     this.previewPath = hash.previewPath
99     this.previewUrl = absoluteAPIUrl + hash.previewPath
100     this.embedPath = hash.embedPath
101     this.embedUrl = absoluteAPIUrl + hash.embedPath
102     this.views = hash.views
103     this.likes = hash.likes
104     this.dislikes = hash.dislikes
105     this.nsfw = hash.nsfw
106     this.account = hash.account
107     this.channel = hash.channel
108
109     this.byAccount = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
110     this.byVideoChannel = Actor.CREATE_BY_STRING(hash.channel.name, hash.channel.host)
111     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
112     this.videoChannelAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.channel)
113
114     this.category.label = peertubeTranslate(this.category.label, translations)
115     this.licence.label = peertubeTranslate(this.licence.label, translations)
116     this.language.label = peertubeTranslate(this.language.label, translations)
117     this.privacy.label = peertubeTranslate(this.privacy.label, translations)
118
119     this.scheduledUpdate = hash.scheduledUpdate
120     this.originallyPublishedAt = hash.originallyPublishedAt ? new Date(hash.originallyPublishedAt.toString()) : null
121
122     if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
123
124     this.blacklisted = hash.blacklisted
125     this.blacklistedReason = hash.blacklistedReason
126
127     this.userHistory = hash.userHistory
128   }
129
130   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
131     // Video is not NSFW, skip
132     if (this.nsfw === false) return false
133
134     // Return user setting if logged in
135     if (user) return user.nsfwPolicy !== 'display'
136
137     // Return default instance config
138     return serverConfig.instance.defaultNSFWPolicy !== 'display'
139   }
140 }