This commit is contained in:
syuilo
2018-04-12 04:05:03 +09:00
parent 553fccd719
commit 92dd4b3e5a
3 changed files with 73 additions and 5 deletions

View File

@@ -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<IMessagingMessage>('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<any>}
*/
export const pack = (
message: any,