2017-06-05 14:53:49 -05:00
|
|
|
import * as express from 'express'
|
2017-07-05 06:26:25 -05:00
|
|
|
import * as Promise from 'bluebird'
|
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-05-15 15:22:03 -05:00
|
|
|
import { quickAndDirtyUpdatesVideoToFriends } from '../../../lib'
|
2017-06-10 15:15:25 -05:00
|
|
|
import { PodInstance, VideoInstance } from '../../../models'
|
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-07-05 06:26:25 -05:00
|
|
|
const functionsHash: { [ id: string ]: (...args) => Promise<any> } = {}
|
2017-01-17 13:50:02 -06:00
|
|
|
functionsHash[ENDPOINT_ACTIONS.ADD] = addRemoteVideoRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.UPDATE] = updateRemoteVideoRetryWrapper
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.REMOVE] = removeRemoteVideo
|
|
|
|
functionsHash[ENDPOINT_ACTIONS.REPORT_ABUSE] = reportAbuseRemoteVideo
|
|
|
|
|
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) {
|
2016-06-18 09:13:54 -05:00
|
|
|
const requests = 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-07-07 09:57:28 -05:00
|
|
|
Promise.each(requests, (request: any) => {
|
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) {
|
|
|
|
logger.error('Unkown 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-05 06:26:25 -05:00
|
|
|
.catch(err => logger.error('Error managing remote videos.', { error: 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-02-21 14:35:59 -06:00
|
|
|
const requests = req.body.data
|
|
|
|
const fromPod = res.locals.secure.pod
|
|
|
|
|
2017-07-07 09:57:28 -05:00
|
|
|
Promise.each(requests, (request: any) => {
|
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-05 06:26:25 -05:00
|
|
|
.catch(err => logger.error('Error managing remote videos.', { error: 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-02-26 11:57:33 -06:00
|
|
|
const requests = req.body.data
|
|
|
|
const fromPod = res.locals.secure.pod
|
|
|
|
|
2017-07-07 09:57:28 -05:00
|
|
|
Promise.each(requests, (request: any) => {
|
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-05 06:26:25 -05:00
|
|
|
.catch(err => logger.error('Error managing remote videos.', { error: err }))
|
2017-02-26 11:57:33 -06:00
|
|
|
|
|
|
|
return res.type('json').status(204).end()
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function processVideosEventsRetryWrapper (eventData: any, fromPod: PodInstance) {
|
2017-02-26 11:57:33 -06:00
|
|
|
const options = {
|
|
|
|
arguments: [ eventData, fromPod ],
|
|
|
|
errorMessage: 'Cannot process videos events with many retries.'
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return retryTransactionWrapper(processVideosEvents, options)
|
2017-02-26 11:57:33 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function processVideosEvents (eventData: any, fromPod: PodInstance) {
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.sequelize.transaction(t => {
|
|
|
|
return fetchOwnedVideo(eventData.remoteId)
|
|
|
|
.then(videoInstance => {
|
|
|
|
const options = { transaction: t }
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
let columnToUpdate
|
|
|
|
let qaduType
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-05 06:26:25 -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-07-05 06:26:25 -05:00
|
|
|
case REQUEST_VIDEO_EVENT_TYPES.LIKES:
|
|
|
|
columnToUpdate = 'likes'
|
|
|
|
qaduType = REQUEST_VIDEO_QADU_TYPES.LIKES
|
|
|
|
break
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
case REQUEST_VIDEO_EVENT_TYPES.DISLIKES:
|
|
|
|
columnToUpdate = 'dislikes'
|
|
|
|
qaduType = REQUEST_VIDEO_QADU_TYPES.DISLIKES
|
|
|
|
break
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
default:
|
|
|
|
throw new Error('Unknown video event type.')
|
|
|
|
}
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
const query = {}
|
|
|
|
query[columnToUpdate] = eventData.count
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return videoInstance.increment(query, options).then(() => ({ videoInstance, qaduType }))
|
2017-03-08 14:35:43 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(({ videoInstance, qaduType }) => {
|
|
|
|
const qadusParams = [
|
|
|
|
{
|
|
|
|
videoId: videoInstance.id,
|
|
|
|
type: qaduType
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
return quickAndDirtyUpdatesVideoToFriends(qadusParams, t)
|
2017-02-26 11:57:33 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
})
|
|
|
|
.then(() => logger.info('Remote video event processed for video %s.', eventData.remoteId))
|
|
|
|
.catch(err => {
|
|
|
|
logger.debug('Cannot process a video event.', { error: err })
|
|
|
|
throw err
|
2017-02-26 11:57:33 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function quickAndDirtyUpdateVideoRetryWrapper (videoData: any, 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-07-05 06:26:25 -05:00
|
|
|
return retryTransactionWrapper(quickAndDirtyUpdateVideo, options)
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function quickAndDirtyUpdateVideo (videoData: any, fromPod: PodInstance) {
|
2017-03-19 03:16:33 -05:00
|
|
|
let videoName
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.sequelize.transaction(t => {
|
|
|
|
return fetchRemoteVideo(fromPod.host, videoData.remoteId)
|
|
|
|
.then(videoInstance => {
|
|
|
|
const options = { transaction: t }
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
videoName = videoInstance.name
|
2017-03-19 03:16:33 -05:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
if (videoData.views) {
|
|
|
|
videoInstance.set('views', videoData.views)
|
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
if (videoData.likes) {
|
|
|
|
videoInstance.set('likes', videoData.likes)
|
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
if (videoData.dislikes) {
|
|
|
|
videoInstance.set('dislikes', videoData.dislikes)
|
|
|
|
}
|
2017-02-21 14:35:59 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return videoInstance.save(options)
|
2017-02-21 14:35:59 -06:00
|
|
|
})
|
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(() => logger.info('Remote video %s quick and dirty updated', videoName))
|
|
|
|
.catch(err => logger.debug('Cannot quick and dirty update the remote video.', { error: err }))
|
2017-02-21 14:35:59 -06:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:24:47 -06:00
|
|
|
// Handle retries on fail
|
2017-07-05 06:26:25 -05:00
|
|
|
function addRemoteVideoRetryWrapper (videoToCreateData: any, 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-07-05 06:26:25 -05:00
|
|
|
return retryTransactionWrapper(addRemoteVideo, options)
|
2017-01-06 16:24:47 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function addRemoteVideo (videoToCreateData: any, fromPod: PodInstance) {
|
2017-01-06 16:24:47 -06:00
|
|
|
logger.debug('Adding remote video "%s".', videoToCreateData.remoteId)
|
2016-07-05 14:36:01 -05:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.sequelize.transaction(t => {
|
|
|
|
return db.Video.loadByHostAndRemoteId(fromPod.host, videoToCreateData.remoteId)
|
|
|
|
.then(video => {
|
|
|
|
if (video) throw new Error('RemoteId and host pair is not unique.')
|
2017-01-27 05:05:19 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return undefined
|
2017-01-27 05:05:19 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(() => {
|
|
|
|
const name = videoToCreateData.author
|
|
|
|
const podId = fromPod.id
|
|
|
|
// This author is from another pod so we do not associate a user
|
|
|
|
const userId = null
|
2016-12-11 14:50:51 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.Author.findOrCreateAuthor(name, podId, userId, t)
|
2016-12-11 14:50:51 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(author => {
|
|
|
|
const tags = videoToCreateData.tags
|
2016-12-11 14:50:51 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.Tag.findOrCreateTags(tags, t).then(tagInstances => ({ author, tagInstances }))
|
2016-12-24 09:59:17 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(({ author, tagInstances }) => {
|
|
|
|
const videoData = {
|
|
|
|
name: videoToCreateData.name,
|
|
|
|
remoteId: videoToCreateData.remoteId,
|
|
|
|
extname: videoToCreateData.extname,
|
|
|
|
infoHash: videoToCreateData.infoHash,
|
|
|
|
category: videoToCreateData.category,
|
|
|
|
licence: videoToCreateData.licence,
|
|
|
|
language: videoToCreateData.language,
|
|
|
|
nsfw: videoToCreateData.nsfw,
|
|
|
|
description: videoToCreateData.description,
|
|
|
|
authorId: author.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
|
2016-12-11 14:50:51 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
const video = db.Video.build(videoData)
|
|
|
|
return { tagInstances, video }
|
2016-12-11 14:50:51 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(({ tagInstances, video }) => {
|
|
|
|
return db.Video.generateThumbnailFromData(video, videoToCreateData.thumbnailData).then(() => ({ tagInstances, video }))
|
2016-12-24 09:59:17 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(({ tagInstances, video }) => {
|
|
|
|
const options = {
|
|
|
|
transaction: t
|
|
|
|
}
|
2016-12-24 09:59:17 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return video.save(options).then(videoCreated => ({ tagInstances, videoCreated }))
|
2016-12-24 09:59:17 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(({ tagInstances, videoCreated }) => {
|
|
|
|
const options = {
|
|
|
|
transaction: t
|
|
|
|
}
|
2016-12-24 09:59:17 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return videoCreated.setTags(tagInstances, options)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.then(() => logger.info('Remote video %s inserted.', videoToCreateData.name))
|
|
|
|
.catch(err => {
|
|
|
|
logger.debug('Cannot insert the remote video.', { error: err })
|
|
|
|
throw err
|
2016-12-24 09:59:17 -06:00
|
|
|
})
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|
|
|
|
|
2017-01-06 16:24:47 -06:00
|
|
|
// Handle retries on fail
|
2017-07-05 06:26:25 -05:00
|
|
|
function updateRemoteVideoRetryWrapper (videoAttributesToUpdate: any, 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-07-05 06:26:25 -05:00
|
|
|
return retryTransactionWrapper(updateRemoteVideo, options)
|
2017-01-06 16:24:47 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function updateRemoteVideo (videoAttributesToUpdate: any, fromPod: PodInstance) {
|
2017-01-06 16:24:47 -06:00
|
|
|
logger.debug('Updating remote video "%s".', videoAttributesToUpdate.remoteId)
|
2016-12-11 14:50:51 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.sequelize.transaction(t => {
|
|
|
|
return fetchRemoteVideo(fromPod.host, videoAttributesToUpdate.remoteId)
|
|
|
|
.then(videoInstance => {
|
|
|
|
const tags = videoAttributesToUpdate.tags
|
2016-12-30 04:27:42 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.Tag.findOrCreateTags(tags, t).then(tagInstances => ({ videoInstance, tagInstances }))
|
2016-12-30 04:27:42 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(({ videoInstance, tagInstances }) => {
|
|
|
|
const options = { transaction: 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('infoHash', videoAttributesToUpdate.infoHash)
|
|
|
|
videoInstance.set('duration', videoAttributesToUpdate.duration)
|
|
|
|
videoInstance.set('createdAt', videoAttributesToUpdate.createdAt)
|
|
|
|
videoInstance.set('updatedAt', videoAttributesToUpdate.updatedAt)
|
|
|
|
videoInstance.set('extname', videoAttributesToUpdate.extname)
|
|
|
|
videoInstance.set('views', videoAttributesToUpdate.views)
|
|
|
|
videoInstance.set('likes', videoAttributesToUpdate.likes)
|
|
|
|
videoInstance.set('dislikes', videoAttributesToUpdate.dislikes)
|
|
|
|
|
|
|
|
return videoInstance.save(options).then(() => ({ videoInstance, tagInstances }))
|
2016-12-30 04:27:42 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.then(({ videoInstance, tagInstances }) => {
|
|
|
|
const options = { transaction: t }
|
2016-12-30 04:27:42 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return videoInstance.setTags(tagInstances, options)
|
2016-12-30 04:27:42 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
})
|
|
|
|
.then(() => logger.info('Remote video %s updated', videoAttributesToUpdate.name))
|
|
|
|
.catch(err => {
|
|
|
|
// This is just a debug because we will retry the insert
|
|
|
|
logger.debug('Cannot update the remote video.', { error: err })
|
|
|
|
throw err
|
2016-12-30 04:27:42 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function removeRemoteVideo (videoToRemoveData: any, fromPod: PodInstance) {
|
2016-12-30 04:27:42 -06:00
|
|
|
// We need the instance because we have to remove some other stuffs (thumbnail etc)
|
2017-07-05 06:26:25 -05:00
|
|
|
return fetchRemoteVideo(fromPod.host, videoToRemoveData.remoteId)
|
|
|
|
.then(video => {
|
|
|
|
logger.debug('Removing remote video %s.', video.remoteId)
|
|
|
|
return video.destroy()
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
logger.debug('Could not fetch remote video.', { host: fromPod.host, remoteId: videoToRemoveData.remoteId, error: err })
|
2017-01-11 11:06:51 -06:00
|
|
|
})
|
2017-01-04 13:59:23 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function reportAbuseRemoteVideo (reportData: any, fromPod: PodInstance) {
|
|
|
|
return fetchOwnedVideo(reportData.videoRemoteId)
|
|
|
|
.then(video => {
|
|
|
|
logger.debug('Reporting remote abuse for video %s.', video.id)
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
const videoAbuseData = {
|
|
|
|
reporterUsername: reportData.reporterUsername,
|
|
|
|
reason: reportData.reportReason,
|
|
|
|
reporterPodId: fromPod.id,
|
|
|
|
videoId: video.id
|
2017-01-11 11:06:51 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return db.VideoAbuse.create(videoAbuseData)
|
2017-01-11 11:06:51 -06:00
|
|
|
})
|
2017-07-05 06:26:25 -05:00
|
|
|
.catch(err => logger.error('Cannot create remote abuse video.', { error: err }))
|
2017-01-04 13:59:23 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function fetchOwnedVideo (id: string) {
|
|
|
|
return db.Video.load(id)
|
|
|
|
.then(video => {
|
|
|
|
if (!video) throw new Error('Video not found')
|
2017-02-26 11:57:33 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return video
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2017-02-26 11:57:33 -06:00
|
|
|
logger.error('Cannot load owned video from id.', { error: err, id })
|
2017-07-05 06:26:25 -05:00
|
|
|
throw err
|
|
|
|
})
|
2017-02-26 11:57:33 -06:00
|
|
|
}
|
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
function fetchRemoteVideo (podHost: string, remoteId: string) {
|
|
|
|
return db.Video.loadByHostAndRemoteId(podHost, remoteId)
|
|
|
|
.then(video => {
|
|
|
|
if (!video) throw new Error('Video not found')
|
2017-01-04 13:59:23 -06:00
|
|
|
|
2017-07-05 06:26:25 -05:00
|
|
|
return video
|
|
|
|
})
|
|
|
|
.catch(err => {
|
2017-01-06 16:24:47 -06:00
|
|
|
logger.error('Cannot load video from host and remote id.', { error: err, podHost, remoteId })
|
2017-07-05 06:26:25 -05:00
|
|
|
throw err
|
|
|
|
})
|
2016-06-18 09:13:54 -05:00
|
|
|
}
|