Sync receiver of DM channel at init time for existing portal

This commit is contained in:
Andrew Ferrazzutti 2022-03-20 03:12:17 -04:00
parent 60c47e5a20
commit b59b8a68c3
3 changed files with 31 additions and 3 deletions

View File

@ -49,7 +49,7 @@ from ..types.request import (
CommandResultDoneValue
)
from .types import PortalChannelInfo
from .types import PortalChannelInfo, UserInfoUnion
from .errors import InvalidAccessToken
from .error_helper import raise_unsuccessful_response
@ -214,6 +214,13 @@ class Client:
channel_id=channel_id.serialize()
)
async def get_participants(self, channel_id: Long) -> list[UserInfoUnion]:
return await self._api_user_request_result(
ResultListType(UserInfoUnion),
"get_participants",
channel_id=channel_id.serialize()
)
async def get_chats(self, channel_id: Long, sync_from: Long | None, limit: int | None) -> list[Chatlog]:
return await self._api_user_request_result(
ResultListType(Chatlog),

View File

@ -397,7 +397,10 @@ class Portal(DBPortal, BasePortal):
)
"""
async def _update_participants(self, source: u.User, participants: list[UserInfoUnion]) -> bool:
async def _update_participants(self, source: u.User, participants: list[UserInfoUnion] | None = None) -> bool:
if participants is None:
self.log.debug("Called _update_participants with no participants, fetching them now...")
participants = await source.client.get_participants(self.ktid)
changed = False
if not self._main_intent:
assert self.is_direct, "_main_intent for non-direct chat portal should have been set already"
@ -982,8 +985,14 @@ class Portal(DBPortal, BasePortal):
self.by_mxid[self.mxid] = self
if not self.is_direct:
self._main_intent = self.az.intent
elif self.mxid:
# TODO Save kt_sender in DB instead? Depends on if DM channels are shared...
user = await u.User.get_by_ktid(self.kt_receiver)
assert user, f"Found no user for this portal's receiver of {self.kt_receiver}"
# TODO Should this backfill? Useful for forgotten channels
await self._update_participants(user)
else:
self.log.debug("Not setting _main_intent of direct chat until after checking participant list")
self.log.debug("Not setting _main_intent of new direct chat until after checking participant list")
@classmethod
@async_getter_lock

View File

@ -351,6 +351,17 @@ export default class PeerClient {
})
}
/**
* @param {Object} req
* @param {string} req.mxid
* @param {Long} req.channel_id
*/
getParticipants = async (req) => {
const userClient = this.#getUser(req.mxid)
const talkChannel = userClient.getChannel(req.channel_id)
return await talkChannel.getAllLatestUserInfo()
}
/**
* @param {Object} req
* @param {string} req.mxid
@ -458,6 +469,7 @@ export default class PeerClient {
get_own_profile: this.getOwnProfile,
get_profile: this.getProfile,
get_portal_channel_info: this.getPortalChannelInfo,
get_participants: this.getParticipants,
get_chats: this.getChats,
send_message: this.sendMessage,
}[req.command] || this.handleUnknownCommand