|
@@ -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",
|