aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-like.ts
blob: 78ed1aaf284774a92f4ac32e8ec951928f8a7d38 (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
import { Transaction } from 'sequelize'
import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub'
import { ActorModel } from '../../../models/activitypub/actor'
import { VideoModel } from '../../../models/video/video'
import { getVideoLikeActivityPubUrl } from '../url'
import {
  audiencify,
  broadcastToFollowers,
  getActorsInvolvedInVideo,
  getAudience,
  getObjectFollowersAudience,
  getOriginVideoAudience,
  unicastTo
} from './misc'

async function sendLikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
  const url = getVideoLikeActivityPubUrl(byActor, video)

  const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
  const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
  const data = await likeActivityData(url, byActor, video, t, audience)

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

async function sendLikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
  const url = getVideoLikeActivityPubUrl(byActor, video)

  const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
  const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
  const data = await likeActivityData(url, byActor, video, t, audience)

  const followersException = [ byActor ]
  return broadcastToFollowers(data, byActor, accountsInvolvedInVideo, t, followersException)
}

async function likeActivityData (
  url: string,
  byActor: ActorModel,
  video: VideoModel,
  t: Transaction,
  audience?: ActivityAudience
): Promise<ActivityLike> {
  if (!audience) {
    audience = await getAudience(byActor, t)
  }

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

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

export {
  sendLikeToOrigin,
  sendLikeToVideoFollowers,
  likeActivityData
}