2021-05-11 05:15:29 -04:00
|
|
|
import { isRedundancyAccepted } from '@server/lib/redundancy'
|
2020-09-17 07:59:02 -04:00
|
|
|
import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub'
|
2018-01-03 10:38:50 -05:00
|
|
|
import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
|
2021-05-11 05:15:29 -04: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'
|
2021-06-03 10:02:29 -04:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2020-05-07 08:58:24 -04:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2021-05-11 05:15:29 -04:00
|
|
|
import { ActorModel } from '../../../models/actor/actor'
|
|
|
|
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
|
2021-06-03 10:02:29 -04:00
|
|
|
import { MActorFull, MActorSignature } from '../../../types/models'
|
|
|
|
import { APActorUpdater } from '../actors/updater'
|
2018-10-02 08:39:35 -04:00
|
|
|
import { createOrUpdateCacheFile } from '../cache-file'
|
2021-06-03 08:30:09 -04:00
|
|
|
import { createOrUpdateVideoPlaylist } from '../playlists'
|
2022-03-18 06:17:35 -04:00
|
|
|
import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
|
2021-06-02 09:47:05 -04:00
|
|
|
import { APVideoUpdater, getOrCreateAPVideo } from '../videos'
|
2019-08-02 04:53:36 -04:00
|
|
|
|
|
|
|
async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
|
|
|
|
const { activity, byActor } = options
|
2017-11-10 08:34:45 -05:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
const objectType = activity.object.type
|
2017-11-09 11:51:58 -05:00
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
if (objectType === 'Video') {
|
2021-06-02 05:54:29 -04:00
|
|
|
return retryTransactionWrapper(processUpdateVideo, activity)
|
2018-09-11 10:27:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
|
2018-09-19 08:44:20 -04:00
|
|
|
// We need more attributes
|
|
|
|
const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
|
|
|
|
return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
2017-11-10 08:34:45 -05:00
|
|
|
|
2018-09-11 10:27:07 -04:00
|
|
|
if (objectType === 'CacheFile') {
|
2018-09-19 08:44:20 -04:00
|
|
|
// We need more attributes
|
|
|
|
const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
|
|
|
|
return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
|
2018-09-11 10:27:07 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 04:55:40 -05:00
|
|
|
if (objectType === 'Playlist') {
|
|
|
|
return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
|
|
|
|
}
|
|
|
|
|
2018-06-13 04:06:50 -04:00
|
|
|
return undefined
|
2017-11-09 11:51:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processUpdateActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2021-06-02 05:54:29 -04:00
|
|
|
async function processUpdateVideo (activity: ActivityUpdate) {
|
2020-09-17 07:59:02 -04:00
|
|
|
const videoObject = activity.object as VideoObject
|
2017-12-14 11:38:41 -05:00
|
|
|
|
2018-06-13 04:06:50 -04:00
|
|
|
if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
|
|
|
|
logger.debug('Video sent by update is not valid.', { videoObject })
|
|
|
|
return undefined
|
|
|
|
}
|
2018-06-12 14:04:58 -04:00
|
|
|
|
2021-06-02 09:47:05 -04:00
|
|
|
const { video, created } = await getOrCreateAPVideo({
|
2021-02-26 05:50:18 -05:00
|
|
|
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 05:54:29 -04:00
|
|
|
const updater = new APVideoUpdater(videoObject, video)
|
|
|
|
return updater.update(activity.to)
|
2018-09-11 10:27:07 -04:00
|
|
|
}
|
|
|
|
|
2019-08-15 05:53:26 -04:00
|
|
|
async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
|
2020-04-07 09:27:41 -04:00
|
|
|
if (await isRedundancyAccepted(activity, byActor) !== true) return
|
|
|
|
|
2018-09-11 10:27:07 -04:00
|
|
|
const cacheFileObject = activity.object as CacheFileObject
|
|
|
|
|
2018-09-24 07:07:33 -04:00
|
|
|
if (!isCacheFileObjectValid(cacheFileObject)) {
|
|
|
|
logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
|
2018-09-11 10:27:07 -04:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2021-06-02 09:47:05 -04:00
|
|
|
const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })
|
2018-09-24 07:07:33 -04:00
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2018-10-02 08:39:35 -04:00
|
|
|
await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
|
2018-09-24 07:07:33 -04:00
|
|
|
})
|
2018-09-11 10:27:07 -04:00
|
|
|
|
2018-09-24 07:07:33 -04:00
|
|
|
if (video.isOwned()) {
|
|
|
|
// Don't resend the activity to the sender
|
|
|
|
const exceptions = [ byActor ]
|
|
|
|
|
|
|
|
await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
|
|
|
|
}
|
2017-11-10 08:34:45 -05:00
|
|
|
}
|
2018-01-03 10:38:50 -05:00
|
|
|
|
2021-06-03 10:02:29 -04:00
|
|
|
async function processUpdateActor (actor: MActorFull, activity: ActivityUpdate) {
|
|
|
|
const actorObject = activity.object as ActivityPubActor
|
2018-01-03 10:38:50 -05:00
|
|
|
|
2021-06-03 10:02:29 -04:00
|
|
|
logger.debug('Updating remote account "%s".', actorObject.url)
|
2018-01-03 10:38:50 -05:00
|
|
|
|
2021-06-03 10:02:29 -04:00
|
|
|
const updater = new APActorUpdater(actorObject, actor)
|
|
|
|
return updater.update()
|
2018-01-03 10:38:50 -05:00
|
|
|
}
|
2019-02-26 04:55:40 -05:00
|
|
|
|
2019-08-15 05:53:26 -04:00
|
|
|
async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
|
2019-02-26 04:55:40 -05: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)
|
|
|
|
|
2021-06-17 10:02:38 -04:00
|
|
|
await createOrUpdateVideoPlaylist(playlistObject, activity.to)
|
2019-02-26 04:55:40 -05:00
|
|
|
}
|