summary refs log tree commit diff
path: root/synapse/handlers/_base.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-10-16 15:18:42 +0100
committerErik Johnston <erik@matrix.org>2015-10-16 15:18:42 +0100
commit23ed7dc0e711891cff62ca362eb7fbfd5782c68d (patch)
tree10e885b6149118268287c6933dfd02f6328ae204 /synapse/handlers/_base.py
parentMerge branch 'develop' of github.com:matrix-org/synapse into erikj/search (diff)
parentMerge pull request #309 from matrix-org/erikj/_filter_events_for_client (diff)
downloadsynapse-23ed7dc0e711891cff62ca362eb7fbfd5782c68d.tar.xz
Merge branch 'develop' of github.com:matrix-org/synapse into erikj/search
Diffstat (limited to 'synapse/handlers/_base.py')
-rw-r--r--synapse/handlers/_base.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/synapse/handlers/_base.py b/synapse/handlers/_base.py
index c488ee0f6d..ee2d571329 100644
--- a/synapse/handlers/_base.py
+++ b/synapse/handlers/_base.py
@@ -45,6 +45,52 @@ class BaseHandler(object):
 
         self.event_builder_factory = hs.get_event_builder_factory()
 
+    @defer.inlineCallbacks
+    def _filter_events_for_client(self, user_id, events):
+        event_id_to_state = yield self.store.get_state_for_events(
+            frozenset(e.event_id for e in events),
+            types=(
+                (EventTypes.RoomHistoryVisibility, ""),
+                (EventTypes.Member, user_id),
+            )
+        )
+
+        def allowed(event, state):
+            if event.type == EventTypes.RoomHistoryVisibility:
+                return True
+
+            membership_ev = state.get((EventTypes.Member, user_id), None)
+            if membership_ev:
+                membership = membership_ev.membership
+            else:
+                membership = Membership.LEAVE
+
+            if membership == Membership.JOIN:
+                return True
+
+            history = state.get((EventTypes.RoomHistoryVisibility, ''), None)
+            if history:
+                visibility = history.content.get("history_visibility", "shared")
+            else:
+                visibility = "shared"
+
+            if visibility == "public":
+                return True
+            elif visibility == "shared":
+                return True
+            elif visibility == "joined":
+                return membership == Membership.JOIN
+            elif visibility == "invited":
+                return membership == Membership.INVITE
+
+            return True
+
+        defer.returnValue([
+            event
+            for event in events
+            if allowed(event, event_id_to_state[event.event_id])
+        ])
+
     def ratelimit(self, user_id):
         time_now = self.clock.time()
         allowed, time_allowed = self.ratelimiter.send_message(