800417a7994174a55e81082aafcb47a12d8a270f
[oweals/peertube.git] / client / src / app / shared / video / video-miniature.component.ts
1 import { ChangeDetectionStrategy, Component, Inject, Input, LOCALE_ID, OnInit } from '@angular/core'
2 import { User } from '../users'
3 import { Video } from './video.model'
4 import { ServerService } from '@app/core'
5 import { VideoPrivacy, VideoState } from '../../../../../shared'
6 import { I18n } from '@ngx-translate/i18n-polyfill'
7
8 export type OwnerDisplayType = 'account' | 'videoChannel' | 'auto'
9 export type MiniatureDisplayOptions = {
10   date?: boolean
11   views?: boolean
12   by?: boolean
13   privacyLabel?: boolean
14   privacyText?: boolean
15   state?: boolean
16   blacklistInfo?: boolean
17   nsfw?: boolean
18 }
19
20 @Component({
21   selector: 'my-video-miniature',
22   styleUrls: [ './video-miniature.component.scss' ],
23   templateUrl: './video-miniature.component.html',
24   changeDetection: ChangeDetectionStrategy.OnPush
25 })
26 export class VideoMiniatureComponent implements OnInit {
27   @Input() user: User
28   @Input() video: Video
29
30   @Input() ownerDisplayType: OwnerDisplayType = 'account'
31   @Input() displayOptions: MiniatureDisplayOptions = {
32     date: true,
33     views: true,
34     by: true,
35     privacyLabel: false,
36     privacyText: false,
37     state: false,
38     blacklistInfo: false
39   }
40   @Input() displayAsRow = false
41
42   private ownerDisplayTypeChosen: 'account' | 'videoChannel'
43
44   constructor (
45     private serverService: ServerService,
46     private i18n: I18n,
47     @Inject(LOCALE_ID) private localeId: string
48   ) { }
49
50   get isVideoBlur () {
51     return this.video.isVideoNSFWForUser(this.user, this.serverService.getConfig())
52   }
53
54   ngOnInit () {
55     if (this.ownerDisplayType === 'account' || this.ownerDisplayType === 'videoChannel') {
56       this.ownerDisplayTypeChosen = this.ownerDisplayType
57       return
58     }
59
60     // If the video channel name an UUID (not really displayable, we changed this behaviour in v1.0.0-beta.12)
61     // -> Use the account name
62     if (
63       this.video.channel.name === `${this.video.account.name}_channel` ||
64       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}$/)
65     ) {
66       this.ownerDisplayTypeChosen = 'account'
67     } else {
68       this.ownerDisplayTypeChosen = 'videoChannel'
69     }
70   }
71
72   displayOwnerAccount () {
73     return this.ownerDisplayTypeChosen === 'account'
74   }
75
76   displayOwnerVideoChannel () {
77     return this.ownerDisplayTypeChosen === 'videoChannel'
78   }
79
80   isUnlistedVideo () {
81     return this.video.privacy.id === VideoPrivacy.UNLISTED
82   }
83
84   isPrivateVideo () {
85     return this.video.privacy.id === VideoPrivacy.PRIVATE
86   }
87
88   getStateLabel (video: Video) {
89     if (video.privacy.id !== VideoPrivacy.PRIVATE && video.state.id === VideoState.PUBLISHED) {
90       return this.i18n('Published')
91     }
92
93     if (video.scheduledUpdate) {
94       const updateAt = new Date(video.scheduledUpdate.updateAt.toString()).toLocaleString(this.localeId)
95       return this.i18n('Publication scheduled on ') + updateAt
96     }
97
98     if (video.state.id === VideoState.TO_TRANSCODE && video.waitTranscoding === true) {
99       return this.i18n('Waiting transcoding')
100     }
101
102     if (video.state.id === VideoState.TO_TRANSCODE) {
103       return this.i18n('To transcode')
104     }
105
106     if (video.state.id === VideoState.TO_IMPORT) {
107       return this.i18n('To import')
108     }
109
110     return ''
111   }
112 }