X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fsend%2Fsend-undo.ts;h=d2b738befc164c53447ea5d8675a6e4cebb3d837;hb=9cfeb3cf989fffccdfe3e575903dc00baab255b2;hp=8f46a051e192f646e64c071ac9b5419a7e3d6695;hpb=d8553faa4939889fa7b7ef7329aa474a81cbbdb9;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/send/send-undo.ts b/server/lib/activitypub/send/send-undo.ts index 8f46a051e..d2b738bef 100644 --- a/server/lib/activitypub/send/send-undo.ts +++ b/server/lib/activitypub/send/send-undo.ts @@ -1,99 +1,139 @@ import { Transaction } from 'sequelize' -import { ActivityCreate, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub/activity' -import { getServerAccount } from '../../../helpers/utils' -import { AccountInstance } from '../../../models' -import { AccountFollowInstance } from '../../../models/account/account-follow-interface' -import { VideoInstance } from '../../../models/video/video-interface' -import { getAccountFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url' -import { broadcastToFollowers, getAccountsToForwardVideoAction, unicastTo } from './misc' -import { createActivityData, createDislikeActivityData } from './send-create' -import { followActivityData } from './send-follow' -import { likeActivityData } from './send-like' - -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 = await followActivityData(followUrl, me, following) - const data = await undoActivityData(undoUrl, me, object) - - return unicastTo(data, me, following.inboxUrl, t) +import { + ActivityAnnounce, + ActivityAudience, + ActivityCreate, + ActivityDislike, + ActivityFollow, + ActivityLike, + ActivityUndo +} from '../../../../shared/models/activitypub' +import { logger } from '../../../helpers/logger' +import { VideoModel } from '../../../models/video/video' +import { + MActor, + MActorAudience, + MActorFollowActors, + MActorLight, + MVideo, + MVideoAccountLight, + MVideoRedundancyVideo, + MVideoShare +} from '../../../types/models' +import { audiencify, getAudience } from '../audience' +import { getUndoActivityPubUrl, getVideoDislikeActivityPubUrlByLocalActor, getVideoLikeActivityPubUrlByLocalActor } from '../url' +import { buildAnnounceWithVideoAudience } from './send-announce' +import { buildCreateActivity } from './send-create' +import { buildDislikeActivity } from './send-dislike' +import { buildFollowActivity } from './send-follow' +import { buildLikeActivity } from './send-like' +import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils' + +function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) { + const me = actorFollow.ActorFollower + const following = actorFollow.ActorFollowing + + // Same server as ours + if (!following.serverId) return + + logger.info('Creating job to send an unfollow request to %s.', following.url) + + const undoUrl = getUndoActivityPubUrl(actorFollow.url) + + const followActivity = buildFollowActivity(actorFollow.url, me, following) + const undoActivity = undoActivityData(undoUrl, me, followActivity) + + t.afterCommit(() => unicastTo(undoActivity, me, following.inboxUrl)) } -async function sendUndoLikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { - const likeUrl = getVideoLikeActivityPubUrl(byAccount, video) - const undoUrl = getUndoActivityPubUrl(likeUrl) +async function sendUndoAnnounce (byActor: MActorLight, videoShare: MVideoShare, video: MVideo, t: Transaction) { + logger.info('Creating job to undo announce %s.', videoShare.url) - const object = await likeActivityData(likeUrl, byAccount, video) - const data = await undoActivityData(undoUrl, byAccount, object) + const undoUrl = getUndoActivityPubUrl(videoShare.url) - return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) -} + const { activity: announceActivity, actorsInvolvedInVideo } = await buildAnnounceWithVideoAudience(byActor, videoShare, video, t) + const undoActivity = undoActivityData(undoUrl, byActor, announceActivity) -async function sendUndoLikeToVideoFollowers (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { - const likeUrl = getVideoLikeActivityPubUrl(byAccount, video) - const undoUrl = getUndoActivityPubUrl(likeUrl) + const followersException = [ byActor ] + return broadcastToFollowers(undoActivity, byActor, actorsInvolvedInVideo, t, followersException) +} - const object = await likeActivityData(likeUrl, byAccount, video) - const data = await undoActivityData(undoUrl, byAccount, object) +async function sendUndoLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { + logger.info('Creating job to undo a like of video %s.', video.url) - const accountsToForwardView = await getAccountsToForwardVideoAction(byAccount, video) - const serverAccount = await getServerAccount() + const likeUrl = getVideoLikeActivityPubUrlByLocalActor(byActor, video) + const likeActivity = buildLikeActivity(likeUrl, byActor, video) - const followersException = [ byAccount ] - return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException) + return sendUndoVideoRelatedActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t }) } -async function sendUndoDislikeToOrigin (byAccount: AccountInstance, video: VideoInstance, t: Transaction) { - const dislikeUrl = getVideoDislikeActivityPubUrl(byAccount, video) - const undoUrl = getUndoActivityPubUrl(dislikeUrl) +async function sendUndoDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { + logger.info('Creating job to undo a dislike of video %s.', video.url) - const dislikeActivity = createDislikeActivityData(byAccount, video) - const object = await createActivityData(undoUrl, byAccount, dislikeActivity) + const dislikeUrl = getVideoDislikeActivityPubUrlByLocalActor(byActor, video) + const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video) - const data = await undoActivityData(undoUrl, byAccount, object) - - return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) + return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: 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) +async function sendUndoCacheFile (byActor: MActor, redundancyModel: MVideoRedundancyVideo, t: Transaction) { + logger.info('Creating job to undo cache file %s.', redundancyModel.url) - const data = await undoActivityData(undoUrl, byAccount, object) + const associatedVideo = redundancyModel.getVideo() + if (!associatedVideo) { + logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url) + return + } - const accountsToForwardView = await getAccountsToForwardVideoAction(byAccount, video) - const serverAccount = await getServerAccount() + const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(associatedVideo.id) + const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject()) - const followersException = [ byAccount ] - return broadcastToFollowers(data, serverAccount, accountsToForwardView, t, followersException) + return sendUndoVideoRelatedActivity({ byActor, video, url: redundancyModel.url, activity: createActivity, transaction: t }) } // --------------------------------------------------------------------------- export { sendUndoFollow, - sendUndoLikeToOrigin, - sendUndoLikeToVideoFollowers, - sendUndoDislikeToOrigin, - sendUndoDislikeToVideoFollowers + sendUndoLike, + sendUndoDislike, + sendUndoAnnounce, + sendUndoCacheFile } // --------------------------------------------------------------------------- -async function undoActivityData (url: string, byAccount: AccountInstance, object: ActivityFollow | ActivityLike | ActivityCreate) { - const activity: ActivityUndo = { - type: 'Undo', - id: url, - actor: byAccount.url, - object +function undoActivityData ( + url: string, + 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 + ) +} + +async function sendUndoVideoRelatedActivity (options: { + byActor: MActor + video: MVideoAccountLight + url: string + activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce + transaction: Transaction +}) { + const activityBuilder = (audience: ActivityAudience) => { + const undoUrl = getUndoActivityPubUrl(options.url) + + return undoActivityData(undoUrl, options.byActor, options.activity, audience) } - return activity + return sendVideoRelatedActivity(activityBuilder, options) }