aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process-delete.ts
blob: 0d5756e9c1fc21c624bcbdcc27d190fde51fb7ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import { ActivityDelete } from '../../../shared/models/activitypub/activity'
import { getOrCreateAccount } from '../../helpers/activitypub'
import { retryTransactionWrapper } from '../../helpers/database-utils'
import { logger } from '../../helpers/logger'
import { database as db } from '../../initializers'
import { AccountInstance } from '../../models/account/account-interface'
import { VideoChannelInstance } from '../../models/video/video-channel-interface'
import { VideoInstance } from '../../models/video/video-interface'

async function processDeleteActivity (activity: ActivityDelete) {
  const account = await getOrCreateAccount(activity.actor)

  if (account.url === activity.id) {
    return processDeleteAccount(account)
  }

  {
    let videoObject = await db.Video.loadByUrlAndPopulateAccount(activity.id)
    if (videoObject !== undefined) {
      return processDeleteVideo(account, videoObject)
    }
  }

  {
    let videoChannelObject = await db.VideoChannel.loadByUrl(activity.id)
    if (videoChannelObject !== undefined) {
      return processDeleteVideoChannel(account, videoChannelObject)
    }
  }

  return
}

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

export {
  processDeleteActivity
}

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

async function processDeleteVideo (account: AccountInstance, videoToDelete: VideoInstance) {
  const options = {
    arguments: [ account, videoToDelete ],
    errorMessage: 'Cannot remove the remote video with many retries.'
  }

  await retryTransactionWrapper(deleteRemoteVideo, options)
}

async function deleteRemoteVideo (account: AccountInstance, videoToDelete: VideoInstance) {
  logger.debug('Removing remote video "%s".', videoToDelete.uuid)

  await db.sequelize.transaction(async t => {
    if (videoToDelete.VideoChannel.Account.id !== account.id) {
      throw new Error('Account ' + account.url + ' does not own video channel ' + videoToDelete.VideoChannel.url)
    }

    await videoToDelete.destroy({ transaction: t })
  })

  logger.info('Remote video with uuid %s removed.', videoToDelete.uuid)
}

async function processDeleteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) {
  const options = {
    arguments: [ account, videoChannelToRemove ],
    errorMessage: 'Cannot remove the remote video channel with many retries.'
  }

  await retryTransactionWrapper(deleteRemoteVideoChannel, options)
}

async function deleteRemoteVideoChannel (account: AccountInstance, videoChannelToRemove: VideoChannelInstance) {
  logger.debug('Removing remote video channel "%s".', videoChannelToRemove.uuid)

  await db.sequelize.transaction(async t => {
    if (videoChannelToRemove.Account.id !== account.id) {
      throw new Error('Account ' + account.url + ' does not own video channel ' + videoChannelToRemove.url)
    }

    await videoChannelToRemove.destroy({ transaction: t })
  })

  logger.info('Remote video channel with uuid %s removed.', videoChannelToRemove.uuid)
}

async function processDeleteAccount (accountToRemove: AccountInstance) {
  const options = {
    arguments: [ accountToRemove ],
    errorMessage: 'Cannot remove the remote account with many retries.'
  }

  await retryTransactionWrapper(deleteRemoteAccount, options)
}

async function deleteRemoteAccount (accountToRemove: AccountInstance) {
  logger.debug('Removing remote account "%s".', accountToRemove.uuid)

  await db.sequelize.transaction(async t => {
    await accountToRemove.destroy({ transaction: t })
  })

  logger.info('Remote account with uuid %s removed.', accountToRemove.uuid)
}