Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add support for filters in paginate_room_events
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston committed Jul 14, 2016
1 parent e5142f6 commit d554ca5
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion synapse/storage/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,50 @@ def upper_bound(token, engine, inclusive=True):
)


def filter_to_clause(event_filter):
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)

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)

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):
Expand Down Expand Up @@ -320,7 +364,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.
Expand All @@ -344,6 +388,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 ?"
Expand Down

0 comments on commit d554ca5

Please sign in to comment.