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

134 lines
5.3 KiB
TypeScript
Raw Normal View History

2018-09-11 16:27:07 +02:00
import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
2018-01-03 16:38:50 +01:00
import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
2018-08-14 15:28:30 +02:00
import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
2017-12-28 11:16:08 +01:00
import { logger } from '../../../helpers/logger'
2017-12-12 17:53:50 +01:00
import { sequelizeTypescript } from '../../../initializers'
2018-01-03 16:38:50 +01:00
import { AccountModel } from '../../../models/account/account'
2017-12-14 17:38:41 +01:00
import { ActorModel } from '../../../models/activitypub/actor'
import { VideoChannelModel } from '../../../models/video/video-channel'
import { fetchAvatarIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor'
import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
2018-09-11 16:27:07 +02:00
import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
import { createCacheFile, updateCacheFile } from '../cache-file'
2017-11-10 14:34:45 +01:00
async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) {
const objectType = activity.object.type
2017-11-09 17:51:58 +01:00
if (objectType === 'Video') {
return retryTransactionWrapper(processUpdateVideo, byActor, activity)
2018-09-11 16:27:07 +02:00
}
if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
// We need more attributes
const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
2017-11-09 17:51:58 +01:00
}
2017-11-10 14:34:45 +01:00
2018-09-11 16:27:07 +02:00
if (objectType === 'CacheFile') {
// We need more attributes
const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
2018-09-11 16:27:07 +02:00
}
return undefined
2017-11-09 17:51:58 +01:00
}
// ---------------------------------------------------------------------------
export {
processUpdateActivity
}
// ---------------------------------------------------------------------------
2018-06-13 14:27:40 +02:00
async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
const videoObject = activity.object as VideoTorrentObject
2017-12-14 17:38:41 +01:00
if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
logger.debug('Video sent by update is not valid.', { videoObject })
return undefined
}
2018-09-19 11:16:23 +02:00
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id })
2018-08-23 17:58:39 +02:00
const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
2018-01-10 17:18:12 +01:00
2018-09-19 11:41:21 +02:00
const updateOptions = {
video,
videoObject,
account: actor.Account,
channel: channelActor.VideoChannel,
updateViews: true,
overrideTo: activity.to
}
return updateVideoFromAP(updateOptions)
2018-09-11 16:27:07 +02:00
}
async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
const cacheFileObject = activity.object as CacheFileObject
if (!isCacheFileObjectValid(cacheFileObject) === false) {
logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
return undefined
}
const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
if (!redundancyModel) {
2018-09-19 11:16:23 +02:00
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.id })
2018-09-11 16:27:07 +02:00
return createCacheFile(cacheFileObject, video, byActor)
}
return updateCacheFile(cacheFileObject, redundancyModel, byActor)
2017-11-10 14:34:45 +01:00
}
2018-01-03 16:38:50 +01:00
2018-06-13 14:27:40 +02:00
async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
const actorAttributesToUpdate = activity.object as ActivityPubActor
2018-01-03 16:38:50 +01:00
logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
let accountOrChannelInstance: AccountModel | VideoChannelModel
2018-01-03 16:38:50 +01:00
let actorFieldsSave: object
let accountOrChannelFieldsSave: object
2018-01-03 16:38:50 +01:00
// Fetch icon?
const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
2018-01-03 16:38:50 +01:00
try {
await sequelizeTypescript.transaction(async t => {
2018-01-04 14:04:02 +01:00
actorFieldsSave = actor.toJSON()
2018-01-03 16:38:50 +01:00
if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
else accountOrChannelInstance = actor.Account
accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
await updateActorInstance(actor, actorAttributesToUpdate)
2018-01-03 16:38:50 +01:00
2018-01-04 14:04:02 +01:00
if (avatarName !== undefined) {
await updateActorAvatarInstance(actor, avatarName, t)
2018-01-03 16:38:50 +01: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 16:38:50 +01:00
})
logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
2018-01-03 16:38:50 +01:00
} catch (err) {
2018-01-04 14:04:02 +01:00
if (actor !== undefined && actorFieldsSave !== undefined) {
resetSequelizeInstance(actor, actorFieldsSave)
2018-01-03 16:38:50 +01:00
}
if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
2018-01-03 16:38:50 +01:00
}
// This is just a debug because we will retry the insert
2018-03-26 15:54:13 +02:00
logger.debug('Cannot update the remote account.', { err })
2018-01-03 16:38:50 +01:00
throw err
}
}