From 06a05d5f4784a7cbb27aa1188385b5679845dad8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 16 Aug 2018 15:25:20 +0200 Subject: Add subscriptions endpoints to REST API --- server/lib/activitypub/actor.ts | 2 +- server/lib/activitypub/send/send-accept.ts | 5 +++++ server/lib/activitypub/send/send-create.ts | 2 ++ server/lib/activitypub/send/send-follow.ts | 3 +++ server/lib/activitypub/send/send-undo.ts | 3 +++ server/lib/activitypub/send/utils.ts | 21 ++++++++++++++++++--- 6 files changed, 32 insertions(+), 4 deletions(-) (limited to 'server/lib/activitypub') diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index b67d9f08b..d84b465b2 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -352,7 +352,7 @@ async function refreshActorIfNeeded (actor: ActorModel): Promise { if (!actor.isOutdated()) return actor try { - const actorUrl = await getUrlFromWebfinger(actor.preferredUsername, actor.getHost()) + const actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost()) const result = await fetchRemoteActor(actorUrl) if (result === undefined) { logger.warn('Cannot fetch remote actor in refresh actor.') diff --git a/server/lib/activitypub/send/send-accept.ts b/server/lib/activitypub/send/send-accept.ts index dfee1ec3e..ef679707b 100644 --- a/server/lib/activitypub/send/send-accept.ts +++ b/server/lib/activitypub/send/send-accept.ts @@ -10,6 +10,11 @@ async function sendAccept (actorFollow: ActorFollowModel) { const follower = actorFollow.ActorFollower const me = actorFollow.ActorFollowing + if (!follower.serverId) { // This should never happen + logger.warn('Do not sending accept to local follower.') + return + } + logger.info('Creating job to accept follower %s.', follower.url) const followUrl = getActorFollowActivityPubUrl(actorFollow) diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index f7a8cf0b3..fc76cdd8a 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts @@ -33,6 +33,8 @@ async function sendCreateVideo (video: VideoModel, t: Transaction) { } async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel, t: Transaction) { + if (!video.VideoChannel.Account.Actor.serverId) return // Local + const url = getVideoAbuseActivityPubUrl(videoAbuse) logger.info('Creating job to send video abuse %s.', url) diff --git a/server/lib/activitypub/send/send-follow.ts b/server/lib/activitypub/send/send-follow.ts index 2faffe6e7..46d08c17b 100644 --- a/server/lib/activitypub/send/send-follow.ts +++ b/server/lib/activitypub/send/send-follow.ts @@ -9,6 +9,9 @@ function sendFollow (actorFollow: ActorFollowModel) { const me = actorFollow.ActorFollower const following = actorFollow.ActorFollowing + // Same server as ours + if (!following.serverId) return + logger.info('Creating job to send follow request to %s.', following.url) const url = getActorFollowActivityPubUrl(actorFollow) diff --git a/server/lib/activitypub/send/send-undo.ts b/server/lib/activitypub/send/send-undo.ts index 4e5dd3973..30d0fd98b 100644 --- a/server/lib/activitypub/send/send-undo.ts +++ b/server/lib/activitypub/send/send-undo.ts @@ -24,6 +24,9 @@ async function sendUndoFollow (actorFollow: ActorFollowModel, 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 followUrl = getActorFollowActivityPubUrl(actorFollow) diff --git a/server/lib/activitypub/send/utils.ts b/server/lib/activitypub/send/utils.ts index 0d28444ec..da437292e 100644 --- a/server/lib/activitypub/send/utils.ts +++ b/server/lib/activitypub/send/utils.ts @@ -6,6 +6,7 @@ import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { JobQueue } from '../../job-queue' import { VideoModel } from '../../../models/video/video' import { getActorsInvolvedInVideo } from '../audience' +import { getServerActor } from '../../../helpers/utils' async function forwardVideoRelatedActivity ( activity: Activity, @@ -118,14 +119,28 @@ async function computeFollowerUris (toActorFollower: ActorModel[], actorsExcepti const toActorFollowerIds = toActorFollower.map(a => a.id) const result = await ActorFollowModel.listAcceptedFollowerSharedInboxUrls(toActorFollowerIds, t) - const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl) + const sharedInboxesException = await buildSharedInboxesException(actorsException) + return result.data.filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) } async function computeUris (toActors: ActorModel[], actorsException: ActorModel[] = []) { - const toActorSharedInboxesSet = new Set(toActors.map(a => a.sharedInboxUrl || a.inboxUrl)) + const serverActor = await getServerActor() + const targetUrls = toActors + .filter(a => a.id !== serverActor.id) // Don't send to ourselves + .map(a => a.sharedInboxUrl || a.inboxUrl) + + const toActorSharedInboxesSet = new Set(targetUrls) - const sharedInboxesException = actorsException.map(f => f.sharedInboxUrl || f.inboxUrl) + const sharedInboxesException = await buildSharedInboxesException(actorsException) return Array.from(toActorSharedInboxesSet) .filter(sharedInbox => sharedInboxesException.indexOf(sharedInbox) === -1) } + +async function buildSharedInboxesException (actorsException: ActorModel[]) { + const serverActor = await getServerActor() + + return actorsException + .map(f => f.sharedInboxUrl || f.inboxUrl) + .concat([ serverActor.sharedInboxUrl ]) +} -- cgit v1.2.3