|
@@ -18,24 +18,11 @@ Template.chat_page.events({
|
|
// see if we can find a chat object in the database
|
|
// see if we can find a chat object in the database
|
|
// to which we'll add the message
|
|
// to which we'll add the message
|
|
const chat = Chats.findOne({ _id: Session.get("chatId") });
|
|
const chat = Chats.findOne({ _id: Session.get("chatId") });
|
|
- if (chat) {// ok - we have a chat to use
|
|
|
|
- let msgs = chat.messages; // pull the messages property
|
|
|
|
- if (!msgs) {// no messages yet, create a new array
|
|
|
|
- msgs = [];
|
|
|
|
- }
|
|
|
|
- // is a good idea to insert data straight from the form
|
|
|
|
- // (i.e. the user) into the database?? certainly not.
|
|
|
|
- // push adds the message to the end of the array
|
|
|
|
- msgs.push({
|
|
|
|
- text: event.target.chat.value,
|
|
|
|
- sender: Meteor.userId()
|
|
|
|
- });
|
|
|
|
|
|
+ if (chat) {
|
|
|
|
+ // OK - we have a chat to use.
|
|
|
|
+ Meteor.call("chats.pushMessage", chat, event.target.chat.value);
|
|
// reset the form
|
|
// reset the form
|
|
event.target.chat.value = "";
|
|
event.target.chat.value = "";
|
|
- // put the messages array onto the chat object
|
|
|
|
- chat.messages = msgs;
|
|
|
|
- // update the chat object in the database.
|
|
|
|
- Chats.update(chat._id, chat);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
});
|
|
});
|