Room subtitle: show member but not profile events

Instead of not showing any kind of RoomMemberEvent.
This commit is contained in:
miruka 2019-08-16 01:51:42 -04:00
parent 729235fe6a
commit 8baf336cf9
3 changed files with 47 additions and 18 deletions

View File

@ -16,7 +16,7 @@ import nio
from . import __about__, utils from . import __about__, utils
from .html_filter import HTML_FILTER from .html_filter import HTML_FILTER
from .models.items import Account, Event, Member, Room from .models.items import Account, Event, Member, Room, TypeSpecifier
from .models.model_store import ModelStore from .models.model_store import ModelStore
@ -304,13 +304,13 @@ class MatrixClient(nio.AsyncClient):
return return
for_us = item.target_id in self.backend.clients for_us = item.target_id in self.backend.clients
is_member_ev = item.event_type == nio.RoomMemberEvent.__name__ is_profile_ev = item.type_specifier == TypeSpecifier.profile_change
# If there were no better events available to show previously # If there were no better events available to show previously
prev_is_member_ev = \ prev_is_member_ev = \
room.last_event["event_type"] == nio.RoomMemberEvent.__name__ room.last_event["event_type"] == nio.RoomMemberEvent.__name__
if is_member_ev and for_us and not prev_is_member_ev: if is_profile_ev and for_us and not prev_is_member_ev:
return return
if item.date < room.last_event["date"]: # If this is a past event if item.date < room.last_event["date"]: # If this is a past event
@ -379,10 +379,13 @@ class MatrixClient(nio.AsyncClient):
return (item.display_name, item.avatar_url) return (item.display_name, item.avatar_url)
async def register_nio_event(self, async def register_nio_event(
self,
room: nio.MatrixRoom, room: nio.MatrixRoom,
ev: nio.Event, ev: nio.Event,
content: str) -> None: content: str,
type_specifier: TypeSpecifier = TypeSpecifier.none,
) -> None:
await self.register_nio_room(room) await self.register_nio_room(room)
@ -408,6 +411,8 @@ class MatrixClient(nio.AsyncClient):
sender_name = sender_name, sender_name = sender_name,
sender_avatar = sender_avatar, sender_avatar = sender_avatar,
type_specifier = type_specifier,
target_id = target_id, target_id = target_id,
target_name = target_name, target_name = target_name,
target_avatar = target_avatar, target_avatar = target_avatar,
@ -533,47 +538,55 @@ class MatrixClient(nio.AsyncClient):
await self.register_nio_event(room, ev, content=co) await self.register_nio_event(room, ev, content=co)
async def process_room_member_event(self, room, ev) -> Optional[str]: async def process_room_member_event(
self, room, ev,
) -> Optional[Tuple[TypeSpecifier, str]]:
prev = ev.prev_content prev = ev.prev_content
now = ev.content now = ev.content
membership = ev.membership membership = ev.membership
prev_membership = ev.prev_membership prev_membership = ev.prev_membership
ev_date = datetime.fromtimestamp(ev.server_timestamp / 1000) ev_date = datetime.fromtimestamp(ev.server_timestamp / 1000)
member_change = TypeSpecifier.membership_change
# Membership changes # Membership changes
if not prev or membership != prev_membership: if not prev or membership != prev_membership:
reason = f" Reason: {now['reason']}" if now.get("reason") else "" reason = f" Reason: {now['reason']}" if now.get("reason") else ""
if membership == "join": if membership == "join":
return ( return (
member_change,
"%1 accepted their invitation." "%1 accepted their invitation."
if prev and prev_membership == "invite" else if prev and prev_membership == "invite" else
"%1 joined the room." "%1 joined the room.",
) )
if membership == "invite": if membership == "invite":
return "%1 invited %2 to the room." return (member_change, "%1 invited %2 to the room.")
if membership == "leave": if membership == "leave":
if ev.state_key == ev.sender: if ev.state_key == ev.sender:
return ( return (
member_change,
f"%1 declined their invitation.{reason}" f"%1 declined their invitation.{reason}"
if prev and prev_membership == "invite" else if prev and prev_membership == "invite" else
f"%1 left the room.{reason}" f"%1 left the room.{reason}",
) )
return ( return (
member_change,
f"%1 withdrew %2's invitation.{reason}" f"%1 withdrew %2's invitation.{reason}"
if prev and prev_membership == "invite" else if prev and prev_membership == "invite" else
f"%1 unbanned %2 from the room.{reason}" f"%1 unbanned %2 from the room.{reason}"
if prev and prev_membership == "ban" else if prev and prev_membership == "ban" else
f"%1 kicked out %2 from the room.{reason}" f"%1 kicked out %2 from the room.{reason}",
) )
if membership == "ban": if membership == "ban":
return f"%1 banned %2 from the room.{reason}" return (member_change, f"%1 banned %2 from the room.{reason}")
# Profile changes # Profile changes
changed = [] changed = []
@ -601,7 +614,10 @@ class MatrixClient(nio.AsyncClient):
self.skipped_events[room.room_id] += 1 self.skipped_events[room.room_id] += 1
return None return None
return "%1 changed their {}.".format(" and ".join(changed)) return (
TypeSpecifier.profile_change,
"%1 changed their {}.".format(" and ".join(changed)),
)
log.warning("Invalid member event - %s", log.warning("Invalid member event - %s",
json.dumps(ev.__dict__, indent=4)) json.dumps(ev.__dict__, indent=4))
@ -609,13 +625,16 @@ class MatrixClient(nio.AsyncClient):
async def onRoomMemberEvent(self, room, ev) -> None: async def onRoomMemberEvent(self, room, ev) -> None:
co = await self.process_room_member_event(room, ev) type_and_content = await self.process_room_member_event(room, ev)
if co is None: if type_and_content is None:
# This is run from register_nio_event otherwise # This is run from register_nio_event otherwise
await self.register_nio_room(room) await self.register_nio_room(room)
else: else:
await self.register_nio_event(room, ev, content=co) type_specifier, content = type_and_content
await self.register_nio_event(
room, ev, content=content, type_specifier=type_specifier,
)
async def onRoomAliasEvent(self, room, ev) -> None: async def onRoomAliasEvent(self, room, ev) -> None:

