From d64dc458999afdc0bfd5f662a583bd1a0f6eebb3 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Mon, 29 Oct 2018 20:32:42 +0900 Subject: User blocking (Following part) (#3035) * block wip * UndoBlock * UnBlock * wip * follow * UI * fix --- src/remote/activitypub/kernel/block/index.ts | 34 ++++++++++++++++++++++++++++ src/remote/activitypub/kernel/index.ts | 5 ++++ src/remote/activitypub/kernel/undo/block.ts | 34 ++++++++++++++++++++++++++++ src/remote/activitypub/kernel/undo/index.ts | 6 ++++- src/remote/activitypub/renderer/block.ts | 8 +++++++ src/remote/activitypub/type.ts | 7 +++++- 6 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/remote/activitypub/kernel/block/index.ts create mode 100644 src/remote/activitypub/kernel/undo/block.ts create mode 100644 src/remote/activitypub/renderer/block.ts (limited to 'src/remote/activitypub') diff --git a/src/remote/activitypub/kernel/block/index.ts b/src/remote/activitypub/kernel/block/index.ts new file mode 100644 index 0000000000..dec591accf --- /dev/null +++ b/src/remote/activitypub/kernel/block/index.ts @@ -0,0 +1,34 @@ +import * as mongo from 'mongodb'; +import User, { IRemoteUser } from '../../../../models/user'; +import config from '../../../../config'; +import * as debug from 'debug'; +import { IBlock } from '../../type'; +import block from '../../../../services/blocking/create'; + +const log = debug('misskey:activitypub'); + +export default async (actor: IRemoteUser, activity: IBlock): Promise => { + const id = typeof activity.object == 'string' ? activity.object : activity.object.id; + + const uri = activity.id || activity; + + log(`Block: ${uri}`); + + if (!id.startsWith(config.url + '/')) { + return null; + } + + const blockee = await User.findOne({ + _id: new mongo.ObjectID(id.split('/').pop()) + }); + + if (blockee === null) { + throw new Error('blockee not found'); + } + + if (blockee.host != null) { + throw new Error('ブロックしようとしているユーザーはローカルユーザーではありません'); + } + + block(actor, blockee); +}; diff --git a/src/remote/activitypub/kernel/index.ts b/src/remote/activitypub/kernel/index.ts index 52b0efc730..61bb89f5e9 100644 --- a/src/remote/activitypub/kernel/index.ts +++ b/src/remote/activitypub/kernel/index.ts @@ -10,6 +10,7 @@ import accept from './accept'; import reject from './reject'; import add from './add'; import remove from './remove'; +import block from './block'; const self = async (actor: IRemoteUser, activity: Object): Promise => { switch (activity.type) { @@ -53,6 +54,10 @@ const self = async (actor: IRemoteUser, activity: Object): Promise => { await undo(actor, activity); break; + case 'Block': + await block(actor, activity); + break; + case 'Collection': case 'OrderedCollection': // TODO diff --git a/src/remote/activitypub/kernel/undo/block.ts b/src/remote/activitypub/kernel/undo/block.ts new file mode 100644 index 0000000000..b735f114d0 --- /dev/null +++ b/src/remote/activitypub/kernel/undo/block.ts @@ -0,0 +1,34 @@ +import * as mongo from 'mongodb'; +import User, { IRemoteUser } from '../../../../models/user'; +import config from '../../../../config'; +import * as debug from 'debug'; +import { IBlock } from '../../type'; +import unblock from '../../../../services/blocking/delete'; + +const log = debug('misskey:activitypub'); + +export default async (actor: IRemoteUser, activity: IBlock): Promise => { + const id = typeof activity.object == 'string' ? activity.object : activity.object.id; + + const uri = activity.id || activity; + + log(`UnBlock: ${uri}`); + + if (!id.startsWith(config.url + '/')) { + return null; + } + + const blockee = await User.findOne({ + _id: new mongo.ObjectID(id.split('/').pop()) + }); + + if (blockee === null) { + throw new Error('blockee not found'); + } + + if (blockee.host != null) { + throw new Error('ブロック解除しようとしているユーザーはローカルユーザーではありません'); + } + + unblock(actor, blockee); +}; diff --git a/src/remote/activitypub/kernel/undo/index.ts b/src/remote/activitypub/kernel/undo/index.ts index 5d9535403b..ba56dd6328 100644 --- a/src/remote/activitypub/kernel/undo/index.ts +++ b/src/remote/activitypub/kernel/undo/index.ts @@ -1,8 +1,9 @@ import * as debug from 'debug'; import { IRemoteUser } from '../../../../models/user'; -import { IUndo, IFollow } from '../../type'; +import { IUndo, IFollow, IBlock } from '../../type'; import unfollow from './follow'; +import unblock from './block'; import Resolver from '../../resolver'; const log = debug('misskey:activitypub'); @@ -31,6 +32,9 @@ export default async (actor: IRemoteUser, activity: IUndo): Promise => { case 'Follow': unfollow(actor, object as IFollow); break; + case 'Block': + unblock(actor, object as IBlock); + break; } return null; diff --git a/src/remote/activitypub/renderer/block.ts b/src/remote/activitypub/renderer/block.ts new file mode 100644 index 0000000000..316fc13c05 --- /dev/null +++ b/src/remote/activitypub/renderer/block.ts @@ -0,0 +1,8 @@ +import config from '../../../config'; +import { ILocalUser, IRemoteUser } from "../../../models/user"; + +export default (blocker?: ILocalUser, blockee?: IRemoteUser) => ({ + type: 'Block', + actor: `${config.url}/users/${blocker._id}`, + object: blockee.uri +}); diff --git a/src/remote/activitypub/type.ts b/src/remote/activitypub/type.ts index 5c06ee4ffe..2344035013 100644 --- a/src/remote/activitypub/type.ts +++ b/src/remote/activitypub/type.ts @@ -108,6 +108,10 @@ export interface IAnnounce extends IActivity { type: 'Announce'; } +export interface IBlock extends IActivity { + type: 'Block'; +} + export type Object = ICollection | IOrderedCollection | @@ -120,4 +124,5 @@ export type Object = IAdd | IRemove | ILike | - IAnnounce; + IAnnounce | + IBlock; -- cgit v1.2.3-freya