Skip to content

Commit

Permalink
AP delete message (#579)
Browse files Browse the repository at this point in the history
  • Loading branch information
mei23 authored Oct 31, 2020
1 parent 0e6b4c3 commit c1bfc7f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
21 changes: 20 additions & 1 deletion src/remote/activitypub/db-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -33,6 +34,24 @@ export default class DbResolver {
return null;
}

public async getMessageFromApId(value: string | IObject): Promise<MessagingMessage | null> {
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
*/
Expand Down
14 changes: 12 additions & 2 deletions src/remote/activitypub/kernel/delete/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,15 +17,24 @@ export default async function(actor: IRemoteUser, uri: string): Promise<string>
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) {
return '投稿を削除しようとしているユーザーは投稿の作成者ではありません';
}

await deleteNode(actor, note);
return 'ok: deleted';
return 'ok: note deleted';
} finally {
unlock();
}
Expand Down
11 changes: 2 additions & 9 deletions src/server/api/endpoints/messaging/messages/delete.ts
Original file line number Diff line number Diff line change
@@ -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 = {
stability: 'stable',
Expand Down Expand Up @@ -55,12 +55,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);
});
31 changes: 31 additions & 0 deletions src/services/messages/delete.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit c1bfc7f

Please sign in to comment.