-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Feature: Add filter to /messages. Add 'contains_url' to filter. #922
Changes from all commits
b64aa6d
e5142f6
d554ca5
a98d215
f91faf0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Copyright 2016 OpenMarket Ltd | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from synapse.storage.prepare_database import get_statements | ||
|
||
import logging | ||
import ujson | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
ALTER_TABLE = """ | ||
ALTER TABLE events ADD COLUMN sender TEXT; | ||
ALTER TABLE events ADD COLUMN contains_url BOOLEAN; | ||
""" | ||
|
||
|
||
def run_create(cur, database_engine, *args, **kwargs): | ||
for statement in get_statements(ALTER_TABLE.splitlines()): | ||
cur.execute(statement) | ||
|
||
cur.execute("SELECT MIN(stream_ordering) FROM events") | ||
rows = cur.fetchall() | ||
min_stream_id = rows[0][0] | ||
|
||
cur.execute("SELECT MAX(stream_ordering) FROM events") | ||
rows = cur.fetchall() | ||
max_stream_id = rows[0][0] | ||
|
||
if min_stream_id is not None and max_stream_id is not None: | ||
progress = { | ||
"target_min_stream_id_inclusive": min_stream_id, | ||
"max_stream_id_exclusive": max_stream_id + 1, | ||
"rows_inserted": 0, | ||
} | ||
progress_json = ujson.dumps(progress) | ||
|
||
sql = ( | ||
"INSERT into background_updates (update_name, progress_json)" | ||
" VALUES (?, ?)" | ||
) | ||
|
||
sql = database_engine.convert_param_style(sql) | ||
|
||
cur.execute(sql, ("event_fields_sender_url", progress_json)) | ||
|
||
|
||
def run_upgrade(cur, database_engine, *args, **kwargs): | ||
pass |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -95,6 +95,54 @@ def upper_bound(token, engine, inclusive=True): | |||
) | ||||
|
||||
|
||||
def filter_to_clause(event_filter): | ||||
# NB: This may create SQL clauses that don't optimise well (and we don't | ||||
# have indices on all possible clauses). E.g. it may create | ||||
# "room_id == X AND room_id != X", which postgres doesn't optimise. | ||||
|
||||
if not event_filter: | ||||
return "", [] | ||||
|
||||
clauses = [] | ||||
args = [] | ||||
|
||||
if event_filter.types: | ||||
clauses.append( | ||||
"(%s)" % " OR ".join("type = ?" for _ in event_filter.types) | ||||
) | ||||
args.extend(event_filter.types) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We currently allow wildcard matches for event types. synapse/synapse/api/filtering.py Line 235 in a98d215
|
||||
|
||||
for typ in event_filter.not_types: | ||||
clauses.append("type != ?") | ||||
args.append(typ) | ||||
|
||||
if event_filter.senders: | ||||
clauses.append( | ||||
"(%s)" % " OR ".join("sender = ?" for _ in event_filter.senders) | ||||
) | ||||
args.extend(event_filter.senders) | ||||
|
||||
for sender in event_filter.not_senders: | ||||
clauses.append("sender != ?") | ||||
args.append(sender) | ||||
|
||||
if event_filter.rooms: | ||||
clauses.append( | ||||
"(%s)" % " OR ".join("room_id = ?" for _ in event_filter.rooms) | ||||
) | ||||
args.extend(event_filter.rooms) | ||||
|
||||
for room_id in event_filter.not_rooms: | ||||
clauses.append("room_id != ?") | ||||
args.append(room_id) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth adding the rooms filter for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rather have this function do the expected thing and filter on room_ids too, so its usable elsewhere. Separately I guess we could check that the room_id matches the filter, but even if it doesn't I think the query would be relatively quick There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I'd be impressed if postgres had a hard time optimising There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sighs |
||||
|
||||
if event_filter.contains_url: | ||||
clauses.append("contains_url = ?") | ||||
args.append(event_filter.contains_url) | ||||
|
||||
return " AND ".join(clauses), args | ||||
|
||||
|
||||
class StreamStore(SQLBaseStore): | ||||
@defer.inlineCallbacks | ||||
def get_appservice_room_stream(self, service, from_key, to_key, limit=0): | ||||
|
@@ -320,7 +368,7 @@ def f(txn): | |||
|
||||
@defer.inlineCallbacks | ||||
def paginate_room_events(self, room_id, from_key, to_key=None, | ||||
direction='b', limit=-1): | ||||
direction='b', limit=-1, event_filter=None): | ||||
# Tokens really represent positions between elements, but we use | ||||
# the convention of pointing to the event before the gap. Hence | ||||
# we have a bit of asymmetry when it comes to equalities. | ||||
|
@@ -344,6 +392,12 @@ def paginate_room_events(self, room_id, from_key, to_key=None, | |||
RoomStreamToken.parse(to_key), self.database_engine | ||||
)) | ||||
|
||||
filter_clause, filter_args = filter_to_clause(event_filter) | ||||
|
||||
if filter_clause: | ||||
bounds += " AND " + filter_clause | ||||
args.extend(filter_args) | ||||
|
||||
if int(limit) > 0: | ||||
args.append(int(limit)) | ||||
limit_str = " LIMIT ?" | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add an index on (room_id, contains_url, ...whatever columns the pagination queries can by indexed against)?
It's probably not necessary if if people are posting things with URLs frequently enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, though I'm slightly loathed to put tooooo many indices on the events table. I'd also want to check what actual queries are common, as e.g. file pagination would require a
(room_id, topological, stream_ordering, contains_url)
indexThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you mean a
(room_id, contains_url, topological, stream_ordering)
index?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Err, yes, quite.