]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/shared/shared-video-miniature/video-miniature.component.ts
Merge branch 'release/3.2.0' into develop
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / shared-video-miniature / video-miniature.component.ts
CommitLineData
5fb2e288 1import { switchMap } from 'rxjs/operators'
b7819090
C
2import {
3 ChangeDetectionStrategy,
4 ChangeDetectorRef,
5 Component,
6 EventEmitter,
7 Inject,
8 Input,
9 LOCALE_ID,
10 OnInit,
11 Output
12} from '@angular/core'
67ed6552 13import { AuthService, ScreenService, ServerService, User } from '@app/core'
583eb04b 14import { ServerConfig, VideoPlaylistType, VideoPrivacy, VideoState } from '@shared/models'
06ec4bdd 15import { ActorAvatarSize } from '../shared-actor-image/actor-avatar.component'
67ed6552
C
16import { Video } from '../shared-main'
17import { VideoPlaylistService } from '../shared-video-playlist'
18import { VideoActionsDisplayType } from './video-actions-dropdown.component'
501bc6c2 19
e2409062
C
20export type MiniatureDisplayOptions = {
21 date?: boolean
22 views?: boolean
23 by?: boolean
c2caa99b 24 avatar?: boolean
e2409062
C
25 privacyLabel?: boolean
26 privacyText?: boolean
27 state?: boolean
28 blacklistInfo?: boolean
29 nsfw?: boolean
30}
0bdad52f 31export type VideoLinkType = 'internal' | 'lazy-load' | 'external'
22a16e36 32
501bc6c2
C
33@Component({
34 selector: 'my-video-miniature',
ec8d8440 35 styleUrls: [ './video-miniature.component.scss' ],
89724816
C
36 templateUrl: './video-miniature.component.html',
37 changeDetection: ChangeDetectionStrategy.OnPush
501bc6c2 38})
22a16e36 39export class VideoMiniatureComponent implements OnInit {
df98563e
C
40 @Input() user: User
41 @Input() video: Video
e2409062 42
e2409062
C
43 @Input() displayOptions: MiniatureDisplayOptions = {
44 date: true,
45 views: true,
46 by: true,
c2caa99b 47 avatar: false,
e2409062
C
48 privacyLabel: false,
49 privacyText: false,
50 state: false,
51 blacklistInfo: false
52 }
3a0fb65c 53 @Input() displayVideoActions = true
0f7407d9 54
06ec4bdd
C
55 @Input() actorImageSize: ActorAvatarSize = '40'
56
0f7407d9 57 @Input() displayAsRow = false
3a0fb65c 58
0bdad52f 59 @Input() videoLinkType: VideoLinkType = 'internal'
5fb2e288 60
5baee5fc
RK
61 @Output() videoBlocked = new EventEmitter()
62 @Output() videoUnblocked = new EventEmitter()
3a0fb65c 63 @Output() videoRemoved = new EventEmitter()
d473fd94 64 @Output() videoAccountMuted = new EventEmitter()
3a0fb65c
C
65
66 videoActionsDisplayOptions: VideoActionsDisplayType = {
67 playlist: true,
68 download: false,
69 update: true,
70 blacklist: true,
71 delete: true,
b764380a 72 report: true,
d473fd94
RK
73 duplicate: true,
74 mute: true
3a0fb65c
C
75 }
76 showActions = false
ba430d75 77 serverConfig: ServerConfig
22a16e36 78
b7819090
C
79 addToWatchLaterText: string
80 addedToWatchLaterText: string
81 inWatchLaterPlaylist: boolean
435258ea 82 channelLinkTitle = ''
b7819090
C
83
84 watchLaterPlaylist: {
85 id: number
86 playlistElementId?: number
87 }
88
0bdad52f
C
89 videoRouterLink: any[] = []
90 videoHref: string
91 videoTarget: string
5fb2e288 92
733dbc53 93 private ownerDisplayType: 'account' | 'videoChannel'
501bc6c2 94
e2409062 95 constructor (
3a0fb65c 96 private screenService: ScreenService,
e2409062 97 private serverService: ServerService,
b7819090
C
98 private authService: AuthService,
99 private videoPlaylistService: VideoPlaylistService,
100 private cd: ChangeDetectorRef,
e2409062 101 @Inject(LOCALE_ID) private localeId: string
435258ea 102 ) {}
0883b324 103
d1a63fc7 104 get isVideoBlur () {
ba430d75 105 return this.video.isVideoNSFWForUser(this.user, this.serverConfig)
d1a63fc7
C
106 }
107
22a16e36 108 ngOnInit () {
ba430d75
C
109 this.serverConfig = this.serverService.getTmpConfig()
110 this.serverService.getConfig()
5fb2e288
C
111 .subscribe(config => {
112 this.serverConfig = config
113 this.buildVideoLink()
114 })
ba430d75 115
3a0fb65c 116 this.setUpBy()
22a16e36 117
66357162 118 this.channelLinkTitle = $localize`${this.video.channel.name} (channel page)`
435258ea 119
8dfceec4
C
120 // We rely on mouseenter to lazy load actions
121 if (this.screenService.isInTouchScreen()) {
743f023c 122 this.loadActions()
22a16e36 123 }
92fb909c 124 }
22a16e36 125
5fb2e288 126 buildVideoLink () {
0bdad52f 127 if (this.videoLinkType === 'internal' || !this.video.url) {
a1eda903 128 this.videoRouterLink = [ '/w', this.video.uuid ]
0bdad52f
C
129 return
130 }
5fb2e288 131
0bdad52f
C
132 if (this.videoLinkType === 'external') {
133 this.videoRouterLink = null
134 this.videoHref = this.video.url
135 this.videoTarget = '_blank'
5fb2e288
C
136 return
137 }
138
0bdad52f
C
139 // Lazy load
140 this.videoRouterLink = [ '/search/lazy-load-video', { url: this.video.url } ]
5fb2e288
C
141 }
142
22a16e36 143 displayOwnerAccount () {
733dbc53 144 return this.ownerDisplayType === 'account'
22a16e36
C
145 }
146
147 displayOwnerVideoChannel () {
733dbc53 148 return this.ownerDisplayType === 'videoChannel'
22a16e36 149 }
017c3dca
C
150
151 isUnlistedVideo () {
152 return this.video.privacy.id === VideoPrivacy.UNLISTED
153 }
154
155 isPrivateVideo () {
156 return this.video.privacy.id === VideoPrivacy.PRIVATE
157 }
e2409062
C
158
159 getStateLabel (video: Video) {
dedc7abb
C
160 if (!video.state) return ''
161
e2409062 162 if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
66357162 163 return $localize`Published`
e2409062
C
164 }
165
166 if (video.scheduledUpdate) {
167 const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
66357162 168 return $localize`Publication scheduled on ` + updateAt
e2409062
C
169 }
170
171 if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
66357162 172 return $localize`Waiting transcoding`
e2409062
C
173 }
174
175 if (video.state.id === VideoState.TO_TRANSCODE) {
66357162 176 return $localize`To transcode`
e2409062
C
177 }
178
179 if (video.state.id === VideoState.TO_IMPORT) {
66357162 180 return $localize`To import`
e2409062
C
181 }
182
183 return ''
184 }
3a0fb65c
C
185
186 loadActions () {
187 if (this.displayVideoActions) this.showActions = true
b7819090
C
188
189 this.loadWatchLater()
3a0fb65c
C
190 }
191
5baee5fc
RK
192 onVideoBlocked () {
193 this.videoBlocked.emit()
3a0fb65c
C
194 }
195
5baee5fc
RK
196 onVideoUnblocked () {
197 this.videoUnblocked.emit()
3a0fb65c
C
198 }
199
200 onVideoRemoved () {
201 this.videoRemoved.emit()
202 }
203
d473fd94
RK
204 onVideoAccountMuted () {
205 this.videoAccountMuted.emit()
206 }
207
b7819090
C
208 isUserLoggedIn () {
209 return this.authService.isLoggedIn()
210 }
211
212 onWatchLaterClick (currentState: boolean) {
213 if (currentState === true) this.removeFromWatchLater()
214 else this.addToWatchLater()
215
216 this.inWatchLaterPlaylist = !currentState
217 }
218
219 addToWatchLater () {
220 const body = { videoId: this.video.id }
221
222 this.videoPlaylistService.addVideoInPlaylist(this.watchLaterPlaylist.id, body).subscribe(
223 res => {
224 this.watchLaterPlaylist.playlistElementId = res.videoPlaylistElement.id
225 }
226 )
227 }
228
229 removeFromWatchLater () {
51b34a11 230 this.videoPlaylistService.removeVideoFromPlaylist(this.watchLaterPlaylist.id, this.watchLaterPlaylist.playlistElementId, this.video.id)
b7819090
C
231 .subscribe(
232 _ => { /* empty */ }
233 )
234 }
235
236 isWatchLaterPlaylistDisplayed () {
5fb2e288 237 return this.displayVideoActions && this.isUserLoggedIn() && this.inWatchLaterPlaylist !== undefined
b7819090
C
238 }
239
0f7407d9
C
240 getClasses () {
241 return {
242 'display-as-row': this.displayAsRow
243 }
244 }
245
3a0fb65c 246 private setUpBy () {
733dbc53 247 const accountName = this.video.account.name
3a0fb65c 248
deb8b9cd 249 // If the video channel name is an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
733dbc53 250 // Or has not been customized (default created channel display name)
3a0fb65c
C
251 // -> Use the account name
252 if (
733dbc53
C
253 this.video.channel.displayName === `Default ${accountName} channel` ||
254 this.video.channel.displayName === `Main ${accountName} channel` ||
3a0fb65c
C
255 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}$/)
256 ) {
733dbc53 257 this.ownerDisplayType = 'account'
3a0fb65c 258 } else {
733dbc53 259 this.ownerDisplayType = 'videoChannel'
3a0fb65c
C
260 }
261 }
b7819090
C
262
263 private loadWatchLater () {
51b34a11
C
264 if (!this.isUserLoggedIn() || this.inWatchLaterPlaylist !== undefined) return
265
266 this.authService.userInformationLoaded
267 .pipe(switchMap(() => this.videoPlaylistService.listenToVideoPlaylistChange(this.video.id)))
268 .subscribe(existResult => {
269 const watchLaterPlaylist = this.authService.getUser().specialPlaylists.find(p => p.type === VideoPlaylistType.WATCH_LATER)
270 const existsInWatchLater = existResult.find(r => r.playlistId === watchLaterPlaylist.id)
271 this.inWatchLaterPlaylist = false
272
273 this.watchLaterPlaylist = {
274 id: watchLaterPlaylist.id
275 }
276
277 if (existsInWatchLater) {
278 this.inWatchLaterPlaylist = true
279 this.watchLaterPlaylist.playlistElementId = existsInWatchLater.playlistElementId
280 }
281
282 this.cd.markForCheck()
283 })
284
285 this.videoPlaylistService.runPlaylistCheck(this.video.id)
b7819090 286 }
501bc6c2 287}