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

97 lines
4.0 KiB
TypeScript
Raw Normal View History

import { ActivityUpdate, VideoTorrentObject } from '../../../../shared/models/activitypub'
2018-01-03 09:38:50 -06:00
import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
2018-08-14 08:28:30 -05:00
import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
2017-12-28 04:16:08 -06:00
import { logger } from '../../../helpers/logger'
2017-12-12 10:53:50 -06:00
import { sequelizeTypescript } from '../../../initializers'
2018-01-03 09:38:50 -06:00
import { AccountModel } from '../../../models/account/account'
2017-12-14 10:38:41 -06:00
import { ActorModel } from '../../../models/activitypub/actor'
import { VideoChannelModel } from '../../../models/video/video-channel'
2018-01-04 07:04:02 -06:00
import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
2018-08-22 09:15:35 -05:00
import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannel, updateVideoFromAP } from '../videos'
import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
2017-11-10 07:34:45 -06:00
async function processUpdateActivity (activity: ActivityUpdate) {
2017-12-14 10:38:41 -06:00
const actor = await getOrCreateActorAndServerAndModel(activity.actor)
const objectType = activity.object.type
2017-11-09 10:51:58 -06:00
if (objectType === 'Video') {
2018-06-13 07:27:40 -05:00
return retryTransactionWrapper(processUpdateVideo, actor, activity)
} else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
2018-06-13 07:27:40 -05:00
return retryTransactionWrapper(processUpdateActor, actor, activity)
2017-11-09 10:51:58 -06:00
}
2017-11-10 07:34:45 -06:00
return undefined
2017-11-09 10:51:58 -06:00
}
// ---------------------------------------------------------------------------
export {
processUpdateActivity
}
// ---------------------------------------------------------------------------
2018-06-13 07:27:40 -05:00
async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
const videoObject = activity.object as VideoTorrentObject
2017-12-14 10:38:41 -06:00
if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
logger.debug('Video sent by update is not valid.', { videoObject })
return undefined
}
2018-08-22 09:15:35 -05:00
const { video } = await getOrCreateVideoAndAccountAndChannel(videoObject.id)
const channelActor = await getOrCreateVideoChannel(videoObject)
2018-01-10 10:18:12 -06:00
2018-08-22 09:15:35 -05:00
return updateVideoFromAP(video, videoObject, actor, channelActor, activity.to)
2017-11-10 07:34:45 -06:00
}
2018-01-03 09:38:50 -06:00
2018-06-13 07:27:40 -05:00
async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
const actorAttributesToUpdate = activity.object as ActivityPubActor
2018-01-03 09:38:50 -06:00
logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
let accountOrChannelInstance: AccountModel | VideoChannelModel
2018-01-03 09:38:50 -06:00
let actorFieldsSave: object
let accountOrChannelFieldsSave: object
2018-01-03 09:38:50 -06:00
// Fetch icon?
const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
2018-01-03 09:38:50 -06:00
try {
await sequelizeTypescript.transaction(async t => {
2018-01-04 07:04:02 -06:00
actorFieldsSave = actor.toJSON()
2018-01-03 09:38:50 -06:00
if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
else accountOrChannelInstance = actor.Account
accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
await updateActorInstance(actor, actorAttributesToUpdate)
2018-01-03 09:38:50 -06:00
2018-01-04 07:04:02 -06:00
if (avatarName !== undefined) {
await updateActorAvatarInstance(actor, avatarName, t)
2018-01-03 09:38:50 -06:00
}
await actor.save({ transaction: t })
accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
await accountOrChannelInstance.save({ transaction: t })
2018-01-03 09:38:50 -06:00
})
logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
2018-01-03 09:38:50 -06:00
} catch (err) {
2018-01-04 07:04:02 -06:00
if (actor !== undefined && actorFieldsSave !== undefined) {
resetSequelizeInstance(actor, actorFieldsSave)
2018-01-03 09:38:50 -06:00
}
if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
2018-01-03 09:38:50 -06:00
}
// This is just a debug because we will retry the insert
2018-03-26 08:54:13 -05:00
logger.debug('Cannot update the remote account.', { err })
2018-01-03 09:38:50 -06:00
throw err
}
}