aboutsummaryrefslogblamecommitdiffhomepage
path: root/server/lib/activitypub/process/process-undo.ts
blob: 257eb6c2b5e722d167d39b573cada405c8239f6a (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                       
                                                                                                                                     
                                                                             

                                                                         
                                                                    
                                                                                  

                                                                     
                                                                                  
                                                                   

                                                                               

                                                                       
 

                                                                                

                                        
                                       
                                                                      


                                         
                                                     
                                                                             


     



                                                                         
                                         
                                                                              


                                           
                                                                                













                                                                                                                          
 
                                                                                   

                                                      


                                                                                             
 
                                                     
                                                                           
 
                                                                                          

                                                                                                                              

                                          
                                                      
 

                                                


    
                                                                                      


                                                    
 


                                                                                        
 
                                                     
                                                                           
 
                                                                                          

                                                                                                                                    

                                          
                                                         
                    
 
                                                


    

                                                                              
                                                                                        

                                                                   
                                                                                     

                                                     
                                                                                 



                                                                             
 

                                                                                                                             
                                               









                                                                       
                                                                                             

                                                                         


                                                                                                      


                                           

                                                
                                    


                                                                                     

    














                                                                                                      
import { VideoModel } from '@server/models/video/video'
import { ActivityAnnounce, ActivityFollow, ActivityLike, ActivityUndo, CacheFileObject } from '../../../../shared/models/activitypub'
import { DislikeObject } from '../../../../shared/models/activitypub/objects'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
import { sequelizeTypescript } from '../../../initializers/database'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
import { ActorModel } from '../../../models/actor/actor'
import { ActorFollowModel } from '../../../models/actor/actor-follow'
import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
import { VideoShareModel } from '../../../models/video/video-share'
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
import { MActorSignature } from '../../../types/models'
import { forwardVideoRelatedActivity } from '../send/shared/send-utils'
import { federateVideoIfNeeded, getOrCreateAPVideo } from '../videos'

async function processUndoActivity (options: APProcessorOptions<ActivityUndo>) {
  const { activity, byActor } = options
  const activityToUndo = activity.object

  if (activityToUndo.type === 'Like') {
    return retryTransactionWrapper(processUndoLike, byActor, activity)
  }

  if (activityToUndo.type === 'Create') {
    if (activityToUndo.object.type === 'CacheFile') {
      return retryTransactionWrapper(processUndoCacheFile, byActor, activity)
    }
  }

  if (activityToUndo.type === 'Dislike') {
    return retryTransactionWrapper(processUndoDislike, byActor, activity)
  }

  if (activityToUndo.type === 'Follow') {
    return retryTransactionWrapper(processUndoFollow, byActor, activityToUndo)
  }

  if (activityToUndo.type === 'Announce') {
    return retryTransactionWrapper(processUndoAnnounce, byActor, activityToUndo)
  }

  logger.warn('Unknown activity object type %s -> %s when undo activity.', activityToUndo.type, { activity: activity.id })

  return undefined
}

// ---------------------------------------------------------------------------

export {
  processUndoActivity
}

// ---------------------------------------------------------------------------

async function processUndoLike (byActor: MActorSignature, activity: ActivityUndo) {
  const likeActivity = activity.object as ActivityLike

  const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: likeActivity.object })
  // We don't care about likes of remote videos
  if (!onlyVideo.isOwned()) return

  return sequelizeTypescript.transaction(async t => {
    if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)

    const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(onlyVideo.id, t)
    const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, likeActivity.id, t)
    if (!rate || rate.type !== 'like') throw new Error(`Unknown like by account ${byActor.Account.id} for video ${video.id}.`)

    await rate.destroy({ transaction: t })
    await video.decrement('likes', { transaction: t })

    video.likes--
    await federateVideoIfNeeded(video, false, t)
  })
}

async function processUndoDislike (byActor: MActorSignature, activity: ActivityUndo) {
  const dislike = activity.object.type === 'Dislike'
    ? activity.object
    : activity.object.object as DislikeObject

  const { video: onlyVideo } = await getOrCreateAPVideo({ videoObject: dislike.object })
  // We don't care about likes of remote videos
  if (!onlyVideo.isOwned()) return

  return sequelizeTypescript.transaction(async t => {
    if (!byActor.Account) throw new Error('Unknown account ' + byActor.url)

    const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(onlyVideo.id, t)
    const rate = await AccountVideoRateModel.loadByAccountAndVideoOrUrl(byActor.Account.id, video.id, dislike.id, t)
    if (!rate || rate.type !== 'dislike') throw new Error(`Unknown dislike by account ${byActor.Account.id} for video ${video.id}.`)

    await rate.destroy({ transaction: t })
    await video.decrement('dislikes', { transaction: t })
    video.dislikes--

    await federateVideoIfNeeded(video, false, t)
  })
}

// ---------------------------------------------------------------------------

async function processUndoCacheFile (byActor: MActorSignature, activity: ActivityUndo) {
  const cacheFileObject = activity.object.object as CacheFileObject

  const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })

  return sequelizeTypescript.transaction(async t => {
    const cacheFile = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
    if (!cacheFile) {
      logger.debug('Cannot undo unknown video cache %s.', cacheFileObject.id)
      return
    }

    if (cacheFile.actorId !== byActor.id) throw new Error('Cannot delete redundancy ' + cacheFile.url + ' of another actor.')

    await cacheFile.destroy({ transaction: t })

    if (video.isOwned()) {
      // Don't resend the activity to the sender
      const exceptions = [ byActor ]

      await forwardVideoRelatedActivity(activity, t, exceptions, video)
    }
  })
}

function processUndoAnnounce (byActor: MActorSignature, announceActivity: ActivityAnnounce) {
  return sequelizeTypescript.transaction(async t => {
    const share = await VideoShareModel.loadByUrl(announceActivity.id, t)
    if (!share) throw new Error(`Unknown video share ${announceActivity.id}.`)

    if (share.actorId !== byActor.id) throw new Error(`${share.url} is not shared by ${byActor.url}.`)

    await share.destroy({ transaction: t })

    if (share.Video.isOwned()) {
      // Don't resend the activity to the sender
      const exceptions = [ byActor ]

      await forwardVideoRelatedActivity(announceActivity, t, exceptions, share.Video)
    }
  })
}

// ---------------------------------------------------------------------------

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)

    if (!actorFollow) throw new Error(`'Unknown actor follow ${follower.id} -> ${following.id}.`)

    await actorFollow.destroy({ transaction: t })

    return undefined
  })
}