2017-06-10 22:15:25 +02:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
|
2017-05-22 20:58:25 +02:00
|
|
|
import { database as db } from '../../initializers/database'
|
2017-07-10 19:43:21 +02:00
|
|
|
import { AbstractRequestScheduler, RequestsObjects } from './abstract-request-scheduler'
|
2017-05-15 22:22:03 +02:00
|
|
|
import { logger } from '../../helpers'
|
|
|
|
import {
|
|
|
|
REQUESTS_VIDEO_QADU_LIMIT_PODS,
|
|
|
|
REQUESTS_VIDEO_QADU_LIMIT_PER_POD,
|
|
|
|
REQUEST_VIDEO_QADU_ENDPOINT,
|
|
|
|
REQUEST_VIDEO_QADU_TYPES
|
|
|
|
} from '../../initializers'
|
2017-07-10 19:43:21 +02:00
|
|
|
import { RequestsVideoQaduGrouped, PodInstance } from '../../models'
|
|
|
|
import { RemoteQaduVideoRequest, RequestVideoQaduType } from '../../../shared'
|
|
|
|
|
|
|
|
// We create a custom interface because we need "videos" attribute for our computations
|
|
|
|
interface RequestsObjectsCustom<U> extends RequestsObjects<U> {
|
|
|
|
[ id: string ]: {
|
|
|
|
toPod: PodInstance
|
|
|
|
endpoint: string
|
|
|
|
ids: number[] // ids
|
|
|
|
datas: U[]
|
|
|
|
|
|
|
|
videos: {
|
2017-07-11 16:01:56 +02:00
|
|
|
[ uuid: string ]: {
|
|
|
|
uuid: string
|
2017-07-10 19:43:21 +02:00
|
|
|
likes?: number
|
|
|
|
dislikes?: number
|
|
|
|
views?: number
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 22:22:03 +02:00
|
|
|
|
2017-06-10 22:15:25 +02:00
|
|
|
export type RequestVideoQaduSchedulerOptions = {
|
2017-06-16 10:36:18 +02:00
|
|
|
type: RequestVideoQaduType
|
2017-07-11 16:01:56 +02:00
|
|
|
videoId: number
|
2017-06-10 22:15:25 +02:00
|
|
|
transaction?: Sequelize.Transaction
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
class RequestVideoQaduScheduler extends AbstractRequestScheduler<RequestsVideoQaduGrouped> {
|
2017-02-21 21:35:59 +01:00
|
|
|
constructor () {
|
|
|
|
super()
|
|
|
|
|
|
|
|
// We limit the size of the requests
|
2017-05-15 22:22:03 +02:00
|
|
|
this.limitPods = REQUESTS_VIDEO_QADU_LIMIT_PODS
|
|
|
|
this.limitPerPod = REQUESTS_VIDEO_QADU_LIMIT_PER_POD
|
2017-02-21 21:35:59 +01:00
|
|
|
|
|
|
|
this.description = 'video QADU requests'
|
|
|
|
}
|
|
|
|
|
|
|
|
getRequestModel () {
|
|
|
|
return db.RequestVideoQadu
|
|
|
|
}
|
|
|
|
|
|
|
|
getRequestToPodModel () {
|
|
|
|
return db.RequestVideoQadu
|
|
|
|
}
|
|
|
|
|
2017-07-10 19:43:21 +02:00
|
|
|
buildRequestsObjects (requests: RequestsVideoQaduGrouped) {
|
|
|
|
const requestsToMakeGrouped: RequestsObjectsCustom<RemoteQaduVideoRequest> = {}
|
2017-02-21 21:35:59 +01:00
|
|
|
|
|
|
|
Object.keys(requests).forEach(toPodId => {
|
|
|
|
requests[toPodId].forEach(data => {
|
|
|
|
const request = data.request
|
|
|
|
const video = data.video
|
|
|
|
const pod = data.pod
|
|
|
|
const hashKey = toPodId
|
|
|
|
|
|
|
|
if (!requestsToMakeGrouped[hashKey]) {
|
|
|
|
requestsToMakeGrouped[hashKey] = {
|
|
|
|
toPod: pod,
|
2017-05-15 22:22:03 +02:00
|
|
|
endpoint: REQUEST_VIDEO_QADU_ENDPOINT,
|
2017-02-21 21:35:59 +01:00
|
|
|
ids: [], // request ids, to delete them from the DB in the future
|
|
|
|
datas: [], // requests data
|
|
|
|
videos: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 21:35:43 +01:00
|
|
|
// Maybe another attribute was filled for this video
|
|
|
|
let videoData = requestsToMakeGrouped[hashKey].videos[video.id]
|
2017-07-11 16:01:56 +02:00
|
|
|
if (!videoData) videoData = { uuid: null }
|
2017-03-08 21:35:43 +01:00
|
|
|
|
2017-02-21 21:35:59 +01:00
|
|
|
switch (request.type) {
|
2017-09-04 21:45:05 +02:00
|
|
|
case REQUEST_VIDEO_QADU_TYPES.LIKES:
|
|
|
|
videoData.likes = video.likes
|
|
|
|
break
|
2017-02-21 21:35:59 +01:00
|
|
|
|
2017-09-04 21:45:05 +02:00
|
|
|
case REQUEST_VIDEO_QADU_TYPES.DISLIKES:
|
|
|
|
videoData.dislikes = video.dislikes
|
|
|
|
break
|
2017-02-21 21:35:59 +01:00
|
|
|
|
2017-09-04 21:45:05 +02:00
|
|
|
case REQUEST_VIDEO_QADU_TYPES.VIEWS:
|
|
|
|
videoData.views = video.views
|
|
|
|
break
|
2017-02-21 21:35:59 +01:00
|
|
|
|
2017-09-04 21:45:05 +02:00
|
|
|
default:
|
|
|
|
logger.error('Unknown request video QADU type %s.', request.type)
|
|
|
|
return
|
2017-02-21 21:35:59 +01:00
|
|
|
}
|
|
|
|
|
2017-07-11 16:01:56 +02:00
|
|
|
// Do not forget the uuid so the remote pod can identify the video
|
|
|
|
videoData.uuid = video.uuid
|
2017-02-21 21:35:59 +01:00
|
|
|
requestsToMakeGrouped[hashKey].ids.push(request.id)
|
2017-02-26 19:27:08 +01:00
|
|
|
|
|
|
|
// Maybe there are multiple quick and dirty update for the same video
|
2017-09-04 21:45:05 +02:00
|
|
|
// We use this hash map to dedupe them
|
2017-02-21 21:35:59 +01:00
|
|
|
requestsToMakeGrouped[hashKey].videos[video.id] = videoData
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-09-04 21:45:05 +02:00
|
|
|
// Now we deduped similar quick and dirty updates, we can build our requests data
|
2017-02-21 21:35:59 +01:00
|
|
|
Object.keys(requestsToMakeGrouped).forEach(hashKey => {
|
2017-07-11 16:01:56 +02:00
|
|
|
Object.keys(requestsToMakeGrouped[hashKey].videos).forEach(videoUUID => {
|
|
|
|
const videoData = requestsToMakeGrouped[hashKey].videos[videoUUID]
|
2017-02-21 21:35:59 +01:00
|
|
|
|
|
|
|
requestsToMakeGrouped[hashKey].datas.push({
|
|
|
|
data: videoData
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-09-04 21:45:05 +02:00
|
|
|
// We don't need it anymore, it was just to build our data array
|
2017-02-21 21:35:59 +01:00
|
|
|
delete requestsToMakeGrouped[hashKey].videos
|
|
|
|
})
|
|
|
|
|
|
|
|
return requestsToMakeGrouped
|
|
|
|
}
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
createRequest ({ type, videoId, transaction }: RequestVideoQaduSchedulerOptions) {
|
2017-06-10 22:15:25 +02:00
|
|
|
const dbRequestOptions: Sequelize.BulkCreateOptions = {}
|
2017-02-21 21:35:59 +01:00
|
|
|
if (transaction) dbRequestOptions.transaction = transaction
|
|
|
|
|
|
|
|
// Send the update to all our friends
|
2017-07-05 13:26:25 +02:00
|
|
|
return db.Pod.listAllIds(transaction).then(podIds => {
|
2017-02-21 21:35:59 +01:00
|
|
|
const queries = []
|
|
|
|
podIds.forEach(podId => {
|
|
|
|
queries.push({ type, videoId, podId })
|
|
|
|
})
|
|
|
|
|
2017-07-05 13:26:25 +02:00
|
|
|
return db.RequestVideoQadu.bulkCreate(queries, dbRequestOptions)
|
2017-02-21 21:35:59 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 22:22:03 +02:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
RequestVideoQaduScheduler
|
|
|
|
}
|