8e46ce44bc9a7507935d0482bed1a1d63eefe07a
[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 minutes = Math.floor(duration / 60)
46     const seconds = duration % 60
47     const minutesPadding = minutes >= 10 ? '' : '0'
48     const secondsPadding = seconds >= 10 ? '' : '0'
49
50     return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
51   }
52
53   constructor (hash: VideoServerModel) {
54     const absoluteAPIUrl = getAbsoluteAPIUrl()
55
56     this.createdAt = new Date(hash.createdAt.toString())
57     this.categoryLabel = hash.categoryLabel
58     this.category = hash.category
59     this.licenceLabel = hash.licenceLabel
60     this.licence = hash.licence
61     this.languageLabel = hash.languageLabel
62     this.language = hash.language
63     this.description = hash.description
64     this.duration = hash.duration
65     this.durationLabel = Video.createDurationString(hash.duration)
66     this.id = hash.id
67     this.uuid = hash.uuid
68     this.isLocal = hash.isLocal
69     this.name = hash.name
70     this.thumbnailPath = hash.thumbnailPath
71     this.thumbnailUrl = absoluteAPIUrl + hash.thumbnailPath
72     this.previewPath = hash.previewPath
73     this.previewUrl = absoluteAPIUrl + hash.previewPath
74     this.embedPath = hash.embedPath
75     this.embedUrl = absoluteAPIUrl + hash.embedPath
76     this.views = hash.views
77     this.likes = hash.likes
78     this.dislikes = hash.dislikes
79     this.nsfw = hash.nsfw
80     this.account = hash.account
81
82     this.by = Account.CREATE_BY_STRING(hash.account.name, hash.account.host)
83   }
84
85   isVideoNSFWForUser (user: User) {
86     // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
87     return (this.nsfw && (!user || user.displayNSFW === false))
88   }
89 }