Check server presence support and comment code

This commit is contained in:
vslg 2020-07-02 21:25:24 -03:00 committed by miruka
parent d5e0a3340d
commit 91ed600997
6 changed files with 27 additions and 3 deletions

View File

@ -155,6 +155,7 @@ class Backend:
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
# Get or create presence for account
presence = self.presences.setdefault(client.user_id, Presence()) presence = self.presences.setdefault(client.user_id, Presence())
presence.members["account", client.user_id] = account presence.members["account", client.user_id] = account
@ -182,6 +183,7 @@ class Backend:
self.clients[user_id] = client self.clients[user_id] = client
self.models["accounts"][user_id] = account self.models["accounts"][user_id] = account
# Get or create presence for account
presence = self.presences.setdefault(user_id, Presence()) presence = self.presences.setdefault(user_id, Presence())
presence.members["account", user_id] = account presence.members["account", user_id] = account

View File

@ -253,6 +253,7 @@ class MatrixClient(nio.AsyncClient):
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)
# Need to await, else presence will flash on other clients
await self.set_presence(state) await self.set_presence(state)
self.start_task = asyncio.ensure_future(self._start()) self.start_task = asyncio.ensure_future(self._start())
@ -1223,6 +1224,8 @@ class MatrixClient(nio.AsyncClient):
await super().set_presence("offline" if presence == "invisible" await super().set_presence("offline" if presence == "invisible"
else presence) else presence)
# Assign invisible on model in here, because server will tell us we are
# offline
if presence == "invisible": if presence == "invisible":
self.models["accounts"][self.user_id].presence = \ self.models["accounts"][self.user_id].presence = \
Presence.State.invisible Presence.State.invisible
@ -1594,10 +1597,12 @@ class MatrixClient(nio.AsyncClient):
invited = member.invited, invited = member.invited,
) )
# Associate presence with member, if it exists
if user_id in self.backend.presences: if user_id in self.backend.presences:
presence.members[room.room_id, user_id] = member_item presence.members[room.room_id, user_id] = member_item
presence.update_members() # And then update presence fields
presence.update_members()
self.models[self.user_id, room.room_id, "members"][user_id] = \ self.models[self.user_id, room.room_id, "members"][user_id] = \
member_item member_item

View File

@ -61,6 +61,10 @@ class Presence():
def update_members(self): def update_members(self):
for member in self.members.values(): for member in self.members.values():
# Do not update if member is changing to invisible
# Because when setting invisible presence will give us presence
# event telling us we are offline, we do not want to set member
# presence to offline.
if ( if (
member.presence == self.State.invisible member.presence == self.State.invisible
) and ( ) and (
@ -90,6 +94,9 @@ class Account(ModelItem):
local_unreads: bool = False local_unreads: bool = False
local_highlights: bool = False local_highlights: bool = False
# For some reason, Account cannot inherit Presence, because QML keeps
# complaining type error on unknown file
presence_support: bool = False
presence: Presence.State = Presence.State.offline presence: Presence.State = Presence.State.offline
currently_active: bool = False currently_active: bool = False
last_active_ago: int = -1 last_active_ago: int = -1

View File

@ -133,7 +133,6 @@ class NioCallbacks:
DevicesUpdated(self.user_id) DevicesUpdated(self.user_id)
# Room events, invite events and misc events callbacks # Room events, invite events and misc events callbacks
async def onRoomMessageText( async def onRoomMessageText(
@ -600,6 +599,7 @@ class NioCallbacks:
presence.last_active_ago = ev.last_active_ago or -1 presence.last_active_ago = ev.last_active_ago or -1
presence.currently_active = ev.currently_active or False presence.currently_active = ev.currently_active or False
# Add all existing members related to this presence
for room_id in self.models[self.user_id, "rooms"]: for room_id in self.models[self.user_id, "rooms"]:
member = self.models[self.user_id, room_id, "members"].get( member = self.models[self.user_id, room_id, "members"].get(
ev.user_id, ev.user_id,
@ -608,9 +608,15 @@ class NioCallbacks:
if member: if member:
presence.members[room_id, ev.user_id] = member presence.members[room_id, ev.user_id] = member
# Update members and accounts
presence.update_members() presence.update_members()
# Check if presence event is ours
if ev.user_id in self.models["accounts"]: if ev.user_id in self.models["accounts"]:
# Servers that send presence events support presence
self.models["accounts"][ev.user_id].presence_support = True
# Save the presence for the next resume
await self.client.backend.saved_accounts.add(ev.user_id) 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

View File

@ -56,6 +56,7 @@ HMenu {
} }
HMenuItem { HMenuItem {
visible: presence
enabled: presence !== "unavailable" && firstSyncDone enabled: presence !== "unavailable" && firstSyncDone
icon.name: "user-presence" icon.name: "user-presence"
icon.color: theme.controls.presence.unavailable icon.color: theme.controls.presence.unavailable
@ -64,6 +65,7 @@ HMenu {
} }
HMenuItem { HMenuItem {
visible: presence
enabled: presence !== "invisible" && firstSyncDone enabled: presence !== "invisible" && firstSyncDone
icon.name: "user-presence" icon.name: "user-presence"
icon.color: theme.controls.presence.offline icon.color: theme.controls.presence.offline

View File

@ -137,7 +137,9 @@ HTile {
contextMenu: AccountContextMenu { contextMenu: AccountContextMenu {
userId: model.id userId: model.id
presence: model.presence presence: model.presence_support ? model.presence : null
// Gray out buttons before first sync
firstSyncDone: model.first_sync_done firstSyncDone: model.first_sync_done
} }