]>
Commit | Line | Data |
---|---|---|
1 | import { Video as VideoServerModel, VideoFile } from '../../../../../shared' | |
2 | import { User } from '../../shared' | |
3 | ||
4 | export class Video implements VideoServerModel { | |
5 | author: string | |
6 | by: string | |
7 | createdAt: Date | |
8 | updatedAt: Date | |
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 | |
18 | id: number | |
19 | uuid: string | |
20 | isLocal: boolean | |
21 | name: string | |
22 | podHost: string | |
23 | tags: string[] | |
24 | thumbnailPath: string | |
25 | thumbnailUrl: string | |
26 | previewPath: string | |
27 | previewUrl: string | |
28 | views: number | |
29 | likes: number | |
30 | dislikes: number | |
31 | nsfw: boolean | |
32 | files: VideoFile[] | |
33 | ||
34 | private static createByString (author: string, podHost: string) { | |
35 | return author + '@' + podHost | |
36 | } | |
37 | ||
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' | |
43 | ||
44 | return minutesPadding + minutes.toString() + ':' + secondsPadding + seconds.toString() | |
45 | } | |
46 | ||
47 | constructor (hash: { | |
48 | author: string, | |
49 | createdAt: Date | string, | |
50 | categoryLabel: string, | |
51 | category: number, | |
52 | licenceLabel: string, | |
53 | licence: number, | |
54 | languageLabel: string | |
55 | language: number | |
56 | description: string, | |
57 | duration: number | |
58 | id: number, | |
59 | uuid: string, | |
60 | isLocal: boolean, | |
61 | name: string, | |
62 | podHost: string, | |
63 | tags: string[], | |
64 | thumbnailPath: string, | |
65 | previewPath: string, | |
66 | views: number, | |
67 | likes: number, | |
68 | dislikes: number, | |
69 | nsfw: boolean, | |
70 | files: VideoFile[] | |
71 | }) { | |
72 | this.author = hash.author | |
73 | this.createdAt = new Date(hash.createdAt.toString()) | |
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 | |
84 | this.uuid = hash.uuid | |
85 | this.isLocal = hash.isLocal | |
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 | |
91 | this.previewPath = hash.previewPath | |
92 | this.previewUrl = API_URL + hash.previewPath | |
93 | this.views = hash.views | |
94 | this.likes = hash.likes | |
95 | this.dislikes = hash.dislikes | |
96 | this.nsfw = hash.nsfw | |
97 | this.files = hash.files | |
98 | ||
99 | this.by = Video.createByString(hash.author, hash.podHost) | |
100 | } | |
101 | ||
102 | isRemovableBy (user) { | |
103 | return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true) | |
104 | } | |
105 | ||
106 | isBlackistableBy (user) { | |
107 | return user && user.isAdmin() === true && this.isLocal === false | |
108 | } | |
109 | ||
110 | isUpdatableBy (user) { | |
111 | return user && this.isLocal === true && user.username === this.author | |
112 | } | |
113 | ||
114 | isVideoNSFWForUser (user: User) { | |
115 | // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos... | |
116 | return (this.nsfw && (!user || user.displayNSFW === false)) | |
117 | } | |
118 | ||
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 | ||
126 | patch (values: Object) { | |
127 | Object.keys(values).forEach((key) => { | |
128 | this[key] = values[key] | |
129 | }) | |
130 | } | |
131 | ||
132 | toJSON () { | |
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, | |
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, | |
150 | nsfw: this.nsfw, | |
151 | files: this.files | |
152 | } | |
153 | } | |
154 | } |