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/watching.ts | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 server/controllers/api/videos/watching.ts (limited to 'server/controllers/api/videos/watching.ts') 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