From cd0b0012d9da9109b0ea4adf0feb84b6c3b43f86 Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Sun, 1 Nov 2020 12:14:42 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=A1=E3=83=83=E3=82=BB=E3=83=BC=E3=82=B8?= =?UTF-8?q?=20(=E3=83=88=E3=83=BC=E3=82=AF/=E3=83=81=E3=83=A3=E3=83=83?= =?UTF-8?q?=E3=83=88)=20=E5=89=8A=E9=99=A4=E3=81=AE=E9=80=A3=E5=90=88=20(#?= =?UTF-8?q?6789)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/remote/activitypub/db-resolver.ts | 21 ++++++++++++- src/remote/activitypub/kernel/delete/note.ts | 14 +++++++-- .../endpoints/messaging/messages/delete.ts | 11 ++----- src/services/messages/delete.ts | 31 +++++++++++++++++++ 4 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 src/services/messages/delete.ts diff --git a/src/remote/activitypub/db-resolver.ts b/src/remote/activitypub/db-resolver.ts index 6f1cb1e11..cad2212b7 100644 --- a/src/remote/activitypub/db-resolver.ts +++ b/src/remote/activitypub/db-resolver.ts @@ -2,7 +2,8 @@ import config from '../../config'; import { Note } from '../../models/entities/note'; import { User, IRemoteUser } from '../../models/entities/user'; import { UserPublickey } from '../../models/entities/user-publickey'; -import { Notes, Users, UserPublickeys } from '../../models'; +import { MessagingMessage } from '../../models/entities/messaging-message'; +import { Notes, Users, UserPublickeys, MessagingMessages } from '../../models'; import { IObject, getApId } from './type'; import { resolvePerson } from './models/person'; import { ensure } from '../../prelude/ensure'; @@ -33,6 +34,24 @@ export default class DbResolver { return null; } + public async getMessageFromApId(value: string | IObject): Promise { + const parsed = this.parseUri(value); + + if (parsed.id) { + return (await MessagingMessages.findOne({ + id: parsed.id + })) || null; + } + + if (parsed.uri) { + return (await MessagingMessages.findOne({ + uri: parsed.uri + })) || null; + } + + return null; + } + /** * AP Person => Misskey User in DB */ diff --git a/src/remote/activitypub/kernel/delete/note.ts b/src/remote/activitypub/kernel/delete/note.ts index 9d7574c29..8fa2285db 100644 --- a/src/remote/activitypub/kernel/delete/note.ts +++ b/src/remote/activitypub/kernel/delete/note.ts @@ -3,6 +3,7 @@ import deleteNode from '../../../../services/note/delete'; import { apLogger } from '../../logger'; import DbResolver from '../../db-resolver'; import { getApLock } from '../../../../misc/app-lock'; +import { deleteMessage } from '../../../../services/messages/delete'; const logger = apLogger; @@ -16,7 +17,16 @@ export default async function(actor: IRemoteUser, uri: string): Promise const note = await dbResolver.getNoteFromApId(uri); if (note == null) { - return 'note not found'; + const message = await dbResolver.getMessageFromApId(uri); + if (message == null) return 'message not found'; + + if (message.userId !== actor.id) { + return '投稿を削除しようとしているユーザーは投稿の作成者ではありません'; + } + + await deleteMessage(message); + + return 'ok: message deleted'; } if (note.userId !== actor.id) { @@ -24,7 +34,7 @@ export default async function(actor: IRemoteUser, uri: string): Promise } await deleteNode(actor, note); - return 'ok: deleted'; + return 'ok: note deleted'; } finally { unlock(); } diff --git a/src/server/api/endpoints/messaging/messages/delete.ts b/src/server/api/endpoints/messaging/messages/delete.ts index 9f751a062..2fce93e9b 100644 --- a/src/server/api/endpoints/messaging/messages/delete.ts +++ b/src/server/api/endpoints/messaging/messages/delete.ts @@ -1,10 +1,10 @@ import $ from 'cafy'; import { ID } from '../../../../../misc/cafy-id'; import define from '../../../define'; -import { publishMessagingStream, publishGroupMessagingStream } from '../../../../../services/stream'; import * as ms from 'ms'; import { ApiError } from '../../../error'; import { MessagingMessages } from '../../../../../models'; +import { deleteMessage } from '../../../../../services/messages/delete'; export const meta = { desc: { @@ -53,12 +53,5 @@ export default define(meta, async (ps, user) => { throw new ApiError(meta.errors.noSuchMessage); } - await MessagingMessages.delete(message.id); - - if (message.recipientId) { - publishMessagingStream(message.userId, message.recipientId, 'deleted', message.id); - publishMessagingStream(message.recipientId, message.userId, 'deleted', message.id); - } else if (message.groupId) { - publishGroupMessagingStream(message.groupId, 'deleted', message.id); - } + await deleteMessage(message); }); diff --git a/src/services/messages/delete.ts b/src/services/messages/delete.ts new file mode 100644 index 000000000..0efff85f3 --- /dev/null +++ b/src/services/messages/delete.ts @@ -0,0 +1,31 @@ +import config from '../../config'; +import { ensure } from '../../prelude/ensure'; +import { MessagingMessages, Users } from '../../models'; +import { MessagingMessage } from '../../models/entities/messaging-message'; +import { publishGroupMessagingStream, publishMessagingStream } from '../stream'; +import { renderActivity } from '../../remote/activitypub/renderer'; +import renderDelete from '../../remote/activitypub/renderer/delete'; +import renderTombstone from '../../remote/activitypub/renderer/tombstone'; +import { deliver } from '../../queue'; + +export async function deleteMessage(message: MessagingMessage) { + await MessagingMessages.delete(message.id); + postDeleteMessage(message); +} + +async function postDeleteMessage(message: MessagingMessage) { + if (message.recipientId) { + const user = await Users.findOne(message.userId).then(ensure); + const recipient = await Users.findOne(message.recipientId).then(ensure); + + if (Users.isLocalUser(user)) publishMessagingStream(message.userId, message.recipientId, 'deleted', message.id); + if (Users.isLocalUser(recipient)) publishMessagingStream(message.recipientId, message.userId, 'deleted', message.id); + + if (Users.isLocalUser(user) && Users.isRemoteUser(recipient)) { + const activity = renderActivity(renderDelete(renderTombstone(`${config.url}/notes/${message.id}`), user)); + deliver(user, activity, recipient.inbox); + } + } else if (message.groupId) { + publishGroupMessagingStream(message.groupId, 'deleted', message.id); + } +}