2017-06-12 10:12:30 -07:00
|
|
|
import * as mongo from 'mongodb';
|
2018-02-01 15:06:01 -08:00
|
|
|
import deepcopy = require('deepcopy');
|
|
|
|
import { pack as packUser } from './user';
|
|
|
|
import { pack as packFile } from './drive-file';
|
2018-03-29 04:32:18 -07:00
|
|
|
import db from '../db/mongodb';
|
2018-04-11 12:05:03 -07:00
|
|
|
import MessagingHistory, { deleteMessagingHistory } from './messaging-history';
|
2017-01-16 16:12:33 -08:00
|
|
|
|
2018-03-28 22:48:47 -07:00
|
|
|
const MessagingMessage = db.get<IMessagingMessage>('messagingMessages');
|
2018-02-01 15:06:01 -08:00
|
|
|
export default MessagingMessage;
|
2017-03-02 15:24:48 -08:00
|
|
|
|
2017-06-12 10:12:30 -07:00
|
|
|
export interface IMessagingMessage {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-28 22:48:47 -07:00
|
|
|
createdAt: Date;
|
2018-02-01 15:21:30 -08:00
|
|
|
text: string;
|
2018-03-31 03:53:30 -07:00
|
|
|
textHtml: string;
|
2018-03-28 22:48:47 -07:00
|
|
|
userId: mongo.ObjectID;
|
|
|
|
recipientId: mongo.ObjectID;
|
|
|
|
isRead: boolean;
|
|
|
|
fileId: mongo.ObjectID;
|
2017-06-12 10:12:30 -07:00
|
|
|
}
|
|
|
|
|
2017-03-02 15:24:48 -08:00
|
|
|
export function isValidText(text: string): boolean {
|
|
|
|
return text.length <= 1000 && text.trim() != '';
|
|
|
|
}
|
2018-02-01 15:06:01 -08:00
|
|
|
|
2018-04-11 12:05:03 -07:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-01 15:06:01 -08:00
|
|
|
/**
|
|
|
|
* Pack a messaging message for API response
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
message: any,
|
|
|
|
me?: any,
|
|
|
|
options?: {
|
|
|
|
populateRecipient: boolean
|
|
|
|
}
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
|
|
|
const opts = options || {
|
|
|
|
populateRecipient: true
|
|
|
|
};
|
|
|
|
|
|
|
|
let _message: any;
|
|
|
|
|
|
|
|
// Populate the message if 'message' is ID
|
|
|
|
if (mongo.ObjectID.prototype.isPrototypeOf(message)) {
|
|
|
|
_message = await MessagingMessage.findOne({
|
|
|
|
_id: message
|
|
|
|
});
|
|
|
|
} else if (typeof message === 'string') {
|
|
|
|
_message = await MessagingMessage.findOne({
|
|
|
|
_id: new mongo.ObjectID(message)
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
_message = deepcopy(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rename _id to id
|
|
|
|
_message.id = _message._id;
|
|
|
|
delete _message._id;
|
|
|
|
|
|
|
|
// Populate user
|
2018-03-28 22:48:47 -07:00
|
|
|
_message.user = await packUser(_message.userId, me);
|
2018-02-01 15:06:01 -08:00
|
|
|
|
2018-03-28 22:48:47 -07:00
|
|
|
if (_message.fileId) {
|
2018-02-01 15:06:01 -08:00
|
|
|
// Populate file
|
2018-03-28 22:48:47 -07:00
|
|
|
_message.file = await packFile(_message.fileId);
|
2018-02-01 15:06:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.populateRecipient) {
|
|
|
|
// Populate recipient
|
2018-03-28 22:48:47 -07:00
|
|
|
_message.recipient = await packUser(_message.recipientId, me);
|
2018-02-01 15:06:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_message);
|
|
|
|
});
|