Skip to content

Commit 0d1a339

Browse files
committed
maybe bencode responses in event propagation
if data is a string or bytes dont try to bencode the repsonse
1 parent 97a093d commit 0d1a339

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

sogs/events.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,23 @@ def _user_from_conn(conn):
3737
return model.User(session_id='05' + hexlify(conn.pubkey).decode())
3838

3939

40+
def _maybe_serialize(data):
41+
"""maybe bt encode data, if data is a bytes dont encode,
42+
if data is a string turn it into bytes and dont encode, otherwise bt encode"""
43+
if isinstance(data, bytes):
44+
return data
45+
if isinstance(data, str):
46+
return data.encode()
47+
return bt_serialize(data)
48+
49+
4050
def _propagate_event(eventname, *args):
4151
""" propagate an event to everyone who cares about it """
4252
assert event_name_valid(eventname)
4353
global omq, _pools
4454
sent = 0
4555
for conn in _pools[eventname]:
46-
omq.send(conn, f'sogs.event.{eventname}', *(bt_serialize(a) for a in args))
56+
omq.send(conn, f'sogs.event.{eventname}', *(_maybe_serialize(a) for a in args))
4757
sent += 1
4858
if sent:
4959
app.logger.info(f"sent {eventname} to {sent} subscribers")

0 commit comments

Comments
 (0)