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