aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process-announce.ts
blob: 2aa9ad5c745ed7892094fea53aaabddb6a5b7e69 (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
106
107
108
109
110
import { ActivityAdd, ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub/activity'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
import { database as db } from '../../../initializers/index'
import { AccountInstance } from '../../../models/account/account-interface'
import { VideoInstance } from '../../../models/index'
import { VideoChannelInstance } from '../../../models/video/video-channel-interface'
import { getOrCreateAccountAndServer } from '../account'
import { forwardActivity } from '../send/misc'
import { processAddActivity } from './process-add'
import { processCreateActivity } from './process-create'

async function processAnnounceActivity (activity: ActivityAnnounce) {
  const announcedActivity = activity.object
  const accountAnnouncer = await getOrCreateAccountAndServer(activity.actor)

  if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'VideoChannel') {
    return processVideoChannelShare(accountAnnouncer, activity)
  } else if (announcedActivity.type === 'Add' && announcedActivity.object.type === 'Video') {
    return processVideoShare(accountAnnouncer, activity)
  }

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

  return undefined
}

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

export {
  processAnnounceActivity
}

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

function processVideoChannelShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
  const options = {
    arguments: [ accountAnnouncer, activity ],
    errorMessage: 'Cannot share the video channel with many retries.'
  }

  return retryTransactionWrapper(shareVideoChannel, options)
}

async function shareVideoChannel (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
  const announcedActivity = activity.object as ActivityCreate

  return db.sequelize.transaction(async t => {
    // Add share entry
    const videoChannel: VideoChannelInstance = await processCreateActivity(announcedActivity)
    const share = {
      accountId: accountAnnouncer.id,
      videoChannelId: videoChannel.id
    }

    const [ , created ] = await db.VideoChannelShare.findOrCreate({
      where: share,
      defaults: share,
      transaction: t
    })

    if (videoChannel.isOwned() && created === true) {
      // Don't resend the activity to the sender
      const exceptions = [ accountAnnouncer ]
      await forwardActivity(activity, t, exceptions)
    }

    return undefined
  })
}

function processVideoShare (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
  const options = {
    arguments: [ accountAnnouncer, activity ],
    errorMessage: 'Cannot share the video with many retries.'
  }

  return retryTransactionWrapper(shareVideo, options)
}

function shareVideo (accountAnnouncer: AccountInstance, activity: ActivityAnnounce) {
  const announcedActivity = activity.object as ActivityAdd

  return db.sequelize.transaction(async t => {
    // Add share entry
    const video: VideoInstance = await processAddActivity(announcedActivity)

    const share = {
      accountId: accountAnnouncer.id,
      videoId: video.id
    }

    const [ , created ] = await db.VideoShare.findOrCreate({
      where: share,
      defaults: share,
      transaction: t
    })

    if (video.isOwned() && created === true) {
      // Don't resend the activity to the sender
      const exceptions = [ accountAnnouncer ]
      await forwardActivity(activity, t, exceptions)
    }

    return undefined
  })
}