minstant.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. Chats = new Mongo.Collection("chats");
  2. if (Meteor.isClient) {
  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("rendering 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', function () {
  15. // the user they want to chat to has id equal to
  16. // the id sent in after /chat/...
  17. var otherUserId = this.params._id;
  18. // find a chat that has two users that match current user id
  19. // and the requested user id
  20. var filter = {$or:[
  21. {user1Id:Meteor.userId(), user2Id:otherUserId},
  22. {user2Id:Meteor.userId(), user1Id:otherUserId}
  23. ]};
  24. var chat = Chats.findOne(filter);
  25. if (!chat){// no chat matching the filter - need to insert a new one
  26. chatId = Chats.insert({user1Id:Meteor.userId(), user2Id:otherUserId});
  27. }
  28. else {// there is a chat going already - use that.
  29. chatId = chat._id;
  30. }
  31. if (chatId){// looking good, save the id to the session
  32. Session.set("chatId",chatId);
  33. }
  34. this.render("navbar", {to:"header"});
  35. this.render("chat_page", {to:"main"});
  36. });
  37. ///
  38. // helper functions
  39. ///
  40. Template.available_user_list.helpers({
  41. users:function(){
  42. return Meteor.users.find();
  43. }
  44. })
  45. Template.available_user.helpers({
  46. getUsername:function(userId){
  47. user = Meteor.users.findOne({_id:userId});
  48. return user.profile.username;
  49. },
  50. isMyUser:function(userId){
  51. if (userId == Meteor.userId()){
  52. return true;
  53. }
  54. else {
  55. return false;
  56. }
  57. }
  58. })
  59. Template.chat_page.helpers({
  60. messages:function(){
  61. var chat = Chats.findOne({_id:Session.get("chatId")});
  62. return chat.messages;
  63. },
  64. other_user:function(){
  65. return ""
  66. },
  67. })
  68. Template.chat_page.events({
  69. // this event fires when the user sends a message on the chat page
  70. 'submit .js-send-chat':function(event){
  71. // stop the form from triggering a page reload
  72. event.preventDefault();
  73. // see if we can find a chat object in the database
  74. // to which we'll add the message
  75. var chat = Chats.findOne({_id:Session.get("chatId")});
  76. if (chat){// ok - we have a chat to use
  77. var msgs = chat.messages; // pull the messages property
  78. if (!msgs){// no messages yet, create a new array
  79. msgs = [];
  80. }
  81. // is a good idea to insert data straight from the form
  82. // (i.e. the user) into the database?? certainly not.
  83. // push adds the message to the end of the array
  84. msgs.push({text: event.target.chat.value});
  85. // reset the form
  86. event.target.chat.value = "";
  87. // put the messages array onto the chat object
  88. chat.messages = msgs;
  89. // update the chat object in the database.
  90. Chats.update(chat._id, chat);
  91. }
  92. }
  93. })
  94. }
  95. // start up script that creates some users for testing
  96. // users have the username 'user1@test.com' .. 'user8@test.com'
  97. // and the password test123
  98. if (Meteor.isServer) {
  99. Meteor.startup(function () {
  100. if (!Meteor.users.findOne()){
  101. for (var i=1;i<9;i++){
  102. var email = "user"+i+"@test.com";
  103. var username = "user"+i;
  104. var avatar = "ava"+i+".png"
  105. console.log("creating a user with password 'test123' and username/ email: "+email);
  106. Meteor.users.insert({profile:{username:username, avatar:avatar}, emails:[{address:email}],services:{ password:{"bcrypt" : "$2a$10$I3erQ084OiyILTv8ybtQ4ON6wusgPbMZ6.P33zzSDei.BbDL.Q4EO"}}});
  107. }
  108. }
  109. });
  110. }