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

126 lines
4.6 KiB
TypeScript
Raw Normal View History

2017-11-20 09:43:39 +01:00
import { Transaction } from 'sequelize'
import {
ActivityAudience,
ActivityCreate,
ActivityFollow,
ActivityLike,
ActivityUndo
} from '../../../../shared/models/activitypub/activity'
2017-11-20 09:43:39 +01:00
import { AccountInstance } from '../../../models'
import { AccountFollowInstance } from '../../../models/account/account-follow-interface'
2017-11-23 14:37:00 +01:00
import { VideoInstance } from '../../../models/video/video-interface'
import { getAccountFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
2017-11-28 08:45:03 +01:00
import {
broadcastToFollowers,
getAccountsInvolvedInVideo,
getAudience,
getObjectFollowersAudience,
getOriginVideoAudience,
unicastTo
} from './misc'
2017-11-23 14:37:00 +01:00
import { createActivityData, createDislikeActivityData } from './send-create'
2017-11-20 09:43:39 +01:00
import { followActivityData } from './send-follow'
2017-11-23 14:19:55 +01:00
import { likeActivityData } from './send-like'
2017-11-20 09:43:39 +01:00
async function sendUndoFollow (accountFollow: AccountFollowInstance, t: Transaction) {
const me = accountFollow.AccountFollower
const following = accountFollow.AccountFollowing
const followUrl = getAccountFollowActivityPubUrl(accountFollow)
const undoUrl = getUndoActivityPubUrl(followUrl)
const object = followActivityData(followUrl, me, following)
const data = await undoActivityData(undoUrl, me, object, t)
2017-11-20 09:43:39 +01:00
return unicastTo(data, me, following.inboxUrl, t)
}
2017-11-23 14:19:55 +01:00
async function sendUndoLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
const likeUrl = getVideoLikeActivityPubUrl(byAccount, video)
const undoUrl = getUndoActivityPubUrl(likeUrl)
const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
2017-11-28 08:45:03 +01:00
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
const object = await likeActivityData(likeUrl, byAccount, video, t)
const data = await undoActivityData(undoUrl, byAccount, object, t, audience)
2017-11-23 14:19:55 +01:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
async function sendUndoLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
const likeUrl = getVideoLikeActivityPubUrl(byAccount, video)
const undoUrl = getUndoActivityPubUrl(likeUrl)
const toAccountsFollowers = await getAccountsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(toAccountsFollowers)
const object = await likeActivityData(likeUrl, byAccount, video, t)
const data = await undoActivityData(undoUrl, byAccount, object, t, audience)
2017-11-23 14:19:55 +01:00
const followersException = [ byAccount ]
return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException)
2017-11-23 14:19:55 +01:00
}
async function sendUndoDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video)
const undoUrl = getUndoActivityPubUrl(dislikeUrl)
const accountsInvolvedInVideo = await getAccountsInvolvedInVideo(video, t)
2017-11-28 08:45:03 +01:00
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
2017-11-23 14:19:55 +01:00
const dislikeActivity = createDislikeActivityData(byAccount, video)
2017-11-30 14:15:17 +01:00
const object = await createActivityData(undoUrl, byAccount, dislikeActivity, t)
2017-11-23 14:19:55 +01:00
2017-11-30 14:15:17 +01:00
const data = await undoActivityData(undoUrl, byAccount, object, t, audience)
2017-11-23 14:19:55 +01:00
return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
}
async function sendUndoDislikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) {
const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video)
const undoUrl = getUndoActivityPubUrl(dislikeUrl)
const dislikeActivity = createDislikeActivityData(byAccount, video)
const object = await createActivityData(undoUrl, byAccount, dislikeActivity, t)
2017-11-23 14:19:55 +01:00
const data = await undoActivityData(undoUrl, byAccount, object, t)
2017-11-23 14:19:55 +01:00
const toAccountsFollowers = await getAccountsInvolvedInVideo(video, t)
2017-11-23 14:19:55 +01:00
const followersException = [ byAccount ]
return broadcastToFollowers(data, byAccount, toAccountsFollowers, t, followersException)
2017-11-23 14:19:55 +01:00
}
2017-11-20 09:43:39 +01:00
// ---------------------------------------------------------------------------
export {
2017-11-23 14:19:55 +01:00
sendUndoFollow,
sendUndoLikeToOrigin,
sendUndoLikeToVideoFollowers,
sendUndoDislikeToOrigin,
sendUndoDislikeToVideoFollowers
2017-11-20 09:43:39 +01:00
}
// ---------------------------------------------------------------------------
async function undoActivityData (
url: string,
byAccount: AccountInstance,
object: ActivityFollow | ActivityLike | ActivityCreate,
t: Transaction,
audience?: ActivityAudience
) {
if (!audience) {
audience = await getAudience(byAccount, t)
}
2017-11-20 09:43:39 +01:00
const activity: ActivityUndo = {
type: 'Undo',
id: url,
actor: byAccount.url,
to: audience.to,
cc: audience.cc,
2017-11-20 09:43:39 +01:00
object
}
return activity
}