Merge pull request #104 from vSLG/presence

Save status_msg to accounts.json
This commit is contained in:
miruka 2020-07-16 18:28:46 -04:00 committed by GitHub
commit 15de44f47a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 72 additions and 30 deletions

View File

@ -157,6 +157,7 @@ class Backend:
device_id: str,
homeserver: str = "https://matrix.org",
state: str = "online",
status_msg: str = "",
) -> None:
"""Create and register a `MatrixClient` with known account details."""
@ -167,7 +168,7 @@ class Backend:
self.clients[user_id] = client
await client.resume(user_id, token, device_id, state)
await client.resume(user_id, token, device_id, state, status_msg)
async def load_saved_accounts(self) -> List[str]:
@ -185,6 +186,7 @@ class Backend:
device_id = info["device_id"],
homeserver = info["homeserver"],
state = info.get("presence", "online"),
status_msg = info.get("status_msg", ""),
)
return user_id

View File

@ -276,10 +276,11 @@ class MatrixClient(nio.AsyncClient):
async def resume(
self,
user_id: str,
token: str,
device_id: str,
state: str = "online",
user_id: str,
token: str,
device_id: str,
state: str = "online",
status_msg: str = "",
) -> None:
"""Login to the server using an existing access token."""
@ -287,8 +288,9 @@ class MatrixClient(nio.AsyncClient):
account = self.models["accounts"][user_id]
await self.receive_response(response)
self._presence = "offline" if state == "invisible" else state
account.presence = Presence.State(state)
self._presence = "offline" if state == "invisible" else state
account.presence = Presence.State(state)
account.status_msg = status_msg
if state != "offline":
account.connecting = True
@ -392,6 +394,12 @@ class MatrixClient(nio.AsyncClient):
async def _stop(self) -> None:
"""Stop client tasks. Will prevent client to receive further events."""
# Remove account model from presence update
presence = self.backend.presences.get(self.user_id, None)
if presence:
presence.account = None
tasks = (
self.profile_task,
self.sync_task,
@ -407,12 +415,6 @@ class MatrixClient(nio.AsyncClient):
self.first_sync_done.clear()
# Remove account model from presence update
presence = self.backend.presences.get(self.user_id, None)
if presence:
presence.account = None
async def update_own_profile(self) -> None:
"""Fetch our profile from server and Update our model `Account`."""
@ -1345,6 +1347,7 @@ class MatrixClient(nio.AsyncClient):
status_msg = status_msg if status_msg is not None else (
self.models["accounts"][self.user_id].status_msg
)
set_status_msg = True
if presence == "offline":
# Do not do anything if account is offline and setting to offline
@ -1361,9 +1364,13 @@ class MatrixClient(nio.AsyncClient):
account.presence == Presence.State.offline and
presence != "offline"
):
# In this case we will not run super().set_presence()
set_status_msg = False
account.connecting = True
self.start_task = asyncio.ensure_future(self._start())
self._presence = "offline" if presence == "invisible" else presence
if (
Presence.State(presence) != account.presence and
presence != "offline"
@ -1376,15 +1383,18 @@ class MatrixClient(nio.AsyncClient):
if save:
account.save_presence = True
await self.backend.saved_accounts.update(
self.user_id, presence=presence,
self.user_id, presence=presence, status_msg=status_msg,
)
else:
account.save_presence = False
await super().set_presence(
"offline" if presence == "invisible" else presence,
status_msg,
)
if set_status_msg:
account.status_msg = status_msg
await super().set_presence(
"offline" if presence == "invisible" else presence,
status_msg,
)
async def import_keys(self, infile: str, passphrase: str) -> None:

View File

@ -116,10 +116,12 @@ class Presence:
):
return
self.account.presence = self.presence if (
self.account.presence != self.State.echo_invisible
) else self.State.invisible
self.account.status_msg = self.status_msg
if self.account.presence == self.State.echo_invisible:
self.account.presence = self.State.invisible
else:
self.account.presence = self.presence
self.account.status_msg = self.status_msg
self.account.last_active_at = self.last_active_at
self.account.currently_active = self.currently_active

View File

@ -1,5 +1,6 @@
# SPDX-License-Identifier: LGPL-3.0-or-later
import asyncio
import json
import logging as log
from dataclasses import dataclass, field
@ -742,6 +743,8 @@ class NioCallbacks:
# Check if presence event is ours
if (
ev.user_id in self.models["accounts"] and
self.models["accounts"][ev.user_id].presence !=
Presence.State.offline and
not (
presence.presence == Presence.State.offline and
self.models["accounts"][ev.user_id].presence !=
@ -750,6 +753,20 @@ class NioCallbacks:
):
account = self.models["accounts"][ev.user_id]
# Set status_msg if none is set on the server and we have one
if (
not presence.status_msg and
account.status_msg and
ev.user_id in self.client.backend.clients and
account.presence != Presence.State.echo_invisible
):
asyncio.ensure_future(
self.client.backend.clients[ev.user_id].set_presence(
presence.presence.value,
account.status_msg,
),
)
# Do not fight back presence from other clients
self.client.backend.clients[ev.user_id]._presence = ev.presence
@ -758,11 +775,17 @@ class NioCallbacks:
# Save the presence for the next resume
if account.save_presence:
status_msg = presence.status_msg
state = presence.presence
if account.presence == Presence.State.echo_invisible:
status_msg = account.status_msg
state = Presence.State.invisible
await self.client.backend.saved_accounts.update(
user_id = ev.user_id,
presence = presence.presence.value if (
account.presence != Presence.State.echo_invisible
) else "invisible",
user_id = ev.user_id,
status_msg = status_msg,
presence = state.value,
)
presence.update_account()

View File

@ -194,6 +194,7 @@ class Accounts(JSONDataFile):
"device_id": client.device_id,
"enabled": True,
"presence": account.presence.value,
"status_msg": account.status_msg,
"order": account.order,
},
})
@ -201,10 +202,11 @@ class Accounts(JSONDataFile):
async def update(
self,
user_id: str,
enabled: Optional[str] = None,
presence: Optional[str] = None,
order: Optional[int] = None,
user_id: str,
enabled: Optional[str] = None,
presence: Optional[str] = None,
order: Optional[int] = None,
status_msg: Optional[str] = None,
) -> None:
"""Update an account if found in the config file and write to disk."""
@ -222,6 +224,9 @@ class Accounts(JSONDataFile):
if order is not None:
saved[user_id]["order"] = order
if status_msg is not None:
saved[user_id]["status_msg"] = status_msg
await self.write({**saved})