]>
Commit | Line | Data |
---|---|---|
b7819090 C |
1 | import { |
2 | ChangeDetectionStrategy, | |
3 | ChangeDetectorRef, | |
4 | Component, | |
5 | EventEmitter, | |
6 | Inject, | |
7 | Input, | |
8 | LOCALE_ID, | |
9 | OnInit, | |
10 | Output | |
11 | } from '@angular/core' | |
b1fa3eba C |
12 | import { User } from '../users' |
13 | import { Video } from './video.model' | |
b7819090 C |
14 | import { AuthService, ServerService } from '@app/core' |
15 | import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '../../../../../shared' | |
e2409062 | 16 | import { I18n } from '@ngx-translate/i18n-polyfill' |
3a0fb65c C |
17 | import { VideoActionsDisplayType } from '@app/shared/video/video-actions-dropdown.component' |
18 | import { ScreenService } from '@app/shared/misc/screen.service' | |
b7819090 | 19 | import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' |
51b34a11 | 20 | import { switchMap } from 'rxjs/operators' |
501bc6c2 | 21 | |
22a16e36 | 22 | export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto' |
e2409062 C |
23 | export type MiniatureDisplayOptions = { |
24 | date?: boolean | |
25 | views?: boolean | |
26 | by?: boolean | |
c2caa99b | 27 | avatar?: boolean |
e2409062 C |
28 | privacyLabel?: boolean |
29 | privacyText?: boolean | |
30 | state?: boolean | |
31 | blacklistInfo?: boolean | |
32 | nsfw?: boolean | |
33 | } | |
22a16e36 | 34 | |
501bc6c2 C |
35 | @Component({ |
36 | selector: 'my-video-miniature', | |
ec8d8440 | 37 | styleUrls: [ './video-miniature.component.scss' ], |
89724816 C |
38 | templateUrl: './video-miniature.component.html', |
39 | changeDetection: ChangeDetectionStrategy.OnPush | |
501bc6c2 | 40 | }) |
22a16e36 | 41 | export class VideoMiniatureComponent implements OnInit { |
df98563e C |
42 | @Input() user: User |
43 | @Input() video: Video | |
e2409062 | 44 | |
22a16e36 | 45 | @Input() ownerDisplayType: OwnerDisplayType = 'account' |
e2409062 C |
46 | @Input() displayOptions: MiniatureDisplayOptions = { |
47 | date: true, | |
48 | views: true, | |
49 | by: true, | |
c2caa99b | 50 | avatar: false, |
e2409062 C |
51 | privacyLabel: false, |
52 | privacyText: false, | |
53 | state: false, | |
54 | blacklistInfo: false | |
55 | } | |
56 | @Input() displayAsRow = false | |
3a0fb65c C |
57 | @Input() displayVideoActions = true |
58 | ||
59 | @Output() videoBlacklisted = new EventEmitter() | |
60 | @Output() videoUnblacklisted = new EventEmitter() | |
61 | @Output() videoRemoved = new EventEmitter() | |
62 | ||
63 | videoActionsDisplayOptions: VideoActionsDisplayType = { | |
64 | playlist: true, | |
65 | download: false, | |
66 | update: true, | |
67 | blacklist: true, | |
68 | delete: true, | |
b764380a | 69 | report: true, |
988af781 | 70 | duplicate: true |
3a0fb65c C |
71 | } |
72 | showActions = false | |
ba430d75 | 73 | serverConfig: ServerConfig |
22a16e36 | 74 | |
b7819090 C |
75 | addToWatchLaterText: string |
76 | addedToWatchLaterText: string | |
77 | inWatchLaterPlaylist: boolean | |
78 | ||
79 | watchLaterPlaylist: { | |
80 | id: number | |
81 | playlistElementId?: number | |
82 | } | |
83 | ||
22a16e36 | 84 | private ownerDisplayTypeChosen: 'account' | 'videoChannel' |
501bc6c2 | 85 | |
e2409062 | 86 | constructor ( |
3a0fb65c | 87 | private screenService: ScreenService, |
e2409062 C |
88 | private serverService: ServerService, |
89 | private i18n: I18n, | |
b7819090 C |
90 | private authService: AuthService, |
91 | private videoPlaylistService: VideoPlaylistService, | |
92 | private cd: ChangeDetectorRef, | |
e2409062 | 93 | @Inject(LOCALE_ID) private localeId: string |
b7819090 C |
94 | ) { |
95 | ||
96 | } | |
0883b324 | 97 | |
d1a63fc7 | 98 | get isVideoBlur () { |
ba430d75 | 99 | return this.video.isVideoNSFWForUser(this.user, this.serverConfig) |
d1a63fc7 C |
100 | } |
101 | ||
22a16e36 | 102 | ngOnInit () { |
ba430d75 C |
103 | this.serverConfig = this.serverService.getTmpConfig() |
104 | this.serverService.getConfig() | |
105 | .subscribe(config => this.serverConfig = config) | |
106 | ||
3a0fb65c | 107 | this.setUpBy() |
22a16e36 | 108 | |
8dfceec4 C |
109 | // We rely on mouseenter to lazy load actions |
110 | if (this.screenService.isInTouchScreen()) { | |
743f023c | 111 | this.loadActions() |
22a16e36 | 112 | } |
92fb909c | 113 | } |
22a16e36 C |
114 | |
115 | displayOwnerAccount () { | |
116 | return this.ownerDisplayTypeChosen === 'account' | |
117 | } | |
118 | ||
119 | displayOwnerVideoChannel () { | |
120 | return this.ownerDisplayTypeChosen === 'videoChannel' | |
121 | } | |
017c3dca C |
122 | |
123 | isUnlistedVideo () { | |
124 | return this.video.privacy.id === VideoPrivacy.UNLISTED | |
125 | } | |
126 | ||
127 | isPrivateVideo () { | |
128 | return this.video.privacy.id === VideoPrivacy.PRIVATE | |
129 | } | |
e2409062 C |
130 | |
131 | getStateLabel (video: Video) { | |
dedc7abb C |
132 | if (!video.state) return '' |
133 | ||
e2409062 C |
134 | if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) { |
135 | return this.i18n('Published') | |
136 | } | |
137 | ||
138 | if (video.scheduledUpdate) { | |
139 | const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId) | |
140 | return this.i18n('Publication scheduled on ') + updateAt | |
141 | } | |
142 | ||
143 | if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) { | |
144 | return this.i18n('Waiting transcoding') | |
145 | } | |
146 | ||
147 | if (video.state.id === VideoState.TO_TRANSCODE) { | |
148 | return this.i18n('To transcode') | |
149 | } | |
150 | ||
151 | if (video.state.id === VideoState.TO_IMPORT) { | |
152 | return this.i18n('To import') | |
153 | } | |
154 | ||
155 | return '' | |
156 | } | |
3a0fb65c C |
157 | |
158 | loadActions () { | |
159 | if (this.displayVideoActions) this.showActions = true | |
b7819090 C |
160 | |
161 | this.loadWatchLater() | |
3a0fb65c C |
162 | } |
163 | ||
164 | onVideoBlacklisted () { | |
165 | this.videoBlacklisted.emit() | |
166 | } | |
167 | ||
168 | onVideoUnblacklisted () { | |
169 | this.videoUnblacklisted.emit() | |
170 | } | |
171 | ||
172 | onVideoRemoved () { | |
173 | this.videoRemoved.emit() | |
174 | } | |
175 | ||
b7819090 C |
176 | isUserLoggedIn () { |
177 | return this.authService.isLoggedIn() | |
178 | } | |
179 | ||
180 | onWatchLaterClick (currentState: boolean) { | |
181 | if (currentState === true) this.removeFromWatchLater() | |
182 | else this.addToWatchLater() | |
183 | ||
184 | this.inWatchLaterPlaylist = !currentState | |
185 | } | |
186 | ||
187 | addToWatchLater () { | |
188 | const body = { videoId: this.video.id } | |
189 | ||
190 | this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe( | |
191 | res => { | |
192 | this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id | |
193 | } | |
194 | ) | |
195 | } | |
196 | ||
197 | removeFromWatchLater () { | |
51b34a11 | 198 | this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id) |
b7819090 C |
199 | .subscribe( |
200 | _ => { /* empty */ } | |
201 | ) | |
202 | } | |
203 | ||
204 | isWatchLaterPlaylistDisplayed () { | |
80adb036 | 205 | return this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined |
b7819090 C |
206 | } |
207 | ||
3a0fb65c C |
208 | private setUpBy () { |
209 | if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') { | |
210 | this.ownerDisplayTypeChosen = this.ownerDisplayType | |
211 | return | |
212 | } | |
213 | ||
214 | // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12) | |
215 | // -> Use the account name | |
216 | if ( | |
217 | this.video.channel.name === `${this.video.account.name}_channel` || | |
218 | this.video.channel.name.match(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/) | |
219 | ) { | |
220 | this.ownerDisplayTypeChosen = 'account' | |
221 | } else { | |
222 | this.ownerDisplayTypeChosen = 'videoChannel' | |
223 | } | |
224 | } | |
b7819090 C |
225 | |
226 | private loadWatchLater () { | |
51b34a11 C |
227 | if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return |
228 | ||
229 | this.authService.userInformationLoaded | |
230 | .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id))) | |
231 | .subscribe(existResult => { | |
232 | const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER) | |
233 | const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id) | |
234 | this.inWatchLaterPlaylist = false | |
235 | ||
236 | this.watchLaterPlaylist = { | |
237 | id: watchLaterPlaylist.id | |
238 | } | |
239 | ||
240 | if (existsInWatchLater) { | |
241 | this.inWatchLaterPlaylist = true | |
242 | this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId | |
243 | } | |
244 | ||
245 | this.cd.markForCheck() | |
246 | }) | |
247 | ||
248 | this.videoPlaylistService.runPlaylistCheck(this.video.id) | |
b7819090 | 249 | } |
501bc6c2 | 250 | } |