my-video-playlists.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 { VideoExistInPlaylist } 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: VideoExistInPlaylist = {}
  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. playlistId: result.id,
  29. startTimestamp: element.startTimestamp,
  30. stopTimestamp: element.stopTimestamp
  31. })
  32. }
  33. }
  34. return res.json(existObject)
  35. }