Skip to content

Commit 22b99b1

Browse files
authored
Merge pull request #111 from non-Jedi/consolidate_state_events
Update api to allow application service timestamp massaging
2 parents 3b094e6 + 92a217b commit 22b99b1

File tree

1 file changed

+50
-29
lines changed

1 file changed

+50
-29
lines changed

matrix_client/api.py

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -178,30 +178,37 @@ def event_stream(self, from_token, timeout=30000):
178178
}
179179
)
180180

181-
def send_state_event(self, room_id, event_type, content, state_key=""):
182-
"""Perform /rooms/$room_id/state/$event_type
181+
def send_state_event(self, room_id, event_type, content, state_key="",
182+
timestamp=None):
183+
"""Perform PUT /rooms/$room_id/state/$event_type
183184
184185
Args:
185186
room_id(str): The room ID to send the state event in.
186187
event_type(str): The state event type to send.
187188
content(dict): The JSON content to send.
188189
state_key(str): Optional. The state key for the event.
190+
timestamp(int): Optional. Set origin_server_ts (For application services only)
189191
"""
190192
path = "/rooms/%s/state/%s" % (
191193
quote(room_id), quote(event_type),
192194
)
193195
if state_key:
194196
path += "/%s" % (quote(state_key))
195-
return self._send("PUT", path, content)
197+
params = {}
198+
if timestamp:
199+
params["ts"] = timestamp
200+
return self._send("PUT", path, content, query_params=params)
196201

197-
def send_message_event(self, room_id, event_type, content, txn_id=None):
198-
"""Perform /rooms/$room_id/send/$event_type
202+
def send_message_event(self, room_id, event_type, content, txn_id=None,
203+
timestamp=None):
204+
"""Perform PUT /rooms/$room_id/send/$event_type
199205
200206
Args:
201207
room_id(str): The room ID to send the message event in.
202208
event_type(str): The event type to send.
203209
content(dict): The JSON content to send.
204210
txn_id(int): Optional. The transaction ID to use.
211+
timestamp(int): Optional. Set origin_server_ts (For application services only)
205212
"""
206213
if not txn_id:
207214
txn_id = str(self.txn_id) + str(int(time() * 1000))
@@ -211,13 +218,16 @@ def send_message_event(self, room_id, event_type, content, txn_id=None):
211218
path = "/rooms/%s/send/%s/%s" % (
212219
quote(room_id), quote(event_type), quote(str(txn_id)),
213220
)
214-
return self._send("PUT", path, content)
221+
params = {}
222+
if timestamp:
223+
params["ts"] = timestamp
224+
return self._send("PUT", path, content, query_params=params)
215225

216226
# content_type can be a image,audio or video
217227
# extra information should be supplied, see
218228
# https://matrix.org/docs/spec/r0.0.1/client_server.html
219229
def send_content(self, room_id, item_url, item_name, msg_type,
220-
extra_information=None):
230+
extra_information=None, timestamp=None):
221231
if extra_information is None:
222232
extra_information = {}
223233

@@ -227,10 +237,12 @@ def send_content(self, room_id, item_url, item_name, msg_type,
227237
"body": item_name,
228238
"info": extra_information
229239
}
230-
return self.send_message_event(room_id, "m.room.message", content_pack)
240+
return self.send_message_event(room_id, "m.room.message", content_pack,
241+
timestamp=timestamp)
231242

