aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-announce.ts
blob: 6c078b0475531ce34e41b324a467f0b07dd4a91e (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
import { Transaction } from 'sequelize'
import { ActivityAnnounce, ActivityAudience } from '@shared/models'
import { logger } from '../../../helpers/logger'
import { MActorLight, MVideo } from '../../../types/models'
import { MVideoShare } from '../../../types/models/video'
import { audiencify, getAudience } from '../audience'
import { getActorsInvolvedInVideo, getAudienceFromFollowersOf } from './shared'
import { broadcastToFollowers } from './shared/send-utils'

async function buildAnnounceWithVideoAudience (
  byActor: MActorLight,
  videoShare: MVideoShare,
  video: MVideo,
  t: Transaction
) {
  const announcedObject = video.url

  const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
  const audience = getAudienceFromFollowersOf(actorsInvolvedInVideo)

  const activity = buildAnnounceActivity(videoShare.url, byActor, announcedObject, audience)

  return { activity, actorsInvolvedInVideo }
}

async function sendVideoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, transaction: Transaction) {
  const { activity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, transaction)

  logger.info('Creating job to send announce %s.', videoShare.url)

  return broadcastToFollowers({
    data: activity,
    byActor,
    toFollowersOf: actorsInvolvedInVideo,
    transaction,
    actorsException: [ byActor ],
    contextType: 'Announce'
  })
}

function buildAnnounceActivity (url: string, byActor: MActorLight, object: string, audience?: ActivityAudience): ActivityAnnounce {
  if (!audience) audience = getAudience(byActor)

  return audiencify({
    type: 'Announce' as 'Announce',
    id: url,
    actor: byActor.url,
    object
  }, audience)
}

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

export {
  sendVideoAnnounce,
  buildAnnounceActivity,
  buildAnnounceWithVideoAudience
}