1 // import * as express from 'express'
2 // import * as Bluebird from 'bluebird'
3 // import * as Sequelize from 'sequelize'
5 // import { database as db } from '../../../initializers/database'
7 // REQUEST_ENDPOINT_ACTIONS,
9 // REQUEST_VIDEO_EVENT_TYPES,
10 // REQUEST_VIDEO_QADU_TYPES
11 // } from '../../../initializers'
14 // signatureValidator,
15 // remoteVideosValidator,
16 // remoteQaduVideosValidator,
17 // remoteEventsVideosValidator
18 // } from '../../../middlewares'
19 // import { logger, retryTransactionWrapper, resetSequelizeInstance } from '../../../helpers'
20 // import { quickAndDirtyUpdatesVideoToFriends, fetchVideoChannelByHostAndUUID } from '../../../lib'
21 // import { PodInstance, VideoFileInstance } from '../../../models'
23 // RemoteVideoRequest,
24 // RemoteVideoCreateData,
25 // RemoteVideoUpdateData,
26 // RemoteVideoRemoveData,
27 // RemoteVideoReportAbuseData,
28 // RemoteQaduVideoRequest,
29 // RemoteQaduVideoData,
30 // RemoteVideoEventRequest,
31 // RemoteVideoEventData,
32 // RemoteVideoChannelCreateData,
33 // RemoteVideoChannelUpdateData,
34 // RemoteVideoChannelRemoveData,
35 // RemoteVideoAccountRemoveData,
36 // RemoteVideoAccountCreateData
37 // } from '../../../../shared'
38 // import { VideoInstance } from '../../../models/video/video-interface'
40 // const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
42 // // Functions to call when processing a remote request
43 // // FIXME: use RemoteVideoRequestType as id type
44 // const functionsHash: { [ id: string ]: (...args) => Promise<any> } = {}
45 // functionsHash[ENDPOINT_ACTIONS.ADD_VIDEO] = addRemoteVideoRetryWrapper
46 // functionsHash[ENDPOINT_ACTIONS.UPDATE_VIDEO] = updateRemoteVideoRetryWrapper
47 // functionsHash[ENDPOINT_ACTIONS.REMOVE_VIDEO] = removeRemoteVideoRetryWrapper
48 // functionsHash[ENDPOINT_ACTIONS.ADD_CHANNEL] = addRemoteVideoChannelRetryWrapper
49 // functionsHash[ENDPOINT_ACTIONS.UPDATE_CHANNEL] = updateRemoteVideoChannelRetryWrapper
50 // functionsHash[ENDPOINT_ACTIONS.REMOVE_CHANNEL] = removeRemoteVideoChannelRetryWrapper
51 // functionsHash[ENDPOINT_ACTIONS.REPORT_ABUSE] = reportAbuseRemoteVideoRetryWrapper
52 // functionsHash[ENDPOINT_ACTIONS.ADD_ACCOUNT] = addRemoteVideoAccountRetryWrapper
53 // functionsHash[ENDPOINT_ACTIONS.REMOVE_ACCOUNT] = removeRemoteVideoAccountRetryWrapper
55 // const remoteVideosRouter = express.Router()
57 // remoteVideosRouter.post('/',
58 // signatureValidator,
60 // remoteVideosValidator,
64 // remoteVideosRouter.post('/qadu',
65 // signatureValidator,
67 // remoteQaduVideosValidator,
71 // remoteVideosRouter.post('/events',
72 // signatureValidator,
74 // remoteEventsVideosValidator,
78 // // ---------------------------------------------------------------------------
84 // // ---------------------------------------------------------------------------
86 // function remoteVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
87 // const requests: RemoteVideoRequest[] = req.body.data
88 // const fromPod = res.locals.secure.pod
90 // // We need to process in the same order to keep consistency
91 // Bluebird.each(requests, request => {
92 // const data = request.data
94 // // Get the function we need to call in order to process the request
95 // const fun = functionsHash[request.type]
96 // if (fun === undefined) {
97 // logger.error('Unknown remote request type %s.', request.type)
101 // return fun.call(this, data, fromPod)
103 // .catch(err => logger.error('Error managing remote videos.', err))
105 // // Don't block the other pod
106 // return res.type('json').status(204).end()
109 // function remoteVideosQadu (req: express.Request, res: express.Response, next: express.NextFunction) {
110 // const requests: RemoteQaduVideoRequest[] = req.body.data
111 // const fromPod = res.locals.secure.pod
113 // Bluebird.each(requests, request => {
114 // const videoData = request.data
116 // return quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod)
118 // .catch(err => logger.error('Error managing remote videos.', err))
120 // return res.type('json').status(204).end()
123 // function remoteVideosEvents (req: express.Request, res: express.Response, next: express.NextFunction) {
124 // const requests: RemoteVideoEventRequest[] = req.body.data
125 // const fromPod = res.locals.secure.pod
127 // Bluebird.each(requests, request => {
128 // const eventData = request.data
130 // return processVideosEventsRetryWrapper(eventData, fromPod)
132 // .catch(err => logger.error('Error managing remote videos.', err))
134 // return res.type('json').status(204).end()
137 // async function processVideosEventsRetryWrapper (eventData: RemoteVideoEventData, fromPod: PodInstance) {
139 // arguments: [ eventData, fromPod ],
140 // errorMessage: 'Cannot process videos events with many retries.'
143 // await retryTransactionWrapper(processVideosEvents, options)
146 // async function processVideosEvents (eventData: RemoteVideoEventData, fromPod: PodInstance) {
147 // await db.sequelize.transaction(async t => {
148 // const sequelizeOptions = { transaction: t }
149 // const videoInstance = await fetchLocalVideoByUUID(eventData.uuid, t)
151 // let columnToUpdate
154 // switch (eventData.eventType) {
155 // case REQUEST_VIDEO_EVENT_TYPES.VIEWS:
156 // columnToUpdate = 'views'
157 // qaduType = REQUEST_VIDEO_QADU_TYPES.VIEWS
160 // case REQUEST_VIDEO_EVENT_TYPES.LIKES:
161 // columnToUpdate = 'likes'
162 // qaduType = REQUEST_VIDEO_QADU_TYPES.LIKES
165 // case REQUEST_VIDEO_EVENT_TYPES.DISLIKES:
166 // columnToUpdate = 'dislikes'
167 // qaduType = REQUEST_VIDEO_QADU_TYPES.DISLIKES
171 // throw new Error('Unknown video event type.')
175 // query[columnToUpdate] = eventData.count
177 // await videoInstance.increment(query, sequelizeOptions)
179 // const qadusParams = [
181 // videoId: videoInstance.id,
185 // await quickAndDirtyUpdatesVideoToFriends(qadusParams, t)
188 // logger.info('Remote video event processed for video with uuid %s.', eventData.uuid)
191 // async function quickAndDirtyUpdateVideoRetryWrapper (videoData: RemoteQaduVideoData, fromPod: PodInstance) {
193 // arguments: [ videoData, fromPod ],
194 // errorMessage: 'Cannot update quick and dirty the remote video with many retries.'
197 // await retryTransactionWrapper(quickAndDirtyUpdateVideo, options)
200 // async function quickAndDirtyUpdateVideo (videoData: RemoteQaduVideoData, fromPod: PodInstance) {
201 // let videoUUID = ''
203 // await db.sequelize.transaction(async t => {
204 // const videoInstance = await fetchVideoByHostAndUUID(fromPod.host, videoData.uuid, t)
205 // const sequelizeOptions = { transaction: t }
207 // videoUUID = videoInstance.uuid
209 // if (videoData.views) {
210 // videoInstance.set('views', videoData.views)
213 // if (videoData.likes) {
214 // videoInstance.set('likes', videoData.likes)
217 // if (videoData.dislikes) {
218 // videoInstance.set('dislikes', videoData.dislikes)
221 // await videoInstance.save(sequelizeOptions)
224 // logger.info('Remote video with uuid %s quick and dirty updated', videoUUID)
227 // async function reportAbuseRemoteVideoRetryWrapper (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) {
229 // arguments: [ reportData, fromPod ],
230 // errorMessage: 'Cannot create remote abuse video with many retries.'
233 // await retryTransactionWrapper(reportAbuseRemoteVideo, options)
236 // async function reportAbuseRemoteVideo (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) {
237 // logger.debug('Reporting remote abuse for video %s.', reportData.videoUUID)
239 // await db.sequelize.transaction(async t => {
240 // const videoInstance = await fetchLocalVideoByUUID(reportData.videoUUID, t)
241 // const videoAbuseData = {
242 // reporterUsername: reportData.reporterUsername,
243 // reason: reportData.reportReason,
244 // reporterPodId: fromPod.id,
245 // videoId: videoInstance.id
248 // await db.VideoAbuse.create(videoAbuseData)
252 // logger.info('Remote abuse for video uuid %s created', reportData.videoUUID)
255 // async function fetchLocalVideoByUUID (id: string, t: Sequelize.Transaction) {
257 // const video = await db.Video.loadLocalVideoByUUID(id, t)
259 // if (!video) throw new Error('Video ' + id + ' not found')
263 // logger.error('Cannot load owned video from id.', { error: err.stack, id })
268 // async function fetchVideoByHostAndUUID (podHost: string, uuid: string, t: Sequelize.Transaction) {
270 // const video = await db.Video.loadByHostAndUUID(podHost, uuid, t)
271 // if (!video) throw new Error('Video not found')
275 // logger.error('Cannot load video from host and uuid.', { error: err.stack, podHost, uuid })