From a1b6087bf45f29c8ceae68598b293bbbfcd86779 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?poljar=20=28Damir=20Jeli=C4=87=29?= Date: Tue, 6 Mar 2018 15:52:30 +0100 Subject: [PATCH] rooms: Print out membership events. --- matrix/rooms.py | 57 +++++++++++++++++++++++++++++++++++++++++++------ matrix/utils.py | 10 ++++----- 2 files changed, 56 insertions(+), 11 deletions(-) diff --git a/matrix/rooms.py b/matrix/rooms.py index ed2e0e3..48862d3 100644 --- a/matrix/rooms.py +++ b/matrix/rooms.py @@ -127,11 +127,29 @@ class RoomInfo(): raise ValueError if event_dict["content"]["membership"] == "join": - return RoomMemberJoin.from_dict(event_dict) - elif event_dict["content"]["membership"] == "leave": - return RoomMemberLeave.from_dict(event_dict) + event = RoomMemberJoin.from_dict(event_dict) - return None + try: + message = RoomMembershipMessage( + event.event_id, event.sender, event.age, + "has joined", "join") + return event, message + except AttributeError: + return event, None + + elif event_dict["content"]["membership"] == "leave": + event = RoomMemberLeave.from_dict(event_dict) + + try: + msg = ("has left" if event.sender == event.leaving_user else + "has been kicked") + message = RoomMembershipMessage( + event.event_id, event.sender, event.age, msg, "quit") + return event, message + except AttributeError: + return event, None + + return None, None @staticmethod def _parse_events(parsed_dict): @@ -143,8 +161,10 @@ class RoomInfo(): if event["type"] == "m.room.message": other_events.append(RoomInfo._message_from_event(event)) elif event["type"] == "m.room.member": - membership_events.append( - RoomInfo._membership_from_dict(event)) + m_event, msg = RoomInfo._membership_from_dict(event) + membership_events.append(m_event) + if msg: + other_events.append(msg) elif event["type"] == "m.room.power_levels": other_events.append(RoomPowerLevels.from_dict(event)) elif event["type"] == "m.room.topic": @@ -413,6 +433,31 @@ class RoomMessageMedia(RoomMessageEvent): self._print_message(msg, room, buff, tags) +class RoomMembershipMessage(RoomEvent): + def __init__(self, event_id, sender, age, message, prefix): + self.message = message + self.prefix = prefix + RoomEvent.__init__(self, event_id, sender, age) + + def execute(self, server, room, buff, tags): + nick, color_name = sender_to_nick_and_color(room, self.sender) + event_tags = add_event_tags(self.event_id, nick, None, []) + + data = ("{prefix}{color}{author}{ncolor} " + "({user_id}) {message} {room}").format( + prefix=W.prefix(self.prefix), + color=W.color(color_name), + author=nick, + ncolor=W.color("reset"), + user_id=self.sender, + message=self.message, + room=room.alias) + date = date_from_age(self.age) + tags_string = ",".join(event_tags) + + W.prnt_date_tags(buff, date, tags_string, data) + + class RoomMemberJoin(RoomEvent): def __init__(self, event_id, sender, age, display_name): diff --git a/matrix/utils.py b/matrix/utils.py index d9a4f67..cc1969a 100644 --- a/matrix/utils.py +++ b/matrix/utils.py @@ -165,12 +165,12 @@ def tags_for_message(message_type): return default_tags[message_type] -def add_event_tags(event_id, nick, color, tags): - if not tags: - tags = tags_for_message("message") - +def add_event_tags(event_id, nick, color=None, tags=[]): tags.append("nick_{nick}".format(nick=nick)) - tags.append("prefix_nick_{color}".format(color=color_for_tags(color))) + + if color: + tags.append("prefix_nick_{color}".format(color=color_for_tags(color))) + tags.append("matrix_id_{event_id}".format(event_id=event_id)) return tags