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

184 lines
6.5 KiB
TypeScript
Raw Normal View History

import { VideoModel } from '@server/models/video/video'
import {
ActivityAnnounce,
ActivityCreate,
ActivityDislike,
ActivityFollow,
ActivityLike,
ActivityUndo,
ActivityUndoObject,
CacheFileObject
} from '../../../../shared/models/activitypub'
2017-12-28 04:16:08 -06:00
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
2020-05-07 07:58:24 -05:00
import { sequelizeTypescript } from '../../../initializers/database'
2017-12-12 10:53:50 -06:00
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
2021-05-11 04:15:29 -05:00
import { ActorModel } from '../../../models/actor/actor'
import { ActorFollowModel } from '../../../models/actor/actor-follow'
2018-09-11 09:27:07 -05:00
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
import { VideoShareModel } from '../../../models/video/video-share'
2020-06-18 03:45:25 -05:00
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
import { MActorSignature } from '../../../types/models'
import { fetchAPObject } from '../activity'
import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos'
2017-11-20 02:43:39 -06:00
async function processUndoActivity (options: APProcessorOptions<ActivityUndo<ActivityUndoObject>>) {
2019-08-02 03:53:36 -05:00
const { activity, byActor } = options
2017-11-20 02:43:39 -06:00
const activityToUndo = activity.object
2017-11-23 07:19:55 -06:00
if (activityToUndo.type === 'Like') {
2018-09-19 08:47:55 -05:00
return retryTransactionWrapper(processUndoLike, byActor, activity)
2018-09-11 09:27:07 -05:00
}
if (activityToUndo.type === 'Create') {
const objectToUndo = await fetchAPObject<CacheFileObject>(activityToUndo.object)
if (objectToUndo.type === 'CacheFile') {
return retryTransactionWrapper(processUndoCacheFile, byActor, activity, objectToUndo)
2018-09-11 09:27:07 -05:00
}
}
if (activityToUndo.type === 'Dislike') {
return retryTransactionWrapper(processUndoDislike, byActor, activity)
}
2018-09-11 09:27:07 -05:00
if (activityToUndo.type === 'Follow') {
return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
2018-09-11 09:27:07 -05:00
}
if (activityToUndo.type === 'Announce') {
return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
2017-11-20 02:43:39 -06:00
}
logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })
return undefined
}
// ---------------------------------------------------------------------------
export {
processUndoActivity
}
// ---------------------------------------------------------------------------
2017-11-23 07:19:55 -06:00
async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo<ActivityLike>) {
const likeActivity = activity.object
const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: likeActivity.object })
// We don't care about likes of remote videos
if (!onlyVideo.isOwned()) return
2018-01-10 10:18:12 -06:00
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
2018-09-19 08:47:55 -05:00
if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
2017-11-23 07:19:55 -06:00
2022-06-28 07:57:51 -05:00
const video = await VideoModel.loadFull(onlyVideo.id, t)
2019-08-01 07:19:18 -05:00
const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
2022-07-13 04:07:01 -05:00
if (!rate || rate.type !== 'like') {
logger.warn('Unknown like by account %d for video %d.', byActor.Account.id, video.id)
return
}
2017-11-23 07:19:55 -06:00
await rate.destroy({ transaction: t })
await video.decrement('likes', { transaction: t })
2017-11-23 07:19:55 -06:00
video.likes--
await federateVideoIfNeeded(video, false, t)
2017-11-23 07:19:55 -06:00
})
}
async function processUndoDislike (byActor: MActorSignature, activity: ActivityUndo<ActivityDislike>) {
const dislikeActivity = activity.object
const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: dislikeActivity.object })
// We don't care about likes of remote videos
if (!onlyVideo.isOwned()) return
2018-01-10 10:18:12 -06:00
2017-12-12 10:53:50 -06:00
return sequelizeTypescript.transaction(async t => {
2018-09-19 08:47:55 -05:00
if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)
2017-11-23 07:19:55 -06:00
2022-06-28 07:57:51 -05:00
const video = await VideoModel.loadFull(onlyVideo.id, t)
const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislikeActivity.id, t)
2022-07-13 04:07:01 -05:00
if (!rate || rate.type !== 'dislike') {
logger.warn(`Unknown dislike by account %d for video %d.`, byActor.Account.id, video.id)
return
}
2017-11-23 07:19:55 -06:00
await rate.destroy({ transaction: t })
await video.decrement('dislikes', { transaction: t })
video.dislikes--
2017-11-23 07:19:55 -06:00
await federateVideoIfNeeded(video, false, t)
2017-11-23 07:19:55 -06:00
})
}
// ---------------------------------------------------------------------------
async function processUndoCacheFile (
byActor: MActorSignature,
activity: ActivityUndo<ActivityCreate<CacheFileObject>>,
cacheFileObject: CacheFileObject
) {
2021-06-02 08:47:05 -05:00
const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })
2018-09-11 09:27:07 -05:00
return sequelizeTypescript.transaction(async t => {
2021-06-15 02:17:19 -05:00
const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
2019-04-24 03:28:57 -05:00
if (!cacheFile) {
logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
return
}
2018-09-11 09:27:07 -05:00
2018-09-19 08:47:55 -05:00
if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')
2021-06-15 02:17:19 -05:00
await cacheFile.destroy({ transaction: 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, t, exceptions, video)
}
})
}
2019-08-15 04:53:26 -05:00
function processUndoAnnounce (byActor: MActorSignature, announceActivity: ActivityAnnounce) {
2018-05-11 08:10:13 -05:00
return sequelizeTypescript.transaction(async t => {
const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
2022-07-13 04:07:01 -05:00
if (!share) {
logger.warn('Unknown video share %d', announceActivity.id)
return
}
2018-09-04 03:22:10 -05:00
if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)
2018-05-11 08:10:13 -05:00
await share.destroy({ transaction: t })
if (share.Video.isOwned()) {
// Don't resend the activity to the sender
2018-09-04 03:22:10 -05:00
const exceptions = [ byActor ]
await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
}
2018-05-11 08:10:13 -05:00
})
}
// ---------------------------------------------------------------------------
function processUndoFollow (follower: MActorSignature, followActivity: ActivityFollow) {
return sequelizeTypescript.transaction(async t => {
const following = await ActorModel.loadByUrlAndPopulateAccountAndChannel(followActivity.object, t)
const actorFollow = await ActorFollowModel.loadByActorAndTarget(follower.id, following.id, t)
2022-07-13 04:07:01 -05:00
if (!actorFollow) {
logger.warn('Unknown actor follow %d -> %d.', follower.id, following.id)
return
}
await actorFollow.destroy({ transaction: t })
return undefined
})
}