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