Source: components/event-bridge-store.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. const BridgeStore = require("./bridge-store");
  14. const StoredEvent = require("../models/events/event");
  15. const util = require("util");
  16. /**
  17. * Construct a store suitable for event mapping information. Data is stored
  18. * as {@link StoredEvent}s.
  19. * @constructor
  20. * @param {Datastore} db The connected NEDB database instance
  21. */
  22. function EventBridgeStore(db) {
  23. this.db = db;
  24. }
  25. util.inherits(EventBridgeStore, BridgeStore);
  26. /**
  27. * Insert an event, clobbering based on the ID of the StoredEvent.
  28. * @param {StoredEvent} event
  29. * @return {Promise}
  30. */
  31. EventBridgeStore.prototype.upsertEvent = function(event) {
  32. return this.upsert({
  33. id: event.getId()
  34. }, event.serialize());
  35. }
  36. /**
  37. * Get an existing event based on the provided matrix IDs.
  38. * @param {string} roomId The ID of the room.
  39. * @param {string} eventId The ID of the event.
  40. * @return {?StoredEvent} A promise which resolves to the StoredEvent or null.
  41. */
  42. EventBridgeStore.prototype.getEntryByMatrixId = function(roomId, eventId) {
  43. return this.selectOne({
  44. "matrix.roomId": roomId,
  45. "matrix.eventId": eventId,
  46. }, this.convertTo(function(doc) {
  47. return StoredEvent.deserialize(doc);
  48. }));
  49. }
  50. /**
  51. * Get an existing event based on the provided remote IDs.
  52. * @param {string} roomId The ID of the room.
  53. * @param {string} eventId The ID of the event.
  54. * @return {?StoredEvent} A promise which resolves to the StoredEvent or null.
  55. */
  56. EventBridgeStore.prototype.getEntryByRemoteId = function(roomId, eventId) {
  57. return this.selectOne({
  58. "remote.roomId": roomId,
  59. "remote.eventId": eventId,
  60. }, this.convertTo(function(doc) {
  61. return StoredEvent.deserialize(doc);
  62. }));
  63. }
  64. /**
  65. * Remove entries based on the event data.
  66. * @param {StoredEvent} event The event to remove.
  67. * @return {Promise}
  68. */
  69. EventBridgeStore.prototype.removeEvent = function(event) {
  70. return this.delete({
  71. id: event.getId(),
  72. });
  73. };
  74. /**
  75. * Remove entries based on the matrix IDs.
  76. * @param {string} roomId The ID of the room.
  77. * @param {string} eventId The ID of the event.
  78. * @return {Promise}
  79. */
  80. EventBridgeStore.prototype.removeEventByMatrixId = function(roomId, eventId) {
  81. return this.delete({
  82. "matrix.roomId": roomId,
  83. "matrix.eventId": eventId,
  84. });
  85. };
  86. /**
  87. * Remove entries based on the matrix IDs.
  88. * @param {string} roomId The ID of the room.
  89. * @param {string} eventId The ID of the event.
  90. * @return {Promise}
  91. */
  92. EventBridgeStore.prototype.removeEventByRemoteId = function(roomId, eventId) {
  93. return this.delete({
  94. "remote.roomId": roomId,
  95. "remote.eventId": eventId,
  96. });
  97. };
  98. module.exports = EventBridgeStore;