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

162 lines
6.4 KiB
TypeScript
Raw Normal View History

2021-05-11 04:15:29 -05:00
import { isRedundancyAccepted } from '@server/lib/redundancy'
import { ActorImageType } from '@shared/models'
2020-09-17 06:59:02 -05:00
import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub'
2018-01-03 09:38:50 -06:00
import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
2021-05-11 04:15:29 -05:00
import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
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'
2020-05-07 07:58:24 -05:00
import { sequelizeTypescript } from '../../../initializers/database'
2018-01-03 09:38:50 -06:00
import { AccountModel } from '../../../models/account/account'
2021-05-11 04:15:29 -05:00
import { ActorModel } from '../../../models/actor/actor'
import { VideoChannelModel } from '../../../models/video/video-channel'
2021-05-11 04:15:29 -05:00
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
2021-06-02 04:54:29 -05:00
import { MActorSignature } from '../../../types/models'
2021-04-06 10:01:35 -05:00
import { getImageInfoIfExists, updateActorImageInstance, updateActorInstance } from '../actor'
2018-10-02 07:39:35 -05:00
import { createOrUpdateCacheFile } from '../cache-file'
2019-02-26 03:55:40 -06:00
import { createOrUpdateVideoPlaylist } from '../playlist'
2021-05-11 04:15:29 -05:00
import { forwardVideoRelatedActivity } from '../send/utils'
2021-06-02 04:54:29 -05:00
import { APVideoUpdater, getOrCreateVideoAndAccountAndChannel } from '../videos'
2019-08-02 03:53:36 -05:00
async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
const { activity, byActor } = options
2017-11-10 07:34:45 -06:00
const objectType = activity.object.type
2017-11-09 10:51:58 -06:00
if (objectType === 'Video') {
2021-06-02 04:54:29 -05:00
return retryTransactionWrapper(processUpdateVideo, activity)
2018-09-11 09:27:07 -05: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 10:51:58 -06:00
}
2017-11-10 07:34:45 -06:00
2018-09-11 09:27:07 -05:00
if (objectType === 'CacheFile') {
// We need more attributes
const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
2018-09-11 09:27:07 -05:00
}
2019-02-26 03:55:40 -06:00
if (objectType === 'Playlist') {
return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
}
return undefined
2017-11-09 10:51:58 -06:00
}
// ---------------------------------------------------------------------------
export {
processUpdateActivity
}
// ---------------------------------------------------------------------------
2021-06-02 04:54:29 -05:00
async function processUpdateVideo (activity: ActivityUpdate) {
2020-09-17 06:59:02 -05:00
const videoObject = activity.object as VideoObject
2017-12-14 10:38:41 -06:00
if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
logger.debug('Video sent by update is not valid.', { videoObject })
return undefined
}
2021-02-26 04:50:18 -06:00
const { video, created } = await getOrCreateVideoAndAccountAndChannel({
videoObject: videoObject.id,
allowRefresh: false,
fetchType: 'all'
})
// We did not have this video, it has been created so no need to update
if (created) return
2021-06-02 04:54:29 -05:00
const updater = new APVideoUpdater(videoObject, video)
return updater.update(activity.to)
2018-09-11 09:27:07 -05:00
}
2019-08-15 04:53:26 -05:00
async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
if (await isRedundancyAccepted(activity, byActor) !== true) return
2018-09-11 09:27:07 -05:00
const cacheFileObject = activity.object as CacheFileObject
if (!isCacheFileObjectValid(cacheFileObject)) {
logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
2018-09-11 09:27:07 -05:00
return undefined
}
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
await sequelizeTypescript.transaction(async t => {
2018-10-02 07:39:35 -05:00
await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
})
2018-09-11 09:27:07 -05:00
if (video.isOwned()) {
// Don't resend the activity to the sender
const exceptions = [ byActor ]
await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
}
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
2019-05-31 07:02:26 -05:00
logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
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?
2021-04-06 10:01:35 -05:00
const avatarInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.AVATAR)
const bannerInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.BANNER)
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
2021-04-07 03:36:13 -05:00
await updateActorImageInstance(actor, ActorImageType.AVATAR, avatarInfo, t)
await updateActorImageInstance(actor, ActorImageType.BANNER, bannerInfo, t)
2018-01-03 09:38:50 -06:00
await actor.save({ transaction: t })
2019-04-18 04:28:17 -05:00
accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
accountOrChannelInstance.description = actorAttributesToUpdate.summary
if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
await accountOrChannelInstance.save({ transaction: t })
2018-01-03 09:38:50 -06:00
})
2019-05-31 07:02:26 -05:00
logger.info('Remote account %s updated', actorAttributesToUpdate.url)
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
}
}
2019-02-26 03:55:40 -06:00
2019-08-15 04:53:26 -05:00
async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
2019-02-26 03:55:40 -06:00
const playlistObject = activity.object as PlaylistObject
const byAccount = byActor.Account
if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
}