ChatManager.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. ChatManager = class ChatManager {
  2. /**
  3. * Constructor.
  4. *
  5. * @param {Collection} chats
  6. * A Chats collection instance.
  7. *
  8. * @returns {void}
  9. */
  10. constructor(chats) {
  11. this.chats = chats;
  12. }
  13. /**
  14. * Order two comparable objects as min, max
  15. *
  16. * @param {Object} s1
  17. * @param {Object} s2
  18. * @returns {*[]}
  19. * An array of the two objects, minimum first, maximum last.
  20. */
  21. static ordered(s1, s2) {
  22. let id1;
  23. let id2;
  24. // Ensure ordering as id1 <= id2. Equality should not happen, but handle it correctly nonetheless.
  25. if (s1 <= s2) {
  26. id1 = s1;
  27. id2 = s2;
  28. }
  29. else {
  30. id1 = s2;
  31. id2 = s1;
  32. }
  33. return [id1, id2];
  34. }
  35. /**
  36. * Find the id of a chat between two users.
  37. *
  38. * @param {String} myId
  39. * The current user id.
  40. * @param {String} otherUserId
  41. * The id of an other user.
  42. * @param {Session} session
  43. * The session global.
  44. *
  45. * @returns {void}
  46. */
  47. assignChatId(myId, otherUserId, session) {
  48. let [user1Id, user2Id] = ChatManager.ordered(myId, otherUserId);
  49. // Since ids are ordered, we do not need a $or.
  50. const chat = this.chats.findOne({ user1Id, user2Id });
  51. // Meteor._debug(filter, chat);
  52. // No chat matching the filter - need to insert a new one.
  53. if (!chat) {
  54. Meteor.call("chats.insert", { user1Id, user2Id }, function (err, chatId) {
  55. if (err) {
  56. throw new Meteor.Error("access-denied", "Could not insert new chat.");
  57. }
  58. session.set("chatId", chatId);
  59. console.log("-- No chat found between " + user1Id + " and " + user2Id + ". Created " + chatId);
  60. });
  61. }
  62. // There is a chat going already - use that.
  63. else {
  64. const chatId = chat._id;
  65. session.set("chatId", chatId);
  66. console.log("-- Chat " + chatId + " found between " + user1Id + " and " + user2Id + ".");
  67. }
  68. }
  69. /**
  70. * Meteor Method: insert a chat.
  71. *
  72. * @param {Object} chat
  73. * A chat object, with user1Id, user2Id, and messages keys.
  74. * @returns {String}
  75. * The id of the inserted chat document.
  76. */
  77. static insertMethod(chat) {
  78. check(chat, {
  79. user1Id: String,
  80. user2Id: String
  81. });
  82. chat.messages = [];
  83. const chatId = Chats.insert(chat);
  84. // Meteor._debug("chats.insert", chat, chatId);
  85. return chatId;
  86. }
  87. /**
  88. * Meteor Method: push a message to a chat.
  89. *
  90. * @param {Object} chat
  91. * A chat object, with user1Id, user2Id, and messages keys.
  92. * @param {String} chatValue
  93. * The message to push.
  94. */
  95. static pushMessageMethod(chat, chatValue) {
  96. check(chat, {
  97. _id: String,
  98. user1Id: String,
  99. user2Id: String,
  100. messages: Array
  101. });
  102. // is a good idea to insert data straight from the form
  103. // (i.e. the user) into the database?? certainly not.
  104. // push adds the message to the end of the array
  105. const changes = {
  106. $push: {
  107. messages: {
  108. text: chatValue,
  109. sender: this.userId
  110. }
  111. }
  112. };
  113. // update the chat object in the database.
  114. Chats.update(chat._id, changes);
  115. }
  116. };