From 436b3c7d0c93bd6019a576425506c4c8f2963632 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Mon, 1 Sep 2014 17:54:54 +0100 Subject: [PATCH 001/111] Ratelimiter object --- synapse/api/ratelimiting.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 synapse/api/ratelimiting.py diff --git a/synapse/api/ratelimiting.py b/synapse/api/ratelimiting.py new file mode 100644 index 0000000000..a730abd59b --- /dev/null +++ b/synapse/api/ratelimiting.py @@ -0,0 +1,36 @@ +import collections + + +class Ratelimiter(object): + + def __init__(self): + self.message_counts = collections.OrderedDict() + + def prune_message_counts(self, time_now): + for user_id in self.message_counts.keys(): + message_count, time_start, msg_rate_hz = ( + self.message_counts[user_id] + ) + time_delta = time_now - time_start + if message_count - time_delta * msg_rate_hz > 0: + break + else: + del self.message_counts[user_id] + + def send_message(self, user_id, time_now, msg_rate_hz, burst_count): + self.prune_message_counts(time_now) + message_count, time_start, _ignored = self.message_counts.pop( + user_id, (0., time_now, None), + ) + time_delta = time_now - time_start + if message_count - time_delta * msg_rate_hz < 0: + a + if message_count - (time_now - time_start) * msg_rate_hz > burst_count: + allowed = False + else: + allowed = True + message_count += 1 + self.message_counts[user_id] = ( + message_count, time_start, msg_rate_hz + ) + return allowed From dd2cd9312a9f9f8f782da9ed8f99544176ea9fc1 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Tue, 2 Sep 2014 15:06:20 +0100 Subject: [PATCH 002/111] Test ratelimiter --- synapse/api/ratelimiting.py | 67 ++++++++++++++++++++++++---------- tests/api/__init__.py | 0 tests/api/test_ratelimiting.py | 39 ++++++++++++++++++++ 3 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 tests/api/__init__.py create mode 100644 tests/api/test_ratelimiting.py diff --git a/synapse/api/ratelimiting.py b/synapse/api/ratelimiting.py index a730abd59b..ab26c2376a 100644 --- a/synapse/api/ratelimiting.py +++ b/synapse/api/ratelimiting.py @@ -2,35 +2,64 @@ import collections class Ratelimiter(object): + """ + Ratelimit message sending by user. + """ def __init__(self): self.message_counts = collections.OrderedDict() - def prune_message_counts(self, time_now): - for user_id in self.message_counts.keys(): - message_count, time_start, msg_rate_hz = ( - self.message_counts[user_id] - ) - time_delta = time_now - time_start - if message_count - time_delta * msg_rate_hz > 0: - break - else: - del self.message_counts[user_id] - - def send_message(self, user_id, time_now, msg_rate_hz, burst_count): - self.prune_message_counts(time_now) + def send_message(self, user_id, time_now_s, msg_rate_hz, burst_count): + """Can the user send a message? + Args: + user_id: The user sending a message. + time_now_s: The time now. + msg_rate_hz: The long term number of messages a user can send in a + second. + burst_count: How many messages the user can send before being + limited. + Returns: + A pair of a bool indicating if they can send a message now and a + time in seconds of when they can next send a message. + """ + self.prune_message_counts(time_now_s) message_count, time_start, _ignored = self.message_counts.pop( - user_id, (0., time_now, None), + user_id, (0., time_now_s, None), ) - time_delta = time_now - time_start - if message_count - time_delta * msg_rate_hz < 0: - a - if message_count - (time_now - time_start) * msg_rate_hz > burst_count: + time_delta = time_now_s - time_start + sent_count = message_count - time_delta * msg_rate_hz + if sent_count < 0: + allowed = True + time_start = time_now_s + messagecount = 1. + elif sent_count > burst_count - 1.: allowed = False else: allowed = True message_count += 1 + self.message_counts[user_id] = ( message_count, time_start, msg_rate_hz ) - return allowed + + if msg_rate_hz > 0: + time_allowed = ( + time_start + (message_count - burst_count + 1) / msg_rate_hz + ) + if time_allowed < time_now_s: + time_allowed = time_now_s + else: + time_allowed = -1 + + return allowed, time_allowed + + def prune_message_counts(self, time_now_s): + for user_id in self.message_counts.keys(): + message_count, time_start, msg_rate_hz = ( + self.message_counts[user_id] + ) + time_delta = time_now_s - time_start + if message_count - time_delta * msg_rate_hz > 0: + break + else: + del self.message_counts[user_id] diff --git a/tests/api/__init__.py b/tests/api/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/api/test_ratelimiting.py b/tests/api/test_ratelimiting.py new file mode 100644 index 0000000000..dc2f83c7eb --- /dev/null +++ b/tests/api/test_ratelimiting.py @@ -0,0 +1,39 @@ +from synapse.api.ratelimiting import Ratelimiter + +import unittest + +class TestRatelimiter(unittest.TestCase): + + def test_allowed(self): + limiter = Ratelimiter() + allowed, time_allowed = limiter.send_message( + user_id="test_id", time_now_s=0, msg_rate_hz=0.1, burst_count=1, + ) + self.assertTrue(allowed) + self.assertEquals(10., time_allowed) + + allowed, time_allowed = limiter.send_message( + user_id="test_id", time_now_s=5, msg_rate_hz=0.1, burst_count=1, + ) + self.assertFalse(allowed) + self.assertEquals(10., time_allowed) + + allowed, time_allowed = limiter.send_message( + user_id="test_id", time_now_s=10, msg_rate_hz=0.1, burst_count=1 + ) + self.assertTrue(allowed) + self.assertEquals(20., time_allowed) + + def test_pruning(self): + limiter = Ratelimiter() + allowed, time_allowed = limiter.send_message( + user_id="test_id_1", time_now_s=0, msg_rate_hz=0.1, burst_count=1, + ) + + self.assertIn("test_id_1", limiter.message_counts) + + allowed, time_allowed = limiter.send_message( + user_id="test_id_2", time_now_s=10, msg_rate_hz=0.1, burst_count=1 + ) + + self.assertNotIn("test_id_1", limiter.message_counts) From b4984d5e152e5a6d4998b08c30d714faefa1b785 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 2 Sep 2014 15:29:38 +0100 Subject: [PATCH 003/111] Updated howto.rst to use the new APIs. Updated JSFiddles to use 8008. Linked new fiddles with howto.rst. Added more explanations. --- docs/client-server/howto.rst | 535 ++++++++++++++++++----- jsfiddles/create_room_send_msg/demo.html | 2 +- jsfiddles/create_room_send_msg/demo.js | 8 +- jsfiddles/event_stream/demo.html | 2 +- jsfiddles/event_stream/demo.js | 8 +- jsfiddles/example_app/demo.html | 2 +- jsfiddles/example_app/demo.js | 28 +- jsfiddles/register_login/demo.html | 2 +- jsfiddles/register_login/demo.js | 8 +- jsfiddles/room_memberships/demo.html | 2 +- jsfiddles/room_memberships/demo.js | 10 +- 11 files changed, 467 insertions(+), 140 deletions(-) diff --git a/docs/client-server/howto.rst b/docs/client-server/howto.rst index 3660c73d36..54b80e7dea 100644 --- a/docs/client-server/howto.rst +++ b/docs/client-server/howto.rst @@ -1,6 +1,4 @@ -TODO(kegan): Tweak joinalias API keys/path? Event stream historical > live needs -a token (currently doesn't). im/sync responses include outdated event formats -(room membership change messages). Room config (specifically: message history, +TODO(kegan): Room config (specifically: message history, public rooms). /register seems super simplistic compared to /login, maybe it would be better if /register used the same technique as /login? /register should be "user" not "user_id". @@ -15,7 +13,7 @@ implementation, there may be variations in relation to registering/logging in which are not covered in extensive detail in this guide. If you haven't already, get a home server up and running on -``http://localhost:8080``. +``http://localhost:8008``. Accounts @@ -23,14 +21,14 @@ Accounts Before you can send and receive messages, you must **register** for an account. If you already have an account, you must **login** into it. -**Try out the fiddle: http://jsfiddle.net/jrf1h02d/** +**Try out the fiddle: http://jsfiddle.net/4q2jyxng/** Registration ------------ The aim of registration is to get a user ID and access token which you will need when accessing other APIs:: - curl -XPOST -d '{"user_id":"example", "password":"wordpass"}' "http://localhost:8080/_matrix/client/api/v1/register" + curl -XPOST -d '{"user_id":"example", "password":"wordpass"}' "http://localhost:8008/_matrix/client/api/v1/register" { "access_token": "QGV4YW1wbGU6bG9jYWxob3N0.AqdSzFmFYrLrTmteXc", @@ -51,13 +49,17 @@ Login ----- The aim when logging in is to get an access token for your existing user ID:: - curl -XGET "http://localhost:8080/_matrix/client/api/v1/login" + curl -XGET "http://localhost:8008/_matrix/client/api/v1/login" { - "type": "m.login.password" + "flows": [ + { + "type": "m.login.password" + } + ] } - curl -XPOST -d '{"type":"m.login.password", "user":"example", "password":"wordpass"}' "http://localhost:8080/_matrix/client/api/v1/login" + curl -XPOST -d '{"type":"m.login.password", "user":"example", "password":"wordpass"}' "http://localhost:8008/_matrix/client/api/v1/login" { "access_token": "QGV4YW1wbGU6bG9jYWxob3N0.vRDLTgxefmKWQEtgGd", @@ -80,14 +82,14 @@ Communicating In order to communicate with another user, you must **create a room** with that user and **send a message** to that room. -**Try out the fiddle: http://jsfiddle.net/jnwqcshc/** +**Try out the fiddle: http://jsfiddle.net/zL3zto9g/** Creating a room --------------- If you want to send a message to someone, you have to be in a room with them. To create a room:: - curl -XPOST -d '{"room_alias_name":"tutorial"}' "http://localhost:8080/_matrix/client/api/v1/rooms?access_token=QGV4YW1wbGU6bG9jYWxob3N0.vRDLTgxefmKWQEtgGd" + curl -XPOST -d '{"room_alias_name":"tutorial"}' "http://localhost:8008/_matrix/client/api/v1/createRoom?access_token=YOUR_ACCESS_TOKEN" { "room_alias": "#tutorial:localhost", @@ -105,13 +107,19 @@ Sending messages ---------------- You can now send messages to this room:: - curl -XPUT -d '{"msgtype":"m.text", "body":"hello"}' "http://localhost:8080/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh:localhost/messages/%40example%3Alocalhost/msgid1?access_token=QGV4YW1wbGU6bG9jYWxob3N0.vRDLTgxefmKWQEtgGd" + curl -XPOST -d '{"msgtype":"m.text", "body":"hello"}' "http://localhost:8008/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh%3Alocalhost/send/m.room.message?access_token=YOUR_ACCESS_TOKEN" + + { + "event_id": "YUwRidLecu" + } + +The event ID returned is a unique ID which identifies this message. NB: There are no limitations to the types of messages which can be exchanged. -The only requirement is that ``"msgtype"`` is specified. - -NB: Depending on the room config, users who join the room may be able to see -message history from before they joined. +The only requirement is that ``"msgtype"`` is specified. The Matrix +specification outlines the following standard types: ``m.text``, ``m.image``, +``m.audio``, ``m.video``, ``m.location``, ``m.emote``. See the specification for +more information on these types. Users and rooms =============== @@ -121,33 +129,32 @@ these rules may specify if you require an **invitation** from someone already in the room in order to **join the room**. In addition, you may also be able to join a room **via a room alias** if one was set up. -**Try out the fiddle: http://jsfiddle.net/og1xokcr/** +**Try out the fiddle: http://jsfiddle.net/7fhotf1b/** Inviting a user to a room ------------------------- You can directly invite a user to a room like so:: - curl -XPUT -d '{"membership":"invite"}' "http://localhost:8080/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh:localhost/members/%40myfriend%3Alocalhost/state?access_token=QGV4YW1wbGU6bG9jYWxob3N0.vRDLTgxefmKWQEtgGd" + curl -XPOST -d '{"user_id":"@myfriend:localhost"}' "http://localhost:8008/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh%3Alocalhost/invite?access_token=YOUR_ACCESS_TOKEN" This informs ``@myfriend:localhost`` of the room ID ``!CvcvRuDYDzTOzfKKgh:localhost`` and allows them to join the room. Joining a room via an invite ---------------------------- -If you receive an invite, you can join the room by changing the membership to -join:: +If you receive an invite, you can join the room:: - curl -XPUT -d '{"membership":"join"}' "http://localhost:8080/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh:localhost/members/%40myfriend%3Alocalhost/state?access_token=QG15ZnJpZW5kOmxvY2FsaG9zdA...XKuGdVsovHmwMyDDvK" + curl -XPOST -d '{}' "http://localhost:8008/_matrix/client/api/v1/rooms/%21CvcvRuDYDzTOzfKKgh%3Alocalhost/join?access_token=YOUR_ACCESS_TOKEN" NB: Only the person invited (``@myfriend:localhost``) can change the membership -state to ``"join"``. +state to ``"join"``. Repeatedly joining a room does nothing. Joining a room via an alias --------------------------- Alternatively, if you know the room alias for this room and the room config allows it, you can directly join a room via the alias:: - curl -XPUT -d '{}' "http://localhost:8080/_matrix/client/api/v1/join/%23tutorial%3Alocalhost?access_token=QG15ZnJpZW5kOmxvY2FsaG9zdA...XKuGdVsovHmwMyDDvK" + curl -XPOST -d '{}' "http://localhost:8008/_matrix/client/api/v1/join/%23tutorial%3Alocalhost?access_token=YOUR_ACCESS_TOKEN" { "room_id": "!CvcvRuDYDzTOzfKKgh:localhost" @@ -166,128 +173,442 @@ An event is some interesting piece of data that a client may be interested in. It can be a message in a room, a room invite, etc. There are many different ways of getting events, depending on what the client already knows. -**Try out the fiddle: http://jsfiddle.net/5uk4dqe2/** +**Try out the fiddle: http://jsfiddle.net/vw11mg37/** Getting all state ----------------- If the client doesn't know any information on the rooms the user is invited/joined on, they can get all the user's state for all rooms:: - curl -XGET "http://localhost:8080/_matrix/client/api/v1/im/sync?access_token=QG15ZnJpZW5kOmxvY2FsaG9zdA...XKuGdVsovHmwMyDDvK" + curl -XGET "http://localhost:8008/_matrix/client/api/v1/initialSync?access_token=YOUR_ACCESS_TOKEN" - [ - { - "membership": "join", - "messages": { - "chunk": [ - { - "content": { - "body": "@example:localhost joined the room.", - "hsob_ts": 1408444664249, + { + "end": "s39_18_0", + "presence": [ + { + "content": { + "last_active_ago": 1061436, + "user_id": "@example:localhost" + }, + "type": "m.presence" + } + ], + "rooms": [ + { + "membership": "join", + "messages": { + "chunk": [ + { + "content": { + "@example:localhost": 10, + "default": 0 + }, + "event_id": "wAumPSTsWF", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.power_levels", + "user_id": "@example:localhost" + }, + { + "content": { + "join_rule": "public" + }, + "event_id": "jrLVqKHKiI", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.join_rules", + "user_id": "@example:localhost" + }, + { + "content": { + "level": 10 + }, + "event_id": "WpmTgsNWUZ", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.add_state_level", + "user_id": "@example:localhost" + }, + { + "content": { + "level": 0 + }, + "event_id": "qUMBJyKsTQ", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.send_event_level", + "user_id": "@example:localhost" + }, + { + "content": { + "ban_level": 5, + "kick_level": 5 + }, + "event_id": "YAaDmKvoUW", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.ops_levels", + "user_id": "@example:localhost" + }, + { + "content": { + "avatar_url": null, + "displayname": null, + "membership": "join" + }, + "event_id": "RJbPMtCutf", "membership": "join", - "membership_source": "@example:localhost", - "membership_target": "@example:localhost", - "msgtype": "m.text" + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@example:localhost", + "ts": 1409665586730, + "type": "m.room.member", + "user_id": "@example:localhost" }, - "event_id": "lZjmmlrEvo", - "msg_id": "m1408444664249", - "room_id": "!CvcvRuDYDzTOzfKKgh:localhost", - "type": "m.room.message", - "user_id": "_homeserver_" - }, + { + "content": { + "body": "hello", + "hsob_ts": 1409665660439, + "msgtype": "m.text" + }, + "event_id": "YUwRidLecu", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "ts": 1409665660439, + "type": "m.room.message", + "user_id": "@example:localhost" + }, + { + "content": { + "membership": "invite" + }, + "event_id": "YjNuBKnPsb", + "membership": "invite", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@myfriend:localhost", + "ts": 1409666426819, + "type": "m.room.member", + "user_id": "@example:localhost" + }, + { + "content": { + "avatar_url": null, + "displayname": null, + "membership": "join", + "prev": "join" + }, + "event_id": "KWwdDjNZnm", + "membership": "join", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@example:localhost", + "ts": 1409666551582, + "type": "m.room.member", + "user_id": "@example:localhost" + }, + { + "content": { + "avatar_url": null, + "displayname": null, + "membership": "join" + }, + "event_id": "JFLVteSvQc", + "membership": "join", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@example:localhost", + "ts": 1409666587265, + "type": "m.room.member", + "user_id": "@example:localhost" + } + ], + "end": "s39_18_0", + "start": "t1-11_18_0" + }, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state": [ { "content": { - "body": "hello", - "hsob_ts": 1408445405672, - "msgtype": "m.text" + "creator": "@example:localhost" }, - "event_id": "BiBJqamISg", - "msg_id": "msgid1", - "room_id": "!CvcvRuDYDzTOzfKKgh:localhost", - "type": "m.room.message", + "event_id": "dMUoqVTZca", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.create", "user_id": "@example:localhost" }, - [...] { "content": { - "body": "@myfriend:localhost joined the room.", - "hsob_ts": 1408446501661, - "membership": "join", - "membership_source": "@myfriend:localhost", - "membership_target": "@myfriend:localhost", - "msgtype": "m.text" + "@example:localhost": 10, + "default": 0 }, - "event_id": "IMmXbOzFAa", - "msg_id": "m1408446501661", - "room_id": "!CvcvRuDYDzTOzfKKgh:localhost", - "type": "m.room.message", - "user_id": "_homeserver_" + "event_id": "wAumPSTsWF", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.power_levels", + "user_id": "@example:localhost" + }, + { + "content": { + "join_rule": "public" + }, + "event_id": "jrLVqKHKiI", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.join_rules", + "user_id": "@example:localhost" + }, + { + "content": { + "level": 10 + }, + "event_id": "WpmTgsNWUZ", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.add_state_level", + "user_id": "@example:localhost" + }, + { + "content": { + "level": 0 + }, + "event_id": "qUMBJyKsTQ", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.send_event_level", + "user_id": "@example:localhost" + }, + { + "content": { + "ban_level": 5, + "kick_level": 5 + }, + "event_id": "YAaDmKvoUW", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.ops_levels", + "user_id": "@example:localhost" + }, + { + "content": { + "membership": "invite" + }, + "event_id": "YjNuBKnPsb", + "membership": "invite", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@myfriend:localhost", + "ts": 1409666426819, + "type": "m.room.member", + "user_id": "@example:localhost" + }, + { + "content": { + "avatar_url": null, + "displayname": null, + "membership": "join" + }, + "event_id": "JFLVteSvQc", + "membership": "join", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@example:localhost", + "ts": 1409666587265, + "type": "m.room.member", + "user_id": "@example:localhost" } - ], - "end": "20", - "start": "0" - }, - "room_id": "!CvcvRuDYDzTOzfKKgh:localhost" - } - ] + ] + } + ] + } -This returns all the room IDs of rooms the user is invited/joined on, as well as -all of the messages and feedback for these rooms. This can be a LOT of data. You -may just want the most recent message for each room. This can be achieved by -applying pagination stream parameters to this request:: +This returns all the room information the user is invited/joined on, as well as +all of the presences relevant for these rooms. This can be a LOT of data. You +may just want the most recent event for each room. This can be achieved by +applying query parameters to ``limit`` this request:: - curl -XGET "http://localhost:8080/_matrix/client/api/v1/im/sync?access_token=QG15ZnJpZW5kOmxvY2FsaG9zdA...XKuGdVsovHmwMyDDvK&from=END&to=START&limit=1" + curl -XGET "http://localhost:8008/_matrix/client/api/v1/initialSync?limit=1&access_token=YOUR_ACCESS_TOKEN" - [ - { - "membership": "join", - "messages": { - "chunk": [ + { + "end": "s39_18_0", + "presence": [ + { + "content": { + "last_active_ago": 1279484, + "user_id": "@example:localhost" + }, + "type": "m.presence" + } + ], + "rooms": [ + { + "membership": "join", + "messages": { + "chunk": [ + { + "content": { + "avatar_url": null, + "displayname": null, + "membership": "join" + }, + "event_id": "JFLVteSvQc", + "membership": "join", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@example:localhost", + "ts": 1409666587265, + "type": "m.room.member", + "user_id": "@example:localhost" + } + ], + "end": "s39_18_0", + "start": "t10-30_18_0" + }, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state": [ { "content": { - "body": "@myfriend:localhost joined the room.", - "hsob_ts": 1408446501661, - "membership": "join", - "membership_source": "@myfriend:localhost", - "membership_target": "@myfriend:localhost", - "msgtype": "m.text" + "creator": "@example:localhost" }, - "event_id": "IMmXbOzFAa", - "msg_id": "m1408446501661", - "room_id": "!CvcvRuDYDzTOzfKKgh:localhost", - "type": "m.room.message", - "user_id": "_homeserver_" + "event_id": "dMUoqVTZca", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.create", + "user_id": "@example:localhost" + }, + { + "content": { + "@example:localhost": 10, + "default": 0 + }, + "event_id": "wAumPSTsWF", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.power_levels", + "user_id": "@example:localhost" + }, + { + "content": { + "join_rule": "public" + }, + "event_id": "jrLVqKHKiI", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.join_rules", + "user_id": "@example:localhost" + }, + { + "content": { + "level": 10 + }, + "event_id": "WpmTgsNWUZ", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.add_state_level", + "user_id": "@example:localhost" + }, + { + "content": { + "level": 0 + }, + "event_id": "qUMBJyKsTQ", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.send_event_level", + "user_id": "@example:localhost" + }, + { + "content": { + "ban_level": 5, + "kick_level": 5 + }, + "event_id": "YAaDmKvoUW", + "required_power_level": 10, + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "", + "ts": 1409665585188, + "type": "m.room.ops_levels", + "user_id": "@example:localhost" + }, + { + "content": { + "membership": "invite" + }, + "event_id": "YjNuBKnPsb", + "membership": "invite", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@myfriend:localhost", + "ts": 1409666426819, + "type": "m.room.member", + "user_id": "@example:localhost" + }, + { + "content": { + "avatar_url": null, + "displayname": null, + "membership": "join" + }, + "event_id": "JFLVteSvQc", + "membership": "join", + "room_id": "!MkDbyRqnvTYnoxjLYx:localhost", + "state_key": "@example:localhost", + "ts": 1409666587265, + "type": "m.room.member", + "user_id": "@example:localhost" } - ], - "end": "20", - "start": "21" - }, - "room_id": "!CvcvRuDYDzTOzfKKgh:localhost" - } - ] + ] + } + ] + } Getting live state ------------------ Once you know which rooms the client has previously interacted with, you need to listen for incoming events. This can be done like so:: - curl -XGET "http://localhost:8080/_matrix/client/api/v1/events?access_token=QG15ZnJpZW5kOmxvY2FsaG9zdA...XKuGdVsovHmwMyDDvK&from=END" + curl -XGET "http://localhost:8008/_matrix/client/api/v1/events?access_token=YOUR_ACCESS_TOKEN" { "chunk": [], - "end": "215", - "start": "215" + "end": "s39_18_0", + "start": "s39_18_0" } This will block waiting for an incoming event, timing out after several seconds. Even if there are no new events (as in the example above), there will be some pagination stream response keys. The client should make subsequent requests -using the value of the ``"end"`` key (in this case ``215``) as the ``from`` -query parameter. This value should be stored so when the client reopens your app -after a period of inactivity, you can resume from where you got up to in the -event stream. If it has been a long period of inactivity, there may be LOTS of -events waiting for the user. In this case, you may wish to get all state instead -and then resume getting live state from a newer end token. +using the value of the ``"end"`` key (in this case ``s39_18_0``) as the ``from`` +query parameter e.g. ``http://localhost:8008/_matrix/client/api/v1/events?access +_token=YOUR_ACCESS_TOKEN&from=s39_18_0``. This value should be stored so when the +client reopens your app after a period of inactivity, you can resume from where +you got up to in the event stream. If it has been a long period of inactivity, +there may be LOTS of events waiting for the user. In this case, you may wish to +get all state instead and then resume getting live state from a newer end token. NB: The timeout can be changed by adding a ``timeout`` query parameter, which is in milliseconds. A timeout of 0 will not block. @@ -300,4 +621,4 @@ creating and joining rooms, sending messages, getting member lists and getting historical messages for a room. This covers most functionality of a messaging application. -**Try out the fiddle: http://jsfiddle.net/L8r3o1wr/** +**Try out the fiddle: http://jsfiddle.net/uztL3yme/** diff --git a/jsfiddles/create_room_send_msg/demo.html b/jsfiddles/create_room_send_msg/demo.html index 31c26c7631..088ff7ac0f 100644 --- a/jsfiddles/create_room_send_msg/demo.html +++ b/jsfiddles/create_room_send_msg/demo.html @@ -1,5 +1,5 @@
-

This room creation / message sending demo requires a home server to be running on http://localhost:8080

+

This room creation / message sending demo requires a home server to be running on http://localhost:8008

diff --git a/jsfiddles/create_room_send_msg/demo.js b/jsfiddles/create_room_send_msg/demo.js index 61044da743..3dc7263830 100644 --- a/jsfiddles/create_room_send_msg/demo.js +++ b/jsfiddles/create_room_send_msg/demo.js @@ -10,7 +10,7 @@ $('.login').live('click', function() { var user = $("#userLogin").val(); var password = $("#passwordLogin").val(); $.ajax({ - url: "http://localhost:8080/_matrix/client/api/v1/login", + url: "http://localhost:8008/_matrix/client/api/v1/login", type: "POST", contentType: "application/json; charset=utf-8", data: JSON.stringify({ user: user, password: password, type: "m.login.password" }), @@ -25,7 +25,7 @@ $('.login').live('click', function() { }); var getCurrentRoomList = function() { - var url = "http://localhost:8080/_matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1"; + var url = "http://localhost:8008/_matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1"; $.getJSON(url, function(data) { var rooms = data.rooms; for (var i=0; i -

This event stream demo requires a home server to be running on http://localhost:8080

+

This event stream demo requires a home server to be running on http://localhost:8008

diff --git a/jsfiddles/event_stream/demo.js b/jsfiddles/event_stream/demo.js index 997d1a2240..5c81e08caa 100644 --- a/jsfiddles/event_stream/demo.js +++ b/jsfiddles/event_stream/demo.js @@ -7,7 +7,7 @@ var eventStreamInfo = { var roomInfo = []; var longpollEventStream = function() { - var url = "http://localhost:8080/_matrix/client/api/v1/events?access_token=$token&from=$from"; + var url = "http://localhost:8008/_matrix/client/api/v1/events?access_token=$token&from=$from"; url = url.replace("$token", accountInfo.access_token); url = url.replace("$from", eventStreamInfo.from); @@ -48,7 +48,7 @@ $('.login').live('click', function() { var user = $("#userLogin").val(); var password = $("#passwordLogin").val(); $.ajax({ - url: "http://localhost:8080/_matrix/client/api/v1/login", + url: "http://localhost:8008/_matrix/client/api/v1/login", type: "POST", contentType: "application/json; charset=utf-8", data: JSON.stringify({ user: user, password: password, type: "m.login.password" }), @@ -65,7 +65,7 @@ $('.login').live('click', function() { var getCurrentRoomList = function() { $("#roomId").val(""); - var url = "http://localhost:8080/_matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1"; + var url = "http://localhost:8008/_matrix/client/api/v1/initialSync?access_token=" + accountInfo.access_token + "&limit=1"; $.getJSON(url, function(data) { var rooms = data.rooms; for (var i=0; i