1
0
Fork 0
peertube/server/lib/activitypub/process/process-create.ts

239 lines
8.4 KiB
TypeScript
Raw Normal View History

2017-12-14 11:38:41 -05:00
import * as Bluebird from 'bluebird'
import { ActivityCreate, VideoTorrentObject } from '../../../../shared'
2017-12-12 11:53:50 -05:00
import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
2017-12-14 11:38:41 -05:00
import { VideoRateType } from '../../../../shared/models/videos'
2017-11-20 03:43:39 -05:00
import { logger, retryTransactionWrapper } from '../../../helpers'
2017-12-12 11:53:50 -05:00
import { sequelizeTypescript } from '../../../initializers'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
2017-12-14 11:38:41 -05:00
import { ActorModel } from '../../../models/activitypub/actor'
import { TagModel } from '../../../models/video/tag'
2017-12-12 11:53:50 -05:00
import { VideoModel } from '../../../models/video/video'
import { VideoAbuseModel } from '../../../models/video/video-abuse'
2017-12-14 11:38:41 -05:00
import { VideoFileModel } from '../../../models/video/video-file'
import { getOrCreateActorAndServerAndModel } from '../actor'
import { forwardActivity } from '../send/misc'
2017-12-14 11:38:41 -05:00
import { generateThumbnailFromUrl } from '../videos'
import { addVideoShares, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
2017-11-09 11:51:58 -05:00
2017-11-10 08:34:45 -05:00
async function processCreateActivity (activity: ActivityCreate) {
2017-11-09 11:51:58 -05:00
const activityObject = activity.object
const activityType = activityObject.type
2017-12-14 11:38:41 -05:00
const actor = await getOrCreateActorAndServerAndModel(activity.actor)
2017-11-09 11:51:58 -05:00
2017-11-22 10:25:03 -05:00
if (activityType === 'View') {
2017-12-14 11:38:41 -05:00
return processCreateView(actor, activity)
2017-11-23 08:19:55 -05:00
} else if (activityType === 'Dislike') {
2017-12-14 11:38:41 -05:00
return processCreateDislike(actor, activity)
} else if (activityType === 'Video') {
return processCreateVideo(actor, activity)
2017-11-15 09:12:23 -05:00
} else if (activityType === 'Flag') {
2017-12-14 11:38:41 -05:00
return processCreateVideoAbuse(actor, activityObject as VideoAbuseObject)
2017-11-09 11:51:58 -05:00
}
logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
2017-11-10 08:34:45 -05:00
return Promise.resolve(undefined)
2017-11-09 11:51:58 -05:00
}
// ---------------------------------------------------------------------------
export {
processCreateActivity
}
// ---------------------------------------------------------------------------
2017-12-14 11:38:41 -05: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)) {
await createRates(videoToCreateData.likes.orderedItems, video, 'like')
}
if (videoToCreateData.dislikes && Array.isArray(videoToCreateData.dislikes.orderedItems)) {
await createRates(videoToCreateData.dislikes.orderedItems, video, 'dislike')
}
if (videoToCreateData.shares && Array.isArray(videoToCreateData.shares.orderedItems)) {
await addVideoShares(video, videoToCreateData.shares.orderedItems)
}
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 08:19:55 -05:00
const options = {
2017-12-14 11:38:41 -05:00
arguments: [ byActor, activity ],
2017-11-23 08:19:55 -05:00
errorMessage: 'Cannot dislike the video with many retries.'
}
return retryTransactionWrapper(createVideoDislike, options)
}
2017-12-14 11:38:41 -05:00
function createVideoDislike (byActor: ActorModel, activity: ActivityCreate) {
const dislike = activity.object as DislikeObject
2017-12-14 11:38:41 -05:00
const byAccount = byActor.Account
if (!byAccount) throw new Error('Cannot create dislike with the non account actor ' + byActor.url)
2017-11-23 08:19:55 -05:00
2017-12-12 11:53:50 -05:00
return sequelizeTypescript.transaction(async t => {
const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
2017-11-23 08:19:55 -05: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 11:53:50 -05:00
const [ , created ] = await AccountVideoRateModel.findOrCreate({
2017-11-23 08:19:55 -05:00
where: rate,
defaults: rate,
transaction: t
2017-11-23 08:19:55 -05:00
})
2017-11-30 07:51:53 -05:00
if (created === true) await video.increment('dislikes', { transaction: t })
2017-11-23 08:19:55 -05:00
if (video.isOwned() && created === true) {
// Don't resend the activity to the sender
2017-12-14 11:38:41 -05:00
const exceptions = [ byActor ]
await forwardActivity(activity, t, exceptions)
}
2017-11-23 08:19:55 -05:00
})
}
2017-12-14 11:38:41 -05:00
async function processCreateView (byAccount: ActorModel, activity: ActivityCreate) {
const view = activity.object as ViewObject
2017-12-12 11:53:50 -05:00
const video = await VideoModel.loadByUrlAndPopulateAccount(view.object)
2017-11-22 10:25:03 -05:00
if (!video) throw new Error('Unknown video ' + view.object)
2017-12-14 11:38:41 -05:00
const account = await ActorModel.loadByUrl(view.actor)
2017-11-22 10:25:03 -05:00
if (!account) throw new Error('Unknown account ' + view.actor)
await video.increment('views')
if (video.isOwned()) {
// Don't resend the activity to the sender
const exceptions = [ byAccount ]
await forwardActivity(activity, undefined, exceptions)
}
2017-11-22 10:25:03 -05:00
}
2017-12-14 11:38:41 -05:00
function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
2017-11-15 09:12:23 -05:00
const options = {
2017-12-14 11:38:41 -05:00
arguments: [ actor, videoAbuseToCreateData ],
2017-11-15 09:12:23 -05:00
errorMessage: 'Cannot insert the remote video abuse with many retries.'
}
return retryTransactionWrapper(addRemoteVideoAbuse, options)
}
2017-12-14 11:38:41 -05:00
function addRemoteVideoAbuse (actor: ActorModel, videoAbuseToCreateData: VideoAbuseObject) {
2017-11-15 09:12:23 -05:00
logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
2017-12-14 11:38:41 -05:00
const account = actor.Account
if (!account) throw new Error('Cannot create dislike with the non account actor ' + actor.url)
2017-12-12 11:53:50 -05:00
return sequelizeTypescript.transaction(async t => {
const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
2017-11-15 09:12:23 -05:00
if (!video) {
logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
2017-11-17 09:52:26 -05:00
return undefined
2017-11-15 09:12:23 -05:00
}
const videoAbuseData = {
reporterAccountId: account.id,
reason: videoAbuseToCreateData.content,
videoId: video.id
}
2017-12-12 11:53:50 -05:00
await VideoAbuseModel.create(videoAbuseData)
2017-11-15 09:12:23 -05:00
logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
})
}