Skip to content

Commit 129137b

Browse files
WofWcaHocuri
authored andcommitted
fix(jsonrpc): fix ChatListItem::is_self_in_group
Make it return the correct value for non-Group chats. This also should improve performance, thanks to the fact that we now don't have to query all the chat's contacts. Instead we only need confirm that self-contact is among the group members, and only when the chat type is `Group`.
1 parent ec3f765 commit 129137b

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

deltachat-jsonrpc/src/api/types/chat.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ pub struct FullChat {
6161
is_contact_request: bool,
6262

6363
is_device_chat: bool,
64+
/// Note that this is different from
65+
/// [`ChatListItem::is_self_in_group`](`crate::api::types::chat_list::ChatListItemFetchResult::ChatListItem::is_self_in_group`).
66+
/// This property should only be accessed
67+
/// when [`FullChat::chat_type`] is [`Chattype::Group`].
68+
//
69+
// We could utilize [`Chat::is_self_in_chat`],
70+
// but that would be an extra DB query.
6471
self_in_group: bool,
6572
is_muted: bool,
6673
ephemeral_timer: u32, //TODO look if there are more important properties in newer core versions

deltachat-jsonrpc/src/api/types/chat_list.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use anyhow::{Context, Result};
22
use deltachat::chat::{Chat, ChatId};
33
use deltachat::chatlist::get_last_message_for_chat;
44
use deltachat::constants::*;
5-
use deltachat::contact::{Contact, ContactId};
5+
use deltachat::contact::Contact;
66
use deltachat::{
77
chat::{get_chat_contacts, ChatVisibility},
88
chatlist::Chatlist,
@@ -126,11 +126,8 @@ pub(crate) async fn get_chat_list_item_by_id(
126126
None => (None, None),
127127
};
128128

129-
let chat_contacts = get_chat_contacts(ctx, chat_id).await?;
130-
131-
let self_in_group = chat_contacts.contains(&ContactId::SELF);
132-
133129
let (dm_chat_contact, was_seen_recently) = if chat.get_type() == Chattype::Single {
130+
let chat_contacts = get_chat_contacts(ctx, chat_id).await?;
134131
let contact = chat_contacts.first();
135132
let was_seen_recently = match contact {
136133
Some(contact) => Contact::get_by_id(ctx, *contact)
@@ -165,7 +162,7 @@ pub(crate) async fn get_chat_list_item_by_id(
165162
fresh_message_counter,
166163
is_self_talk: chat.is_self_talk(),
167164
is_device_talk: chat.is_device_talk(),
168-
is_self_in_group: self_in_group,
165+
is_self_in_group: chat.is_self_in_chat(ctx).await?,
169166
is_sending_location: chat.is_sending_locations(),
170167
is_archived: visibility == ChatVisibility::Archived,
171168
is_pinned: visibility == ChatVisibility::Pinned,

src/chat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1494,7 +1494,7 @@ impl Chat {
14941494
/// Checks if the user is part of a chat
14951495
/// and has basically the permissions to edit the chat therefore.
14961496
/// The function does not check if the chat type allows editing of concrete elements.
1497-
pub(crate) async fn is_self_in_chat(&self, context: &Context) -> Result<bool> {
1497+
pub async fn is_self_in_chat(&self, context: &Context) -> Result<bool> {
14981498
match self.typ {
14991499
Chattype::Single | Chattype::OutBroadcast | Chattype::Mailinglist => Ok(true),
15001500
Chattype::Group => is_contact_in_chat(context, self.id, ContactId::SELF).await,

0 commit comments

Comments
 (0)