my-video-playlists.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import * as express from 'express'
  2. import { asyncMiddleware, authenticate } from '../../../middlewares'
  3. import { doVideosInPlaylistExistValidator } from '../../../middlewares/validators/videos/video-playlists'
  4. import { VideoPlaylistModel } from '../../../models/video/video-playlist'
  5. import { VideosExistInPlaylists } from '../../../../shared/models/videos/playlist/video-exist-in-playlist.model'
  6. const myVideoPlaylistsRouter = express.Router()
  7. myVideoPlaylistsRouter.get('/me/video-playlists/videos-exist',
  8. authenticate,
  9. doVideosInPlaylistExistValidator,
  10. asyncMiddleware(doVideosInPlaylistExist)
  11. )
  12. // ---------------------------------------------------------------------------
  13. export {
  14. myVideoPlaylistsRouter
  15. }
  16. // ---------------------------------------------------------------------------
  17. async function doVideosInPlaylistExist (req: express.Request, res: express.Response) {
  18. const videoIds = req.query.videoIds.map(i => parseInt(i + '', 10))
  19. const user = res.locals.oauth.token.User
  20. const results = await VideoPlaylistModel.listPlaylistIdsOf(user.Account.id, videoIds)
  21. const existObject: VideosExistInPlaylists = {}
  22. for (const videoId of videoIds) {
  23. existObject[videoId] = []
  24. }
  25. for (const result of results) {
  26. for (const element of result.VideoPlaylistElements) {
  27. existObject[element.videoId].push({
  28. playlistElementId: element.id,
  29. playlistId: result.id,
  30. startTimestamp: element.startTimestamp,
  31. stopTimestamp: element.stopTimestamp
  32. })
  33. }
  34. }
  35. return res.json(existObject)
  36. }