aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/send/send-like.ts
blob: a5408ac6a2acbcd4975fb505b1da78eb49b0112f (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
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 { broadcastToFollowers, unicastTo } from './utils'
import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getVideoAudience } from '../audience'
import { logger } from '../../../helpers/logger'

async function sendLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
  logger.info('Creating job to like %s.', video.url)

  const url = getVideoLikeActivityPubUrl(byActor, video)

  const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)

  // Send to origin
  if (video.isOwned() === false) {
    const audience = getVideoAudience(video, accountsInvolvedInVideo)
    const data = buildLikeActivity(url, byActor, video, audience)

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

  // Send to followers
  const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
  const activity = buildLikeActivity(url, byActor, video, audience)

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

function buildLikeActivity (url: string, byActor: ActorModel, video: VideoModel, audience?: ActivityAudience): ActivityLike {
  if (!audience) audience = getAudience(byActor)

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

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

export {
  sendLike,
  buildLikeActivity
}