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

64 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-11-23 13:19:55 +00:00
import { Transaction } from 'sequelize'
2017-12-12 16:53:50 +00:00
import { ActivityAudience, ActivityLike } from '../../../../shared/models/activitypub'
import { AccountModel } from '../../../models/account/account'
import { VideoModel } from '../../../models/video/video'
2017-11-23 13:19:55 +00:00
import { getVideoLikeActivityPubUrl } from '../url'
import {
broadcastToFollowers,
getAccountsInvolvedInVideo,
2017-11-23 13:19:55 +00:00
getAudience,
getOriginVideoAudience,
getObjectFollowersAudience,
2017-11-23 13:19:55 +00:00
unicastTo
} from './misc'
2017-12-12 16:53:50 +00:00
async function sendLikeToOrigin (byAccount: AccountModel, video: VideoModel, t: Transaction) {
2017-11-23 13:19:55 +00:00
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)
2017-11-23 13:19:55 +00:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
2017-12-12 16:53:50 +00:00
async function sendLikeToVideoFollowers (byAccount: AccountModel, video: VideoModel, t: Transaction) {
2017-11-23 13:19:55 +00:00
const url = getVideoLikeActivityPubUrl(byAccount, video)
const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
const data = await likeActivityData(url, byAccount, video, t, audience)
2017-11-23 13:19:55 +00:00
const followersException = [ byAccount ]
return broadcastToFollowers(data, byAccount, accountsInvolvedInVideo, t, followersException)
2017-11-23 13:19:55 +00:00
}
async function likeActivityData (
url: string,
2017-12-12 16:53:50 +00:00
byAccount: AccountModel,
video: VideoModel,
t: Transaction,
audience?: ActivityAudience
2017-12-12 16:53:50 +00:00
): Promise<ActivityLike> {
2017-11-23 13:19:55 +00:00
if (!audience) {
audience = await getAudience(byAccount, t)
2017-11-23 13:19:55 +00:00
}
2017-12-12 16:53:50 +00:00
return {
2017-11-23 13:19:55 +00:00
type: 'Like',
id: url,
actor: byAccount.url,
to: audience.to,
cc: audience.cc,
object: video.url
}
}
// ---------------------------------------------------------------------------
export {
sendLikeToOrigin,
sendLikeToVideoFollowers,
likeActivityData
}