Source: components/membership-cache.js

  1. /**
  2. * Caches membership of virtual users to rooms in memory
  3. * and also stores the state of whether users are registered.
  4. */
  5. class MembershipCache {
  6. constructor () {
  7. this._membershipMap = {
  8. // room_id: { user_id: "join|invite|leave|ban|null" } null=unknown
  9. };
  10. this._registeredUsers = new Set();
  11. }
  12. /**
  13. * Get's the *cached* state of a user's membership for a room.
  14. * This DOES NOT check to verify the value is correct (i.e the
  15. * room may have state reset and left the user from the room).
  16. *
  17. * This only caches users from the appservice.
  18. *
  19. * @param {string}} roomId Room id to check the state of.
  20. * @param {string} userId The userid to check the state of.
  21. * @returns {string} The membership state of the user, e.g. "joined"
  22. */
  23. getMemberEntry(roomId, userId) {
  24. if (this._membershipMap[roomId] === undefined) {
  25. return null;
  26. }
  27. return this._membershipMap[roomId][userId];
  28. }
  29. /**
  30. * Set the *cached* state of a user's membership for a room.
  31. * Use this to optimise intents so that they do not attempt
  32. * to join a room if we know they are joined.
  33. * This DOES NOT set the actual membership of the room.
  34. *
  35. * This only caches users from the appservice.
  36. * @param {string} roomId Room id to set the state of.
  37. * @param {string} userId The userid to set the state of.
  38. * @param {string} membership The membership value to set for the user
  39. * e.g joined.
  40. */
  41. setMemberEntry(roomId, userId, membership) {
  42. if (this._membershipMap[roomId] === undefined) {
  43. this._membershipMap[roomId] = {};
  44. }
  45. // Bans and invites do not mean the user exists.
  46. if (membership === "join" || membership === "leave") {
  47. this._registeredUsers.add(userId);
  48. }
  49. this._membershipMap[roomId][userId] = membership;
  50. }
  51. isUserRegistered(userId) {
  52. return this._registeredUsers.has(userId);
  53. }
  54. }
  55. module.exports = MembershipCache;