2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2017-10-25 04:55:06 -05:00
|
|
|
import * as Bluebird from 'bluebird'
|
2017-10-24 12:41:09 -05:00
|
|
|
import * as Sequelize from 'sequelize'
|
2016-06-18 09:13:54 -05:00
|
|
|
|
2017-05-22 13:58:25 -05:00
|
|
|
import { database as db } from '../../../initializers/database'
|
2017-05-15 15:22:03 -05:00
|
|
|
import {
|
|
|
|
REQUEST_ENDPOINT_ACTIONS,
|
|
|
|
REQUEST_ENDPOINTS,
|
|
|
|
REQUEST_VIDEO_EVENT_TYPES,
|
|
|
|
REQUEST_VIDEO_QADU_TYPES
|
|
|
|
} from '../../../initializers'
|
|
|
|
import {
|
|
|
|
checkSignature,
|
|
|
|
signatureValidator,
|
|
|
|
remoteVideosValidator,
|
|
|
|
remoteQaduVideosValidator,
|
|
|
|
remoteEventsVideosValidator
|
|
|
|
} from '../../../middlewares'
|
2017-07-05 06:26:25 -05:00
|
|
|
import { logger, retryTransactionWrapper } from '../../../helpers'
|
2017-10-25 04:55:06 -05:00
|
|
|
import { quickAndDirtyUpdatesVideoToFriends, fetchVideoChannelByHostAndUUID } from '../../../lib'
|
2017-09-12 05:53:55 -05:00
|
|
|
import { PodInstance, VideoFileInstance } from '../../../models'
|
2017-07-10 12:43:21 -05:00
|
|
|
import {
|
|
|
|
RemoteVideoRequest,
|
|
|
|
RemoteVideoCreateData,
|
|
|
|
RemoteVideoUpdateData,
|
|
|
|
RemoteVideoRemoveData,
|
|
|
|
RemoteVideoReportAbuseData,
|
|
|
|
RemoteQaduVideoRequest,
|
|
|
|
RemoteQaduVideoData,
|
|
|
|
RemoteVideoEventRequest,
|
2017-10-24 12:41:09 -05:00
|
|
|
RemoteVideoEventData,
|
|
|
|
RemoteVideoChannelCreateData,
|
|
|
|
RemoteVideoChannelUpdateData,
|
|
|
|
RemoteVideoChannelRemoveData,
|
|
|
|
RemoteVideoAuthorRemoveData,
|
|
|
|
RemoteVideoAuthorCreateData
|
2017-07-10 12:43:21 -05:00
|
|
|
} from '../../../../shared'
|
2017-05-15 15:22:03 -05:00
|
|
|
|
|
|
|
const ENDPOINT_ACTIONS = REQUEST_ENDPOINT_ACTIONS[REQUEST_ENDPOINTS.VIDEOS]
|
2017-01-17 13:50:02 -06:00
|
|
|
|
|
|
|
// Functions to call when processing a remote request
|
2017-10-24 12:41:09 -05:00
|
|
|
// FIXME: use RemoteVideoRequestType as id type
|
2017-07-05 06:26:25 -05:00
|
|
|
const functionsHash: { [ id: string ]: (...args) => Promise<any> } = {}
|
2017-10-24 12:41:09 -05:00
|
|
|
functionsHash[ENDPOINT_ACTIONS.ADD_VIDEO] = addRemoteVideoRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.UPDATE_VIDEO] = updateRemoteVideoRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.REMOVE_VIDEO] = removeRemoteVideoRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.ADD_CHANNEL] = addRemoteVideoChannelRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.UPDATE_CHANNEL] = updateRemoteVideoChannelRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.REMOVE_CHANNEL] = removeRemoteVideoChannelRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.REPORT_ABUSE] = reportAbuseRemoteVideoRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.ADD_AUTHOR] = addRemoteVideoAuthorRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.REMOVE_AUTHOR] = removeRemoteVideoAuthorRetryWrapper
|
2017-01-17 13:50:02 -06:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
const remoteVideosRouter = express.Router()
|
2016-06-18 09:13:54 -05:00
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
remoteVideosRouter.post('/',
|
|
|
|
signatureValidator,
|
|
|
|
checkSignature,
|
|
|
|
remoteVideosValidator,
|
2016-06-18 09:13:54 -05:00
|
|
|
remoteVideos
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
remoteVideosRouter.post('/qadu',
|
|
|
|
signatureValidator,
|
|
|
|
checkSignature,
|
|
|
|
remoteQaduVideosValidator,
|
2017-02-21 14:35:59 -06:00
|
|
|
remoteVideosQadu
|
|
|
|
)
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
remoteVideosRouter.post('/events',
|
|
|
|
signatureValidator,
|
|
|
|
checkSignature,
|
|
|
|
remoteEventsVideosValidator,
|
2017-02-26 11:57:33 -06:00
|
|
|
remoteVideosEvents
|
|
|
|
)
|
|
|
|
|
2016-06-18 09:13:54 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-05-15 15:22:03 -05:00
|
|
|
export {
|
|
|
|
remoteVideosRouter
|
|
|
|
}
|
2016-06-18 09:13:54 -05:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function remoteVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-10 12:43:21 -05:00
|
|
|
const requests: RemoteVideoRequest[] = req.body.data
|
2016-12-29 11:02:03 -06:00
|
|
|
const fromPod = res.locals.secure.pod
|
2016-06-18 09:13:54 -05:00
|
|
|
|
|
|
|
// We need to process in the same order to keep consistency
|
2017-10-25 04:55:06 -05:00
|
|
|
Bluebird.each(requests, request => {
|
2017-01-04 13:59:23 -06:00
|
|
|
const data = request.data
|
2016-06-18 09:13:54 -05:00
|
|
|
|
2017-01-17 13:50:02 -06:00
|
|
|
// Get the function we need to call in order to process the request
|
|
|
|
const fun = functionsHash[request.type]
|
|
|
|
if (fun === undefined) {
|
2017-09-12 05:53:55 -05:00
|
|
|
logger.error('Unknown remote request type %s.', request.type)
|
2017-07-05 06:26:25 -05:00
|
|
|
return
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
2017-01-17 13:50:02 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return fun.call(this, data, fromPod)
|
2016-06-18 09:13:54 -05:00
|
|
|
})
|
2017-07-07 11:26:12 -05:00
|
|
|
.catch(err => logger.error('Error managing remote videos.', err))
|
2016-06-18 09:13:54 -05:00
|
|
|
|
2017-07-07 09:57:28 -05:00
|
|
|
// Don't block the other pod
|
2016-06-18 09:13:54 -05:00
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function remoteVideosQadu (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-10 12:43:21 -05:00
|
|
|
const requests: RemoteQaduVideoRequest[] = req.body.data
|
2017-02-21 14:35:59 -06:00
|
|
|
const fromPod = res.locals.secure.pod
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
Bluebird.each(requests, request => {
|
2017-02-21 14:35:59 -06:00
|
|
|
const videoData = request.data
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return quickAndDirtyUpdateVideoRetryWrapper(videoData, fromPod)
|
2017-02-21 14:35:59 -06:00
|
|
|
})
|
2017-07-07 11:26:12 -05:00
|
|
|
.catch(err => logger.error('Error managing remote videos.', err))
|
2017-02-21 14:35:59 -06:00
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
2017-06-10 15:15:25 -05:00
|
|
|
function remoteVideosEvents (req: express.Request, res: express.Response, next: express.NextFunction) {
|
2017-07-10 12:43:21 -05:00
|
|
|
const requests: RemoteVideoEventRequest[] = req.body.data
|
2017-02-26 11:57:33 -06:00
|
|
|
const fromPod = res.locals.secure.pod
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
Bluebird.each(requests, request => {
|
2017-02-26 11:57:33 -06:00
|
|
|
const eventData = request.data
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return processVideosEventsRetryWrapper(eventData, fromPod)
|
2017-02-26 11:57:33 -06:00
|
|
|
})
|
2017-07-07 11:26:12 -05:00
|
|
|
.catch(err => logger.error('Error managing remote videos.', err))
|
2017-02-26 11:57:33 -06:00
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function processVideosEventsRetryWrapper (eventData: RemoteVideoEventData, fromPod: PodInstance) {
|
2017-02-26 11:57:33 -06:00
|
|
|
const options = {
|
|
|
|
arguments: [ eventData, fromPod ],
|
|
|
|
errorMessage: 'Cannot process videos events with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(processVideosEvents, options)
|
2017-02-26 11:57:33 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function processVideosEvents (eventData: RemoteVideoEventData, fromPod: PodInstance) {
|
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const sequelizeOptions = { transaction: t }
|
|
|
|
const videoInstance = await fetchVideoByUUID(eventData.uuid, t)
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
let columnToUpdate
|
|
|
|
let qaduType
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
switch (eventData.eventType) {
|
|
|
|
case REQUEST_VIDEO_EVENT_TYPES.VIEWS:
|
|
|
|
columnToUpdate = 'views'
|
|
|
|
qaduType = REQUEST_VIDEO_QADU_TYPES.VIEWS
|
|
|
|
break
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
case REQUEST_VIDEO_EVENT_TYPES.LIKES:
|
|
|
|
columnToUpdate = 'likes'
|
|
|
|
qaduType = REQUEST_VIDEO_QADU_TYPES.LIKES
|
|
|
|
break
|
2017-09-04 14:45:05 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
case REQUEST_VIDEO_EVENT_TYPES.DISLIKES:
|
|
|
|
columnToUpdate = 'dislikes'
|
|
|
|
qaduType = REQUEST_VIDEO_QADU_TYPES.DISLIKES
|
|
|
|
break
|
2017-09-04 14:45:05 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
default:
|
|
|
|
throw new Error('Unknown video event type.')
|
|
|
|
}
|
2017-09-04 14:45:05 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const query = {}
|
|
|
|
query[columnToUpdate] = eventData.count
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await videoInstance.increment(query, sequelizeOptions)
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const qadusParams = [
|
|
|
|
{
|
|
|
|
videoId: videoInstance.id,
|
|
|
|
type: qaduType
|
|
|
|
}
|
|
|
|
]
|
|
|
|
await quickAndDirtyUpdatesVideoToFriends(qadusParams, t)
|
2017-02-26 11:57:33 -06:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video event processed for video with uuid %s.', eventData.uuid)
|
2017-02-26 11:57:33 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function quickAndDirtyUpdateVideoRetryWrapper (videoData: RemoteQaduVideoData, fromPod: PodInstance) {
|
2017-02-21 14:35:59 -06:00
|
|
|
const options = {
|
|
|
|
arguments: [ videoData, fromPod ],
|
|
|
|
errorMessage: 'Cannot update quick and dirty the remote video with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(quickAndDirtyUpdateVideo, options)
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function quickAndDirtyUpdateVideo (videoData: RemoteQaduVideoData, fromPod: PodInstance) {
|
2017-09-12 05:53:55 -05:00
|
|
|
let videoUUID = ''
|
2017-03-19 03:16:33 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const videoInstance = await fetchVideoByHostAndUUID(fromPod.host, videoData.uuid, t)
|
|
|
|
const sequelizeOptions = { transaction: t }
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
videoUUID = videoInstance.uuid
|
2017-03-19 03:16:33 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
if (videoData.views) {
|
|
|
|
videoInstance.set('views', videoData.views)
|
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
if (videoData.likes) {
|
|
|
|
videoInstance.set('likes', videoData.likes)
|
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
if (videoData.dislikes) {
|
|
|
|
videoInstance.set('dislikes', videoData.dislikes)
|
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await videoInstance.save(sequelizeOptions)
|
2017-02-21 14:35:59 -06:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video with uuid %s quick and dirty updated', videoUUID)
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:24:47 -06:00
|
|
|
// Handle retries on fail
|
2017-10-25 04:55:06 -05:00
|
|
|
async function addRemoteVideoRetryWrapper (videoToCreateData: RemoteVideoCreateData, fromPod: PodInstance) {
|
2017-01-15 12:13:16 -06:00
|
|
|
const options = {
|
|
|
|
arguments: [ videoToCreateData, fromPod ],
|
|
|
|
errorMessage: 'Cannot insert the remote video with many retries.'
|
|
|
|
}
|
2017-01-06 16:24:47 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(addRemoteVideo, options)
|
2017-01-06 16:24:47 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function addRemoteVideo (videoToCreateData: RemoteVideoCreateData, fromPod: PodInstance) {
|
2017-07-11 09:01:56 -05:00
|
|
|
logger.debug('Adding remote video "%s".', videoToCreateData.uuid)
|
2016-07-05 14:36:01 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const sequelizeOptions = {
|
|
|
|
transaction: t
|
|
|
|
}
|
2016-12-11 14:50:51 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const videoFromDatabase = await db.Video.loadByUUID(videoToCreateData.uuid)
|
|
|
|
if (videoFromDatabase) throw new Error('UUID already exists.')
|
|
|
|
|
|
|
|
const videoChannel = await db.VideoChannel.loadByHostAndUUID(fromPod.host, videoToCreateData.channelUUID, t)
|
|
|
|
if (!videoChannel) throw new Error('Video channel ' + videoToCreateData.channelUUID + ' not found.')
|
|
|
|
|
|
|
|
const tags = videoToCreateData.tags
|
|
|
|
const tagInstances = await db.Tag.findOrCreateTags(tags, t)
|
|
|
|
|
|
|
|
const videoData = {
|
|
|
|
name: videoToCreateData.name,
|
|
|
|
uuid: videoToCreateData.uuid,
|
|
|
|
category: videoToCreateData.category,
|
|
|
|
licence: videoToCreateData.licence,
|
|
|
|
language: videoToCreateData.language,
|
|
|
|
nsfw: videoToCreateData.nsfw,
|
|
|
|
description: videoToCreateData.description,
|
|
|
|
channelId: videoChannel.id,
|
|
|
|
duration: videoToCreateData.duration,
|
|
|
|
createdAt: videoToCreateData.createdAt,
|
|
|
|
// FIXME: updatedAt does not seems to be considered by Sequelize
|
|
|
|
updatedAt: videoToCreateData.updatedAt,
|
|
|
|
views: videoToCreateData.views,
|
|
|
|
likes: videoToCreateData.likes,
|
|
|
|
dislikes: videoToCreateData.dislikes,
|
|
|
|
remote: true
|
|
|
|
}
|
2016-12-11 14:50:51 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const video = db.Video.build(videoData)
|
|
|
|
await db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData)
|
|
|
|
const videoCreated = await video.save(sequelizeOptions)
|
|
|
|
|
|
|
|
const tasks = []
|
|
|
|
for (const fileData of videoToCreateData.files) {
|
|
|
|
const videoFileInstance = db.VideoFile.build({
|
|
|
|
extname: fileData.extname,
|
|
|
|
infoHash: fileData.infoHash,
|
|
|
|
resolution: fileData.resolution,
|
|
|
|
size: fileData.size,
|
|
|
|
videoId: videoCreated.id
|
2016-12-24 09:59:17 -06:00
|
|
|
})
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
tasks.push(videoFileInstance.save(sequelizeOptions))
|
|
|
|
}
|
2017-08-25 04:36:23 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await Promise.all(tasks)
|
2016-12-24 09:59:17 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await videoCreated.setTags(tagInstances, sequelizeOptions)
|
2016-12-24 09:59:17 -06:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:24:47 -06:00
|
|
|
// Handle retries on fail
|
2017-10-25 04:55:06 -05:00
|
|
|
async function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: RemoteVideoUpdateData, fromPod: PodInstance) {
|
2017-01-15 12:13:16 -06:00
|
|
|
const options = {
|
2017-01-15 15:22:41 -06:00
|
|
|
arguments: [ videoAttributesToUpdate, fromPod ],
|
2017-01-15 12:13:16 -06:00
|
|
|
errorMessage: 'Cannot update the remote video with many retries'
|
|
|
|
}
|
2017-01-06 16:24:47 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(updateRemoteVideo, options)
|
2017-01-06 16:24:47 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function updateRemoteVideo (videoAttributesToUpdate: RemoteVideoUpdateData, fromPod: PodInstance) {
|
2017-07-11 09:01:56 -05:00
|
|
|
logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid)
|
2016-12-11 14:50:51 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
try {
|
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const sequelizeOptions = {
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
|
|
|
|
const videoInstance = await fetchVideoByHostAndUUID(fromPod.host, videoAttributesToUpdate.uuid, t)
|
|
|
|
const tags = videoAttributesToUpdate.tags
|
|
|
|
|
|
|
|
const tagInstances = await db.Tag.findOrCreateTags(tags, t)
|
|
|
|
|
|
|
|
videoInstance.set('name', videoAttributesToUpdate.name)
|
|
|
|
videoInstance.set('category', videoAttributesToUpdate.category)
|
|
|
|
videoInstance.set('licence', videoAttributesToUpdate.licence)
|
|
|
|
videoInstance.set('language', videoAttributesToUpdate.language)
|
|
|
|
videoInstance.set('nsfw', videoAttributesToUpdate.nsfw)
|
|
|
|
videoInstance.set('description', videoAttributesToUpdate.description)
|
|
|
|
videoInstance.set('duration', videoAttributesToUpdate.duration)
|
|
|
|
videoInstance.set('createdAt', videoAttributesToUpdate.createdAt)
|
|
|
|
videoInstance.set('updatedAt', videoAttributesToUpdate.updatedAt)
|
|
|
|
videoInstance.set('views', videoAttributesToUpdate.views)
|
|
|
|
videoInstance.set('likes', videoAttributesToUpdate.likes)
|
|
|
|
videoInstance.set('dislikes', videoAttributesToUpdate.dislikes)
|
|
|
|
|
|
|
|
await videoInstance.save(sequelizeOptions)
|
|
|
|
|
|
|
|
// Remove old video files
|
|
|
|
const videoFileDestroyTasks: Bluebird<void>[] = []
|
|
|
|
for (const videoFile of videoInstance.VideoFiles) {
|
|
|
|
videoFileDestroyTasks.push(videoFile.destroy(sequelizeOptions))
|
|
|
|
}
|
|
|
|
await Promise.all(videoFileDestroyTasks)
|
|
|
|
|
|
|
|
const videoFileCreateTasks: Bluebird<VideoFileInstance>[] = []
|
|
|
|
for (const fileData of videoAttributesToUpdate.files) {
|
|
|
|
const videoFileInstance = db.VideoFile.build({
|
|
|
|
extname: fileData.extname,
|
|
|
|
infoHash: fileData.infoHash,
|
|
|
|
resolution: fileData.resolution,
|
|
|
|
size: fileData.size,
|
|
|
|
videoId: videoInstance.id
|
2017-09-12 05:53:55 -05:00
|
|
|
})
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
videoFileCreateTasks.push(videoFileInstance.save(sequelizeOptions))
|
|
|
|
}
|
2017-08-25 04:36:23 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await Promise.all(videoFileCreateTasks)
|
2016-12-30 04:27:42 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await videoInstance.setTags(tagInstances, sequelizeOptions)
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Remote video with uuid %s updated', videoAttributesToUpdate.uuid)
|
|
|
|
} catch (err) {
|
2017-07-05 06:26:25 -05:00
|
|
|
// This is just a debug because we will retry the insert
|
2017-07-07 11:26:12 -05:00
|
|
|
logger.debug('Cannot update the remote video.', err)
|
2017-07-05 06:26:25 -05:00
|
|
|
throw err
|
2017-10-25 04:55:06 -05:00
|
|
|
}
|
2016-12-30 04:27:42 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function removeRemoteVideoRetryWrapper (videoToRemoveData: RemoteVideoRemoveData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ videoToRemoveData, fromPod ],
|
|
|
|
errorMessage: 'Cannot remove the remote video channel with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(removeRemoteVideo, options)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function removeRemoteVideo (videoToRemoveData: RemoteVideoRemoveData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
logger.debug('Removing remote video "%s".', videoToRemoveData.uuid)
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
2017-10-24 12:41:09 -05:00
|
|
|
// We need the instance because we have to remove some other stuffs (thumbnail etc)
|
2017-10-25 04:55:06 -05:00
|
|
|
const videoInstance = await fetchVideoByHostAndUUID(fromPod.host, videoToRemoveData.uuid, t)
|
|
|
|
await videoInstance.destroy({ transaction: t })
|
2017-10-24 12:41:09 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video with uuid %s removed.', videoToRemoveData.uuid)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function addRemoteVideoAuthorRetryWrapper (authorToCreateData: RemoteVideoAuthorCreateData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ authorToCreateData, fromPod ],
|
|
|
|
errorMessage: 'Cannot insert the remote video author with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(addRemoteVideoAuthor, options)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function addRemoteVideoAuthor (authorToCreateData: RemoteVideoAuthorCreateData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
logger.debug('Adding remote video author "%s".', authorToCreateData.uuid)
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const authorInDatabase = await db.Author.loadAuthorByPodAndUUID(authorToCreateData.uuid, fromPod.id, t)
|
|
|
|
if (authorInDatabase) throw new Error('Author with UUID ' + authorToCreateData.uuid + ' already exists.')
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const videoAuthorData = {
|
|
|
|
name: authorToCreateData.name,
|
|
|
|
uuid: authorToCreateData.uuid,
|
|
|
|
userId: null, // Not on our pod
|
|
|
|
podId: fromPod.id
|
|
|
|
}
|
|
|
|
|
|
|
|
const author = db.Author.build(videoAuthorData)
|
|
|
|
await author.save({ transaction: t })
|
2017-10-24 12:41:09 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video author with uuid %s inserted.', authorToCreateData.uuid)
|
2017-01-04 13:59:23 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function removeRemoteVideoAuthorRetryWrapper (authorAttributesToRemove: RemoteVideoAuthorRemoveData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ authorAttributesToRemove, fromPod ],
|
|
|
|
errorMessage: 'Cannot remove the remote video author with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(removeRemoteVideoAuthor, options)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function removeRemoteVideoAuthor (authorAttributesToRemove: RemoteVideoAuthorRemoveData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
logger.debug('Removing remote video author "%s".', authorAttributesToRemove.uuid)
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const videoAuthor = await db.Author.loadAuthorByPodAndUUID(authorAttributesToRemove.uuid, fromPod.id, t)
|
|
|
|
await videoAuthor.destroy({ transaction: t })
|
2017-10-24 12:41:09 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video author with uuid %s removed.', authorAttributesToRemove.uuid)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function addRemoteVideoChannelRetryWrapper (videoChannelToCreateData: RemoteVideoChannelCreateData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ videoChannelToCreateData, fromPod ],
|
|
|
|
errorMessage: 'Cannot insert the remote video channel with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(addRemoteVideoChannel, options)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function addRemoteVideoChannel (videoChannelToCreateData: RemoteVideoChannelCreateData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const videoChannelInDatabase = await db.VideoChannel.loadByUUID(videoChannelToCreateData.uuid)
|
|
|
|
if (videoChannelInDatabase) {
|
|
|
|
throw new Error('Video channel with UUID ' + videoChannelToCreateData.uuid + ' already exists.')
|
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const authorUUID = videoChannelToCreateData.ownerUUID
|
|
|
|
const podId = fromPod.id
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const author = await db.Author.loadAuthorByPodAndUUID(authorUUID, podId, t)
|
|
|
|
if (!author) throw new Error('Unknown author UUID' + authorUUID + '.')
|
|
|
|
|
|
|
|
const videoChannelData = {
|
|
|
|
name: videoChannelToCreateData.name,
|
|
|
|
description: videoChannelToCreateData.description,
|
|
|
|
uuid: videoChannelToCreateData.uuid,
|
|
|
|
createdAt: videoChannelToCreateData.createdAt,
|
|
|
|
updatedAt: videoChannelToCreateData.updatedAt,
|
|
|
|
remote: true,
|
|
|
|
authorId: author.id
|
|
|
|
}
|
|
|
|
|
|
|
|
const videoChannel = db.VideoChannel.build(videoChannelData)
|
|
|
|
await videoChannel.save({ transaction: t })
|
2017-10-24 12:41:09 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function updateRemoteVideoChannelRetryWrapper (videoChannelAttributesToUpdate: RemoteVideoChannelUpdateData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ videoChannelAttributesToUpdate, fromPod ],
|
|
|
|
errorMessage: 'Cannot update the remote video channel with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(updateRemoteVideoChannel, options)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function updateRemoteVideoChannel (videoChannelAttributesToUpdate: RemoteVideoChannelUpdateData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
logger.debug('Updating remote video channel "%s".', videoChannelAttributesToUpdate.uuid)
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const sequelizeOptions = { transaction: t }
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
const videoChannelInstance = await fetchVideoChannelByHostAndUUID(fromPod.host, videoChannelAttributesToUpdate.uuid, t)
|
|
|
|
videoChannelInstance.set('name', videoChannelAttributesToUpdate.name)
|
|
|
|
videoChannelInstance.set('description', videoChannelAttributesToUpdate.description)
|
|
|
|
videoChannelInstance.set('createdAt', videoChannelAttributesToUpdate.createdAt)
|
|
|
|
videoChannelInstance.set('updatedAt', videoChannelAttributesToUpdate.updatedAt)
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await videoChannelInstance.save(sequelizeOptions)
|
2017-10-24 12:41:09 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video channel with uuid %s updated', videoChannelAttributesToUpdate.uuid)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function removeRemoteVideoChannelRetryWrapper (videoChannelAttributesToRemove: RemoteVideoChannelRemoveData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ videoChannelAttributesToRemove, fromPod ],
|
|
|
|
errorMessage: 'Cannot remove the remote video channel with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(removeRemoteVideoChannel, options)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function removeRemoteVideoChannel (videoChannelAttributesToRemove: RemoteVideoChannelRemoveData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
logger.debug('Removing remote video channel "%s".', videoChannelAttributesToRemove.uuid)
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const videoChannel = await fetchVideoChannelByHostAndUUID(fromPod.host, videoChannelAttributesToRemove.uuid, t)
|
|
|
|
await videoChannel.destroy({ transaction: t })
|
2017-10-24 12:41:09 -05:00
|
|
|
})
|
2017-10-25 04:55:06 -05:00
|
|
|
|
|
|
|
logger.info('Remote video channel with uuid %s removed.', videoChannelAttributesToRemove.uuid)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function reportAbuseRemoteVideoRetryWrapper (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
const options = {
|
|
|
|
arguments: [ reportData, fromPod ],
|
|
|
|
errorMessage: 'Cannot create remote abuse video with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await retryTransactionWrapper(reportAbuseRemoteVideo, options)
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function reportAbuseRemoteVideo (reportData: RemoteVideoReportAbuseData, fromPod: PodInstance) {
|
2017-10-24 12:41:09 -05:00
|
|
|
logger.debug('Reporting remote abuse for video %s.', reportData.videoUUID)
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.sequelize.transaction(async t => {
|
|
|
|
const videoInstance = await fetchVideoByUUID(reportData.videoUUID, t)
|
|
|
|
const videoAbuseData = {
|
|
|
|
reporterUsername: reportData.reporterUsername,
|
|
|
|
reason: reportData.reportReason,
|
|
|
|
reporterPodId: fromPod.id,
|
|
|
|
videoId: videoInstance.id
|
|
|
|
}
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
await db.VideoAbuse.create(videoAbuseData)
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Remote abuse for video uuid %s created', reportData.videoUUID)
|
2017-02-26 11:57:33 -06:00
|
|
|
}
|
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function fetchVideoByUUID (id: string, t: Sequelize.Transaction) {
|
|
|
|
try {
|
|
|
|
const video = await db.Video.loadByUUID(id, t)
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
if (!video) throw new Error('Video ' + id + ' not found')
|
|
|
|
|
|
|
|
return video
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot load owned video from id.', { error: err.stack, id })
|
|
|
|
throw err
|
|
|
|
}
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
async function fetchVideoByHostAndUUID (podHost: string, uuid: string, t: Sequelize.Transaction) {
|
|
|
|
try {
|
|
|
|
const video = await db.Video.loadByHostAndUUID(podHost, uuid, t)
|
|
|
|
if (!video) throw new Error('Video not found')
|
2017-10-24 12:41:09 -05:00
|
|
|
|
2017-10-25 04:55:06 -05:00
|
|
|
return video
|
|
|
|
} catch (err) {
|
|
|
|
logger.error('Cannot load video from host and uuid.', { error: err.stack, podHost, uuid })
|
|
|
|
throw err
|
|
|
|
}
|
2017-10-24 12:41:09 -05:00
|
|
|
}
|