Преглед изворни кода

Allow chat creation between two string user ids.

Frederic G. MARAND пре 9 година
родитељ
комит
5fb50a24cc
5 измењених фајлова са 87 додато и 52 уклоњено
  1. 2 1
      .eslintrc.js
  2. 1 0
      .meteor/packages
  3. 3 50
      client/routes.js
  4. 71 0
      lib/ChatManager.js
  5. 10 1
      shared/methods.js

+ 2 - 1
.eslintrc.js

@@ -29,7 +29,8 @@ module.exports = {
     "SimpleSchema": true,
 
     // Project-specific.
-    "Chats": true
+    "Chats": true,
+    "ChatManager": true
   },
 
   "plugins": ["react"],

+ 1 - 0
.meteor/packages

@@ -21,3 +21,4 @@ accounts-password
 twbs:bootstrap
 ian:accounts-ui-bootstrap-3
 msavin:mongol
+check

+ 3 - 50
client/routes.js

@@ -1,50 +1,5 @@
 Meteor.subscribe("users");
-
-/**
- * Find the id of a chat between two users.
- *
- * @param {String} myId
- *   The current user id.
- * @param {String} otherUserId
- *   The id of an other user.
- *
- * @returns {String}
- *   The id of a chat between both users.
- */
-const getChatId = function (myId, otherUserId) {
-  let id1;
-  let id2;
-  // Ensure ordering as id1 <= id2. Equality should not happen, but handle it correctly nonetheless.
-  if (myId <= otherUserId) {
-    id1 = myId;
-    id2 = otherUserId;
-  }
-  else {
-    id1 = otherUserId;
-    id2 = myId;
-  }
-
-  // Since ids are ordered, we do not need a $or.
-  const filter = { user1Id: id1, user2Id: id2 };
-  const chat = Chats.findOne(filter);
-  // Meteor._debug(filter, chat);
-
-  // No chat matching the filter - need to insert a new one.
-  let chatId;
-  if (!chat) {
-    chatId = Chats.insert({
-      user1Id: id1,
-      user2Id: id2
-    });
-    console.log("--- No chat found between " + id1 + " and " + id2 + ". Created " + chatId);
-  }
-  // There is a chat going already - use that.
-  else {
-    chatId = chat._id;
-    console.log("--- Chat " + chatId + " found between " + id1 + " and " + id2 + ".");
-  }
-  return chatId;
-};
+let chatManager = new ChatManager(Chats);
 
 // set up the main template the the router will use to build pages
 Router.configure({
@@ -73,10 +28,8 @@ Router.route("/chat/:_id", {
       // The user they want to chat to has id equal to the id sent in after /chat/...
       const otherUserId = this.params._id;
       const myId = Meteor.userId();
-      const chatId = getChatId(myId, otherUserId);
-      if (chatId) {
-        // Looking good, save the id to the session.
-        Session.set("chatId", chatId);
+      chatManager.assignChatId(myId, otherUserId, Session);
+      if (Session.get("chatId")) {
         this.render("navbar", { to: "header" });
         this.render("chat_page", {
           to: "main",

+ 71 - 0
lib/ChatManager.js

@@ -0,0 +1,71 @@
+ChatManager = class ChatManager {
+  /**
+   * Constructor.
+   *
+   * @param {Collection} chats
+   *   A Chats collection instance.
+   */
+  constructor(chats) {
+    this.chats = chats;
+  }
+
+  /**
+   * Order two comparable objects as min, max
+   *
+   * @param {Object} s1
+   * @param {Object} s2
+   * @returns {*[]}
+   *   An array of the two objects, minimum first, maximum last.
+   */
+  static ordered(s1, s2) {
+    let id1;
+    let id2;
+    // Ensure ordering as id1 <= id2. Equality should not happen, but handle it correctly nonetheless.
+    if (s1 <= s2) {
+      id1 = s1;
+      id2 = s2;
+    }
+    else {
+      id1 = s2;
+      id2 = s1;
+    }
+    return [id1, id2];
+  }
+
+  /**
+   * Find the id of a chat between two users.
+   *
+   * @param {String} myId
+   *   The current user id.
+   * @param {String} otherUserId
+   *   The id of an other user.
+   * @param {Session} session
+   *   The session global.
+   *
+   * @returns {void}
+   */
+  assignChatId(myId, otherUserId, session) {
+    let [user1Id, user2Id] = ChatManager.ordered(myId, otherUserId);
+
+    // Since ids are ordered, we do not need a $or.
+    const chat = this.chats.findOne({ user1Id, user2Id });
+    // Meteor._debug(filter, chat);
+
+    // No chat matching the filter - need to insert a new one.
+    if (!chat) {
+      Meteor.call("chats.insert", { user1Id, user2Id }, function (err, chatId) {
+        if (err) {
+          throw new Meteor.Error("access-denied", "Could not insert new chat.");
+        }
+        session.set("chatId", chatId);
+        console.log("--- No chat found between " + user1Id + " and " + user2Id + ". Created " + chatId);
+      });
+    }
+    // There is a chat going already - use that.
+    else {
+      const chatId = chat._id;
+      session.set("chatId", chatId);
+      console.log("--- Chat " + chatId + " found between " + user1Id + " and " + user2Id + ".");
+    }
+  }
+};

+ 10 - 1
shared/methods.js

@@ -1,3 +1,12 @@
 Meteor.methods({
-
+  "chats.insert": function (chat) {
+    check(chat, {
+      user1Id: String,
+      user2Id: String
+    });
+    chat.messages = [];
+    const chatId = Chats.insert(chat);
+    Meteor._debug("chats.insert", chat, chatId);
+    return chatId;
+  }
 });