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

123 lines
4.4 KiB
TypeScript
Raw Normal View History

2017-11-20 02:43:39 -06:00
import { Transaction } from 'sequelize'
import {
ActivityAudience,
ActivityCreate,
ActivityFollow,
ActivityLike,
ActivityUndo
2017-12-12 10:53:50 -06:00
} from '../../../../shared/models/activitypub'
2017-12-14 10:38:41 -06:00
import { ActorModel } from '../../../models/activitypub/actor'
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
2017-12-12 10:53:50 -06:00
import { VideoModel } from '../../../models/video/video'
2017-12-14 10:38:41 -06:00
import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
2017-11-28 01:45:03 -06:00
import {
2017-12-19 03:34:56 -06:00
audiencify,
2017-11-28 01:45:03 -06:00
broadcastToFollowers,
2017-12-14 10:38:41 -06:00
getActorsInvolvedInVideo,
2017-11-28 01:45:03 -06:00
getAudience,
getObjectFollowersAudience,
getOriginVideoAudience,
unicastTo
} from './misc'
2017-11-23 07:37:00 -06:00
import { createActivityData, createDislikeActivityData } from './send-create'
2017-11-20 02:43:39 -06:00
import { followActivityData } from './send-follow'
2017-11-23 07:19:55 -06:00
import { likeActivityData } from './send-like'
2017-11-20 02:43:39 -06:00
2017-12-14 10:38:41 -06:00
async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
const me = actorFollow.ActorFollower
const following = actorFollow.ActorFollowing
2017-11-20 02:43:39 -06:00
2017-12-14 10:38:41 -06:00
const followUrl = getActorFollowActivityPubUrl(actorFollow)
2017-11-20 02:43:39 -06:00
const undoUrl = getUndoActivityPubUrl(followUrl)
const object = followActivityData(followUrl, me, following)
const data = await undoActivityData(undoUrl, me, object, t)
2017-11-20 02:43:39 -06:00
return unicastTo(data, me, following.inboxUrl, t)
}
2017-12-14 10:38:41 -06:00
async function sendUndoLikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
2017-11-23 07:19:55 -06:00
const undoUrl = getUndoActivityPubUrl(likeUrl)
2017-12-14 10:38:41 -06:00
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const object = await likeActivityData(likeUrl, byActor, video, t)
const data = await undoActivityData(undoUrl, byActor, object, t, audience)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
2017-11-23 07:19:55 -06:00
}
2017-12-14 10:38:41 -06:00
async function sendUndoLikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
2017-11-23 07:19:55 -06:00
const undoUrl = getUndoActivityPubUrl(likeUrl)
2017-12-14 10:38:41 -06:00
const toActorsFollowers = await getActorsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(toActorsFollowers)
const object = await likeActivityData(likeUrl, byActor, video, t)
const data = await undoActivityData(undoUrl, byActor, object, t, audience)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
const followersException = [ byActor ]
return broadcastToFollowers(data, byActor, toActorsFollowers, t, followersException)
2017-11-23 07:19:55 -06:00
}
2017-12-14 10:38:41 -06:00
async function sendUndoDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
2017-11-23 07:19:55 -06:00
const undoUrl = getUndoActivityPubUrl(dislikeUrl)
2017-12-14 10:38:41 -06:00
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const dislikeActivity = createDislikeActivityData(byActor, video)
const object = await createActivityData(undoUrl, byActor, dislikeActivity, t)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
const data = await undoActivityData(undoUrl, byActor, object, t, audience)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl, t)
2017-11-23 07:19:55 -06:00
}
2017-12-14 10:38:41 -06:00
async function sendUndoDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
2017-11-23 07:19:55 -06:00
const undoUrl = getUndoActivityPubUrl(dislikeUrl)
2017-12-14 10:38:41 -06:00
const dislikeActivity = createDislikeActivityData(byActor, video)
const object = await createActivityData(undoUrl, byActor, dislikeActivity, t)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
const data = await undoActivityData(undoUrl, byActor, object, t)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
const toActorsFollowers = await getActorsInvolvedInVideo(video, t)
2017-11-23 07:19:55 -06:00
2017-12-14 10:38:41 -06:00
const followersException = [ byActor ]
return broadcastToFollowers(data, byActor, toActorsFollowers, t, followersException)
2017-11-23 07:19:55 -06:00
}
2017-11-20 02:43:39 -06:00
// ---------------------------------------------------------------------------
export {
2017-11-23 07:19:55 -06:00
sendUndoFollow,
sendUndoLikeToOrigin,
sendUndoLikeToVideoFollowers,
sendUndoDislikeToOrigin,
sendUndoDislikeToVideoFollowers
2017-11-20 02:43:39 -06:00
}
// ---------------------------------------------------------------------------
async function undoActivityData (
url: string,
2017-12-14 10:38:41 -06:00
byActor: ActorModel,
object: ActivityFollow | ActivityLike | ActivityCreate,
t: Transaction,
audience?: ActivityAudience
2017-12-12 10:53:50 -06:00
): Promise<ActivityUndo> {
if (!audience) {
2017-12-14 10:38:41 -06:00
audience = await getAudience(byActor, t)
}
2017-12-19 03:34:56 -06:00
return audiencify({
2017-11-20 02:43:39 -06:00
type: 'Undo',
id: url,
2017-12-14 10:38:41 -06:00
actor: byActor.url,
2017-11-20 02:43:39 -06:00
object
2017-12-19 03:34:56 -06:00
}, audience)
2017-11-20 02:43:39 -06:00
}