Display Left rooms, shorter onSyncResponse func

This commit is contained in:
miruka 2019-07-06 22:35:42 -04:00
parent 2ac1d6da17
commit cf04f8ad2f
4 changed files with 66 additions and 78 deletions

View File

@ -1,10 +1,11 @@
from datetime import datetime from datetime import datetime
from enum import auto from enum import auto
from typing import Dict, Sequence, Type, Union from typing import Dict, List, Optional, Sequence, Type, Union
from dataclasses import dataclass, field from dataclasses import dataclass, field
import nio import nio
from nio.rooms import MatrixRoom
from .event import AutoStrEnum, Event from .event import AutoStrEnum, Event
@ -19,8 +20,42 @@ class RoomUpdated(Event):
topic: str = "" topic: str = ""
typing_members: Sequence[str] = () typing_members: Sequence[str] = ()
inviter_id: str = "" inviter_id: str = ""
left_event: Dict[str, str] = field(default_factory=dict) left_event: Optional[nio.RoomMemberEvent] = None
@classmethod
def from_nio(cls,
user_id: str,
category: str,
room: MatrixRoom,
info: nio.RoomInfo,
**fields) -> "RoomUpdated":
typing: List[str] = []
if hasattr(info, "ephemeral"):
for ev in info.ephemeral:
if isinstance(ev, nio.TypingNoticeEvent):
typing = ev.users
name = room.name or room.canonical_alias
if not name:
name = room.group_name()
name = "" if name == "Empty room?" else name
return cls(
user_id = user_id,
category = category,
room_id = room.room_id,
display_name = name,
avatar_url = room.gen_avatar_url or "",
topic = room.topic or "",
inviter_id = getattr(room, "inviter", "") or "",
typing_members = typing,
**fields
)
@dataclass @dataclass
@ -71,7 +106,8 @@ class TimelineEventReceived(Event):
target_user_id: str = "" target_user_id: str = ""
@classmethod @classmethod
def from_nio(cls, room, ev, **fields) -> "TimelineEventReceived": def from_nio(cls, room: MatrixRoom, ev: nio.Event, **fields
) -> "TimelineEventReceived":
return cls( return cls(
event_type = type(ev), event_type = type(ev),
room_id = room.room_id, room_id = room.room_id,

View File

@ -7,7 +7,7 @@ import platform
from contextlib import suppress from contextlib import suppress
from datetime import datetime from datetime import datetime
from types import ModuleType from types import ModuleType
from typing import DefaultDict, Dict, List, Optional, Type from typing import DefaultDict, Dict, Optional, Type
from uuid import uuid4 from uuid import uuid4
import nio import nio
@ -185,59 +185,28 @@ class MatrixClient(nio.AsyncClient):
# Callbacks for nio responses # Callbacks for nio responses
@staticmethod
def _get_room_name(room: MatrixRoom) -> Optional[str]:
# FIXME: reimplanted because of nio's non-standard room.display_name
name = room.name or room.canonical_alias
if name:
return name
name = room.group_name()
return None if name == "Empty room?" else name
async def onSyncResponse(self, resp: nio.SyncResponse) -> None: async def onSyncResponse(self, resp: nio.SyncResponse) -> None:
for room_id, _ in resp.rooms.invite.items(): up = rooms.RoomUpdated.from_nio
room: MatrixRoom = self.invited_rooms[room_id]
rooms.RoomUpdated( for room_id, info in resp.rooms.invite.items():
user_id = self.user_id, up(self.user_id, "Invites", self.invited_rooms[room_id], info)
category = "Invites",
room_id = room_id,
display_name = self._get_room_name(room) or "",
avatar_url = room.gen_avatar_url or "",
topic = room.topic or "",
inviter_id = room.inviter or "",
)
for room_id, info in resp.rooms.join.items(): for room_id, info in resp.rooms.join.items():
room = self.rooms[room_id]
if room_id not in self.backend.past_tokens: if room_id not in self.backend.past_tokens:
self.backend.past_tokens[room_id] = info.timeline.prev_batch self.backend.past_tokens[room_id] = info.timeline.prev_batch
typing: List[str] = [] up(self.user_id, "Rooms", self.rooms[room_id], info)
for ev in info.ephemeral:
if isinstance(ev, nio.TypingNoticeEvent):
typing = ev.users
rooms.RoomUpdated( for room_id, info in resp.rooms.leave.items():
user_id = self.user_id, lev = None
category = "Rooms",
room_id = room_id,
display_name = self._get_room_name(room) or "",
avatar_url = room.gen_avatar_url or "",
topic = room.topic or "",
typing_members = typing,
)
for room_id, _ in resp.rooms.leave.items(): for ev in info.timeline.events:
rooms.RoomUpdated( is_member_ev = isinstance(ev, nio.RoomMemberEvent)
user_id = self.user_id,
category = "Left", if is_member_ev and ev.membership in ("leave", "ban"):
room_id = room_id, lev = ev
# left_event TODO
) up(self.user_id, "Left", self.rooms[room_id], info, left_event=lev)
async def onErrorResponse(self, resp: nio.ErrorResponse) -> None: async def onErrorResponse(self, resp: nio.ErrorResponse) -> None:
@ -250,7 +219,6 @@ class MatrixClient(nio.AsyncClient):
# Special %tokens for event contents: # Special %tokens for event contents:
# %S = sender's displayname # %S = sender's displayname
# %T = target (ev.state_key)'s displayname # %T = target (ev.state_key)'s displayname
# pylint: disable=unused-argument # pylint: disable=unused-argument
async def onRoomMessageText(self, room, ev, from_past=False) -> None: async def onRoomMessageText(self, room, ev, from_past=False) -> None:

View File

@ -57,11 +57,10 @@ HColumnLayout {
TypingMembersBar { TypingMembersBar {
Layout.fillWidth: true Layout.fillWidth: true
//Layout.preferredHeight: text ? implicitHeight : 0
} }
InviteBanner { InviteBanner {
visible: category === "Invites" visible: category == "Invites"
inviterId: roomInfo.inviterId inviterId: roomInfo.inviterId
} }
@ -75,10 +74,10 @@ HColumnLayout {
} }
//LeftBanner { //LeftBanner {
//visible: category === "Left" //visible: category == "Left"
//leftEvent: roomInfo.leftEvent //leftEvent: roomInfo.leftEvent
//} //}
//} }
// RoomSidePane { // RoomSidePane {
//id: roomSidePane //id: roomSidePane
@ -145,6 +144,5 @@ HColumnLayout {
//Layout.minimumWidth: theme.avatar.size //Layout.minimumWidth: theme.avatar.size
//Layout.maximumWidth: parent.width //Layout.maximumWidth: parent.width
//} //}
}
} }
} }

