'use strict';

/**
 * Module dependencies
 */
import * as mongo from 'mongodb';
import Message from '../../models/talk-message';
import Group from '../../models/talk-group';
import History from '../../models/talk-history';
import User from '../../models/user';
import DriveFile from '../../models/drive-file';
import serialize from '../../serializers/talk-message';
import event from '../../event';
import es from '../../db/elasticsearch';

/**
 * 最大文字数
 */
const maxTextLength = 500;

/**
 * Create a message
 *
 * @param {Object} params
 * @param {Object} reply
 * @param {Object} user
 * @return {void}
 */
module.exports = async (params, reply, user) =>
{
	// Init 'user' parameter
	let recipient = params.user;
	if (recipient !== undefined && recipient !== null) {
		recipient = await User.findOne({
			_id: new mongo.ObjectID(recipient),
		});

		if (recipient === null) {
			return reply(400, 'user not found');
		}
	} else {
		recipient = null;
	}

	// Init 'group' parameter
	let group = params.group;
	if (group !== undefined && group !== null) {
		group = await Group.findOne({
			_id: new mongo.ObjectID(group),
		});

		if (group === null) {
			return reply(400, 'group not found');
		}
	} else {
		group = null;
	}

	// ユーザーの指定がないかつグループの指定もなかったらエラー
	if (recipient === null && group === null) {
		return reply(400, 'user or group is required');
	}

	// ユーザーとグループ両方指定してたらエラー
	if (recipient !== null && group !== null) {
		return reply(400, 'need translate');
	}

	// Init 'text' parameter
	let text = params.text;
	if (text !== undefined && text !== null) {
		text = text.trim();
		if (text.length === 0) {
			text = null;
		} else if (text.length > maxTextLength) {
			return reply(400, 'too long text');
		}
	} else {
		text = null;
	}

	// Init 'file' parameter
	let file = params.file;
	if (file !== undefined && file !== null) {
		file = await DriveFile.findOne({
			_id: new mongo.ObjectID(file),
			user: user._id
		}, {
			data: false
		});

		if (file === null) {
			return reply(400, 'file not found');
		}
	} else {
		file = null;
	}

	// テキストが無いかつ添付ファイルも無かったらエラー
	if (text === null && file === null) {
		return reply(400, 'text or file is required');
	}

	// メッセージを作成
	const res = await Message.insert({
		created_at: Date.now(),
		file: file ? file._id : undefined,
		recipient: recipient ? recipient._id : undefined,
		group: group ? group._id : undefined,
		text: text ? text : undefined,
		user: user._id
	});

	const message = res.ops[0];

	// Serialize
	const messageObj = await serialize(message);

	// Reponse
	reply(messageObj);

	// Publish to stream
	event.publishTalkMessage(user._id, messageObj);

	// Register to search database
	if (message.text) {
		es.index({
			index: 'talk_messages',
			type: 'message',
			id: message._id.toString(),
			body: {
				text: message.text
			}
		});
	}

	// 履歴を作成しておく(対人)
	if (recipient) {
		// 自分
		History.updateOne({
			user: user._id,
			partner: recipient._id
		}, {
			updated_at: Date.now(),
			user: user._id,
			partner: recipient._id,
			message: message._id
		}, {
			upsert: true
		});

		// 相手
		History.updateOne({
			user: recipient._id,
			partner: user._id
		}, {
			updated_at: Date.now(),
			user: recipient._id,
			partner: user._id,
			message: message._id
		}, {
			upsert: true
		});
	}

	// 履歴を作成しておく(グループ)
	if (group) {
		group.members.forEach(member => {
			History.updateOne({
				user: member,
				group: group._id
			}, {
				updated_at: Date.now(),
				user: member._id,
				group: group._id,
				message: message._id
			}, {
				upsert: true
			});
		});
	}
};

@@ -835,17 +835,16 @@
 cipient)
-,
 %0A%09%09%7D);%0A%0A
@@ -1114,17 +1114,16 @@
 D(group)
-,
 %0A%09%09%7D);%0A%0A
@@ -1182,32 +1182,208 @@
 not found');%0A%09%09%7D
+%0A%0A%09%09// %E3%81%93%E3%81%AE%E3%82%B0%E3%83%AB%E3%83%BC%E3%83%97%E3%81%AE%E3%83%A1%E3%83%B3%E3%83%90%E3%83%BC%E3%81%98%E3%82%83%E3%81%AA%E3%81%8B%E3%81%A3%E3%81%9F%E3%82%89reject%0A%09%09if (group.members%0A%09%09%09%09.map(member =%3E member.toString())%0A%09%09%09%09.indexOf(user._id.toString()) === -1) %7B%0A%09%09%09return reply(403, 'access denied');%0A%09%09%7D
 %0A%09%7D else %7B%0A%09%09gro

Fix bug