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

135 lines
4.7 KiB
TypeScript
Raw Normal View History

2017-11-20 09:43:39 +01:00
import { Transaction } from 'sequelize'
2018-05-11 15:10:13 +02:00
import {
ActivityAnnounce,
ActivityAudience,
2019-08-15 11:53:26 +02:00
ActivityCreate,
ActivityDislike,
2018-05-11 15:10:13 +02:00
ActivityFollow,
ActivityLike,
ActivityUndo
} from '../../../../shared/models/activitypub'
2017-12-12 17:53:50 +01:00
import { VideoModel } from '../../../models/video/video'
2017-12-14 17:38:41 +01:00
import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
2018-09-14 16:51:35 +02:00
import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
import { audiencify, getAudience } from '../audience'
import { buildCreateActivity } from './send-create'
2018-09-11 16:27:07 +02:00
import { buildFollowActivity } from './send-follow'
import { buildLikeActivity } from './send-like'
import { buildAnnounceWithVideoAudience } from './send-announce'
2018-07-30 17:02:40 +02:00
import { logger } from '../../../helpers/logger'
import { buildDislikeActivity } from './send-dislike'
2019-08-15 11:53:26 +02:00
import {
MActor, MActorAudience,
MActorFollowActors,
MActorLight,
MVideo,
MVideoAccountLight,
MVideoRedundancyVideo,
MVideoShare
2020-06-18 10:45:25 +02:00
} from '../../../types/models'
2019-08-15 11:53:26 +02:00
2020-01-31 16:56:52 +01:00
function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) {
2017-12-14 17:38:41 +01:00
const me = actorFollow.ActorFollower
const following = actorFollow.ActorFollowing
2017-11-20 09:43:39 +01:00
// Same server as ours
if (!following.serverId) return
2018-07-30 17:02:40 +02:00
logger.info('Creating job to send an unfollow request to %s.', following.url)
2019-04-08 14:04:57 +02:00
const followUrl = getActorFollowActivityPubUrl(me, following)
2017-11-20 09:43:39 +01:00
const undoUrl = getUndoActivityPubUrl(followUrl)
2018-09-11 16:27:07 +02:00
const followActivity = buildFollowActivity(followUrl, me, following)
const undoActivity = undoActivityData(undoUrl, me, followActivity)
2017-11-20 09:43:39 +01:00
2019-07-29 11:59:29 +02:00
t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl))
2017-11-20 09:43:39 +01:00
}
2019-08-15 11:53:26 +02:00
async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) {
2018-09-14 16:51:35 +02:00
logger.info('Creating job to undo announce %s.', videoShare.url)
2018-07-30 17:02:40 +02:00
2018-09-14 16:51:35 +02:00
const undoUrl = getUndoActivityPubUrl(videoShare.url)
2017-11-23 14:19:55 +01:00
2018-09-14 16:51:35 +02:00
const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t)
const undoActivity = undoActivityData(undoUrl, byActor, announceActivity)
2017-11-23 14:19:55 +01:00
2018-09-14 16:51:35 +02:00
const followersException = [ byActor ]
return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException)
}
2017-11-23 14:19:55 +01:00
2019-08-15 11:53:26 +02:00
async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
2018-09-14 16:51:35 +02:00
logger.info('Creating job to undo a like of video %s.', video.url)
2017-11-23 14:19:55 +01:00
2018-09-14 16:51:35 +02:00
const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
const likeActivity = buildLikeActivity(likeUrl, byActor, video)
2017-11-23 14:19:55 +01:00
2018-09-14 16:51:35 +02:00
return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t })
2017-11-23 14:19:55 +01:00
}
2019-08-15 11:53:26 +02:00
async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) {
2018-07-30 17:02:40 +02:00
logger.info('Creating job to undo a dislike of video %s.', video.url)
2017-12-14 17:38:41 +01:00
const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
2018-11-14 15:01:28 +01:00
const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
2017-11-23 14:19:55 +01:00
return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
2018-09-11 16:27:07 +02:00
}
2019-08-15 11:53:26 +02:00
async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, t: Transaction) {
2018-09-11 16:27:07 +02:00
logger.info('Creating job to undo cache file %s.', redundancyModel.url)
2019-01-29 08:37:25 +01:00
const videoId = redundancyModel.getVideo().id
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
2018-09-11 16:27:07 +02:00
const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject())
2018-09-14 16:51:35 +02:00
return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t })
2018-05-11 15:10:13 +02:00
}
2017-11-20 09:43:39 +01:00
// ---------------------------------------------------------------------------
export {
2017-11-23 14:19:55 +01:00
sendUndoFollow,
sendUndoLike,
2018-05-11 15:10:13 +02:00
sendUndoDislike,
2018-09-11 16:27:07 +02:00
sendUndoAnnounce,
sendUndoCacheFile
2017-11-20 09:43:39 +01:00
}
// ---------------------------------------------------------------------------
function undoActivityData (
url: string,
2019-08-15 11:53:26 +02:00
byActor: MActorAudience,
object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
audience?: ActivityAudience
): ActivityUndo {
if (!audience) audience = getAudience(byActor)
return audiencify(
{
type: 'Undo' as 'Undo',
id: url,
actor: byActor.url,
object
},
audience
)
2017-11-20 09:43:39 +01:00
}
2018-09-14 16:51:35 +02:00
async function sendUndoVideoRelatedActivity (options: {
2020-01-31 16:56:52 +01:00
byActor: MActor
video: MVideoAccountLight
url: string
activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce
2018-09-14 16:51:35 +02:00
transaction: Transaction
}) {
const activityBuilder = (audience: ActivityAudience) => {
const undoUrl = getUndoActivityPubUrl(options.url)
return undoActivityData(undoUrl, options.byActor, options.activity, audience)
}
return sendVideoRelatedActivity(activityBuilder, options)
}