]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+my-library/my-videos/my-videos.component.ts
Square channel avatar consistency
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-library / my-videos / my-videos.component.ts
CommitLineData
bf64ed41 1import { concat, Observable, Subject } from 'rxjs'
67ed6552
C
2import { debounceTime, tap, toArray } from 'rxjs/operators'
3import { Component, OnInit, ViewChild } from '@angular/core'
be447678 4import { ActivatedRoute, Router } from '@angular/router'
241609f1 5import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User, UserService } from '@app/core'
67ed6552
C
6import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
7import { immutableAssign } from '@app/helpers'
d846d99c 8import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
f8c00564 9import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
67ed6552 10import { MiniatureDisplayOptions, OwnerDisplayType, SelectionType, VideosSelectionComponent } from '@app/shared/shared-video-miniature'
67ed6552 11import { VideoSortField } from '@shared/models'
d846d99c 12import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
202f6b6c
C
13
14@Component({
17119e4a
C
15 templateUrl: './my-videos.component.html',
16 styleUrls: [ './my-videos.component.scss' ]
202f6b6c 17})
17119e4a 18export class MyVideosComponent implements OnInit, DisableForReuseHook {
f36da21e
C
19 @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
20 @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
d846d99c 21 @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
489290b8 22
b1d40cff 23 titlePage: string
693263e9 24 selection: SelectionType = {}
0cd4344f 25 pagination: ComponentPagination = {
234b535d 26 currentPage: 1,
1d904806 27 itemsPerPage: 10,
234b535d
C
28 totalItems: null
29 }
e2409062
C
30 miniatureDisplayOptions: MiniatureDisplayOptions = {
31 date: true,
32 views: true,
c4a6f790 33 by: true,
e2409062
C
34 privacyLabel: false,
35 privacyText: true,
36 state: true,
37 blacklistInfo: true
38 }
c4a6f790
C
39 ownerDisplayType: OwnerDisplayType = 'videoChannel'
40
d846d99c
C
41 videoActions: DropdownAction<{ video: Video }>[] = []
42
693263e9 43 videos: Video[] = []
bf64ed41
RK
44 videosSearch: string
45 videosSearchChanged = new Subject<string>()
693263e9 46 getVideosObservableFunction = this.getVideosObservable.bind(this)
202f6b6c 47
241609f1
C
48 user: User
49
b1d40cff
C
50 constructor (
51 protected router: Router,
489290b8 52 protected serverService: ServerService,
b1d40cff
C
53 protected route: ActivatedRoute,
54 protected authService: AuthService,
f8b2c1b4 55 protected notifier: Notifier,
bbe0f064 56 protected screenService: ScreenService,
308c4275 57 private confirmService: ConfirmService,
693263e9 58 private videoService: VideoService
b1d40cff 59 ) {
66357162 60 this.titlePage = $localize`My videos`
202f6b6c
C
61 }
62
bf64ed41 63 ngOnInit () {
d846d99c
C
64 this.buildActions()
65
241609f1
C
66 this.user = this.authService.getUser()
67
bf64ed41 68 this.videosSearchChanged
4f5d0459 69 .pipe(debounceTime(500))
bf64ed41
RK
70 .subscribe(() => {
71 this.videosSelection.reloadVideos()
72 })
73 }
74
4f5d0459
RK
75 resetSearch () {
76 this.videosSearch = ''
77 this.onVideosSearchChanged()
78 }
79
bf64ed41
RK
80 onVideosSearchChanged () {
81 this.videosSearchChanged.next()
82 }
83
693263e9
C
84 disableForReuse () {
85 this.videosSelection.disableForReuse()
9af61e84
C
86 }
87
693263e9
C
88 enabledForReuse () {
89 this.videosSelection.enabledForReuse()
ce0e281d
C
90 }
91
693263e9 92 getVideosObservable (page: number, sort: VideoSortField) {
0cd4344f
C
93 const newPagination = immutableAssign(this.pagination, { currentPage: page })
94
bf64ed41 95 return this.videoService.getMyVideos(newPagination, sort, this.videosSearch)
aa0f1963
RK
96 .pipe(
97 tap(res => this.pagination.totalItems = res.total)
98 )
244e76a5
RK
99 }
100
1f30a185 101 async deleteSelectedVideos () {
693263e9
C
102 const toDeleteVideosIds = Object.keys(this.selection)
103 .filter(k => this.selection[ k ] === true)
2186386c 104 .map(k => parseInt(k, 10))
ce0e281d 105
2186386c 106 const res = await this.confirmService.confirm(
66357162
C
107 $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`,
108 $localize`Delete`
2186386c 109 )
1f30a185
C
110 if (res === false) return
111
112 const observables: Observable<any>[] = []
113 for (const videoId of toDeleteVideosIds) {
2186386c 114 const o = this.videoService.removeVideo(videoId)
489290b8 115 .pipe(tap(() => this.removeVideoFromArray(videoId)))
1f30a185
C
116
117 observables.push(o)
118 }
119
489290b8
C
120 concat(...observables)
121 .pipe(toArray())
1f30a185 122 .subscribe(
489290b8 123 () => {
66357162 124 this.notifier.success($localize`${toDeleteVideosIds.length} videos deleted.`)
693263e9 125 this.selection = {}
1f30a185
C
126 },
127
f8b2c1b4 128 err => this.notifier.error(err.message)
1f30a185 129 )
ce0e281d
C
130 }
131
1f30a185 132 async deleteVideo (video: Video) {
2186386c 133 const res = await this.confirmService.confirm(
66357162
C
134 $localize`Do you really want to delete ${video.name}?`,
135 $localize`Delete`
2186386c 136 )
1f30a185
C
137 if (res === false) return
138
139 this.videoService.removeVideo(video.id)
2186386c 140 .subscribe(
f8b2c1b4 141 () => {
66357162 142 this.notifier.success($localize`Video ${video.name} deleted.`)
693263e9 143 this.removeVideoFromArray(video.id)
2186386c
C
144 },
145
f8b2c1b4 146 error => this.notifier.error(error.message)
2186386c
C
147 )
148 }
1f30a185 149
d846d99c 150 changeOwnership (video: Video) {
74d63469
GR
151 this.videoChangeOwnershipModal.show(video)
152 }
153
d846d99c
C
154 displayLiveInformation (video: Video) {
155 this.liveStreamInformationModal.show(video)
156 }
157
489290b8
C
158 private removeVideoFromArray (id: number) {
159 this.videos = this.videos.filter(v => v.id !== id)
ce0e281d 160 }
d846d99c
C
161
162 private buildActions () {
163 this.videoActions = [
164 {
165 label: $localize`Display live information`,
166 handler: ({ video }) => this.displayLiveInformation(video),
167 isDisplayed: ({ video }) => video.isLive,
168 iconName: 'live'
169 },
170 {
171 label: $localize`Change ownership`,
172 handler: ({ video }) => this.changeOwnership(video),
173 iconName: 'ownership-change'
174 },
175 {
176 label: $localize`Delete`,
177 handler: ({ video }) => this.deleteVideo(video),
178 iconName: 'delete'
179 }
180 ]
181 }
202f6b6c 182}