Fix removing scheduled update
[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.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   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
44   account: {
45     id: number
46     uuid: string
47     name: string
48     displayName: string
49     url: string
50     host: string
51     avatar: Avatar
52   }
53
54   channel: {
55     id: number
56     uuid: string
57     name: string
58     displayName: string
59     url: string
60     host: string
61     avatar: Avatar
62   }
63
64   private static createDurationString (duration: number) {
65     const hours = Math.floor(duration / 3600)
66     const minutes = Math.floor((duration % 3600) / 60)
67     const seconds = duration % 60
68
69     const minutesPadding = minutes >= 10 ? '' : '0'
70     const secondsPadding = seconds >= 10 ? '' : '0'
71     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
72
73     return displayedHours + minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
74   }
75
76   constructor (hash: VideoServerModel, translations = {}) {
77     const absoluteAPIUrl = getAbsoluteAPIUrl()
78
79     this.createdAt = new Date(hash.createdAt.toString())
80     this.publishedAt = new Date(hash.publishedAt.toString())
81     this.category = hash.category
82     this.licence = hash.licence
83     this.language = hash.language
84     this.privacy = hash.privacy
85     this.waitTranscoding = hash.waitTranscoding
86     this.state = hash.state
87     this.description = hash.description
88     this.duration = hash.duration
89     this.durationLabel = Video.createDurationString(hash.duration)
90     this.id = hash.id
91     this.uuid = hash.uuid
92     this.isLocal = hash.isLocal
93     this.name = hash.name
94     this.thumbnailPath = hash.thumbnailPath
95     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
96     this.previewPath = hash.previewPath
97     this.previewUrl = absoluteAPIUrl + hash.previewPath
98     this.embedPath = hash.embedPath
99     this.embedUrl = absoluteAPIUrl + hash.embedPath
100     this.views = hash.views
101     this.likes = hash.likes
102     this.dislikes = hash.dislikes
103     this.nsfw = hash.nsfw
104     this.account = hash.account
105
106     this.by = Actor.CREATE_BY_STRING(hash.account.name, hash.account.host)
107     this.accountAvatarUrl = Actor.GET_ACTOR_AVATAR_URL(this.account)
108
109     this.category.label = peertubeTranslate(this.category.label, translations)
110     this.licence.label = peertubeTranslate(this.licence.label, translations)
111     this.language.label = peertubeTranslate(this.language.label, translations)
112     this.privacy.label = peertubeTranslate(this.privacy.label, translations)
113
114     this.scheduledUpdate = hash.scheduledUpdate
115     if (this.state) this.state.label = peertubeTranslate(this.state.label, translations)
116   }
117
118   isVideoNSFWForUser (user: User, serverConfig: ServerConfig) {
119     // Video is not NSFW, skip
120     if (this.nsfw === false) return false
121
122     // Return user setting if logged in
123     if (user) return user.nsfwPolicy !== 'display'
124
125     // Return default instance config
126     return serverConfig.instance.defaultNSFWPolicy !== 'display'
127   }
128 }