Use presence events to update Account presence
This commit is contained in:
parent
a1e5a1981d
commit
d5e0a3340d
|
@ -150,12 +150,13 @@ class Backend:
|
||||||
for i, account in enumerate(self.models["accounts"].values())
|
for i, account in enumerate(self.models["accounts"].values())
|
||||||
) + 1
|
) + 1
|
||||||
|
|
||||||
|
account = Account(client.user_id, order)
|
||||||
|
|
||||||
self.clients[client.user_id] = client
|
self.clients[client.user_id] = client
|
||||||
self.models["accounts"][client.user_id] = Account(
|
self.models["accounts"][client.user_id] = account
|
||||||
client.user_id,
|
|
||||||
order,
|
presence = self.presences.setdefault(client.user_id, Presence())
|
||||||
presence = Presence.State.online,
|
presence.members["account", client.user_id] = account
|
||||||
)
|
|
||||||
|
|
||||||
return client.user_id
|
return client.user_id
|
||||||
|
|
||||||
|
@ -167,7 +168,7 @@ class Backend:
|
||||||
device_id: str,
|
device_id: str,
|
||||||
homeserver: str = "https://matrix.org",
|
homeserver: str = "https://matrix.org",
|
||||||
order: int = -1,
|
order: int = -1,
|
||||||
presence: str = "online",
|
state: str = "online",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Create and register a `MatrixClient` with known account details."""
|
"""Create and register a `MatrixClient` with known account details."""
|
||||||
|
|
||||||
|
@ -176,10 +177,15 @@ class Backend:
|
||||||
user=user_id, homeserver=homeserver, device_id=device_id,
|
user=user_id, homeserver=homeserver, device_id=device_id,
|
||||||
)
|
)
|
||||||
|
|
||||||
self.clients[user_id] = client
|
account = Account(user_id, order)
|
||||||
self.models["accounts"][user_id] = 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]:
|
async def load_saved_accounts(self) -> List[str]:
|
||||||
|
@ -192,7 +198,7 @@ class Backend:
|
||||||
device_id = info["device_id"],
|
device_id = info["device_id"],
|
||||||
homeserver = info["homeserver"],
|
homeserver = info["homeserver"],
|
||||||
order = info.get("order", -1),
|
order = info.get("order", -1),
|
||||||
presence = info.get("presence", "online"),
|
state = info.get("presence", "online"),
|
||||||
)
|
)
|
||||||
return user_id
|
return user_id
|
||||||
|
|
||||||
|
|
|
@ -246,14 +246,14 @@ class MatrixClient(nio.AsyncClient):
|
||||||
user_id: str,
|
user_id: str,
|
||||||
token: str,
|
token: str,
|
||||||
device_id: str,
|
device_id: str,
|
||||||
presence: str = "online",
|
state: str = "online",
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Login to the server using an existing access token."""
|
"""Login to the server using an existing access token."""
|
||||||
|
|
||||||
response = nio.LoginResponse(user_id, device_id, token)
|
response = nio.LoginResponse(user_id, device_id, token)
|
||||||
await self.receive_response(response)
|
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())
|
self.start_task = asyncio.ensure_future(self._start())
|
||||||
|
|
||||||
|
|
||||||
|
@ -1222,9 +1222,10 @@ class MatrixClient(nio.AsyncClient):
|
||||||
|
|
||||||
await super().set_presence("offline" if presence == "invisible"
|
await super().set_presence("offline" if presence == "invisible"
|
||||||
else presence)
|
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:
|
async def import_keys(self, infile: str, passphrase: str) -> None:
|
||||||
|
|
|
@ -56,15 +56,24 @@ class Presence():
|
||||||
last_active_ago: int = -1
|
last_active_ago: int = -1
|
||||||
status_msg: str = ""
|
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):
|
def update_members(self):
|
||||||
for member in self.members.values():
|
for member in self.members.values():
|
||||||
|
if (
|
||||||
|
member.presence == self.State.invisible
|
||||||
|
) and (
|
||||||
|
self.presence == self.State.offline
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
|
||||||
member.presence = self.presence
|
member.presence = self.presence
|
||||||
member.status_msg = self.status_msg
|
member.status_msg = self.status_msg
|
||||||
member.last_active_ago = self.last_active_ago
|
member.last_active_ago = self.last_active_ago
|
||||||
member.currently_active = self.currently_active
|
member.currently_active = self.currently_active
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Account(ModelItem):
|
class Account(ModelItem):
|
||||||
"""A logged in matrix account."""
|
"""A logged in matrix account."""
|
||||||
|
@ -81,7 +90,10 @@ class Account(ModelItem):
|
||||||
local_unreads: bool = False
|
local_unreads: bool = False
|
||||||
local_highlights: bool = False
|
local_highlights: bool = False
|
||||||
|
|
||||||
presence: Presence.State = Presence.State.offline
|
presence: Presence.State = Presence.State.offline
|
||||||
|
currently_active: bool = False
|
||||||
|
last_active_ago: int = -1
|
||||||
|
status_msg: str = ""
|
||||||
|
|
||||||
def __lt__(self, other: "Account") -> bool:
|
def __lt__(self, other: "Account") -> bool:
|
||||||
"""Sort by order, then by user ID."""
|
"""Sort by order, then by user ID."""
|
||||||
|
|
|
@ -609,4 +609,8 @@ class NioCallbacks:
|
||||||
presence.members[room_id, ev.user_id] = member
|
presence.members[room_id, ev.user_id] = member
|
||||||
|
|
||||||
presence.update_members()
|
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
|
self.client.backend.presences[ev.user_id] = presence
|
||||||
|
|
Loading…
Reference in New Issue
Block a user