aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-announce.ts
blob: 578fbc630a032c471465e0dd6579ffe66273e1a5 (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
import { Transaction } from 'sequelize'
import { ActivityAnnounce, ActivityAudience, ActivityCreate } from '../../../../shared/models/activitypub'
import { VideoPrivacy } from '../../../../shared/models/videos'
import { ActorModel } from '../../../models/activitypub/actor'
import { VideoModel } from '../../../models/video/video'
import { getAnnounceActivityPubUrl } from '../url'
import {
  broadcastToFollowers,
  getActorsInvolvedInVideo,
  getAudience,
  getObjectFollowersAudience,
  getOriginVideoAudience,
  unicastTo
} from './misc'
import { createActivityData } from './send-create'

async function buildVideoAnnounceToFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
  const url = getAnnounceActivityPubUrl(video.url, byActor)
  const videoObject = video.toActivityPubObject()

  const announcedAudience = await getAudience(byActor, t, video.privacy === VideoPrivacy.PUBLIC)
  const announcedActivity = await createActivityData(url, video.VideoChannel.Account.Actor, videoObject, t, announcedAudience)

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

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

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

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

  const videoObject = video.toActivityPubObject()
  const announcedActivity = await createActivityData(url, video.VideoChannel.Account.Actor, videoObject, t)

  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
  const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
  const data = await createActivityData(url, byActor, announcedActivity, t, audience)

  return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
}

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

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

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

export {
  sendVideoAnnounceToFollowers,
  sendVideoAnnounceToOrigin,
  announceActivityData,
  buildVideoAnnounceToFollowers
}