]>
Commit | Line | Data |
---|---|---|
1 | import { User } from '../../shared'; | |
2 | ||
3 | export class Video { | |
4 | author: string; | |
5 | by: string; | |
6 | createdAt: Date; | |
7 | categoryLabel: string; | |
8 | category: string; | |
9 | licenceLabel: string; | |
10 | licence: string; | |
11 | languageLabel: string; | |
12 | language: string; | |
13 | description: string; | |
14 | duration: string; | |
15 | id: string; | |
16 | isLocal: boolean; | |
17 | magnetUri: string; | |
18 | name: string; | |
19 | podHost: string; | |
20 | tags: string[]; | |
21 | thumbnailPath: string; | |
22 | views: number; | |
23 | likes: number; | |
24 | dislikes: number; | |
25 | nsfw: boolean; | |
26 | ||
27 | private static createByString(author: string, podHost: string) { | |
28 | return author + '@' + podHost; | |
29 | } | |
30 | ||
31 | private static createDurationString(duration: number) { | |
32 | const minutes = Math.floor(duration / 60); | |
33 | const seconds = duration % 60; | |
34 | const minutes_padding = minutes >= 10 ? '' : '0'; | |
35 | const seconds_padding = seconds >= 10 ? '' : '0'; | |
36 | ||
37 | return minutes_padding + minutes.toString() + ':' + seconds_padding + seconds.toString(); | |
38 | } | |
39 | ||
40 | constructor(hash: { | |
41 | author: string, | |
42 | createdAt: string, | |
43 | categoryLabel: string, | |
44 | category: string, | |
45 | licenceLabel: string, | |
46 | licence: string, | |
47 | languageLabel: string; | |
48 | language: string; | |
49 | description: string, | |
50 | duration: number; | |
51 | id: string, | |
52 | isLocal: boolean, | |
53 | magnetUri: string, | |
54 | name: string, | |
55 | podHost: string, | |
56 | tags: string[], | |
57 | thumbnailPath: string, | |
58 | views: number, | |
59 | likes: number, | |
60 | dislikes: number, | |
61 | nsfw: boolean | |
62 | }) { | |
63 | this.author = hash.author; | |
64 | this.createdAt = new Date(hash.createdAt); | |
65 | this.categoryLabel = hash.categoryLabel; | |
66 | this.category = hash.category; | |
67 | this.licenceLabel = hash.licenceLabel; | |
68 | this.licence = hash.licence; | |
69 | this.languageLabel = hash.languageLabel; | |
70 | this.language = hash.language; | |
71 | this.description = hash.description; | |
72 | this.duration = Video.createDurationString(hash.duration); | |
73 | this.id = hash.id; | |
74 | this.isLocal = hash.isLocal; | |
75 | this.magnetUri = hash.magnetUri; | |
76 | this.name = hash.name; | |
77 | this.podHost = hash.podHost; | |
78 | this.tags = hash.tags; | |
79 | this.thumbnailPath = hash.thumbnailPath; | |
80 | this.views = hash.views; | |
81 | this.likes = hash.likes; | |
82 | this.dislikes = hash.dislikes; | |
83 | this.nsfw = hash.nsfw; | |
84 | ||
85 | this.by = Video.createByString(hash.author, hash.podHost); | |
86 | } | |
87 | ||
88 | isRemovableBy(user) { | |
89 | return user && this.isLocal === true && (this.author === user.username || user.isAdmin() === true); | |
90 | } | |
91 | ||
92 | isBlackistableBy(user) { | |
93 | return user && user.isAdmin() === true && this.isLocal === false; | |
94 | } | |
95 | ||
96 | isVideoNSFWForUser(user: User) { | |
97 | // If the video is NSFW and the user is not logged in, or the user does not want to display NSFW videos... | |
98 | return (this.nsfw && (!user || user.displayNSFW === false)); | |
99 | } | |
100 | ||
101 | patch(values: Object) { | |
102 | Object.keys(values).forEach((key) => { | |
103 | this[key] = values[key]; | |
104 | }); | |
105 | } | |
106 | ||
107 | toJSON() { | |
108 | return { | |
109 | author: this.author, | |
110 | createdAt: this.createdAt, | |
111 | category: this.category, | |
112 | licence: this.licence, | |
113 | language: this.language, | |
114 | description: this.description, | |
115 | duration: this.duration, | |
116 | id: this.id, | |
117 | isLocal: this.isLocal, | |
118 | magnetUri: this.magnetUri, | |
119 | name: this.name, | |
120 | podHost: this.podHost, | |
121 | tags: this.tags, | |
122 | thumbnailPath: this.thumbnailPath, | |
123 | views: this.views, | |
124 | likes: this.likes, | |
125 | dislikes: this.dislikes, | |
126 | nsfw: this.nsfw | |
127 | }; | |
128 | } | |
129 | } |