|
@@ -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");
|