1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- Meteor.subscribe("users");
- // set up the main template the the router will use to build pages
- Router.configure({
- layoutTemplate: "ApplicationLayout"
- });
- // specify the top level route, the page users see when they arrive at the site
- Router.route("/", function () {
- console.log("rendering root /");
- this.render("navbar", { to: "header" });
- this.render("lobby_page", { to: "main" });
- });
- // specify a route that allows the current user to chat to another users
- Router.route("/chat/:_id", {
- subscriptions: function () {
- Meteor._debug('subscriptions', arguments);
- return Meteor.subscribe("chats");
- },
- action: function () {
- Meteor._debug('action', arguments);
- if (this.ready()) {
- Meteor._debug("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 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
- });
- Meteor._debug("No chat found between " + Meteor.userId() + " and " + otherUserId + ". Created " + chatId);
- }
- // There is a chat going already - use that.
- else {
- chatId = chat._id;
- Meteor._debug("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("chat_page", { to: "main" });
- }
- else {
- Meteor._debug('not ready');
- this.render("Loading");
- }
- }
- });
|