Save status_msg to accounts.json

Restore it if there is not any status_msg set on
the server
This commit is contained in:
vslg 2020-07-16 17:09:14 -03:00
parent 292d88a9bf
commit d57414c06c
5 changed files with 70 additions and 30 deletions

View File

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

View File

@ -277,6 +277,7 @@ class MatrixClient(nio.AsyncClient):
token: str, token: str,
device_id: str, device_id: str,
state: str = "online", state: str = "online",
status_msg: str = "",
) -> None: ) -> None:
"""Login to the server using an existing access token.""" """Login to the server using an existing access token."""
@ -286,6 +287,7 @@ class MatrixClient(nio.AsyncClient):
self._presence = "offline" if state == "invisible" else state self._presence = "offline" if state == "invisible" else state
account.presence = Presence.State(state) account.presence = Presence.State(state)
account.status_msg = status_msg
if state != "offline": if state != "offline":
account.connecting = True account.connecting = True
@ -389,6 +391,12 @@ class MatrixClient(nio.AsyncClient):
async def _stop(self) -> None: async def _stop(self) -> None:
"""Stop client tasks. Will prevent client to receive further events.""" """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 = ( tasks = (
self.profile_task, self.profile_task,
self.sync_task, self.sync_task,
@ -404,12 +412,6 @@ class MatrixClient(nio.AsyncClient):
self.first_sync_done.clear() 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: async def update_own_profile(self) -> None:
"""Fetch our profile from server and Update our model `Account`.""" """Fetch our profile from server and Update our model `Account`."""
@ -1332,6 +1334,7 @@ class MatrixClient(nio.AsyncClient):
status_msg = status_msg if status_msg is not None else ( status_msg = status_msg if status_msg is not None else (
self.models["accounts"][self.user_id].status_msg self.models["accounts"][self.user_id].status_msg
) )
set_status_msg = True
if presence == "offline": if presence == "offline":
# Do not do anything if account is offline and setting to offline # Do not do anything if account is offline and setting to offline
@ -1348,9 +1351,13 @@ class MatrixClient(nio.AsyncClient):
account.presence == Presence.State.offline and account.presence == Presence.State.offline and
presence != "offline" presence != "offline"
): ):
# In this case we will not run super().set_presence()
set_status_msg = False
account.connecting = True account.connecting = True
self.start_task = asyncio.ensure_future(self._start()) self.start_task = asyncio.ensure_future(self._start())
self._presence = "offline" if presence == "invisible" else presence
if ( if (
Presence.State(presence) != account.presence and Presence.State(presence) != account.presence and
presence != "offline" presence != "offline"
@ -1363,11 +1370,12 @@ class MatrixClient(nio.AsyncClient):
if save: if save:
account.save_presence = True account.save_presence = True
await self.backend.saved_accounts.update( await self.backend.saved_accounts.update(
self.user_id, presence=presence, self.user_id, presence=presence, status_msg=status_msg,
) )
else: else:
account.save_presence = False account.save_presence = False
if set_status_msg:
await super().set_presence( await super().set_presence(
"offline" if presence == "invisible" else presence, "offline" if presence == "invisible" else presence,
status_msg, status_msg,

View File

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

View File

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

View File

@ -194,6 +194,7 @@ class Accounts(JSONDataFile):
"device_id": client.device_id, "device_id": client.device_id,
"enabled": True, "enabled": True,
"presence": account.presence.value, "presence": account.presence.value,
"status_msg": account.status_msg,
"order": account.order, "order": account.order,
}, },
}) })
@ -205,6 +206,7 @@ class Accounts(JSONDataFile):
enabled: Optional[str] = None, enabled: Optional[str] = None,
presence: Optional[str] = None, presence: Optional[str] = None,
order: Optional[int] = None, order: Optional[int] = None,
status_msg: Optional[str] = None,
) -> None: ) -> None:
"""Update an account if found in the config file and write to disk.""" """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: if order is not None:
saved[user_id]["order"] = order saved[user_id]["order"] = order
if status_msg is not None:
saved[user_id]["status_msg"] = status_msg
await self.write({**saved}) await self.write({**saved})