]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/shared/video.model.ts
Fix concurrency error when deleting a video
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / shared / video.model.ts
CommitLineData
93e1258c 1import { Video as VideoServerModel, VideoFile } from '../../../../../shared'
df98563e 2import { User } from '../../shared'
92fb909c 3
69f616ab 4export class Video implements VideoServerModel {
df98563e
C
5 author: string
6 by: string
7 createdAt: Date
6d33593a 8 updatedAt: Date
df98563e
C
9 categoryLabel: string
10 category: number
11 licenceLabel: string
12 licence: number
13 languageLabel: string
14 language: number
15 description: string
16 duration: number
17 durationLabel: string
0a6658fd
C
18 id: number
19 uuid: string
df98563e 20 isLocal: boolean
df98563e
C
21 name: string
22 podHost: string
23 tags: string[]
24 thumbnailPath: string
25 thumbnailUrl: string
43f61d26
C
26 previewPath: string
27 previewUrl: string
df98563e
C
28 views: number
29 likes: number
30 dislikes: number
31 nsfw: boolean
93e1258c 32 files: VideoFile[]
aff038cd 33
df98563e
C
34 private static createByString (author: string, podHost: string) {
35 return author + '@' + podHost
aff038cd
C
36 }
37
df98563e
C
38 private static createDurationString (duration: number) {
39 const minutes = Math.floor(duration / 60)
40 const seconds = duration % 60
41 const minutesPadding = minutes >= 10 ? '' : '0'
42 const secondsPadding = seconds >= 10 ? '' : '0'
4fd8aa32 43
df98563e 44 return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString()
4fd8aa32
C
45 }
46
df98563e 47 constructor (hash: {
501bc6c2 48 author: string,
feb4bdfd 49 createdAt: string,
6e07c3de 50 categoryLabel: string,
69f616ab 51 category: number,
d07137b9 52 licenceLabel: string,
69f616ab 53 licence: number,
df98563e
C
54 languageLabel: string
55 language: number
4fd8aa32 56 description: string,
df98563e 57 duration: number
0a6658fd
C
58 id: number,
59 uuid: string,
4fd8aa32 60 isLocal: boolean,
4fd8aa32 61 name: string,
49abbbbe 62 podHost: string,
00a44645 63 tags: string[],
05a9feaa 64 thumbnailPath: string,
43f61d26 65 previewPath: string,
d38b8281
C
66 views: number,
67 likes: number,
68 dislikes: number,
93e1258c
C
69 nsfw: boolean,
70 files: VideoFile[]
501bc6c2 71 }) {
df98563e
C
72 this.author = hash.author
73 this.createdAt = new Date(hash.createdAt)
74 this.categoryLabel = hash.categoryLabel
75 this.category = hash.category
76 this.licenceLabel = hash.licenceLabel
77 this.licence = hash.licence
78 this.languageLabel = hash.languageLabel
79 this.language = hash.language
80 this.description = hash.description
81 this.duration = hash.duration
82 this.durationLabel = Video.createDurationString(hash.duration)
83 this.id = hash.id
0a6658fd 84 this.uuid = hash.uuid
df98563e 85 this.isLocal = hash.isLocal
df98563e
C
86 this.name = hash.name
87 this.podHost = hash.podHost
88 this.tags = hash.tags
89 this.thumbnailPath = hash.thumbnailPath
90 this.thumbnailUrl = API_URL + hash.thumbnailPath
43f61d26
C
91 this.previewPath = hash.previewPath
92 this.previewUrl = API_URL + hash.previewPath
df98563e
C
93 this.views = hash.views
94 this.likes = hash.likes
95 this.dislikes = hash.dislikes
96 this.nsfw = hash.nsfw
93e1258c 97 this.files = hash.files
4fd8aa32 98
df98563e 99 this.by = Video.createByString(hash.author, hash.podHost)
501bc6c2
C
100 }
101
df98563e
C
102 isRemovableBy (user) {
103 return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true)
198b205c
GS
104 }
105
df98563e
C
106 isBlackistableBy (user) {
107 return user && user.isAdmin() === true && this.isLocal === false
501bc6c2 108 }
92fb909c 109
df98563e
C
110 isUpdatableBy (user) {
111 return user && this.isLocal === true && user.username === this.author
9eee32fc
C
112 }
113
df98563e 114 isVideoNSFWForUser (user: User) {
92fb909c 115 // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos...
df98563e 116 return (this.nsfw && (!user || user.displayNSFW === false))
92fb909c 117 }
d8e689b8 118
93e1258c
C
119 getDefaultMagnetUri () {
120 if (this.files === undefined || this.files.length === 0) return ''
121
122 // TODO: choose the original file
123 return this.files[0].magnetUri
124 }
125
df98563e 126 patch (values: Object) {
d8e689b8 127 Object.keys(values).forEach((key) => {
df98563e
C
128 this[key] = values[key]
129 })
d8e689b8
C
130 }
131
df98563e 132 toJSON () {
d8e689b8
C
133 return {
134 author: this.author,
135 createdAt: this.createdAt,
136 category: this.category,
137 licence: this.licence,
138 language: this.language,
139 description: this.description,
140 duration: this.duration,
141 id: this.id,
142 isLocal: this.isLocal,
d8e689b8
C
143 name: this.name,
144 podHost: this.podHost,
145 tags: this.tags,
146 thumbnailPath: this.thumbnailPath,
147 views: this.views,
148 likes: this.likes,
149 dislikes: this.dislikes,
93e1258c
C
150 nsfw: this.nsfw,
151 files: this.files
df98563e 152 }
d8e689b8 153 }
501bc6c2 154}