rooms: Remove membership events, just leave the original one.
This commit is contained in:
parent
e6485c73bb
commit
d70e651d3e
3 changed files with 31 additions and 94 deletions
|
@ -37,9 +37,6 @@ from .rooms import (
|
||||||
RoomNameEvent,
|
RoomNameEvent,
|
||||||
RoomAliasEvent,
|
RoomAliasEvent,
|
||||||
RoomMembershipEvent,
|
RoomMembershipEvent,
|
||||||
RoomMemberJoin,
|
|
||||||
RoomMemberLeave,
|
|
||||||
RoomMemberInvite,
|
|
||||||
RoomTopicEvent,
|
RoomTopicEvent,
|
||||||
RoomMessageText,
|
RoomMessageText,
|
||||||
RoomMessageEmote,
|
RoomMessageEmote,
|
||||||
|
@ -700,10 +697,9 @@ class RoomBuffer(object):
|
||||||
|
|
||||||
date = server_ts_to_weechat(event.timestamp)
|
date = server_ts_to_weechat(event.timestamp)
|
||||||
|
|
||||||
if isinstance(event, RoomMemberJoin):
|
if event.content["membership"] == "join":
|
||||||
if event.prev_content and "membership" in event.prev_content:
|
if event.prev_content and "membership" in event.prev_content:
|
||||||
if (event.prev_content["membership"] == "leave"
|
if (event.prev_content["membership"] != "join"):
|
||||||
or event.prev_content["membership"] == "invite"):
|
|
||||||
join(event, date, is_state)
|
join(event, date, is_state)
|
||||||
else:
|
else:
|
||||||
# TODO print out profile changes
|
# TODO print out profile changes
|
||||||
|
@ -713,21 +709,21 @@ class RoomBuffer(object):
|
||||||
# joined.
|
# joined.
|
||||||
join(event, date, is_state)
|
join(event, date, is_state)
|
||||||
|
|
||||||
elif isinstance(event, RoomMemberLeave):
|
elif event.content["membership"] == "leave":
|
||||||
nick = self.find_nick(event.leaving_user)
|
nick = self.find_nick(event.state_key)
|
||||||
if event.sender == event.leaving_user:
|
if event.sender == event.state_key:
|
||||||
self.weechat_buffer.part(nick, date, not is_state)
|
self.weechat_buffer.part(nick, date, not is_state)
|
||||||
else:
|
else:
|
||||||
self.weechat_buffer.kick(nick, date, not is_state)
|
self.weechat_buffer.kick(nick, date, not is_state)
|
||||||
|
|
||||||
if event.leaving_user in self.displayed_nicks:
|
if event.state_key in self.displayed_nicks:
|
||||||
del self.displayed_nicks[event.leaving_user]
|
del self.displayed_nicks[event.state_key]
|
||||||
|
|
||||||
elif isinstance(event, RoomMemberInvite):
|
elif event.content["membership"] == "invite":
|
||||||
if is_state:
|
if is_state:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.weechat_buffer.invite(event.invited_user, date)
|
self.weechat_buffer.invite(event.state_key, date)
|
||||||
return
|
return
|
||||||
|
|
||||||
room_name = self.room.display_name(self.room.own_user_id)
|
room_name = self.room.display_name(self.room.own_user_id)
|
||||||
|
|
|
@ -138,26 +138,36 @@ class MatrixRoom:
|
||||||
"""
|
"""
|
||||||
return not self.is_named()
|
return not self.is_named()
|
||||||
|
|
||||||
def handle_event(self, event):
|
def _handle_membership(self, event):
|
||||||
if isinstance(event, RoomMemberJoin):
|
if event.content["membership"] == "join":
|
||||||
if event.sender in self.users:
|
if event.sender in self.users:
|
||||||
user = self.users[event.sender]
|
user = self.users[event.sender]
|
||||||
if event.display_name:
|
if "display_name" in event.content:
|
||||||
user.display_name = event.display_name
|
user.display_name = event.content["display_name"]
|
||||||
else:
|
else:
|
||||||
short_name = shorten_sender(event.sender)
|
short_name = shorten_sender(event.sender)
|
||||||
# TODO the default power level doesn't have to be 0
|
# TODO the default power level doesn't have to be 0
|
||||||
level = (self.power_levels[event.sender] if event.sender in
|
level = (self.power_levels[event.sender] if event.sender in
|
||||||
self.power_levels else 0)
|
self.power_levels else 0)
|
||||||
user = MatrixUser(short_name, event.display_name)
|
display_name = (event.content["display_name"]
|
||||||
|
if "display_name" in event.content else None)
|
||||||
|
|
||||||
|
user = MatrixUser(short_name, display_name, level)
|
||||||
self.users[event.sender] = user
|
self.users[event.sender] = user
|
||||||
return True
|
return True
|
||||||
|
|
||||||
elif isinstance(event, RoomMemberLeave):
|
elif event.content["membership"] == "leave":
|
||||||
if event.leaving_user in self.users:
|
if event.state_key in self.users:
|
||||||
del self.users[event.leaving_user]
|
del self.users[event.state_key]
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
elif event.content["membership"] == "invite":
|
||||||
|
pass
|
||||||
|
|
||||||
|
def handle_event(self, event):
|
||||||
|
if isinstance(event, RoomMembershipEvent):
|
||||||
|
self._handle_membership(event)
|
||||||
|
|
||||||
elif isinstance(event, RoomNameEvent):
|
elif isinstance(event, RoomNameEvent):
|
||||||
self.name = event.name
|
self.name = event.name
|
||||||
|
|
||||||
|
@ -180,11 +190,11 @@ class MatrixRoom:
|
||||||
|
|
||||||
class MatrixUser:
|
class MatrixUser:
|
||||||
|
|
||||||
def __init__(self, name, display_name):
|
def __init__(self, name, display_name=None, power_level=0):
|
||||||
# yapf: disable
|
# yapf: disable
|
||||||
self.name = name # type: str
|
self.name = name # type: str
|
||||||
self.display_name = display_name # type: str
|
self.display_name = display_name # type: str
|
||||||
self.power_level = 0 # type: int
|
self.power_level = power_level # type: int
|
||||||
# yapf: enable
|
# yapf: enable
|
||||||
|
|
||||||
|
|
||||||
|
@ -213,24 +223,6 @@ class RoomInfo():
|
||||||
|
|
||||||
return RoomMessageEvent.from_dict(event)
|
return RoomMessageEvent.from_dict(event)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _membership_from_dict(event_dict):
|
|
||||||
if (event_dict["content"]["membership"] not in [
|
|
||||||
"invite", "join", "knock", "leave", "ban"
|
|
||||||
]):
|
|
||||||
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)
|
|
||||||
|
|
||||||
elif event_dict["content"]["membership"] == "invite":
|
|
||||||
return RoomMemberInvite.from_dict(event_dict)
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def parse_event(olm, room_id, event_dict):
|
def parse_event(olm, room_id, event_dict):
|
||||||
# type: (Dict[Any, Any]) -> (RoomEvent, RoomEvent)
|
# type: (Dict[Any, Any]) -> (RoomEvent, RoomEvent)
|
||||||
|
@ -241,7 +233,7 @@ class RoomInfo():
|
||||||
elif event_dict["type"] == "m.room.message":
|
elif event_dict["type"] == "m.room.message":
|
||||||
event = RoomInfo._message_from_event(event_dict)
|
event = RoomInfo._message_from_event(event_dict)
|
||||||
elif event_dict["type"] == "m.room.member":
|
elif event_dict["type"] == "m.room.member":
|
||||||
event = RoomInfo._membership_from_dict(event_dict)
|
event = RoomMembershipEvent.from_dict(event_dict)
|
||||||
elif event_dict["type"] == "m.room.power_levels":
|
elif event_dict["type"] == "m.room.power_levels":
|
||||||
event = RoomPowerLevels.from_dict(event_dict)
|
event = RoomPowerLevels.from_dict(event_dict)
|
||||||
elif event_dict["type"] == "m.room.topic":
|
elif event_dict["type"] == "m.room.topic":
|
||||||
|
@ -333,10 +325,6 @@ class RoomEvent():
|
||||||
self.timestamp = timestamp
|
self.timestamp = timestamp
|
||||||
|
|
||||||
|
|
||||||
class RoomMembershipEvent(RoomEvent):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class RoomRedactedMessageEvent(RoomEvent):
|
class RoomRedactedMessageEvent(RoomEvent):
|
||||||
|
|
||||||
def __init__(self, event_id, sender, timestamp, censor, reason=None):
|
def __init__(self, event_id, sender, timestamp, censor, reason=None):
|
||||||
|
@ -485,19 +473,16 @@ class RoomMessageMedia(RoomMessageEvent):
|
||||||
return cls(event_id, sender, timestamp, mxc_url, description)
|
return cls(event_id, sender, timestamp, mxc_url, description)
|
||||||
|
|
||||||
|
|
||||||
class RoomMemberJoin(RoomMembershipEvent):
|
class RoomMembershipEvent(RoomEvent):
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
event_id,
|
event_id,
|
||||||
sender,
|
sender,
|
||||||
timestamp,
|
timestamp,
|
||||||
display_name,
|
|
||||||
state_key,
|
state_key,
|
||||||
content,
|
content,
|
||||||
prev_content
|
prev_content
|
||||||
):
|
):
|
||||||
self.display_name = display_name
|
|
||||||
self.state_key = state_key
|
self.state_key = state_key
|
||||||
self.content = content
|
self.content = content
|
||||||
self.prev_content = prev_content
|
self.prev_content = prev_content
|
||||||
|
@ -509,58 +494,20 @@ class RoomMemberJoin(RoomMembershipEvent):
|
||||||
sender = sanitize_id(event_dict["sender"])
|
sender = sanitize_id(event_dict["sender"])
|
||||||
timestamp = sanitize_ts(event_dict["origin_server_ts"])
|
timestamp = sanitize_ts(event_dict["origin_server_ts"])
|
||||||
state_key = sanitize_id(event_dict["state_key"])
|
state_key = sanitize_id(event_dict["state_key"])
|
||||||
display_name = None
|
|
||||||
content = event_dict["content"]
|
content = event_dict["content"]
|
||||||
prev_content = (event_dict["unsigned"]["prev_content"]
|
prev_content = (event_dict["unsigned"]["prev_content"]
|
||||||
if "prev_content" in event_dict["unsigned"] else None)
|
if "prev_content" in event_dict["unsigned"] else None)
|
||||||
|
|
||||||
if event_dict["content"]:
|
|
||||||
if "display_name" in event_dict["content"]:
|
|
||||||
display_name = sanitize_text(
|
|
||||||
event_dict["content"]["displayname"])
|
|
||||||
|
|
||||||
return cls(
|
return cls(
|
||||||
event_id,
|
event_id,
|
||||||
sender,
|
sender,
|
||||||
timestamp,
|
timestamp,
|
||||||
display_name,
|
|
||||||
state_key,
|
state_key,
|
||||||
content,
|
content,
|
||||||
prev_content
|
prev_content
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class RoomMemberInvite(RoomMembershipEvent):
|
|
||||||
def __init__(self, event_id, sender, invited_user, timestamp):
|
|
||||||
self.invited_user = invited_user
|
|
||||||
RoomEvent.__init__(self, event_id, sender, timestamp)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_dict(cls, event_dict):
|
|
||||||
event_id = sanitize_id(event_dict["event_id"])
|
|
||||||
sender = sanitize_id(event_dict["sender"])
|
|
||||||
invited_user = sanitize_id(event_dict["state_key"])
|
|
||||||
timestamp = sanitize_ts(event_dict["origin_server_ts"])
|
|
||||||
|
|
||||||
return cls(event_id, sender, invited_user, timestamp)
|
|
||||||
|
|
||||||
|
|
||||||
class RoomMemberLeave(RoomMembershipEvent):
|
|
||||||
|
|
||||||
def __init__(self, event_id, sender, leaving_user, timestamp):
|
|
||||||
self.leaving_user = leaving_user
|
|
||||||
RoomEvent.__init__(self, event_id, sender, timestamp)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def from_dict(cls, event_dict):
|
|
||||||
event_id = sanitize_id(event_dict["event_id"])
|
|
||||||
sender = sanitize_id(event_dict["sender"])
|
|
||||||
leaving_user = sanitize_id(event_dict["state_key"])
|
|
||||||
timestamp = sanitize_ts(event_dict["origin_server_ts"])
|
|
||||||
|
|
||||||
return cls(event_id, sender, leaving_user, timestamp)
|
|
||||||
|
|
||||||
|
|
||||||
class RoomPowerLevels(RoomEvent):
|
class RoomPowerLevels(RoomEvent):
|
||||||
|
|
||||||
def __init__(self, event_id, sender, timestamp, power_levels):
|
def __init__(self, event_id, sender, timestamp, power_levels):
|
||||||
|
|
|
@ -39,12 +39,6 @@ from .rooms import (
|
||||||
MatrixRoom,
|
MatrixRoom,
|
||||||
RoomMessageText,
|
RoomMessageText,
|
||||||
RoomMessageEmote,
|
RoomMessageEmote,
|
||||||
MatrixUser,
|
|
||||||
RoomMembershipEvent,
|
|
||||||
RoomMemberJoin,
|
|
||||||
RoomMemberLeave,
|
|
||||||
RoomMemberInvite,
|
|
||||||
RoomTopicEvent
|
|
||||||
)
|
)
|
||||||
from matrix.api import (
|
from matrix.api import (
|
||||||
MatrixClient,
|
MatrixClient,
|
||||||
|
|
Loading…
Reference in a new issue