fix: Chat::sync_contacts(): Fetch contact addresses in a single query

In order to protect from races with contacts removal and (just in case) dangling contact ids in
`chats_contacts` table.
This commit is contained in:
iequidoo
2023-11-14 21:53:21 -03:00
committed by iequidoo
parent 7c4c980409
commit 607b9e55a9

View File

@@ -1935,11 +1935,18 @@ impl Chat {
/// Sends a `SyncAction` synchronising chat contacts to other devices.
pub(crate) async fn sync_contacts(&self, context: &Context) -> Result<()> {
let mut addrs = Vec::new();
for contact_id in get_chat_contacts(context, self.id).await? {
let contact = Contact::get_by_id(context, contact_id).await?;
addrs.push(contact.get_addr().to_string());
}
let addrs = context
.sql
.query_map(
"SELECT c.addr \
FROM contacts c INNER JOIN chats_contacts cc \
ON c.id=cc.contact_id \
WHERE cc.chat_id=?",
(self.id,),
|row| row.get::<_, String>(0),
|addrs| addrs.collect::<Result<Vec<_>, _>>().map_err(Into::into),
)
.await?;
self.sync(context, SyncAction::SetContacts(addrs)).await
}