]>
Commit | Line | Data |
---|---|---|
1 | import { Component, HostListener, OnInit, ViewChild } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { | |
4 | AuthService, | |
5 | AuthUser, | |
6 | CanComponentDeactivate, | |
7 | HooksService, | |
8 | ServerService, | |
9 | UserService | |
10 | } from '@app/core' | |
11 | import { HTMLServerConfig } from '@shared/models' | |
12 | import { VideoEditType } from './shared/video-edit.type' | |
13 | import { VideoGoLiveComponent } from './video-add-components/video-go-live.component' | |
14 | import { VideoImportTorrentComponent } from './video-add-components/video-import-torrent.component' | |
15 | import { VideoImportUrlComponent } from './video-add-components/video-import-url.component' | |
16 | import { VideoUploadComponent } from './video-add-components/video-upload.component' | |
17 | ||
18 | @Component({ | |
19 | selector: 'my-videos-add', | |
20 | templateUrl: './video-add.component.html', | |
21 | styleUrls: [ './video-add.component.scss' ] | |
22 | }) | |
23 | export class VideoAddComponent implements OnInit, CanComponentDeactivate { | |
24 | @ViewChild('videoUpload') videoUpload: VideoUploadComponent | |
25 | @ViewChild('videoImportUrl') videoImportUrl: VideoImportUrlComponent | |
26 | @ViewChild('videoImportTorrent') videoImportTorrent: VideoImportTorrentComponent | |
27 | @ViewChild('videoGoLive') videoGoLive: VideoGoLiveComponent | |
28 | ||
29 | user: AuthUser = null | |
30 | ||
31 | secondStepType: VideoEditType | |
32 | videoName: string | |
33 | ||
34 | activeNav: string | |
35 | ||
36 | uploadMessages: { | |
37 | noQuota: string | |
38 | autoBlock: string | |
39 | quotaLeftDaily: string | |
40 | quotaLeft: string | |
41 | } | |
42 | ||
43 | hasNoQuotaLeft = false | |
44 | hasNoQuotaLeftDaily = false | |
45 | ||
46 | serverConfig: HTMLServerConfig | |
47 | ||
48 | constructor ( | |
49 | private auth: AuthService, | |
50 | private userService: UserService, | |
51 | private hooks: HooksService, | |
52 | private serverService: ServerService, | |
53 | private route: ActivatedRoute, | |
54 | private router: Router | |
55 | ) {} | |
56 | ||
57 | get isContactFormEnabled () { | |
58 | return this.serverConfig.email.enabled && this.serverConfig.contactForm.enabled | |
59 | } | |
60 | ||
61 | get userInformationLoaded () { | |
62 | return this.auth.userInformationLoaded | |
63 | } | |
64 | ||
65 | ngOnInit () { | |
66 | this.user = this.auth.getUser() | |
67 | ||
68 | this.serverConfig = this.serverService.getHTMLConfig() | |
69 | ||
70 | if (this.route.snapshot.fragment) { | |
71 | this.onNavChange(this.route.snapshot.fragment) | |
72 | } | |
73 | ||
74 | this.buildUploadMessages() | |
75 | ||
76 | this.userService.getMyVideoQuotaUsed() | |
77 | .subscribe(data => { | |
78 | // videoQuota left lower than 10% | |
79 | if (data.videoQuotaUsed > this.user.videoQuota * 0.9) { | |
80 | this.hasNoQuotaLeft = true | |
81 | } | |
82 | ||
83 | // unlimited videoQuota | |
84 | if (this.user.videoQuota === -1) { | |
85 | this.hasNoQuotaLeft = false | |
86 | } | |
87 | ||
88 | // videoQuotaDaily left lower than 10% | |
89 | if (data.videoQuotaUsedDaily > this.user.videoQuotaDaily * 0.9) { | |
90 | this.hasNoQuotaLeftDaily = true | |
91 | } | |
92 | ||
93 | // unlimited videoQuotaDaily | |
94 | if (this.user.videoQuotaDaily === -1) { | |
95 | this.hasNoQuotaLeftDaily = false | |
96 | } | |
97 | }) | |
98 | } | |
99 | ||
100 | private async buildUploadMessages () { | |
101 | // eslint-disable-next-line max-len | |
102 | const noQuota = $localize`Sorry, the upload feature is disabled for your account. If you want to add videos, an admin must unlock your quota.` | |
103 | // eslint-disable-next-line max-len | |
104 | const autoBlock = $localize`Uploaded videos are reviewed before publishing for your account. If you want to add videos without moderation review, an admin must turn off your videos auto-block.` | |
105 | // eslint-disable-next-line max-len | |
106 | const quotaLeftDaily = $localize`Your daily video quota is insufficient. If you want to add more videos, you must wait for 24 hours or an admin must increase your daily quota.` | |
107 | // eslint-disable-next-line max-len | |
108 | const quotaLeft = $localize`Your video quota is insufficient. If you want to add more videos, an admin must increase your quota.` | |
109 | ||
110 | const uploadMessages = { | |
111 | noQuota, | |
112 | autoBlock, | |
113 | quotaLeftDaily, | |
114 | quotaLeft | |
115 | } | |
116 | ||
117 | this.uploadMessages = await this.hooks.wrapObject(uploadMessages, 'common', 'filter:upload.messages.create.result') | |
118 | } | |
119 | ||
120 | onNavChange (newActiveNav: string) { | |
121 | this.activeNav = newActiveNav | |
122 | ||
123 | this.router.navigate([], { fragment: this.activeNav }) | |
124 | } | |
125 | ||
126 | onFirstStepDone (type: VideoEditType, videoName: string) { | |
127 | this.secondStepType = type | |
128 | this.videoName = videoName | |
129 | } | |
130 | ||
131 | onError () { | |
132 | this.videoName = undefined | |
133 | this.secondStepType = undefined | |
134 | } | |
135 | ||
136 | @HostListener('window:beforeunload', [ '$event' ]) | |
137 | onUnload (event: any) { | |
138 | const { text, canDeactivate } = this.canDeactivate() | |
139 | ||
140 | if (canDeactivate) return | |
141 | ||
142 | event.returnValue = text | |
143 | return text | |
144 | } | |
145 | ||
146 | canDeactivate (): { canDeactivate: boolean, text?: string } { | |
147 | if (this.secondStepType === 'upload') return this.videoUpload.canDeactivate() | |
148 | if (this.secondStepType === 'import-url') return this.videoImportUrl.canDeactivate() | |
149 | if (this.secondStepType === 'import-torrent') return this.videoImportTorrent.canDeactivate() | |
150 | if (this.secondStepType === 'go-live') return this.videoGoLive.canDeactivate() | |
151 | ||
152 | return { canDeactivate: true } | |
153 | } | |
154 | ||
155 | isVideoImportHttpEnabled () { | |
156 | return this.serverConfig.import.videos.http.enabled | |
157 | } | |
158 | ||
159 | isVideoImportTorrentEnabled () { | |
160 | return this.serverConfig.import.videos.torrent.enabled | |
161 | } | |
162 | ||
163 | isVideoLiveEnabled () { | |
164 | return this.serverConfig.live.enabled | |
165 | } | |
166 | ||
167 | isInSecondStep () { | |
168 | return !!this.secondStepType | |
169 | } | |
170 | ||
171 | isRootUser () { | |
172 | return this.user.username === 'root' | |
173 | } | |
174 | } |