ChatManager.js 3.2 KB

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