aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-update.ts
blob: 9baf13a87d8be7eb80ead93002db78914cc8c369 (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
import { Transaction } from 'sequelize'
import { ActivityUpdate } from '../../../../shared/models/activitypub'
import { AccountModel } from '../../../models/account/account'
import { VideoModel } from '../../../models/video/video'
import { VideoChannelModel } from '../../../models/video/video-channel'
import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
import { VideoShareModel } from '../../../models/video/video-share'
import { getUpdateActivityPubUrl } from '../url'
import { broadcastToFollowers, getAudience } from './misc'

async function sendUpdateVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
  const byAccount = videoChannel.Account

  const url = getUpdateActivityPubUrl(videoChannel.url, videoChannel.updatedAt.toISOString())
  const videoChannelObject = videoChannel.toActivityPubObject()
  const data = await updateActivityData(url, byAccount, videoChannelObject, t)

  const accountsInvolved = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t)
  accountsInvolved.push(byAccount)

  return broadcastToFollowers(data, byAccount, accountsInvolved, t)
}

async function sendUpdateVideo (video: VideoModel, t: Transaction) {
  const byAccount = video.VideoChannel.Account

  const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
  const videoObject = video.toActivityPubObject()
  const data = await updateActivityData(url, byAccount, videoObject, t)

  const accountsInvolved = await VideoShareModel.loadAccountsByShare(video.id, t)
  accountsInvolved.push(byAccount)

  return broadcastToFollowers(data, byAccount, accountsInvolved, t)
}

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

export {
  sendUpdateVideoChannel,
  sendUpdateVideo
}

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

async function updateActivityData (url: string, byAccount: AccountModel, object: any, t: Transaction): Promise<ActivityUpdate> {
  const { to, cc } = await getAudience(byAccount, t)
  return {
    type: 'Update',
    id: url,
    actor: byAccount.url,
    to,
    cc,
    object
  }
}