Sfoglia il codice sorgente

Refactored chat insert method to ChatManager.

Frederic G. MARAND 9 anni fa
parent
commit
8694d7fdf6
3 ha cambiato i file con 19 aggiunte e 15 eliminazioni
  1. 3 3
      client/routes.js
  2. 15 2
      lib/ChatManager.js
  3. 1 10
      shared/methods.js

+ 3 - 3
client/routes.js

@@ -16,12 +16,12 @@ Router.route("/", function () {
 // specify a route that allows the current user to chat to another users
 Router.route("/chat/:_id", {
   subscriptions: function () {
-    Meteor._debug("Subscriptions");
+    // Meteor._debug("Subscriptions");
     return Meteor.subscribe("chats");
   },
 
   action: function (req, wtf, next) {
-    Meteor._debug("Action", req.url);
+    // Meteor._debug("Action", req.url);
     if (this.ready()) {
       console.log("- Now ready");
 
@@ -35,7 +35,7 @@ Router.route("/chat/:_id", {
           to: "main",
           data: function () {
             const other = Meteor.users.findOne({ _id: otherUserId });
-            console.log("--- Routing data, other: " + (other ? other.profile.username : "not available"));
+            // console.log("-- Routing data, other: " + (other ? other.profile.username : "not available"));
             return {
               other: other
             };

+ 15 - 2
lib/ChatManager.js

@@ -4,6 +4,8 @@ ChatManager = class ChatManager {
    *
    * @param {Collection} chats
    *   A Chats collection instance.
+   *
+   * @returns {void}
    */
   constructor(chats) {
     this.chats = chats;
@@ -58,14 +60,25 @@ ChatManager = class ChatManager {
           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);
+        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 + ".");
+      console.log("-- Chat " + chatId + " found between " + user1Id + " and " + user2Id + ".");
     }
   }
+
+  static insertMethod(chat) {
+    check(chat, {
+      user1Id: String,
+      user2Id: String
+    });
+    chat.messages = [];
+    const chatId = Chats.insert(chat);
+    // Meteor._debug("chats.insert", chat, chatId);
+    return chatId;
+  }
 };

+ 1 - 10
shared/methods.js

@@ -1,12 +1,3 @@
 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;
-  }
+  "chats.insert": ChatManager.insertMethod
 });