Update video duration string to show hours when duration greater than or equal 60min...
[oweals/peertube.git] / client / src / app / shared / video / video.model.ts
1 import { Account } from '@app/shared/account/account.model'
2 import { User } from '../'
3 import { Video as VideoServerModel } from '../../../../../shared'
4 import { Avatar } from '../../../../../shared/models/avatars/avatar.model'
5 import { getAbsoluteAPIUrl } from '../misc/utils'
6
7 export class Video implements VideoServerModel {
8   by: string
9   createdAt: Date
10   updatedAt: Date
11   categoryLabel: string
12   category: number
13   licenceLabel: string
14   licence: number
15   languageLabel: string
16   language: number
17   description: string
18   duration: number
19   durationLabel: string
20   id: number
21   uuid: string
22   isLocal: boolean
23   name: string
24   serverHost: string
25   thumbnailPath: string
26   thumbnailUrl: string
27   previewPath: string
28   previewUrl: string
29   embedPath: string
30   embedUrl: string
31   views: number
32   likes: number
33   dislikes: number
34   nsfw: boolean
35
36   account: {
37     name: string
38     displayName: string
39     url: string
40     host: string
41     avatar: Avatar
42   }
43
44   private static createDurationString (duration: number) {
45     const hours = Math.floor(duration / 3600)
46     const minutes = Math.floor(duration % 3600 / 60)
47     const seconds = duration % 60
48
49     const minutesPadding = minutes >= 10 ? '' : '0'
50     const secondsPadding = seconds >= 10 ? '' : '0'
51     const displayedHours = hours > 0 ? hours.toString() + ':' : ''
52
53     return displayedHours + minutesPadding +
54         minutes.toString() + ':' + secondsPadding + seconds.toString()
55   }
56
57   constructor (hash: VideoServerModel) {
58     const absoluteAPIUrl = getAbsoluteAPIUrl()
59
60     this.createdAt = new Date(hash.createdAt.toString())
61     this.categoryLabel = hash.categoryLabel
62     this.category = hash.category
63     this.licenceLabel = hash.licenceLabel
64     this.licence = hash.licence
65     this.languageLabel = hash.languageLabel
66     this.language = hash.language
67     this.description = hash.description
68     this.duration = hash.duration
69     this.durationLabel = Video.createDurationString(hash.duration)
70     this.id = hash.id
71     this.uuid = hash.uuid
72     this.isLocal = hash.isLocal
73     this.name = hash.name
74     this.thumbnailPath = hash.thumbnailPath
75     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
76     this.previewPath = hash.previewPath
77     this.previewUrl = absoluteAPIUrl + hash.previewPath
78     this.embedPath = hash.embedPath
79     this.embedUrl = absoluteAPIUrl + hash.embedPath
80     this.views = hash.views
81     this.likes = hash.likes
82     this.dislikes = hash.dislikes
83     this.nsfw = hash.nsfw
84     this.account = hash.account
85
86     this.by = Account.CREATE_BY_STRING(hash.account.name, hash.account.host)
87   }
88
89   isVideoNSFWForUser (user: User) {
90     // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
91     return (this.nsfw && (!user || user.displayNSFW === false))
92   }
93 }