232243
# http://matrix.org/docs/spec/client_server/r0.2.0.html#m-location
233-
def send_location(self, room_id, geo_uri, name, thumb_url=None, thumb_info=None):
244+
def send_location(self, room_id, geo_uri, name, thumb_url=None, thumb_info=None,
245+
timestamp=None):
234246
"""Send m.location message event
235247
236248
Args:
@@ -239,6 +251,7 @@ def send_location(self, room_id, geo_uri, name, thumb_url=None, thumb_info=None)
239251
name(str): Description for the location.
240252
thumb_url(str): URL to the thumbnail of the location.
241253
thumb_info(dict): Metadata about the thumbnail, type ImageInfo.
254+
timestamp(int): Optional. Set origin_server_ts (For application services only)
242255
"""
243256
content_pack = {
244257
"geo_uri": geo_uri,
@@ -250,44 +263,51 @@ def send_location(self, room_id, geo_uri, name, thumb_url=None, thumb_info=None)
250263
if thumb_info:
251264
content_pack["thumbnail_info"] = thumb_info
252265

253-
return self.send_message_event(room_id, "m.room.message", content_pack)
266+
return self.send_message_event(room_id, "m.room.message", content_pack,
267+
timestamp=timestamp)
254268

255-
def send_message(self, room_id, text_content, msgtype="m.text"):
256-
"""Perform /rooms/$room_id/send/m.room.message
269+
def send_message(self, room_id, text_content, msgtype="m.text", timestamp=None):
270+
"""Perform PUT /rooms/$room_id/send/m.room.message
257271
258272
Args:
259273
room_id(str): The room ID to send the event in.
260274
text_content(str): The m.text body to send.
275+
timestamp(int): Optional. Set origin_server_ts (For application services only)
261276
"""
262277
return self.send_message_event(
263278
room_id, "m.room.message",
264-
self.get_text_body(text_content, msgtype)
279+
self.get_text_body(text_content, msgtype),
280+
timestamp=timestamp
265281
)
266282

267-
def send_emote(self, room_id, text_content):
268-
"""Perform /rooms/$room_id/send/m.room.message with m.emote msgtype
283+
def send_emote(self, room_id, text_content, timestamp=None):
284+
"""Perform PUT /rooms/$room_id/send/m.room.message with m.emote msgtype
269285
270286
Args:
271287
room_id(str): The room ID to send the event in.
272288
text_content(str): The m.emote body to send.
289+
timestamp(int): Optional. Set origin_server_ts (For application services only)
273290
"""
274291
return self.send_message_event(
275292
room_id, "m.room.message",
276-
self.get_emote_body(text_content)
293+
self.get_emote_body(text_content),
294+
timestamp=timestamp
277295
)
278296

279-
def send_notice(self, room_id, text_content):
280-
"""Perform /rooms/$room_id/send/m.room.message with m.notice msgtype
297+
def send_notice(self, room_id, text_content, timestamp=None):
298+
"""Perform PUT /rooms/$room_id/send/m.room.message with m.notice msgtype
281299
282300
Args:
283301
room_id(str): The room ID to send the event in.
284302
text_content(str): The m.notice body to send.
303+
timestamp(int): Optional. Set origin_server_ts (For application services only)
285304
"""
286305
body = {
287306
"msgtype": "m.notice",
288307
"body": text_content
289308
}
290-
return self.send_message_event(room_id, "m.room.message", body)
309+
return self.send_message_event(room_id, "m.room.message", body,
310+
timestamp=timestamp)
291311

292312
def get_room_messages(self, room_id, token, direction, limit=10, to=None):
293313
"""Perform GET /rooms/{roomId}/messages.
@@ -319,16 +339,17 @@ def get_room_name(self, room_id):
319339
"""
320340
return self._send("GET", "/rooms/" + room_id + "/state/m.room.name")
321341

322-
def set_room_name(self, room_id, name):
342+
def set_room_name(self, room_id, name, timestamp=None):
323343
"""Perform PUT /rooms/$room_id/state/m.room.name
324344
Args:
325345
room_id(str): The room ID
326346
name(str): The new room name
347+
timestamp(int): Optional. Set origin_server_ts (For application services only)
327348
"""
328349
body = {
329350
"name": name
330351
}
331-
return self._send("PUT", "/rooms/" + room_id + "/state/m.room.name", body)
352+
return self.send_state_event(room_id, "m.room.name", body, timestamp=timestamp)
332353

333354
def get_room_topic(self, room_id):
334355
"""Perform GET /rooms/$room_id/state/m.room.topic
@@ -337,16 +358,17 @@ def get_room_topic(self, room_id):
337358
"""
338359
return self._send("GET", "/rooms/" + room_id + "/state/m.room.topic")
339360

340-
def set_room_topic(self, room_id, topic):
361+
def set_room_topic(self, room_id, topic, timestamp=None):
341362
"""Perform PUT /rooms/$room_id/state/m.room.topic
342363
Args:
343364
room_id(str): The room ID
344365
topic(str): The new room topic
366+
timestamp(int): Optional. Set origin_server_ts (For application services only)
345367
"""
346368
body = {
347369
"topic": topic
348370
}
349-
return self._send("PUT", "/rooms/" + room_id + "/state/m.room.topic", body)
371+
return self.send_state_event(room_id, "m.room.topic", body, timestamp=timestamp)
350372

351373
def leave_room(self, room_id):
352374
"""Perform POST /rooms/$room_id/leave
@@ -382,13 +404,15 @@ def get_membership(self, room_id, user_id):
382404
"/rooms/%s/state/m.room.member/%s" % (room_id, user_id)
383405
)
384406

385-
def set_membership(self, room_id, user_id, membership, reason="", profile={}):
407+
def set_membership(self, room_id, user_id, membership, reason="", profile={},
408+
timestamp=None):
386409
"""Perform PUT /rooms/$room_id/state/m.room.member/$user_id
387410
Args:
388411
room_id(str): The room ID
389412
user_id(str): The user ID
390413
membership(str): New membership value
391414
reason(str): The reason
415+
timestamp(int): Optional. Set origin_server_ts (For application services only)
392416
"""
393417
body = {
394418
"membership": membership,
@@ -399,11 +423,8 @@ def set_membership(self, room_id, user_id, membership, reason="", profile={}):
399423
if 'avatar_url' in profile:
400424
body["avatar_url"] = profile["avatar_url"]
401425

402-
return self._send(
403-
"PUT",
404-
"/rooms/%s/state/m.room.member/%s" % (room_id, user_id),
405-
body
406-
)
426+
return self.send_state_event(room_id, "m.room.member", body, state_key=user_id,
427+
timestamp=timestamp)
407428

408429
def ban_user(self, room_id, user_id, reason=""):
409430
"""Perform POST /rooms/$room_id/ban

0 commit comments

Comments
 (0)