-import * as express from 'express'
+import express from 'express'
import toInt from 'validator/lib/toInt'
+import { pickCommonVideoQuery } from '@server/helpers/query'
import { doJSONRequest } from '@server/helpers/requests'
-import { LiveManager } from '@server/lib/live-manager'
+import { VideoViews } from '@server/lib/video-views'
import { openapiOperationDoc } from '@server/middlewares/doc'
import { getServerActor } from '@server/models/application/application'
+import { guessAdditionalAttributesFromQuery } from '@server/models/video/formatter/video-format-utils'
import { MVideoAccountLight } from '@server/types/models'
-import { VideosCommonQuery } from '../../../../shared'
-import { HttpStatusCode } from '../../../../shared/core-utils/miscs'
+import { HttpStatusCode } from '../../../../shared/models'
import { auditLoggerFactory, getAuditIdFromRes, VideoAuditView } from '../../../helpers/audit-logger'
import { buildNSFWFilter, getCountVideos } from '../../../helpers/express-utils'
import { logger } from '../../../helpers/logger'
import { sendView } from '../../../lib/activitypub/send/send-view'
import { JobQueue } from '../../../lib/job-queue'
import { Hooks } from '../../../lib/plugins/hooks'
-import { Redis } from '../../../lib/redis'
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
videosRouter.get('/:id',
openapiOperationDoc({ operationId: 'getVideo' }),
optionalAuthenticate,
- asyncMiddleware(videosCustomGetValidator('only-video-with-rights')),
+ asyncMiddleware(videosCustomGetValidator('for-api')),
asyncMiddleware(checkVideoFollowConstraints),
- asyncMiddleware(getVideo)
+ getVideo
)
videosRouter.post('/:id/views',
openapiOperationDoc({ operationId: 'addView' }),
- asyncMiddleware(videosCustomGetValidator('only-immutable-attributes')),
+ asyncMiddleware(videosCustomGetValidator('only-video')),
asyncMiddleware(viewVideo)
)
res.json(VIDEO_PRIVACIES)
}
-async function getVideo (_req: express.Request, res: express.Response) {
- // We need more attributes
- const userId: number = res.locals.oauth ? res.locals.oauth.token.User.id : null
-
- const video = await Hooks.wrapPromiseFun(
- VideoModel.loadForGetAPI,
- { id: res.locals.onlyVideoWithRights.id, userId },
- 'filter:api.video.get.result'
- )
+function getVideo (_req: express.Request, res: express.Response) {
+ const video = res.locals.videoAPI
if (video.isOutdated()) {
JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: video.url } })
}
async function viewVideo (req: express.Request, res: express.Response) {
- const immutableVideoAttrs = res.locals.onlyImmutableVideo
+ const video = res.locals.onlyVideo
const ip = req.ip
- const exists = await Redis.Instance.doesVideoIPViewExist(ip, immutableVideoAttrs.uuid)
- if (exists) {
- logger.debug('View for ip %s and video %s already exists.', ip, immutableVideoAttrs.uuid)
- return res.status(HttpStatusCode.NO_CONTENT_204).end()
- }
-
- const video = await VideoModel.load(immutableVideoAttrs.id)
-
- const promises: Promise<any>[] = [
- Redis.Instance.setIPVideoView(ip, video.uuid, video.isLive)
- ]
-
- let federateView = true
-
- // Increment our live manager
- if (video.isLive && video.isOwned()) {
- LiveManager.Instance.addViewTo(video.id)
-
- // Views of our local live will be sent by our live manager
- federateView = false
- }
-
- // Increment our video views cache counter
- if (!video.isLive) {
- promises.push(Redis.Instance.addVideoView(video.id))
- }
+ const success = await VideoViews.Instance.processView({ video, ip })
- if (federateView) {
+ if (success) {
const serverActor = await getServerActor()
- promises.push(sendView(serverActor, video, undefined))
- }
+ await sendView(serverActor, video, undefined)
- await Promise.all(promises)
-
- Hooks.runAction('action:api.video.viewed', { video, ip })
+ Hooks.runAction('action:api.video.viewed', { video: video, ip })
+ }
return res.status(HttpStatusCode.NO_CONTENT_204).end()
}
}
async function listVideos (req: express.Request, res: express.Response) {
- const query = req.query as VideosCommonQuery
+ const serverActor = await getServerActor()
+
+ const query = pickCommonVideoQuery(req.query)
const countVideos = getCountVideos(req)
const apiOptions = await Hooks.wrapObject({
- start: query.start,
- count: query.count,
- sort: query.sort,
- includeLocalVideos: true,
- categoryOneOf: query.categoryOneOf,
- licenceOneOf: query.licenceOneOf,
- languageOneOf: query.languageOneOf,
- tagsOneOf: query.tagsOneOf,
- tagsAllOf: query.tagsAllOf,
+ ...query,
+
+ displayOnlyForFollower: {
+ actorId: serverActor.id,
+ orLocalVideos: true
+ },
nsfw: buildNSFWFilter(res, query.nsfw),
- isLive: query.isLive,
- filter: query.filter,
- withFiles: false,
user: res.locals.oauth ? res.locals.oauth.token.User : undefined,
countVideos
}, 'filter:api.videos.list.params')
'filter:api.videos.list.result'
)
- return res.json(getFormattedObjects(resultList.data, resultList.total))
+ return res.json(getFormattedObjects(resultList.data, resultList.total, guessAdditionalAttributesFromQuery(query)))
}
async function removeVideo (_req: express.Request, res: express.Response) {