2017-12-14 17:38:41 +01:00
|
|
|
import * as Bluebird from 'bluebird'
|
|
|
|
import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
|
2017-12-22 09:14:50 +01:00
|
|
|
import { VideoCommentObject } from '../../../../shared/models/activitypub/objects/video-comment-object'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { VideoRateType } from '../../../../shared/models/videos'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
|
|
|
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
|
|
|
import { TagModel } from '../../../models/video/tag'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoAbuseModel } from '../../../models/video/video-abuse'
|
2017-12-22 09:14:50 +01:00
|
|
|
import { VideoCommentModel } from '../../../models/video/video-comment'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { VideoFileModel } from '../../../models/video/video-file'
|
|
|
|
import { getOrCreateActorAndServerAndModel } from '../actor'
|
2017-11-24 13:41:10 +01:00
|
|
|
import { forwardActivity } from '../send/misc'
|
2017-12-14 17:38:41 +01:00
|
|
|
import { generateThumbnailFromUrl } from '../videos'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { addVideoComments, addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-11-10 14:34:45 +01:00
|
|
|
async function processCreateActivity (activity: ActivityCreate) {
|
2017-11-09 17:51:58 +01:00
|
|
|
const activityObject = activity.object
|
|
|
|
const activityType = activityObject.type
|
2017-12-14 17:38:41 +01:00
|
|
|
const actor = await getOrCreateActorAndServerAndModel(activity.actor)
|
2017-11-09 17:51:58 +01:00
|
|
|
|
2017-11-22 16:25:03 +01:00
|
|
|
if (activityType === 'View') {
|
2017-12-14 17:38:41 +01:00
|
|
|
return processCreateView(actor, activity)
|
2017-11-23 14:19:55 +01:00
|
|
|
} else if (activityType === 'Dislike') {
|
2017-12-14 17:38:41 +01:00
|
|
|
return processCreateDislike(actor, activity)
|
|
|
|
} else if (activityType === 'Video') {
|
|
|
|
return processCreateVideo(actor, activity)
|
2017-11-15 15:12:23 +01:00
|
|
|
} else if (activityType === 'Flag') {
|
2017-12-14 17:38:41 +01:00
|
|
|
return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
|
2017-12-22 09:14:50 +01:00
|
|
|
} else if (activityType === 'Note') {
|
|
|
|
return processCreateVideoComment(actor, activity)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
|
2017-11-10 14:34:45 +01:00
|
|
|
return Promise.resolve(undefined)
|
2017-11-09 17:51:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processCreateActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
async function processCreateVideo (
|
|
|
|
actor: ActorModel,
|
|
|
|
activity: ActivityCreate
|
|
|
|
) {
|
|
|
|
const videoToCreateData = activity.object as VideoTorrentObject
|
|
|
|
|
|
|
|
const channel = videoToCreateData.attributedTo.find(a => a.type === 'Group')
|
|
|
|
if (!channel) throw new Error('Cannot find associated video channel to video ' + videoToCreateData.url)
|
|
|
|
|
|
|
|
const channelActor = await getOrCreateActorAndServerAndModel(channel.id)
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
arguments: [ actor, activity, videoToCreateData, channelActor ],
|
|
|
|
errorMessage: 'Cannot insert the remote video with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
const video = await retryTransactionWrapper(createRemoteVideo, options)
|
|
|
|
|
|
|
|
// Process outside the transaction because we could fetch remote data
|
|
|
|
if (videoToCreateData.likes && Array.isArray(videoToCreateData.likes.orderedItems)) {
|
2017-12-28 11:16:08 +01:00
|
|
|
logger.info('Adding likes of video %s.', video.uuid)
|
2017-12-14 17:38:41 +01:00
|
|
|
await createRates(videoToCreateData.likes.orderedItems, video, 'like')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
|
2017-12-28 11:16:08 +01:00
|
|
|
logger.info('Adding dislikes of video %s.', video.uuid)
|
2017-12-14 17:38:41 +01:00
|
|
|
await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
|
2017-12-28 11:16:08 +01:00
|
|
|
logger.info('Adding shares of video %s.', video.uuid)
|
2017-12-14 17:38:41 +01:00
|
|
|
await addVideoShares(video, videoToCreateData.shares.orderedItems)
|
|
|
|
}
|
|
|
|
|
2017-12-28 11:16:08 +01:00
|
|
|
if (videoToCreateData.comments && Array.isArray(videoToCreateData.comments.orderedItems)) {
|
|
|
|
logger.info('Adding comments of video %s.', video.uuid)
|
|
|
|
await addVideoComments(video, videoToCreateData.comments.orderedItems)
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
return video
|
|
|
|
}
|
|
|
|
|
|
|
|
function createRemoteVideo (
|
|
|
|
account: ActorModel,
|
|
|
|
activity: ActivityCreate,
|
|
|
|
videoToCreateData: VideoTorrentObject,
|
|
|
|
channelActor: ActorModel
|
|
|
|
) {
|
|
|
|
logger.debug('Adding remote video %s.', videoToCreateData.id)
|
|
|
|
|
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
const sequelizeOptions = {
|
|
|
|
transaction: t
|
|
|
|
}
|
|
|
|
const videoFromDatabase = await VideoModel.loadByUUIDOrURL(videoToCreateData.uuid, videoToCreateData.id, t)
|
|
|
|
if (videoFromDatabase) return videoFromDatabase
|
|
|
|
|
|
|
|
const videoData = await videoActivityObjectToDBAttributes(channelActor.VideoChannel, videoToCreateData, activity.to, activity.cc)
|
|
|
|
const video = VideoModel.build(videoData)
|
|
|
|
|
|
|
|
// Don't block on request
|
|
|
|
generateThumbnailFromUrl(video, videoToCreateData.icon)
|
|
|
|
.catch(err => logger.warn('Cannot generate thumbnail of %s.', videoToCreateData.id, err))
|
|
|
|
|
|
|
|
const videoCreated = await video.save(sequelizeOptions)
|
|
|
|
|
|
|
|
const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoToCreateData)
|
|
|
|
if (videoFileAttributes.length === 0) {
|
|
|
|
throw new Error('Cannot find valid files for video %s ' + videoToCreateData.url)
|
|
|
|
}
|
|
|
|
|
|
|
|
const tasks: Bluebird<any>[] = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t }))
|
|
|
|
await Promise.all(tasks)
|
|
|
|
|
|
|
|
const tags = videoToCreateData.tag.map(t => t.name)
|
|
|
|
const tagInstances = await TagModel.findOrCreateTags(tags, t)
|
|
|
|
await videoCreated.$set('Tags', tagInstances, sequelizeOptions)
|
|
|
|
|
|
|
|
logger.info('Remote video with uuid %s inserted.', videoToCreateData.uuid)
|
|
|
|
|
|
|
|
return videoCreated
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createRates (actorUrls: string[], video: VideoModel, rate: VideoRateType) {
|
|
|
|
let rateCounts = 0
|
|
|
|
const tasks: Bluebird<any>[] = []
|
|
|
|
|
|
|
|
for (const actorUrl of actorUrls) {
|
|
|
|
const actor = await getOrCreateActorAndServerAndModel(actorUrl)
|
|
|
|
const p = AccountVideoRateModel
|
|
|
|
.create({
|
|
|
|
videoId: video.id,
|
|
|
|
accountId: actor.Account.id,
|
|
|
|
type: rate
|
|
|
|
})
|
|
|
|
.then(() => rateCounts += 1)
|
|
|
|
|
|
|
|
tasks.push(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
await Promise.all(tasks)
|
|
|
|
|
|
|
|
logger.info('Adding %d %s to video %s.', rateCounts, rate, video.uuid)
|
|
|
|
|
|
|
|
// This is "likes" and "dislikes"
|
|
|
|
await video.increment(rate + 's', { by: rateCounts })
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processCreateDislike (byActor: ActorModel, activity: ActivityCreate) {
|
2017-11-23 14:19:55 +01:00
|
|
|
const options = {
|
2017-12-14 17:38:41 +01:00
|
|
|
arguments: [ byActor, activity ],
|
2017-11-23 14:19:55 +01:00
|
|
|
errorMessage: 'Cannot dislike the video with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(createVideoDislike, options)
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
|
2017-11-24 13:41:10 +01:00
|
|
|
const dislike = activity.object as DislikeObject
|
2017-12-14 17:38:41 +01:00
|
|
|
const byAccount = byActor.Account
|
|
|
|
|
|
|
|
if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
|
2017-11-23 14:19:55 +01:00
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
|
2017-11-23 14:19:55 +01:00
|
|
|
if (!video) throw new Error('Unknown video ' + dislike.object)
|
|
|
|
|
|
|
|
const rate = {
|
|
|
|
type: 'dislike' as 'dislike',
|
|
|
|
videoId: video.id,
|
|
|
|
accountId: byAccount.id
|
|
|
|
}
|
2017-12-12 17:53:50 +01:00
|
|
|
const [ , created ] = await AccountVideoRateModel.findOrCreate({
|
2017-11-23 14:19:55 +01:00
|
|
|
where: rate,
|
2017-11-24 13:41:10 +01:00
|
|
|
defaults: rate,
|
|
|
|
transaction: t
|
2017-11-23 14:19:55 +01:00
|
|
|
})
|
2017-11-30 13:51:53 +01:00
|
|
|
if (created === true) await video.increment('dislikes', { transaction: t })
|
2017-11-23 14:19:55 +01:00
|
|
|
|
2017-11-24 13:41:10 +01:00
|
|
|
if (video.isOwned() && created === true) {
|
|
|
|
// Don't resend the activity to the sender
|
2017-12-14 17:38:41 +01:00
|
|
|
const exceptions = [ byActor ]
|
2017-11-24 13:41:10 +01:00
|
|
|
await forwardActivity(activity, t, exceptions)
|
|
|
|
}
|
2017-11-23 14:19:55 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-12-22 09:14:50 +01:00
|
|
|
async function processCreateView (byActor: ActorModel, activity: ActivityCreate) {
|
2017-11-24 13:41:10 +01:00
|
|
|
const view = activity.object as ViewObject
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
const video = await VideoModel.loadByUrlAndPopulateAccount(view.object)
|
2017-11-22 16:25:03 +01:00
|
|
|
|
|
|
|
if (!video) throw new Error('Unknown video ' + view.object)
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
const account = await ActorModel.loadByUrl(view.actor)
|
2017-11-22 16:25:03 +01:00
|
|
|
if (!account) throw new Error('Unknown account ' + view.actor)
|
|
|
|
|
|
|
|
await video.increment('views')
|
|
|
|
|
2017-11-24 13:41:10 +01:00
|
|
|
if (video.isOwned()) {
|
|
|
|
// Don't resend the activity to the sender
|
2017-12-22 09:14:50 +01:00
|
|
|
const exceptions = [ byActor ]
|
2017-11-24 13:41:10 +01:00
|
|
|
await forwardActivity(activity, undefined, exceptions)
|
|
|
|
}
|
2017-11-22 16:25:03 +01:00
|
|
|
}
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
|
2017-11-15 15:12:23 +01:00
|
|
|
const options = {
|
2017-12-14 17:38:41 +01:00
|
|
|
arguments: [ actor, videoAbuseToCreateData ],
|
2017-11-15 15:12:23 +01:00
|
|
|
errorMessage: 'Cannot insert the remote video abuse with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(addRemoteVideoAbuse, options)
|
|
|
|
}
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
|
2017-11-15 15:12:23 +01:00
|
|
|
logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
|
|
|
|
|
2017-12-14 17:38:41 +01:00
|
|
|
const account = actor.Account
|
|
|
|
if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
|
|
|
const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
|
2017-11-15 15:12:23 +01:00
|
|
|
if (!video) {
|
|
|
|
logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
|
2017-11-17 15:52:26 +01:00
|
|
|
return undefined
|
2017-11-15 15:12:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const videoAbuseData = {
|
|
|
|
reporterAccountId: account.id,
|
|
|
|
reason: videoAbuseToCreateData.content,
|
|
|
|
videoId: video.id
|
|
|
|
}
|
|
|
|
|
2017-12-12 17:53:50 +01:00
|
|
|
await VideoAbuseModel.create(videoAbuseData)
|
2017-11-15 15:12:23 +01:00
|
|
|
|
|
|
|
logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
|
|
|
|
})
|
|
|
|
}
|
2017-12-22 09:14:50 +01:00
|
|
|
|
|
|
|
function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
|
|
|
|
const options = {
|
|
|
|
arguments: [ byActor, activity ],
|
|
|
|
errorMessage: 'Cannot create video comment with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(createVideoComment, options)
|
|
|
|
}
|
|
|
|
|
|
|
|
function createVideoComment (byActor: ActorModel, activity: ActivityCreate) {
|
|
|
|
const comment = activity.object as VideoCommentObject
|
|
|
|
const byAccount = byActor.Account
|
|
|
|
|
|
|
|
if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
|
|
|
|
|
|
|
|
return sequelizeTypescript.transaction(async t => {
|
2017-12-27 10:39:31 +01:00
|
|
|
let video = await VideoModel.loadByUrl(comment.inReplyTo, t)
|
2017-12-22 09:14:50 +01:00
|
|
|
|
|
|
|
// This is a new thread
|
|
|
|
if (video) {
|
2017-12-27 10:39:31 +01:00
|
|
|
await VideoCommentModel.create({
|
2017-12-22 09:14:50 +01:00
|
|
|
url: comment.id,
|
|
|
|
text: comment.content,
|
|
|
|
originCommentId: null,
|
|
|
|
inReplyToComment: null,
|
|
|
|
videoId: video.id,
|
2017-12-22 12:10:40 +01:00
|
|
|
accountId: byAccount.id
|
2017-12-22 09:14:50 +01:00
|
|
|
}, { transaction: t })
|
2017-12-27 10:39:31 +01:00
|
|
|
} else {
|
|
|
|
const inReplyToComment = await VideoCommentModel.loadByUrl(comment.inReplyTo, t)
|
|
|
|
if (!inReplyToComment) throw new Error('Unknown replied comment ' + comment.inReplyTo)
|
2017-12-22 09:14:50 +01:00
|
|
|
|
2017-12-27 10:39:31 +01:00
|
|
|
video = await VideoModel.load(inReplyToComment.videoId)
|
2017-12-22 09:14:50 +01:00
|
|
|
|
2017-12-27 10:39:31 +01:00
|
|
|
const originCommentId = inReplyToComment.originCommentId || inReplyToComment.id
|
|
|
|
await VideoCommentModel.create({
|
|
|
|
url: comment.id,
|
|
|
|
text: comment.content,
|
|
|
|
originCommentId,
|
|
|
|
inReplyToCommentId: inReplyToComment.id,
|
|
|
|
videoId: video.id,
|
|
|
|
accountId: byAccount.id
|
|
|
|
}, { transaction: t })
|
|
|
|
}
|
|
|
|
|
|
|
|
if (video.isOwned()) {
|
|
|
|
// Don't resend the activity to the sender
|
|
|
|
const exceptions = [ byActor ]
|
|
|
|
await forwardActivity(activity, t, exceptions)
|
|
|
|
}
|
2017-12-22 09:14:50 +01:00
|
|
|
})
|
|
|
|
}
|