From a999f0dec3b9ec12f8fe605c6d08d226c4d87ae8 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Fri, 19 Dec 2014 14:18:27 +0000 Subject: [PATCH 1/7] Don't ratelimit room create events --- synapse/handlers/directory.py | 2 +- synapse/handlers/message.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/directory.py b/synapse/handlers/directory.py index 66d3b533d9..a907a66e12 100644 --- a/synapse/handlers/directory.py +++ b/synapse/handlers/directory.py @@ -155,4 +155,4 @@ class DirectoryHandler(BaseHandler): "room_id": room_id, "sender": user_id, "content": {"aliases": aliases}, - }) + }, ratelimit=False) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 49c0e98113..01a7183548 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -106,7 +106,7 @@ class MessageHandler(BaseHandler): defer.returnValue(chunk) @defer.inlineCallbacks - def create_and_send_event(self, event_dict): + def create_and_send_event(self, event_dict, ratelimit=True): """ Given a dict from a client, create and handle a new event. Creates an FrozenEvent object, filling out auth_events, prev_events, @@ -123,7 +123,8 @@ class MessageHandler(BaseHandler): self.validator.validate_new(builder) - self.ratelimit(builder.user_id) + if ratelimit: + self.ratelimit(builder.user_id) # TODO(paul): Why does 'event' not have a 'user' object? user = self.hs.parse_userid(builder.user_id) assert self.hs.is_mine(user), "User must be our own: %s" % (user,) From f70e622d59e7b97c539ee03ffc02315b4d626b00 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Fri, 19 Dec 2014 14:30:57 +0000 Subject: [PATCH 2/7] bump_presence_active_time when sending a message event --- synapse/handlers/message.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/synapse/handlers/message.py b/synapse/handlers/message.py index 01a7183548..854b2c73c6 100644 --- a/synapse/handlers/message.py +++ b/synapse/handlers/message.py @@ -19,6 +19,7 @@ from synapse.api.constants import EventTypes, Membership from synapse.api.errors import RoomError from synapse.streams.config import PaginationConfig from synapse.events.validator import EventValidator +from synapse.util.logcontext import PreserveLoggingContext from ._base import BaseHandler @@ -153,6 +154,11 @@ class MessageHandler(BaseHandler): context=context, ) + if event.type == EventTypes.Message: + presence = self.hs.get_handlers().presence_handler + with PreserveLoggingContext(): + presence.bump_presence_active_time(user) + defer.returnValue(event) @defer.inlineCallbacks From 9c71d945d6072323df6d1e183efc4b6f7ad35237 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Fri, 19 Dec 2014 15:16:48 +0000 Subject: [PATCH 3/7] Look for name, topic in the event content rather than the event itself when persisting room name and topic events --- synapse/storage/room.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/synapse/storage/room.py b/synapse/storage/room.py index 2378d65943..c20abbfe4d 100644 --- a/synapse/storage/room.py +++ b/synapse/storage/room.py @@ -135,26 +135,26 @@ class RoomStore(SQLBaseStore): defer.returnValue(ret) def _store_room_topic_txn(self, txn, event): - if hasattr(event, "topic"): + if hasattr(event, "content") and "topic" in event.content: self._simple_insert_txn( txn, "topics", { "event_id": event.event_id, "room_id": event.room_id, - "topic": event.topic, + "topic": event.content["topic"], } ) def _store_room_name_txn(self, txn, event): - if hasattr(event, "name"): + if hasattr(event, "content") and "name" in event.content: self._simple_insert_txn( txn, "room_names", { "event_id": event.event_id, "room_id": event.room_id, - "name": event.name, + "name": event.content["name"], } ) From efd27ff01b201c2e9f52cc90daccbda72709482e Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Fri, 19 Dec 2014 15:31:27 +0000 Subject: [PATCH 4/7] Set a state_key for the topic and room name, otherwise they won't be treated as room state --- synapse/handlers/room.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 9644cd3d34..deefc3c11e 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -130,6 +130,7 @@ class RoomCreationHandler(BaseHandler): "type": EventTypes.Name, "room_id": room_id, "sender": user_id, + "state_key": "", "content": {"name": name}, }) @@ -139,6 +140,7 @@ class RoomCreationHandler(BaseHandler): "type": EventTypes.Topic, "room_id": room_id, "sender": user_id, + "state_key": "", "content": {"topic": topic}, }) From 67a406a7540f21aae2fde325c66422cffa27fbfa Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 19 Dec 2014 17:36:33 +0000 Subject: [PATCH 5/7] Rate limit display names and avatar urls per request rather than per event. --- synapse/handlers/profile.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/profile.py b/synapse/handlers/profile.py index 33a2c167ec..3f11e2dcf4 100644 --- a/synapse/handlers/profile.py +++ b/synapse/handlers/profile.py @@ -194,6 +194,8 @@ class ProfileHandler(BaseHandler): if not self.hs.is_mine(user): return + self.ratelimit(user.to_string()) + joins = yield self.store.get_rooms_for_user_where_membership_is( user.to_string(), [Membership.JOIN], @@ -214,5 +216,5 @@ class ProfileHandler(BaseHandler): "room_id": j.room_id, "state_key": j.state_key, "content": content, - "sender": j.state_key, - }) + "sender": j.state_key + }, ratelimit=False) From 2a5b53bc4a2c2ece46d40921707880d28164b876 Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Fri, 19 Dec 2014 17:39:29 +0000 Subject: [PATCH 6/7] more changelogs --- CHANGES.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 23bdac6a8a..813ad364ea 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,12 @@ Changes in synapse 0.6.0 (2014-12-16) ===================================== * Add new API for media upload and download that supports thumbnailing. + * Replicate media uploads over multiple homeservers so media is always served + to clients from their local homeserver. This obsoletes the + --content-addr parameter and confusion over accessing content directly + from remote homeservers. + * Implement exponential backoff when retrying federation requests when + sending to remote homeservers which are offline. * Implement typing notifications. * Fix bugs where we sent events with invalid signatures due to bugs where we incorrectly persisted events. From 4640239d3480bcc5c37454d45d12a821a405c541 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 19 Dec 2014 17:49:39 +0000 Subject: [PATCH 7/7] Mock ratelimiter to make tests pass. --- tests/handlers/test_presencelike.py | 11 ++++++++--- tests/handlers/test_profile.py | 9 ++++++++- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/handlers/test_presencelike.py b/tests/handlers/test_presencelike.py index 532ecf0f2c..0584e4c8b9 100644 --- a/tests/handlers/test_presencelike.py +++ b/tests/handlers/test_presencelike.py @@ -19,7 +19,7 @@ presence and profiles; namely, the displayname and avatar_url.""" from tests import unittest from twisted.internet import defer -from mock import Mock, call, ANY +from mock import Mock, call, ANY, NonCallableMock from ..utils import MockClock, MockKey @@ -75,8 +75,13 @@ class PresenceProfilelikeDataTestCase(unittest.TestCase): resource_for_federation=Mock(), http_client=None, replication_layer=MockReplication(), - config=self.mock_config, - ) + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]), + config=self.mock_config + ) + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) hs.handlers = PresenceAndProfileHandlers(hs) self.datastore = hs.get_datastore() diff --git a/tests/handlers/test_profile.py b/tests/handlers/test_profile.py index 1660e7e928..25b172aa5e 100644 --- a/tests/handlers/test_profile.py +++ b/tests/handlers/test_profile.py @@ -17,7 +17,7 @@ from tests import unittest from twisted.internet import defer -from mock import Mock +from mock import Mock, NonCallableMock from synapse.api.errors import AuthError from synapse.server import HomeServer @@ -59,7 +59,14 @@ class ProfileTestCase(unittest.TestCase): resource_for_federation=Mock(), replication_layer=self.mock_federation, config=self.mock_config, + ratelimiter=NonCallableMock(spec_set=[ + "send_message", + ]) ) + + self.ratelimiter = hs.get_ratelimiter() + self.ratelimiter.send_message.return_value = (True, 0) + hs.handlers = ProfileHandlers(hs) self.store = hs.get_datastore()