X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fsend%2Fsend-undo.ts;h=352c158fd6be237e7fce9befe1a3147b7d3fcb43;hb=de94ac86a211dec657332d964693857ec235ce40;hp=53fddd0cbfb4b8be0d2f161455b2cc6c363e04f7;hpb=0032ebe94aa83fab761c7de3ceb6210ac4532824;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/send/send-undo.ts b/server/lib/activitypub/send/send-undo.ts index 53fddd0cb..352c158fd 100644 --- a/server/lib/activitypub/send/send-undo.ts +++ b/server/lib/activitypub/send/send-undo.ts @@ -1,100 +1,134 @@ import { Transaction } from 'sequelize' -import { ActivityCreate, ActivityFollow, ActivityLike, ActivityUndo } from '../../../../shared/models/activitypub/activity' -import { AccountInstance } from '../../../models' -import { AccountFollowInstance } from '../../../models/account/account-follow-interface' -import { broadcastToFollowers, getAccountsToForwardVideoAction, unicastTo } from './misc' -import { followActivityData } from './send-follow' -import { getAccountFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url' -import { VideoInstance } from '../../../models/video/video-interface' -import { likeActivityData } from './send-like' -import { createActivityData, createDislikeActivityData } from './send-create' -import { getServerAccount } from '../../../helpers/utils' - -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 videoId = redundancyModel.getVideo().id + const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) + const createActivity = buildCreateActivity(redundancyModel.url, byActor, redundancyModel.toActivityPubObject()) - const accountsToForwardView = await getAccountsToForwardVideoAction(byAccount, video) - const serverAccount = await getServerAccount() - - 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) }