|
@@ -71,6 +71,14 @@ ChatManager = class ChatManager {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Meteor Method: insert a chat.
|
|
|
+ *
|
|
|
+ * @param {Object} chat
|
|
|
+ * A chat object, with user1Id, user2Id, and messages keys.
|
|
|
+ * @returns {String}
|
|
|
+ * The id of the inserted chat document.
|
|
|
+ */
|
|
|
static insertMethod(chat) {
|
|
|
check(chat, {
|
|
|
user1Id: String,
|
|
@@ -81,4 +89,35 @@ ChatManager = class ChatManager {
|
|
|
// Meteor._debug("chats.insert", chat, chatId);
|
|
|
return chatId;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Meteor Method: push a message to a chat.
|
|
|
+ *
|
|
|
+ * @param {Object} chat
|
|
|
+ * A chat object, with user1Id, user2Id, and messages keys.
|
|
|
+ * @param {String} chatValue
|
|
|
+ * The message to push.
|
|
|
+ */
|
|
|
+ static pushMessageMethod(chat, chatValue) {
|
|
|
+ check(chat, {
|
|
|
+ _id: String,
|
|
|
+ user1Id: String,
|
|
|
+ user2Id: String,
|
|
|
+ messages: Array
|
|
|
+ });
|
|
|
+
|
|
|
+ // 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
|
|
|
+ const changes = {
|
|
|
+ $push: {
|
|
|
+ messages: {
|
|
|
+ text: chatValue,
|
|
|
+ sender: this.userId
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+ // update the chat object in the database.
|
|
|
+ Chats.update(chat._id, changes);
|
|
|
+ }
|
|
|
};
|