routes.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. Meteor.subscribe("users");
  2. /**
  3. * Find the id of a chat between two users.
  4. *
  5. * @param {String} myId
  6. * The current user id.
  7. * @param {String} otherUserId
  8. * The id of an other user.
  9. *
  10. * @returns {String}
  11. * The id of a chat between both users.
  12. */
  13. const getChatId = function (myId, otherUserId) {
  14. let id1;
  15. let id2;
  16. // Ensure ordering as id1 <= id2. Equality should not happen, but handle it correctly nonetheless.
  17. if (myId <= otherUserId) {
  18. id1 = myId;
  19. id2 = otherUserId;
  20. }
  21. else {
  22. id1 = otherUserId;
  23. id2 = myId;
  24. }
  25. // Since ids are ordered, we do not need a $or.
  26. const filter = { user1Id: id1, user2Id: id2 };
  27. const chat = Chats.findOne(filter);
  28. // Meteor._debug(filter, chat);
  29. // No chat matching the filter - need to insert a new one.
  30. let chatId;
  31. if (!chat) {
  32. chatId = Chats.insert({
  33. user1Id: id1,
  34. user2Id: id2
  35. });
  36. console.log("--- No chat found between " + id1 + " and " + id2 + ". Created " + chatId);
  37. }
  38. // There is a chat going already - use that.
  39. else {
  40. chatId = chat._id;
  41. console.log("--- Chat " + chatId + " found between " + id1 + " and " + id2 + ".");
  42. }
  43. return chatId;
  44. };
  45. // set up the main template the the router will use to build pages
  46. Router.configure({
  47. layoutTemplate: "ApplicationLayout"
  48. });
  49. // specify the top level route, the page users see when they arrive at the site
  50. Router.route("/", function () {
  51. console.log("Routing root /");
  52. this.render("navbar", { to: "header" });
  53. this.render("lobby_page", { to: "main" });
  54. });
  55. // specify a route that allows the current user to chat to another users
  56. Router.route("/chat/:_id", {
  57. subscriptions: function () {
  58. Meteor._debug("Subscriptions");
  59. return Meteor.subscribe("chats");
  60. },
  61. action: function (req, wtf, next) {
  62. Meteor._debug("Action", req.url);
  63. if (this.ready()) {
  64. console.log("- Now ready");
  65. // The user they want to chat to has id equal to the id sent in after /chat/...
  66. const otherUserId = this.params._id;
  67. const myId = Meteor.userId();
  68. const chatId = getChatId(myId, otherUserId);
  69. if (chatId) {
  70. // Looking good, save the id to the session.
  71. Session.set("chatId", chatId);
  72. this.render("navbar", { to: "header" });
  73. this.render("chat_page", {
  74. to: "main",
  75. data: function () {
  76. const other = Meteor.users.findOne({ _id: otherUserId });
  77. console.log("--- Routing data, other: " + (other ? other.profile.username : "not available"));
  78. return {
  79. other: other
  80. };
  81. }
  82. });
  83. }
  84. else {
  85. this.render("navbar", { to: "header" });
  86. this.render("Error", {
  87. to: "main",
  88. data: new Meteor.Error("no-chat", `Could not find a chat between ${myId} and ${otherUserId}.`)
  89. });
  90. }
  91. }
  92. else {
  93. console.log("- Not yet ready");
  94. this.render("Loading");
  95. }
  96. }
  97. });