Explorar el Código

Task 3: pub/sub.

Frederic G. MARAND hace 9 años
padre
commit
64f8551ff6
Se han modificado 7 ficheros con 33 adiciones y 7 borrados
  1. 19 2
      client/chat_message.js
  2. 1 1
      client/minstant.html
  3. 2 2
      client/routes.js
  4. 1 1
      lib/ChatManager.js
  5. BIN
      public/anonymous.png
  6. BIN
      public/ghost.png
  7. 10 1
      server/server.js

+ 19 - 2
client/chat_message.js

@@ -1,16 +1,33 @@
 Template.chat_message.helpers({
   sender: function () {
     const me = Meteor.userId();
+
     let result;
     if (this.message.sender === me) {
-      result = Meteor.user();
+      if (me) {
+        result = Meteor.user();
+      }
+      else {
+        result = {
+          profile: {
+            username: "Anonymous",
+            avatar: "anonymous.png"
+          }
+        };
+      }
     }
     else if (this.message.sender === this.other._id) {
       result = this.other;
     }
     else {
-      result = `Unknown user ${this.message.sender}.`;
+      result = {
+        profile: {
+          username: `Unknown user ${this.message.sender}.`,
+          avatar: "ghost.png"
+        }
+      };
     }
+    console.log("returning", result);
     return result;
   }
 });

+ 1 - 1
client/minstant.html

@@ -21,7 +21,7 @@
 
   <div class="container">
     <div class="panel panel-default">
-      <div class="panel-body">Users are now sorted by name, unlike the original code.</div>
+      <div class="panel-body">Users are now sorted by name, unlike the original code. Try chatting with someone without logging in.</div>
     </div>
   </div>
 </template>

+ 2 - 2
client/routes.js

@@ -20,7 +20,7 @@ Router.route("/chat/:_id", {
     return Meteor.subscribe("chats");
   },
 
-  action: function (req, wtf, next) {
+  action: function (req) {
     // Meteor._debug("Action", req.url);
     if (this.ready()) {
       console.log("- Now ready");
@@ -28,7 +28,7 @@ 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();
-      chatManager.assignChatId(myId, otherUserId, Session);
+      chatManager.assignChatId(myId ? myId : "anon", otherUserId ? otherUserId : "anon", Session);
       if (Session.get("chatId")) {
         this.render("navbar", { to: "header" });
         this.render("chat_page", {

+ 1 - 1
lib/ChatManager.js

@@ -51,7 +51,7 @@ ChatManager = class ChatManager {
 
     // Since ids are ordered, we do not need a $or.
     const chat = this.chats.findOne({ user1Id, user2Id });
-    // Meteor._debug(filter, chat);
+    Meteor._debug("filter", { user1Id, user2Id }, chat);
 
     // No chat matching the filter - need to insert a new one.
     if (!chat) {

BIN
public/anonymous.png


BIN
public/ghost.png


+ 10 - 1
server/server.js

@@ -21,8 +21,17 @@ Meteor.startup(function () {
   }
 
   Meteor.publish("chats", function () {
-    return Chats.find();
+    Meteor._debug("publishing chats. User", this.userId);
+    let myId = this.userId || "anon";
+    const selector = {
+      $or: [
+        { user1Id: myId },
+        { user2Id: myId }
+      ]
+    };
+    return Chats.find(selector);
   });
+
   Meteor.publish("users", function () {
     return Meteor.users.find();
   });