View File

@ -3,6 +3,7 @@ from datetime import datetime
from typing import Any, Dict, List, Optional from typing import Any, Dict, List, Optional
from ..html_filter import HTML_FILTER from ..html_filter import HTML_FILTER
from ..utils import AutoStrEnum, auto
from .model_item import ModelItem from .model_item import ModelItem
@ -69,6 +70,12 @@ class Member(ModelItem):
return name < other_name return name < other_name
class TypeSpecifier(AutoStrEnum):
none = auto()
profile_change = auto()
membership_change = auto()
@dataclass @dataclass
class Event(ModelItem): class Event(ModelItem):
main_key = "event_id" main_key = "event_id"
@ -84,6 +91,8 @@ class Event(ModelItem):
sender_name: str = field() sender_name: str = field()
sender_avatar: str = field() sender_avatar: str = field()
type_specifier: TypeSpecifier = TypeSpecifier.none
target_id: str = "" target_id: str = ""
target_name: str = "" target_name: str = ""
target_avatar: str = "" target_avatar: str = ""

View File

@ -19,6 +19,7 @@ HPage {
modelSources[["Room", userId]] || [], "room_id", roomId modelSources[["Room", userId]] || [], "room_id", roomId
) || "waiting" ) || "waiting"
onRoomInfoChanged: { onRoomInfoChanged: {
print( JSON.stringify( roomInfo, null, 4))
if (roomInfo.left) { if (roomInfo.left) {
// The room will most likely be gone on client restart // The room will most likely be gone on client restart
window.uiState.page = "Pages/Default.qml" window.uiState.page = "Pages/Default.qml"