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

62 lines
1.7 KiB
TypeScript
Raw Normal View History

2017-11-23 14:19:55 +01:00
import { Transaction } from 'sequelize'
2017-12-12 17:53:50 +01:00
import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub'
2017-12-14 17:38:41 +01:00
import { ActorModel } from '../../../models/activitypub/actor'
2017-12-12 17:53:50 +01:00
import { VideoModel } from '../../../models/video/video'
2017-11-23 14:19:55 +01:00
import { getVideoLikeActivityPubUrl } from '../url'
import {
2017-12-19 10:34:56 +01:00
audiencify,
2017-11-23 14:19:55 +01:00
broadcastToFollowers,
2017-12-14 17:38:41 +01:00
getActorsInvolvedInVideo,
2017-11-23 14:19:55 +01:00
getAudience,
getObjectFollowersAudience,
2017-12-14 17:38:41 +01:00
getOriginVideoAudience,
2017-11-23 14:19:55 +01:00
unicastTo
} from './misc'
async function sendLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
2017-12-14 17:38:41 +01:00
const url = getVideoLikeActivityPubUrl(byActor, video)
2017-11-23 14:19:55 +01:00
2017-12-14 17:38:41 +01:00
const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
2017-11-23 14:19:55 +01:00
// Send to origin
if (video.isOwned() === false) {
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
const data = await likeActivityData(url, byActor, video, t, audience)
2017-11-23 14:19:55 +01:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
2017-11-23 14:19:55 +01:00
// Send to followers
const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
2017-12-14 17:38:41 +01:00
const data = await likeActivityData(url, byActor, video, t, audience)
2017-11-23 14:19:55 +01:00
2017-12-14 17:38:41 +01:00
const followersException = [ byActor ]
return broadcastToFollowers(data, byActor, accountsInvolvedInVideo, t, followersException)
2017-11-23 14:19:55 +01:00
}
async function likeActivityData (
url: string,
2017-12-14 17:38:41 +01:00
byActor: ActorModel,
2017-12-12 17:53:50 +01:00
video: VideoModel,
t: Transaction,
audience?: ActivityAudience
2017-12-12 17:53:50 +01:00
): Promise<ActivityLike> {
2017-11-23 14:19:55 +01:00
if (!audience) {
2017-12-14 17:38:41 +01:00
audience = await getAudience(byActor, t)
2017-11-23 14:19:55 +01:00
}
2017-12-19 10:34:56 +01:00
return audiencify({
2018-03-27 10:26:52 +02:00
type: 'Like' as 'Like',
2017-11-23 14:19:55 +01:00
id: url,
2017-12-14 17:38:41 +01:00
actor: byActor.url,
2017-11-23 14:19:55 +01:00
object: video.url
2017-12-19 10:34:56 +01:00
}, audience)
2017-11-23 14:19:55 +01:00
}
// ---------------------------------------------------------------------------
export {
sendLike,
2017-11-23 14:19:55 +01:00
likeActivityData
}