Просмотр исходного кода

WIP insecure removal. Handle some errors, refactored route action.

Frederic G. MARAND 9 лет назад
Родитель
Сommit
e8615a842e
6 измененных файлов с 78 добавлено и 45 удалено
  1. 0 1
      .meteor/packages
  2. 0 1
      .meteor/versions
  3. 0 1
      client/chat_message.js
  4. 7 0
      client/minstant.html
  5. 5 0
      client/minstant.js
  6. 66 42
      client/routes.js

+ 0 - 1
.meteor/packages

@@ -20,5 +20,4 @@ iron:router
 accounts-password
 twbs:bootstrap
 ian:accounts-ui-bootstrap-3
-insecure
 msavin:mongol

+ 0 - 1
.meteor/versions

@@ -35,7 +35,6 @@ htmljs@1.0.5
 http@1.1.1
 ian:accounts-ui-bootstrap-3@1.2.83
 id-map@1.0.4
-insecure@1.0.4
 iron:controller@1.0.12
 iron:core@1.0.11
 iron:dynamic-template@1.0.12

+ 0 - 1
client/chat_message.js

@@ -11,7 +11,6 @@ Template.chat_message.helpers({
     else {
       result = `Unknown user ${this.message.sender}.`;
     }
-    console.log("chat_message sender", result);
     return result;
   }
 });

+ 7 - 0
client/minstant.html

@@ -30,3 +30,10 @@
 <template name="Loading">
   Loading...
 </template>
+
+<template name="Error">
+  <div class="jumbotron">
+  <h1>{{ dump.error }}</h1>.
+  <p>{{ dump.reason }}</p>
+  </div>
+</template>

+ 5 - 0
client/minstant.js

@@ -0,0 +1,5 @@
+Template.Error.helpers({
+  dump: function () {
+    return this;
+  }
+});

+ 66 - 42
client/routes.js

@@ -1,5 +1,51 @@
 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;
+};
+
 // set up the main template the the router will use to build pages
 Router.configure({
   layoutTemplate: "ApplicationLayout"
@@ -19,59 +65,37 @@ Router.route("/chat/:_id", {
     return Meteor.subscribe("chats");
   },
 
-  action: function (req, wtf, func) {
+  action: function (req, wtf, next) {
     Meteor._debug("Action", req.url);
     if (this.ready()) {
       console.log("- Now ready");
 
       // The user they want to chat to has id equal to the id sent in after /chat/...
       const otherUserId = this.params._id;
-      // find a chat that has two users that match current user id
-      // and the requested user id
-      const filter = {
-        $or: [
-          {
-            user1Id: Meteor.userId(),
-            user2Id: otherUserId
-          },
-          {
-            user2Id: Meteor.userId(),
-            user1Id: otherUserId
+      const myId = Meteor.userId();
+      const chatId = getChatId(myId, otherUserId);
+      if (chatId) {
+        // Looking good, save the id to the session.
+        Session.set("chatId", chatId);
+        this.render("navbar", { to: "header" });
+        this.render("chat_page", {
+          to: "main",
+          data: function () {
+            const other = Meteor.users.findOne({ _id: otherUserId });
+            console.log("--- Routing data, other: " + (other ? other.profile.username : "not available"));
+            return {
+              other: other
+            };
           }
-        ]
-      };
-      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: Meteor.userId(),
-          user2Id: otherUserId
         });
-        console.log("--- No chat found between " + Meteor.userId() + " and " + otherUserId + ". Created " + chatId);
       }
-      // There is a chat going already - use that.
       else {
-        chatId = chat._id;
-        console.log("--- Chat " + chatId + " found between " + Meteor.userId() + " and " + otherUserId + ".");
-      }
-      // Looking good, save the id to the session.
-      if (chatId) {
-        Session.set("chatId", chatId);
+        this.render("navbar", { to: "header" });
+        this.render("Error", {
+          to: "main",
+          data: new Meteor.Error("no-chat", `Could not find a chat between ${myId} and ${otherUserId}.`)
+        });
       }
-      this.render("navbar", { to: "header" });
-      this.render("chat_page", {
-        to: "main",
-        data: function () {
-          const other = Meteor.users.findOne({ _id: otherUserId });
-          console.log("--- Routing data, other: " + (other ? other.profile.username : "not available"));
-          return {
-            other: other
-          };
-        }
-      });
     }
     else {
       console.log("- Not yet ready");