aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-announce.ts
blob: e685323e82affd031aec8d317f2860ed1b88faf0 (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
import { Transaction } from 'sequelize'
import { ActivityAdd } from '../../../../shared/index'
import { ActivityAnnounce, ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
import { AccountModel } from '../../../models/account/account'
import { VideoModel } from '../../../models/video/video'
import { VideoChannelModel } from '../../../models/video/video-channel'
import { getAnnounceActivityPubUrl } from '../url'
import {
  broadcastToFollowers,
  getAccountsInvolvedInVideo,
  getAccountsInvolvedInVideoChannel,
  getAudience,
  getObjectFollowersAudience,
  getOriginVideoAudience,
  getOriginVideoChannelAudience,
  unicastTo
} from './misc'
import { addActivityData } from './send-add'
import { createActivityData } from './send-create'

async function buildVideoAnnounceToFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
  const url = getAnnounceActivityPubUrl(video.url, byAccount)

  const videoChannel = video.VideoChannel
  const announcedActivity = await addActivityData(url, videoChannel.Account, video, videoChannel.url, video.toActivityPubObject(), t)

  const accountsToForwardView = await getAccountsInvolvedInVideo(video, t)
  const audience = getObjectFollowersAudience(accountsToForwardView)
  return announceActivityData(url, byAccount, announcedActivity, t, audience)
}

async function sendVideoAnnounceToFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
  const data = await buildVideoAnnounceToFollowers(byAccount, video, t)

  return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}

async function sendVideoAnnounceToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
  const url = getAnnounceActivityPubUrl(video.url, byAccount)

  const videoChannel = video.VideoChannel
  const announcedActivity = await addActivityData(url, videoChannel.Account, video, videoChannel.url, video.toActivityPubObject(), t)

  const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
  const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
  const data = await createActivityData(url, byAccount, announcedActivity, t, audience)

  return unicastTo(data, byAccount, videoChannel.Account.sharedInboxUrl, t)
}

async function buildVideoChannelAnnounceToFollowers (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
  const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount)
  const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t)

  const accountsToForwardView = await getAccountsInvolvedInVideoChannel(videoChannel, t)
  const audience = getObjectFollowersAudience(accountsToForwardView)
  return announceActivityData(url, byAccount, announcedActivity, t, audience)
}

async function sendVideoChannelAnnounceToFollowers (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
  const data = await buildVideoChannelAnnounceToFollowers(byAccount, videoChannel, t)

  return broadcastToFollowers(data, byAccount, [ byAccount ], t)
}

async function sendVideoChannelAnnounceToOrigin (byAccount: AccountModel, videoChannel: VideoChannelModel, t: Transaction) {
  const url = getAnnounceActivityPubUrl(videoChannel.url, byAccount)
  const announcedActivity = await createActivityData(url, videoChannel.Account, videoChannel.toActivityPubObject(), t)

  const accountsInvolvedInVideo = await getAccountsInvolvedInVideoChannel(videoChannel, t)
  const audience = getOriginVideoChannelAudience(videoChannel, accountsInvolvedInVideo)
  const data = await createActivityData(url, byAccount, announcedActivity, t, audience)

  return unicastTo(data, byAccount, videoChannel.Account.sharedInboxUrl, t)
}

async function announceActivityData (
  url: string,
  byAccount: AccountModel,
  object: ActivityCreate | ActivityAdd,
  t: Transaction,
  audience?: ActivityAudience
): Promise<ActivityAnnounce> {
  if (!audience) {
    audience = await getAudience(byAccount, t)
  }

  return {
    type: 'Announce',
    to: audience.to,
    cc: audience.cc,
    id: url,
    actor: byAccount.url,
    object
  }
}

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

export {
  sendVideoAnnounceToFollowers,
  sendVideoChannelAnnounceToFollowers,
  sendVideoAnnounceToOrigin,
  sendVideoChannelAnnounceToOrigin,
  announceActivityData,
  buildVideoAnnounceToFollowers,
  buildVideoChannelAnnounceToFollowers
}