aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-like.ts
blob: 0c464b2d3f401c6c185632abb945359e12caf0f1 (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
import { Transaction } from 'sequelize'
import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub/activity'
import { AccountInstance, VideoInstance } from '../../../models'
import { getVideoLikeActivityPubUrl } from '../url'
import {
  broadcastToFollowers,
  getAccountsInvolvedInVideo,
  getAudience,
  getOriginVideoAudience,
  getObjectFollowersAudience,
  unicastTo
} from './misc'

async function sendLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
  const url = getVideoLikeActivityPubUrl(byAccount, video)

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

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

async function sendLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
  const url = getVideoLikeActivityPubUrl(byAccount, video)

  const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
  const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
  const data = await likeActivityData(url, byAccount, video, t, audience)

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

async function likeActivityData (
  url: string,
  byAccount: AccountInstance,
  video: VideoInstance,
  t: Transaction,
  audience?: ActivityAudience
) {
  if (!audience) {
    audience = await getAudience(byAccount, t)
  }

  const activity: ActivityLike = {
    type: 'Like',
    id: url,
    actor: byAccount.url,
    to: audience.to,
    cc: audience.cc,
    object: video.url
  }

  return activity
}

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

export {
  sendLikeToOrigin,
  sendLikeToVideoFollowers,
  likeActivityData
}