2
1

my-videos.component.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import { uniqBy } from 'lodash'
  2. import { concat, Observable } from 'rxjs'
  3. import { tap, toArray } from 'rxjs/operators'
  4. import { Component, OnInit, ViewChild } from '@angular/core'
  5. import { ActivatedRoute, Router } from '@angular/router'
  6. import { AuthService, ComponentPagination, ConfirmService, Notifier, ScreenService, ServerService, User } from '@app/core'
  7. import { DisableForReuseHook } from '@app/core/routing/disable-for-reuse-hook'
  8. import { immutableAssign, prepareIcu } from '@app/helpers'
  9. import { AdvancedInputFilter } from '@app/shared/shared-forms'
  10. import { DropdownAction, Video, VideoService } from '@app/shared/shared-main'
  11. import { LiveStreamInformationComponent } from '@app/shared/shared-video-live'
  12. import {
  13. MiniatureDisplayOptions,
  14. SelectionType,
  15. VideoActionsDisplayType,
  16. VideosSelectionComponent
  17. } from '@app/shared/shared-video-miniature'
  18. import { VideoPlaylistService } from '@app/shared/shared-video-playlist'
  19. import { VideoChannel, VideoExistInPlaylist, VideosExistInPlaylists, VideoSortField } from '@shared/models'
  20. import { VideoChangeOwnershipComponent } from './modals/video-change-ownership.component'
  21. @Component({
  22. templateUrl: './my-videos.component.html',
  23. styleUrls: [ './my-videos.component.scss' ]
  24. })
  25. export class MyVideosComponent implements OnInit, DisableForReuseHook {
  26. @ViewChild('videosSelection', { static: true }) videosSelection: VideosSelectionComponent
  27. @ViewChild('videoChangeOwnershipModal', { static: true }) videoChangeOwnershipModal: VideoChangeOwnershipComponent
  28. @ViewChild('liveStreamInformationModal', { static: true }) liveStreamInformationModal: LiveStreamInformationComponent
  29. videosContainedInPlaylists: VideosExistInPlaylists = {}
  30. titlePage: string
  31. selection: SelectionType = {}
  32. pagination: ComponentPagination = {
  33. currentPage: 1,
  34. itemsPerPage: 10,
  35. totalItems: null
  36. }
  37. miniatureDisplayOptions: MiniatureDisplayOptions = {
  38. date: true,
  39. views: true,
  40. by: true,
  41. privacyLabel: false,
  42. privacyText: true,
  43. state: true,
  44. blacklistInfo: true,
  45. forceChannelInBy: true
  46. }
  47. videoDropdownDisplayOptions: VideoActionsDisplayType = {
  48. playlist: false,
  49. download: false,
  50. update: false,
  51. blacklist: false,
  52. delete: true,
  53. report: false,
  54. duplicate: false,
  55. mute: false,
  56. liveInfo: true,
  57. removeFiles: false,
  58. transcoding: false,
  59. studio: true,
  60. stats: true
  61. }
  62. moreVideoActions: DropdownAction<{ video: Video }>[][] = []
  63. videos: Video[] = []
  64. getVideosObservableFunction = this.getVideosObservable.bind(this)
  65. sort: VideoSortField = '-publishedAt'
  66. user: User
  67. inputFilters: AdvancedInputFilter[] = []
  68. disabled = false
  69. private search: string
  70. private userChannels: VideoChannel[] = []
  71. constructor (
  72. protected router: Router,
  73. protected serverService: ServerService,
  74. protected route: ActivatedRoute,
  75. protected authService: AuthService,
  76. protected notifier: Notifier,
  77. protected screenService: ScreenService,
  78. private confirmService: ConfirmService,
  79. private videoService: VideoService,
  80. private playlistService: VideoPlaylistService
  81. ) {
  82. this.titlePage = $localize`My videos`
  83. }
  84. ngOnInit () {
  85. this.buildActions()
  86. this.user = this.authService.getUser()
  87. if (this.route.snapshot.queryParams['search']) {
  88. this.search = this.route.snapshot.queryParams['search']
  89. }
  90. this.authService.userInformationLoaded.subscribe(() => {
  91. this.user = this.authService.getUser()
  92. this.userChannels = this.user.videoChannels
  93. const channelFilters = this.userChannels.map(c => {
  94. return {
  95. value: 'channel:' + c.name,
  96. label: c.name
  97. }
  98. })
  99. this.inputFilters = [
  100. {
  101. title: $localize`Advanced filters`,
  102. children: [
  103. {
  104. value: 'isLive:true',
  105. label: $localize`Only live videos`
  106. }
  107. ]
  108. },
  109. {
  110. title: $localize`Channel filters`,
  111. children: channelFilters
  112. }
  113. ]
  114. })
  115. }
  116. onSearch (search: string) {
  117. this.search = search
  118. this.reloadData()
  119. }
  120. reloadData () {
  121. this.videosSelection.reloadVideos()
  122. }
  123. onChangeSortColumn () {
  124. this.videosSelection.reloadVideos()
  125. }
  126. disableForReuse () {
  127. this.disabled = true
  128. }
  129. enabledForReuse () {
  130. this.disabled = false
  131. }
  132. getVideosObservable (page: number) {
  133. const newPagination = immutableAssign(this.pagination, { currentPage: page })
  134. return this.videoService.getMyVideos({
  135. videoPagination: newPagination,
  136. sort: this.sort,
  137. userChannels: this.userChannels,
  138. search: this.search
  139. }).pipe(
  140. tap(res => this.pagination.totalItems = res.total),
  141. tap(({ data }) => this.fetchVideosContainedInPlaylists(data))
  142. )
  143. }
  144. private fetchVideosContainedInPlaylists (videos: Video[]) {
  145. this.playlistService.doVideosExistInPlaylist(videos.map(v => v.id))
  146. .subscribe(result => {
  147. this.videosContainedInPlaylists = Object.keys(result).reduce((acc, videoId) => ({
  148. ...acc,
  149. [videoId]: uniqBy(result[videoId], (p: VideoExistInPlaylist) => p.playlistId)
  150. }), this.videosContainedInPlaylists)
  151. })
  152. }
  153. async deleteSelectedVideos () {
  154. const toDeleteVideosIds = Object.keys(this.selection)
  155. .filter(k => this.selection[k] === true)
  156. .map(k => parseInt(k, 10))
  157. const res = await this.confirmService.confirm(
  158. prepareIcu($localize`Do you really want to delete {length, plural, =1 {this video} other {{length} videos}}?`)(
  159. { length: toDeleteVideosIds.length },
  160. $localize`Do you really want to delete ${toDeleteVideosIds.length} videos?`
  161. ),
  162. $localize`Delete`
  163. )
  164. if (res === false) return
  165. const observables: Observable<any>[] = []
  166. for (const videoId of toDeleteVideosIds) {
  167. const o = this.videoService.removeVideo(videoId)
  168. .pipe(tap(() => this.removeVideoFromArray(videoId)))
  169. observables.push(o)
  170. }
  171. concat(...observables)
  172. .pipe(toArray())
  173. .subscribe({
  174. next: () => {
  175. this.notifier.success(
  176. prepareIcu($localize`{length, plural, =1 {Video has been deleted} other {{length} videos have been deleted}}`)(
  177. { length: toDeleteVideosIds.length },
  178. $localize`${toDeleteVideosIds.length} have been deleted.`
  179. )
  180. )
  181. this.selection = {}
  182. },
  183. error: err => this.notifier.error(err.message)
  184. })
  185. }
  186. onVideoRemoved (video: Video) {
  187. this.removeVideoFromArray(video.id)
  188. }
  189. changeOwnership (video: Video) {
  190. this.videoChangeOwnershipModal.show(video)
  191. }
  192. private removeVideoFromArray (id: number) {
  193. this.videos = this.videos.filter(v => v.id !== id)
  194. }
  195. private buildActions () {
  196. this.moreVideoActions = [
  197. [
  198. {
  199. label: $localize`Change ownership`,
  200. handler: ({ video }) => this.changeOwnership(video),
  201. iconName: 'ownership-change'
  202. }
  203. ]
  204. ]
  205. }
  206. }