1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-11-28 14:58:15 +09:00

メッセージ (トーク/チャット) 削除の連合 (#6789)

This commit is contained in:
MeiMei 2020-11-01 12:14:42 +09:00 committed by GitHub
parent c055b4d32d
commit cd0b0012d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 65 additions and 12 deletions

View File

@ -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<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
*/

View File

@ -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<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) {
@ -24,7 +34,7 @@ export default async function(actor: IRemoteUser, uri: string): Promise<string>
}
await deleteNode(actor, note);
return 'ok: deleted';
return 'ok: note deleted';
} finally {
unlock();
}

View File

@ -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);
});

View File

@ -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);
}
}