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