From 92dd4b3e5a701823ebb5a453aa42202da8a326ea Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 12 Apr 2018 04:05:03 +0900 Subject: [PATCH] wip --- src/models/messaging-history.ts | 27 +++++++++++++++++++++++ src/models/messaging-message.ts | 38 ++++++++++++++++++++++++++++----- src/models/user.ts | 13 +++++++++++ 3 files changed, 73 insertions(+), 5 deletions(-) diff --git a/src/models/messaging-history.ts b/src/models/messaging-history.ts index 6864e22d2f..5367f81412 100644 --- a/src/models/messaging-history.ts +++ b/src/models/messaging-history.ts @@ -11,3 +11,30 @@ export type IMessagingHistory = { partnerId: mongo.ObjectID; messageId: mongo.ObjectID; }; + +/** + * MessagingHistoryを物理削除します + */ +export async function deleteMessagingHistory(messagingHistory: string | mongo.ObjectID | IMessagingHistory) { + let m: IMessagingHistory; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(messagingHistory)) { + m = await MessagingHistory.findOne({ + _id: messagingHistory + }); + } else if (typeof messagingHistory === 'string') { + m = await MessagingHistory.findOne({ + _id: new mongo.ObjectID(messagingHistory) + }); + } else { + m = messagingHistory as IMessagingHistory; + } + + if (m == null) return; + + // このMessagingHistoryを削除 + await MessagingHistory.remove({ + _id: m._id + }); +} diff --git a/src/models/messaging-message.ts b/src/models/messaging-message.ts index 974ee54ab8..9d62fab4fa 100644 --- a/src/models/messaging-message.ts +++ b/src/models/messaging-message.ts @@ -3,6 +3,7 @@ import deepcopy = require('deepcopy'); import { pack as packUser } from './user'; import { pack as packFile } from './drive-file'; import db from '../db/mongodb'; +import MessagingHistory, { deleteMessagingHistory } from './messaging-history'; const MessagingMessage = db.get('messagingMessages'); export default MessagingMessage; @@ -22,13 +23,40 @@ export function isValidText(text: string): boolean { return text.length <= 1000 && text.trim() != ''; } +/** + * MessagingMessageを物理削除します + */ +export async function deleteMessagingMessage(messagingMessage: string | mongo.ObjectID | IMessagingMessage) { + let m: IMessagingMessage; + + // Populate + if (mongo.ObjectID.prototype.isPrototypeOf(messagingMessage)) { + m = await MessagingMessage.findOne({ + _id: messagingMessage + }); + } else if (typeof messagingMessage === 'string') { + m = await MessagingMessage.findOne({ + _id: new mongo.ObjectID(messagingMessage) + }); + } else { + m = messagingMessage as IMessagingMessage; + } + + if (m == null) return; + + // このMessagingMessageを指すMessagingHistoryをすべて削除 + await Promise.all(( + await MessagingHistory.find({ messageId: m._id }) + ).map(x => deleteMessagingHistory(x))); + + // このMessagingMessageを削除 + await MessagingMessage.remove({ + _id: m._id + }); +} + /** * Pack a messaging message for API response - * - * @param {any} message - * @param {any} me? - * @param {any} options? - * @return {Promise} */ export const pack = ( message: any, diff --git a/src/models/user.ts b/src/models/user.ts index b1a68b0827..6155324be8 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -11,6 +11,8 @@ import AccessToken, { deleteAccessToken } from './access-token'; import NoteWatching, { deleteNoteWatching } from './note-watching'; import Favorite, { deleteFavorite } from './favorite'; import NoteReaction, { deleteNoteReaction } from './note-reaction'; +import MessagingMessage, { deleteMessagingMessage } from './messaging-message'; +import MessagingHistory, { deleteMessagingHistory } from './messaging-history'; const User = db.get('users'); @@ -173,8 +175,19 @@ export async function deleteUser(user: string | mongo.ObjectID | IUser) { ).map(x => deleteFavorite(x))); // このユーザーのMessageをすべて削除 + await Promise.all(( + await MessagingMessage.find({ userId: u._id }) + ).map(x => deleteMessagingMessage(x))); // このユーザーへのMessageをすべて削除 + await Promise.all(( + await MessagingMessage.find({ recipientId: u._id }) + ).map(x => deleteMessagingMessage(x))); + + // このユーザーの関わるMessagingHistoryをすべて削除 + await Promise.all(( + await MessagingHistory.find({ $or: [{ partnerId: u._id }, { userId: u._id }] }) + ).map(x => deleteMessagingHistory(x))); // このユーザーのDriveFileをすべて削除