PeerTube/server/lib/activitypub/send/send-like.ts

50 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-11-23 07:19:55 -06:00
import { Transaction } from 'sequelize'
2017-12-12 10:53:50 -06:00
import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub'
2017-12-14 10:38:41 -06:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 10:53:50 -06:00
import { VideoModel } from '../../../models/video/video'
2017-11-23 07:19:55 -06:00
import { getVideoLikeActivityPubUrl } from '../url'
2018-05-25 04:32:36 -05:00
import { broadcastToFollowers, unicastTo } from './utils'
import { audiencify, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience, getVideoAudience } from '../audience'
2017-11-23 07:19:55 -06:00
async function sendLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
2017-12-14 10:38:41 -06:00
const url = getVideoLikeActivityPubUrl(byActor, video)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
2017-11-23 07:19:55 -06:00
// Send to origin
if (video.isOwned() === false) {
2018-05-25 04:32:36 -05:00
const audience = getVideoAudience(video, accountsInvolvedInVideo)
const data = likeActivityData(url, byActor, video, audience)
2017-11-23 07:19:55 -06:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
2017-11-23 07:19:55 -06:00
// Send to followers
const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
const data = likeActivityData(url, byActor, video, audience)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
const followersException = [ byActor ]
return broadcastToFollowers(data, byActor, accountsInvolvedInVideo, t, followersException)
2017-11-23 07:19:55 -06:00
}
function likeActivityData (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
)
2017-11-23 07:19:55 -06:00
}
// ---------------------------------------------------------------------------
export {
sendLike,
2017-11-23 07:19:55 -06:00
likeActivityData
}