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