PeerTube/server/lib/activitypub/process/process-create.ts

160 lines
6.0 KiB
TypeScript
Raw Normal View History

2017-11-20 02:43:39 -06:00
import { ActivityCreate, VideoChannelObject } from '../../../../shared'
2017-12-12 10:53:50 -06:00
import { DislikeObject, VideoAbuseObject, ViewObject } from '../../../../shared/models/activitypub/objects'
2017-11-20 02:43:39 -06:00
import { logger, retryTransactionWrapper } from '../../../helpers'
2017-12-12 10:53:50 -06:00
import { sequelizeTypescript } from '../../../initializers'
import { AccountModel } from '../../../models/account/account'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
import { VideoModel } from '../../../models/video/video'
import { VideoAbuseModel } from '../../../models/video/video-abuse'
import { VideoChannelModel } from '../../../models/video/video-channel'
2017-11-21 06:43:29 -06:00
import { getOrCreateAccountAndServer } from '../account'
import { forwardActivity } from '../send/misc'
import { getVideoChannelActivityPubUrl } from '../url'
import { addVideoChannelShares, videoChannelActivityObjectToDBAttributes } from './misc'
2017-11-09 10:51:58 -06:00
2017-11-10 07:34:45 -06:00
async function processCreateActivity (activity: ActivityCreate) {
2017-11-09 10:51:58 -06:00
const activityObject = activity.object
const activityType = activityObject.type
2017-11-21 06:43:29 -06:00
const account = await getOrCreateAccountAndServer(activity.actor)
2017-11-09 10:51:58 -06:00
2017-11-22 09:25:03 -06:00
if (activityType === 'View') {
return processCreateView(account, activity)
2017-11-23 07:19:55 -06:00
} else if (activityType === 'Dislike') {
return processCreateDislike(account, activity)
2017-11-22 09:25:03 -06:00
} else if (activityType === 'VideoChannel') {
2017-11-10 07:34:45 -06:00
return processCreateVideoChannel(account, activityObject as VideoChannelObject)
2017-11-15 08:12:23 -06:00
} else if (activityType === 'Flag') {
return processCreateVideoAbuse(account, activityObject as VideoAbuseObject)
2017-11-09 10:51:58 -06:00
}
logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id })
2017-11-10 07:34:45 -06:00
return Promise.resolve(undefined)
2017-11-09 10:51:58 -06:00
}
// ---------------------------------------------------------------------------
export {
processCreateActivity
}
// ---------------------------------------------------------------------------
2017-12-12 10:53:50 -06:00
async function processCreateDislike (byAccount: AccountModel, activity: ActivityCreate) {
2017-11-23 07:19:55 -06:00
const options = {
arguments: [ byAccount, activity ],
2017-11-23 07:19:55 -06:00
errorMessage: 'Cannot dislike the video with many retries.'
}
return retryTransactionWrapper(createVideoDislike, options)
}
2017-12-12 10:53:50 -06:00
function createVideoDislike (byAccount: AccountModel, activity: ActivityCreate) {
const dislike = activity.object as DislikeObject
2017-11-23 07:19:55 -06:00
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
const video = await VideoModel.loadByUrlAndPopulateAccount(dislike.object, t)
2017-11-23 07:19:55 -06: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 10:53:50 -06:00
const [ , created ] = await AccountVideoRateModel.findOrCreate({
2017-11-23 07:19:55 -06:00
where: rate,
defaults: rate,
transaction: t
2017-11-23 07:19:55 -06:00
})
2017-11-30 06:51:53 -06:00
if (created === true) await video.increment('dislikes', { transaction: t })
2017-11-23 07:19:55 -06:00
if (video.isOwned() && created === true) {
// Don't resend the activity to the sender
const exceptions = [ byAccount ]
await forwardActivity(activity, t, exceptions)
}
2017-11-23 07:19:55 -06:00
})
}
2017-12-12 10:53:50 -06:00
async function processCreateView (byAccount: AccountModel, activity: ActivityCreate) {
const view = activity.object as ViewObject
2017-12-12 10:53:50 -06:00
const video = await VideoModel.loadByUrlAndPopulateAccount(view.object)
2017-11-22 09:25:03 -06:00
if (!video) throw new Error('Unknown video ' + view.object)
2017-12-12 10:53:50 -06:00
const account = await AccountModel.loadByUrl(view.actor)
2017-11-22 09:25:03 -06: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 09:25:03 -06:00
}
2017-12-12 10:53:50 -06:00
async function processCreateVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) {
2017-11-09 10:51:58 -06:00
const options = {
2017-11-10 07:34:45 -06:00
arguments: [ account, videoChannelToCreateData ],
errorMessage: 'Cannot insert the remote video channel with many retries.'
2017-11-09 10:51:58 -06:00
}
const videoChannel = await retryTransactionWrapper(addRemoteVideoChannel, options)
if (videoChannelToCreateData.shares && Array.isArray(videoChannelToCreateData.shares.orderedItems)) {
await addVideoChannelShares(videoChannel, videoChannelToCreateData.shares.orderedItems)
}
return videoChannel
2017-11-09 10:51:58 -06:00
}
2017-12-12 10:53:50 -06:00
function addRemoteVideoChannel (account: AccountModel, videoChannelToCreateData: VideoChannelObject) {
2017-11-10 07:34:45 -06:00
logger.debug('Adding remote video channel "%s".', videoChannelToCreateData.uuid)
2017-11-09 10:51:58 -06:00
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
let videoChannel = await VideoChannelModel.loadByUUIDOrUrl(videoChannelToCreateData.uuid, videoChannelToCreateData.id, t)
if (videoChannel) return videoChannel
2017-11-10 07:34:45 -06:00
2017-11-16 08:22:39 -06:00
const videoChannelData = videoChannelActivityObjectToDBAttributes(videoChannelToCreateData, account)
2017-12-12 10:53:50 -06:00
videoChannel = new VideoChannelModel(videoChannelData)
2017-11-20 02:43:39 -06:00
videoChannel.url = getVideoChannelActivityPubUrl(videoChannel)
2017-11-09 10:51:58 -06:00
2017-11-15 10:56:21 -06:00
videoChannel = await videoChannel.save({ transaction: t })
logger.info('Remote video channel with uuid %s inserted.', videoChannelToCreateData.uuid)
2017-11-09 10:51:58 -06:00
2017-11-15 10:56:21 -06:00
return videoChannel
})
2017-11-09 10:51:58 -06:00
}
2017-11-15 08:12:23 -06:00
2017-12-12 10:53:50 -06:00
function processCreateVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) {
2017-11-15 08:12:23 -06:00
const options = {
arguments: [ account, videoAbuseToCreateData ],
errorMessage: 'Cannot insert the remote video abuse with many retries.'
}
return retryTransactionWrapper(addRemoteVideoAbuse, options)
}
2017-12-12 10:53:50 -06:00
function addRemoteVideoAbuse (account: AccountModel, videoAbuseToCreateData: VideoAbuseObject) {
2017-11-15 08:12:23 -06:00
logger.debug('Reporting remote abuse for video %s.', videoAbuseToCreateData.object)
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
const video = await VideoModel.loadByUrlAndPopulateAccount(videoAbuseToCreateData.object, t)
2017-11-15 08:12:23 -06:00
if (!video) {
logger.warn('Unknown video %s for remote video abuse.', videoAbuseToCreateData.object)
2017-11-17 08:52:26 -06:00
return undefined
2017-11-15 08:12:23 -06:00
}
const videoAbuseData = {
reporterAccountId: account.id,
reason: videoAbuseToCreateData.content,
videoId: video.id
}
2017-12-12 10:53:50 -06:00
await VideoAbuseModel.create(videoAbuseData)
2017-11-15 08:12:23 -06:00
logger.info('Remote abuse for video uuid %s created', videoAbuseToCreateData.object)
})
}