From 6e46de095d7169355dd83030f6ce4a582304153a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 5 Oct 2018 11:15:06 +0200 Subject: Add user history and resume videos --- server/controllers/api/videos/captions.ts | 6 +----- server/controllers/api/videos/comment.ts | 6 +++--- server/controllers/api/videos/index.ts | 6 +++++- server/controllers/api/videos/watching.ts | 36 +++++++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 server/controllers/api/videos/watching.ts (limited to 'server/controllers/api/videos') diff --git a/server/controllers/api/videos/captions.ts b/server/controllers/api/videos/captions.ts index 4cf8de1ef..3ba918189 100644 --- a/server/controllers/api/videos/captions.ts +++ b/server/controllers/api/videos/captions.ts @@ -1,10 +1,6 @@ import * as express from 'express' import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate } from '../../../middlewares' -import { - addVideoCaptionValidator, - deleteVideoCaptionValidator, - listVideoCaptionsValidator -} from '../../../middlewares/validators/video-captions' +import { addVideoCaptionValidator, deleteVideoCaptionValidator, listVideoCaptionsValidator } from '../../../middlewares/validators' import { createReqFiles } from '../../../helpers/express-utils' import { CONFIG, sequelizeTypescript, VIDEO_CAPTIONS_MIMETYPE_EXT } from '../../../initializers' import { getFormattedObjects } from '../../../helpers/utils' diff --git a/server/controllers/api/videos/comment.ts b/server/controllers/api/videos/comment.ts index dc25e1e85..4f2b4faee 100644 --- a/server/controllers/api/videos/comment.ts +++ b/server/controllers/api/videos/comment.ts @@ -13,14 +13,14 @@ import { setDefaultPagination, setDefaultSort } from '../../../middlewares' -import { videoCommentThreadsSortValidator } from '../../../middlewares/validators' import { addVideoCommentReplyValidator, addVideoCommentThreadValidator, listVideoCommentThreadsValidator, listVideoThreadCommentsValidator, - removeVideoCommentValidator -} from '../../../middlewares/validators/video-comments' + removeVideoCommentValidator, + videoCommentThreadsSortValidator +} from '../../../middlewares/validators' import { VideoModel } from '../../../models/video/video' import { VideoCommentModel } from '../../../models/video/video-comment' import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger' diff --git a/server/controllers/api/videos/index.ts b/server/controllers/api/videos/index.ts index 15ef8d458..6a73e13d0 100644 --- a/server/controllers/api/videos/index.ts +++ b/server/controllers/api/videos/index.ts @@ -57,6 +57,7 @@ import { videoCaptionsRouter } from './captions' import { videoImportsRouter } from './import' import { resetSequelizeInstance } from '../../../helpers/database-utils' import { rename } from 'fs-extra' +import { watchingRouter } from './watching' const auditLogger = auditLoggerFactory('videos') const videosRouter = express.Router() @@ -86,6 +87,7 @@ videosRouter.use('/', videoCommentRouter) videosRouter.use('/', videoCaptionsRouter) videosRouter.use('/', videoImportsRouter) videosRouter.use('/', ownershipVideoRouter) +videosRouter.use('/', watchingRouter) videosRouter.get('/categories', listVideoCategories) videosRouter.get('/licences', listVideoLicences) @@ -119,6 +121,7 @@ videosRouter.get('/:id/description', asyncMiddleware(getVideoDescription) ) videosRouter.get('/:id', + optionalAuthenticate, asyncMiddleware(videosGetValidator), getVideo ) @@ -433,7 +436,8 @@ async function listVideos (req: express.Request, res: express.Response, next: ex tagsAllOf: req.query.tagsAllOf, nsfw: buildNSFWFilter(res, req.query.nsfw), filter: req.query.filter as VideoFilter, - withFiles: false + withFiles: false, + userId: res.locals.oauth ? res.locals.oauth.token.User.id : undefined }) return res.json(getFormattedObjects(resultList.data, resultList.total)) diff --git a/server/controllers/api/videos/watching.ts b/server/controllers/api/videos/watching.ts new file mode 100644 index 000000000..e8876b47a --- /dev/null +++ b/server/controllers/api/videos/watching.ts @@ -0,0 +1,36 @@ +import * as express from 'express' +import { UserWatchingVideo } from '../../../../shared' +import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, videoWatchingValidator } from '../../../middlewares' +import { UserVideoHistoryModel } from '../../../models/account/user-video-history' +import { UserModel } from '../../../models/account/user' + +const watchingRouter = express.Router() + +watchingRouter.put('/:videoId/watching', + authenticate, + asyncMiddleware(videoWatchingValidator), + asyncRetryTransactionMiddleware(userWatchVideo) +) + +// --------------------------------------------------------------------------- + +export { + watchingRouter +} + +// --------------------------------------------------------------------------- + +async function userWatchVideo (req: express.Request, res: express.Response) { + const user = res.locals.oauth.token.User as UserModel + + const body: UserWatchingVideo = req.body + const { id: videoId } = res.locals.video as { id: number } + + await UserVideoHistoryModel.upsert({ + videoId, + userId: user.id, + currentTime: body.currentTime + }) + + return res.type('json').status(204).end() +} -- cgit v1.2.3