From 7dc06b3d4383321ef85fa9bf2a1bc1d16ecab8c2 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 7 Apr 2018 15:54:11 +0900 Subject: Refactor --- src/remote/activitypub/act/undo.ts | 15 --------------- src/remote/activitypub/act/undo/follow.ts | 25 +++++++++++++++++++++++++ src/remote/activitypub/act/undo/index.ts | 15 +++++++++++++++ 3 files changed, 40 insertions(+), 15 deletions(-) delete mode 100644 src/remote/activitypub/act/undo.ts create mode 100644 src/remote/activitypub/act/undo/follow.ts create mode 100644 src/remote/activitypub/act/undo/index.ts (limited to 'src/remote') diff --git a/src/remote/activitypub/act/undo.ts b/src/remote/activitypub/act/undo.ts deleted file mode 100644 index 9d9f6b0359..0000000000 --- a/src/remote/activitypub/act/undo.ts +++ /dev/null @@ -1,15 +0,0 @@ -import unfollow from './unfollow'; - -export default async (actor, activity): Promise => { - if ('actor' in activity && actor.account.uri !== activity.actor) { - throw new Error('invalid actor'); - } - - switch (activity.object.type) { - case 'Follow': - unfollow(actor, activity.object); - break; - } - - return null; -}; diff --git a/src/remote/activitypub/act/undo/follow.ts b/src/remote/activitypub/act/undo/follow.ts new file mode 100644 index 0000000000..b1c462a3b0 --- /dev/null +++ b/src/remote/activitypub/act/undo/follow.ts @@ -0,0 +1,25 @@ +import parseAcct from '../../../../acct/parse'; +import User from '../../../../models/user'; +import config from '../../../../config'; +import unfollow from '../../../../services/following/delete'; + +export default async (actor, activity): Promise => { + const prefix = config.url + '/@'; + const id = activity.object.id || activity.object; + + if (!id.startsWith(prefix)) { + return null; + } + + const { username, host } = parseAcct(id.slice(prefix.length)); + if (host !== null) { + throw new Error(); + } + + const followee = await User.findOne({ username, host }); + if (followee === null) { + throw new Error(); + } + + await unfollow(actor, followee, activity); +}; diff --git a/src/remote/activitypub/act/undo/index.ts b/src/remote/activitypub/act/undo/index.ts new file mode 100644 index 0000000000..ecd9944b42 --- /dev/null +++ b/src/remote/activitypub/act/undo/index.ts @@ -0,0 +1,15 @@ +import unfollow from './follow'; + +export default async (actor, activity): Promise => { + if ('actor' in activity && actor.account.uri !== activity.actor) { + throw new Error('invalid actor'); + } + + switch (activity.object.type) { + case 'Follow': + unfollow(actor, activity.object); + break; + } + + return null; +}; -- cgit v1.2.3-freya