rooms: Print out membership events.

This commit is contained in:
poljar (Damir Jelić) 2018-03-06 15:52:30 +01:00
parent 996270df0d
commit a1b6087bf4
2 changed files with 56 additions and 11 deletions

View file

@ -127,11 +127,29 @@ class RoomInfo():
raise ValueError raise ValueError
if event_dict["content"]["membership"] == "join": if event_dict["content"]["membership"] == "join":
return RoomMemberJoin.from_dict(event_dict) event = RoomMemberJoin.from_dict(event_dict)
elif event_dict["content"]["membership"] == "leave":
return RoomMemberLeave.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 @staticmethod
def _parse_events(parsed_dict): def _parse_events(parsed_dict):
@ -143,8 +161,10 @@ class RoomInfo():
if event["type"] == "m.room.message": if event["type"] == "m.room.message":
other_events.append(RoomInfo._message_from_event(event)) other_events.append(RoomInfo._message_from_event(event))
elif event["type"] == "m.room.member": elif event["type"] == "m.room.member":
membership_events.append( m_event, msg = RoomInfo._membership_from_dict(event)
RoomInfo._membership_from_dict(event)) membership_events.append(m_event)
if msg:
other_events.append(msg)
elif event["type"] == "m.room.power_levels": elif event["type"] == "m.room.power_levels":
other_events.append(RoomPowerLevels.from_dict(event)) other_events.append(RoomPowerLevels.from_dict(event))
elif event["type"] == "m.room.topic": elif event["type"] == "m.room.topic":
@ -413,6 +433,31 @@ class RoomMessageMedia(RoomMessageEvent):
self._print_message(msg, room, buff, tags) 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): class RoomMemberJoin(RoomEvent):
def __init__(self, event_id, sender, age, display_name): def __init__(self, event_id, sender, age, display_name):

View file

@ -165,12 +165,12 @@ def tags_for_message(message_type):
return default_tags[message_type] return default_tags[message_type]
def add_event_tags(event_id, nick, color, tags): def add_event_tags(event_id, nick, color=None, tags=[]):
if not tags:
tags = tags_for_message("message")
tags.append("nick_{nick}".format(nick=nick)) 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)) tags.append("matrix_id_{event_id}".format(event_id=event_id))
return tags return tags