View File

@ -32,31 +32,17 @@ function onRoomUpdated(
"name": category "name": category
}) })
function roles(for_category) { function pop(for_category) {
return {"userId": user_id, "roomId": room_id, "category": for_category} rooms.popWhere(
{"userId": user_id, "roomId": room_id, "category": for_category}, 1
)
} }
if (category == "Invites") { if (category == "Invites") { pop("Rooms"); pop("Left") }
rooms.popWhere(roles("Rooms"), 1) else if (category == "Rooms") { pop("Invites"); pop("Left") }
rooms.popWhere(roles("Left"), 1) else if (category == "Left") { pop("Invites"); pop("Rooms") }
}
else if (category == "Rooms") {
rooms.popWhere(roles("Invites"), 1)
rooms.popWhere(roles("Left"), 1)
}
else if (category == "Left") {
var old_room = rooms.popWhere(roles("Invites"), 1)[0] ||
rooms.popWhere(roles("Rooms"), 1)[0]
if (old_room) { rooms.upsert({"userId": user_id, "roomId": room_id, "category": category},{
display_name = old_room.displayName
avatar_url = old_room.avatarUrl
topic = old_room.topic
inviter_id = old_room.inviterId
}
}
rooms.upsert(roles(category), {
"userId": user_id, "userId": user_id,
"category": category, "category": category,
"roomId": room_id, "roomId": room_id,
@ -65,7 +51,7 @@ function onRoomUpdated(
"topic": topic, "topic": topic,
"typingText": typingTextFor(typing_members, user_id), "typingText": typingTextFor(typing_members, user_id),
"inviterId": inviter_id, "inviterId": inviter_id,
"leftEvent": left_event, "leftEvent": left_event ? py.getattr(left_event, "__dict__") : {},
}) })
} }