Use presence events to update Account presence

This commit is contained in:
vslg 2020-07-02 18:28:41 -03:00 committed by miruka
parent a1e5a1981d
commit d5e0a3340d
4 changed files with 39 additions and 16 deletions

View File

@ -150,12 +150,13 @@ class Backend:
for i, account in enumerate(self.models["accounts"].values())
) + 1
account = Account(client.user_id, order)
self.clients[client.user_id] = client
self.models["accounts"][client.user_id] = Account(
client.user_id,
order,
presence = Presence.State.online,
)
self.models["accounts"][client.user_id] = account
presence = self.presences.setdefault(client.user_id, Presence())
presence.members["account", client.user_id] = account
return client.user_id
@ -167,7 +168,7 @@ class Backend:
device_id: str,
homeserver: str = "https://matrix.org",
order: int = -1,
presence: str = "online",
state: str = "online",
) -> None:
"""Create and register a `MatrixClient` with known account details."""
@ -176,10 +177,15 @@ class Backend:
user=user_id, homeserver=homeserver, device_id=device_id,
)
self.clients[user_id] = client
self.models["accounts"][user_id] = Account(user_id, order)
account = Account(user_id, order)
await client.resume(user_id, token, device_id, presence)
self.clients[user_id] = client
self.models["accounts"][user_id] = account
presence = self.presences.setdefault(user_id, Presence())
presence.members["account", user_id] = account
await client.resume(user_id, token, device_id, state)
async def load_saved_accounts(self) -> List[str]:
@ -192,7 +198,7 @@ class Backend:
device_id = info["device_id"],
homeserver = info["homeserver"],
order = info.get("order", -1),
presence = info.get("presence", "online"),
state = info.get("presence", "online"),
)
return user_id

View File

@ -246,14 +246,14 @@ class MatrixClient(nio.AsyncClient):
user_id: str,
token: str,
device_id: str,
presence: str = "online",
state: str = "online",
) -> None:
"""Login to the server using an existing access token."""
response = nio.LoginResponse(user_id, device_id, token)
await self.receive_response(response)
asyncio.ensure_future(self.set_presence(presence))
await self.set_presence(state)
self.start_task = asyncio.ensure_future(self._start())
@ -1222,9 +1222,10 @@ class MatrixClient(nio.AsyncClient):
await super().set_presence("offline" if presence == "invisible"
else presence)
self.models["accounts"][self.user].presence = Presence.State(presence)
await self.backend.saved_accounts.add(self.user)
if presence == "invisible":
self.models["accounts"][self.user_id].presence = \
Presence.State.invisible
async def import_keys(self, infile: str, passphrase: str) -> None:

View File

@ -56,15 +56,24 @@ class Presence():
last_active_ago: int = -1
status_msg: str = ""
members: Dict[Tuple[str, str], "Member"] = field(default_factory=dict)
members: Dict[Tuple[str, str], Union["Member", "Account"]] = \
field(default_factory=dict)
def update_members(self):
for member in self.members.values():
if (
member.presence == self.State.invisible
) and (
self.presence == self.State.offline
):
continue
member.presence = self.presence
member.status_msg = self.status_msg
member.last_active_ago = self.last_active_ago
member.currently_active = self.currently_active
@dataclass
class Account(ModelItem):
"""A logged in matrix account."""
@ -82,6 +91,9 @@ class Account(ModelItem):
local_highlights: bool = False
presence: Presence.State = Presence.State.offline
currently_active: bool = False
last_active_ago: int = -1
status_msg: str = ""
def __lt__(self, other: "Account") -> bool:
"""Sort by order, then by user ID."""

View File

@ -609,4 +609,8 @@ class NioCallbacks:
presence.members[room_id, ev.user_id] = member
presence.update_members()
if ev.user_id in self.models["accounts"]:
await self.client.backend.saved_accounts.add(ev.user_id)
self.client.backend.presences[ev.user_id] = presence