summary refs log tree commit diff
path: root/synapse/storage/room.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-10-19 13:37:15 +0100
committerErik Johnston <erik@matrix.org>2015-10-19 13:37:15 +0100
commite0bf0258eeef08bac0930652a3557fddc1021659 (patch)
treed23be2d3097493e190a0c320528a730d99e9f20d /synapse/storage/room.py
parentMerge pull request #313 from matrix-org/paul/tiny-fixes (diff)
parentExplicitly check for Sqlite3Engine (diff)
downloadsynapse-e0bf0258eeef08bac0930652a3557fddc1021659.tar.xz
Merge pull request #307 from matrix-org/erikj/search
Add basic search API
Diffstat (limited to 'synapse/storage/room.py')
-rw-r--r--synapse/storage/room.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/synapse/storage/room.py b/synapse/storage/room.py
index 5e07b7e0e5..13441fcdce 100644
--- a/synapse/storage/room.py
+++ b/synapse/storage/room.py
@@ -19,6 +19,7 @@ from synapse.api.errors import StoreError
 
 from ._base import SQLBaseStore
 from synapse.util.caches.descriptors import cachedInlineCallbacks
+from .engines import PostgresEngine, Sqlite3Engine
 
 import collections
 import logging
@@ -175,6 +176,10 @@ class RoomStore(SQLBaseStore):
                 },
             )
 
+            self._store_event_search_txn(
+                txn, event, "content.topic", event.content["topic"]
+            )
+
     def _store_room_name_txn(self, txn, event):
         if hasattr(event, "content") and "name" in event.content:
             self._simple_insert_txn(
@@ -187,6 +192,33 @@ class RoomStore(SQLBaseStore):
                 }
             )
 
+            self._store_event_search_txn(
+                txn, event, "content.name", event.content["name"]
+            )
+
+    def _store_room_message_txn(self, txn, event):
+        if hasattr(event, "content") and "body" in event.content:
+            self._store_event_search_txn(
+                txn, event, "content.body", event.content["body"]
+            )
+
+    def _store_event_search_txn(self, txn, event, key, value):
+        if isinstance(self.database_engine, PostgresEngine):
+            sql = (
+                "INSERT INTO event_search (event_id, room_id, key, vector)"
+                " VALUES (?,?,?,to_tsvector('english', ?))"
+            )
+        elif isinstance(self.database_engine, Sqlite3Engine):
+            sql = (
+                "INSERT INTO event_search (event_id, room_id, key, value)"
+                " VALUES (?,?,?,?)"
+            )
+        else:
+            # This should be unreachable.
+            raise Exception("Unrecognized database engine")
+
+        txn.execute(sql, (event.event_id, event.room_id, key, value,))
+
     @cachedInlineCallbacks()
     def get_room_name_and_aliases(self, room_id):
         def f(txn):