Source: models/events/event.js

  1. /*
  2. Copyright 2019 The Matrix.org Foundation C.I.C.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * Create a store event.
  15. * @constructor
  16. * @param {string} roomId The matrix room ID
  17. * @param {string} eventId The matrix event ID
  18. * @param {string} remoteRoomId The remote room ID
  19. * @param {string} remoteEventId The remote event ID
  20. * @param {any} extras Any extra data that may be included with the event.
  21. */
  22. function StoredEvent(roomId, eventId, remoteRoomId, remoteEventId, extras) {
  23. this.roomId = roomId;
  24. this.eventId = eventId;
  25. this.remoteRoomId = remoteRoomId;
  26. this.remoteEventId = remoteEventId;
  27. this._extras = extras || {};
  28. }
  29. /**
  30. * Get the unique ID.
  31. * @return {string} A unique ID
  32. */
  33. StoredEvent.prototype.getId = function() {
  34. return this.eventId + this.remoteEventId;
  35. };
  36. /**
  37. * Get the matrix room ID.
  38. * @return {string} The room ID
  39. */
  40. StoredEvent.prototype.getMatrixRoomId = function() {
  41. return this.roomId;
  42. };
  43. /**
  44. * Get the matrix event ID.
  45. * @return {string} The event ID
  46. */
  47. StoredEvent.prototype.getMatrixEventId = function() {
  48. return this.eventId;
  49. };
  50. /**
  51. * Get the remote room ID.
  52. * @return {string} The remote room ID
  53. */
  54. StoredEvent.prototype.getRemoteRoomId = function() {
  55. return this.remoteRoomId;
  56. };
  57. /**
  58. * Get the remote event ID.
  59. * @return {string} The remote event ID
  60. */
  61. StoredEvent.prototype.getRemoteEventId = function() {
  62. return this.remoteEventId;
  63. };
  64. /**
  65. * Get the data value for the given key.
  66. * @param {string} key An arbitrary bridge-specific key.
  67. * @return {*} Stored data for this key. May be undefined.
  68. */
  69. StoredEvent.prototype.get = function(key) {
  70. return this._extras[key];
  71. };
  72. /**
  73. * Set an arbitrary bridge-specific data value for this event. This will be serailized
  74. * under an 'extras' key.
  75. * @param {string} key The key to store the data value under.
  76. * @param {*} val The data value. This value should be serializable via
  77. * <code>JSON.stringify(data)</code>.
  78. */
  79. StoredEvent.prototype.set = function(key, val) {
  80. this._extras[key] = val;
  81. };
  82. /**
  83. * Serialize data about this event into a JSON object.
  84. * @return {Object} The serialised data
  85. */
  86. StoredEvent.prototype.serialize = function() {
  87. return {
  88. id: this.getId(),
  89. matrix: {
  90. roomId: this.roomId,
  91. eventId: this.eventId,
  92. },
  93. remote: {
  94. roomId: this.remoteRoomId,
  95. eventId: this.remoteEventId,
  96. },
  97. extras: this._extras,
  98. };
  99. };
  100. /**
  101. * Set data about this event from a serialized data object.
  102. * @param {Object} data The serialized data
  103. */
  104. StoredEvent.deserialize = function(data) {
  105. return new StoredEvent(
  106. data.matrix.roomId,
  107. data.matrix.eventId,
  108. data.remote.roomId,
  109. data.remote.eventId,
  110. data.extras
  111. );
  112. };
  113. module.exports = StoredEvent;