Optimize model upsert, ensure right type for roles
This commit is contained in:
parent
6b83d79f16
commit
07438150db
@ -1,6 +1,6 @@
|
||||
from datetime import datetime
|
||||
from enum import auto
|
||||
from typing import Dict, Optional, Sequence, Type, Union
|
||||
from typing import Dict, Sequence, Type, Union
|
||||
|
||||
from dataclasses import dataclass, field
|
||||
|
||||
@ -11,15 +11,15 @@ from .event import AutoStrEnum, Event
|
||||
|
||||
@dataclass
|
||||
class RoomUpdated(Event):
|
||||
user_id: str = field()
|
||||
category: str = field()
|
||||
room_id: str = field()
|
||||
display_name: Optional[str] = None
|
||||
avatar_url: Optional[str] = None
|
||||
topic: Optional[str] = None
|
||||
user_id: str = field()
|
||||
category: str = field()
|
||||
room_id: str = field()
|
||||
display_name: str = ""
|
||||
avatar_url: str = ""
|
||||
topic: str = ""
|
||||
|
||||
inviter: Optional[str] = None
|
||||
left_event: Optional[Dict[str, str]] = None
|
||||
inviter: str = ""
|
||||
left_event: Dict[str, str] = field(default_factory=dict)
|
||||
|
||||
|
||||
@dataclass
|
||||
@ -53,8 +53,6 @@ class ContentType(AutoStrEnum):
|
||||
location = auto()
|
||||
|
||||
|
||||
|
||||
|
||||
@dataclass
|
||||
class TimelineEventReceived(Event):
|
||||
event_type: Type[nio.Event] = field()
|
||||
@ -69,7 +67,7 @@ class TimelineEventReceived(Event):
|
||||
show_name_line: bool = False
|
||||
translatable: Union[bool, Sequence[str]] = True
|
||||
|
||||
target_user_id: Optional[str] = None
|
||||
target_user_id: str = ""
|
||||
|
||||
@classmethod
|
||||
def from_nio(cls, room, ev, **fields) -> "TimelineEventReceived":
|
||||
@ -79,7 +77,7 @@ class TimelineEventReceived(Event):
|
||||
event_id = ev.event_id,
|
||||
sender_id = ev.sender,
|
||||
date = datetime.fromtimestamp(ev.server_timestamp / 1000),
|
||||
target_user_id = getattr(ev, "state_key", None),
|
||||
target_user_id = getattr(ev, "state_key", "") or "",
|
||||
**fields
|
||||
)
|
||||
|
||||
|
@ -22,29 +22,29 @@ class AccountDeleted(Event):
|
||||
|
||||
@dataclass
|
||||
class UserUpdated(Event):
|
||||
user_id: str = field()
|
||||
display_name: Optional[str] = None
|
||||
avatar_url: Optional[str] = None
|
||||
status_message: Optional[str] = None
|
||||
user_id: str = field()
|
||||
display_name: str = ""
|
||||
avatar_url: str = ""
|
||||
status_message: str = ""
|
||||
|
||||
|
||||
# Devices
|
||||
|
||||
class Trust(Enum):
|
||||
blacklisted = -1
|
||||
undecided = 0
|
||||
trusted = 1
|
||||
undecided = 0
|
||||
trusted = 1
|
||||
|
||||
|
||||
@dataclass
|
||||
class DeviceUpdated(Event):
|
||||
user_id: str = field()
|
||||
device_id: str = field()
|
||||
ed25519_key: str = field()
|
||||
trust: Trust = Trust.undecided
|
||||
display_name: Optional[str] = None
|
||||
last_seen_ip: Optional[str] = None
|
||||
last_seen_date: Optional[datetime] = None
|
||||
user_id: str = field()
|
||||
device_id: str = field()
|
||||
ed25519_key: str = field()
|
||||
trust: Trust = Trust.undecided
|
||||
display_name: str = ""
|
||||
last_seen_ip: str = ""
|
||||
last_seen_date: datetime = field(default_factory=lambda: datetime(1, 1, 1))
|
||||
|
||||
|
||||
@dataclass
|
||||
|
@ -102,6 +102,7 @@ class MatrixClient(nio.AsyncClient):
|
||||
|
||||
|
||||
async def request_user_update_event(self, user_id: str) -> None:
|
||||
print("Requesting user profile:", user_id)
|
||||
response = await self.get_profile(user_id)
|
||||
|
||||
if isinstance(response, nio.ProfileGetError):
|
||||
@ -109,9 +110,9 @@ class MatrixClient(nio.AsyncClient):
|
||||
|
||||
users.UserUpdated(
|
||||
user_id = user_id,
|
||||
display_name = getattr(response, "displayname", None),
|
||||
avatar_url = getattr(response, "avatar_url", None),
|
||||
status_message = None, # TODO
|
||||
display_name = getattr(response, "displayname", "") or "",
|
||||
avatar_url = getattr(response, "avatar_url", "") or "",
|
||||
status_message = "", # TODO
|
||||
)
|
||||
|
||||
|
||||
@ -157,10 +158,10 @@ class MatrixClient(nio.AsyncClient):
|
||||
user_id = self.user_id,
|
||||
category = "Invites",
|
||||
room_id = room_id,
|
||||
display_name = self._get_room_name(room),
|
||||
avatar_url = room.gen_avatar_url,
|
||||
topic = room.topic,
|
||||
inviter = room.inviter,
|
||||
display_name = self._get_room_name(room) or "",
|
||||
avatar_url = room.gen_avatar_url or "",
|
||||
topic = room.topic or "",
|
||||
inviter = room.inviter or "",
|
||||
)
|
||||
|
||||
for room_id, _ in resp.rooms.join.items():
|
||||
@ -170,9 +171,9 @@ class MatrixClient(nio.AsyncClient):
|
||||
user_id = self.user_id,
|
||||
category = "Rooms",
|
||||
room_id = room_id,
|
||||
display_name = self._get_room_name(room),
|
||||
avatar_url = room.gen_avatar_url,
|
||||
topic = room.topic,
|
||||
display_name = self._get_room_name(room) or "",
|
||||
avatar_url = room.gen_avatar_url or "",
|
||||
topic = room.topic or "",
|
||||
)
|
||||
|
||||
for room_id, _ in resp.rooms.leave.items():
|
||||
@ -300,6 +301,15 @@ class MatrixClient(nio.AsyncClient):
|
||||
|
||||
|
||||
async def onRoomMemberEvent(self, room, ev) -> None:
|
||||
# TODO: ignore for past events
|
||||
if ev.content["membership"] != "leave":
|
||||
users.UserUpdated(
|
||||
user_id = ev.state_key,
|
||||
display_name = ev.content["displayname"] or "",
|
||||
avatar_url = ev.content["avatar_url"] or "",
|
||||
status_message = "", # TODO
|
||||
)
|
||||
|
||||
co = await self._get_room_member_event_content(ev)
|
||||
TimelineEventReceived.from_nio(room, ev, content=co)
|
||||
|
||||
|
@ -81,10 +81,24 @@ SortFilterProxyModel {
|
||||
return model.get(model.count)
|
||||
}
|
||||
|
||||
if (update_if_exist != false) {
|
||||
model.set(indices[0], new_item)
|
||||
}
|
||||
return model.get(indices[0])
|
||||
var existing = model.get(indices[0])
|
||||
if (update_if_exist == false) { return existing }
|
||||
|
||||
// Really update only if existing and new item have a difference
|
||||
for (var role in existing) {
|
||||
if (Boolean(existing[role].getTime)) {
|
||||
if (existing[role].getTime() != new_item[role].getTime()) {
|
||||
model.set(indices[0], new_item)
|
||||
return existing
|
||||
}
|
||||
} else {
|
||||
if (existing[role] != new_item[role]) {
|
||||
model.set(indices[0], new_item)
|
||||
return existing
|
||||
}
|
||||
}
|
||||
}
|
||||
return existing
|
||||
}
|
||||
|
||||
function pop(index) {
|
||||
|
@ -35,7 +35,7 @@ HColumnLayout {
|
||||
RoomHeader {
|
||||
id: roomHeader
|
||||
displayName: roomInfo.displayName
|
||||
topic: roomInfo.topic || ""
|
||||
topic: roomInfo.topic
|
||||
|
||||
Layout.fillWidth: true
|
||||
Layout.preferredHeight: HStyle.avatar.size
|
||||
|
@ -72,7 +72,7 @@ function onTimelineEventReceived(
|
||||
"isLocalEcho": is_local_echo,
|
||||
"showNameLine": show_name_line,
|
||||
"translatable": translatable,
|
||||
"targetUserId": target_user_id || "",
|
||||
"targetUserId": target_user_id,
|
||||
}
|
||||
|
||||
// Replace any matching local echo
|
||||
|
@ -6,7 +6,6 @@ function onAccountDeleted(user_id) {
|
||||
accounts.popWhere({"userId": user_id}, 1)
|
||||
}
|
||||
|
||||
// TODO: get updated from nio rooms
|
||||
function onUserUpdated(user_id, display_name, avatar_url, status_message) {
|
||||
users.upsert({"userId": user_id}, {
|
||||
"userId": user_id,
|
||||
|
@ -40,7 +40,7 @@ Column {
|
||||
|
||||
HTextField {
|
||||
id: statusEdit
|
||||
text: user.statusMessage || ""
|
||||
text: user.statusMessage
|
||||
placeholderText: qsTr("Set status message")
|
||||
font.pixelSize: HStyle.fontSize.small
|
||||
background: null
|
||||
|
Loading…
Reference in New Issue
Block a user