routes.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. Meteor.subscribe("users");
  2. let chatManager = new ChatManager(Chats);
  3. // set up the main template the the router will use to build pages
  4. Router.configure({
  5. layoutTemplate: "ApplicationLayout"
  6. });
  7. // specify the top level route, the page users see when they arrive at the site
  8. Router.route("/", function () {
  9. console.log("Routing root /");
  10. this.render("navbar", { to: "header" });
  11. this.render("lobby_page", { to: "main" });
  12. });
  13. // specify a route that allows the current user to chat to another users
  14. Router.route("/chat/:_id", {
  15. subscriptions: function () {
  16. // Meteor._debug("Subscriptions");
  17. return Meteor.subscribe("chats");
  18. },
  19. action: function (req) {
  20. // Meteor._debug("Action", req.url);
  21. if (this.ready()) {
  22. console.log("- Now ready");
  23. // The user they want to chat to has id equal to the id sent in after /chat/...
  24. const otherUserId = this.params._id;
  25. const myId = Meteor.userId();
  26. chatManager.assignChatId(myId ? myId : "anon", otherUserId ? otherUserId : "anon", Session);
  27. if (Session.get("chatId")) {
  28. this.render("navbar", { to: "header" });
  29. this.render("chat_page", {
  30. to: "main",
  31. data: function () {
  32. const other = Meteor.users.findOne({ _id: otherUserId });
  33. // console.log("-- Routing data, other: " + (other ? other.profile.username : "not available"));
  34. return {
  35. other: other
  36. };
  37. }
  38. });
  39. }
  40. else {
  41. this.render("navbar", { to: "header" });
  42. this.render("Error", {
  43. to: "main",
  44. data: new Meteor.Error("no-chat", `Could not find a chat between ${myId} and ${otherUserId}.`)
  45. });
  46. }
  47. }
  48. else {
  49. console.log("- Not yet ready");
  50. this.render("Loading");
  51. }
  52. }
  53. });