aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/custom-validators/video-playlists.ts
blob: c962c5532348c27f17203ed28f20b30ee0d7fa83 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { exists } from './misc'
import * as validator from 'validator'
import { CONSTRAINTS_FIELDS, VIDEO_PLAYLIST_PRIVACIES, VIDEO_PLAYLIST_TYPES } from '../../initializers'
import * as express from 'express'
import { VideoPlaylistModel } from '../../models/video/video-playlist'

const PLAYLISTS_CONSTRAINT_FIELDS = CONSTRAINTS_FIELDS.VIDEO_PLAYLISTS

function isVideoPlaylistNameValid (value: any) {
  return exists(value) && validator.isLength(value, PLAYLISTS_CONSTRAINT_FIELDS.NAME)
}

function isVideoPlaylistDescriptionValid (value: any) {
  return value === null || (exists(value) && validator.isLength(value, PLAYLISTS_CONSTRAINT_FIELDS.DESCRIPTION))
}

function isVideoPlaylistPrivacyValid (value: number) {
  return validator.isInt(value + '') && VIDEO_PLAYLIST_PRIVACIES[ value ] !== undefined
}

function isVideoPlaylistTimestampValid (value: any) {
  return value === null || (exists(value) && validator.isInt('' + value, { min: 0 }))
}

function isVideoPlaylistTypeValid (value: any) {
  return exists(value) && VIDEO_PLAYLIST_TYPES[ value ] !== undefined
}

async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') {
  const videoPlaylist = fetchType === 'summary'
    ? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined)
    : await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined)

  if (!videoPlaylist) {
    res.status(404)
       .json({ error: 'Video playlist not found' })
       .end()

    return false
  }

  res.locals.videoPlaylist = videoPlaylist
  return true
}

// ---------------------------------------------------------------------------

export {
  doesVideoPlaylistExist,
  isVideoPlaylistNameValid,
  isVideoPlaylistDescriptionValid,
  isVideoPlaylistPrivacyValid,
  isVideoPlaylistTimestampValid,
  isVideoPlaylistTypeValid
}