From 945ed3f7cb1f23073a8a01133f528cead56d7b4a Mon Sep 17 00:00:00 2001 From: Efim Poberezkin <8711996+efim-poberezkin@users.noreply.github.com> Date: Mon, 31 Jan 2022 13:20:26 +0400 Subject: [PATCH] fix queries returning duplicate contacts (#242) --- src/Simplex/Chat/Store.hs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Simplex/Chat/Store.hs b/src/Simplex/Chat/Store.hs index e7f4db161..c26acf460 100644 --- a/src/Simplex/Chat/Store.hs +++ b/src/Simplex/Chat/Store.hs @@ -1861,9 +1861,16 @@ getDirectChatPreviews_ db User {userId} = do LEFT JOIN chat_items ci ON ci.contact_id = CIMaxDates.contact_id AND ci.item_ts = CIMaxDates.MaxDate WHERE ct.user_id = ? + AND c.connection_id IN ( + SELECT cc.connection_id + FROM connections cc + WHERE cc.user_id = ct.user_id AND cc.contact_id = ct.contact_id AND (cc.conn_status = ? OR cc.conn_status = ?) + ORDER BY cc.connection_id DESC + LIMIT 1 + ) ORDER BY ci.item_ts DESC |] - (Only userId) + (userId, ConnReady, ConnSndReady) where toDirectChatPreview :: TimeZone -> ContactRow :. MaybeChatItemRow -> AChat toDirectChatPreview tz (contactRow :. ciRow_) = @@ -1956,10 +1963,12 @@ getContact_ db userId contactId = c.contact_id, c.group_member_id, c.snd_file_id, c.rcv_file_id, c.user_contact_link_id, c.created_at FROM contacts ct JOIN contact_profiles cp ON ct.contact_profile_id = cp.contact_profile_id - JOIN connections c ON c.contact_id = ct.contact_id - WHERE ct.user_id = ? AND ct.contact_id = ? + LEFT JOIN connections c ON c.contact_id = ct.contact_id + WHERE ct.user_id = ? AND ct.contact_id = ? AND (c.conn_status = ? OR c.conn_status = ?) + ORDER BY c.connection_id DESC + LIMIT 1 |] - (userId, contactId) + (userId, contactId, ConnReady, ConnSndReady) ) getDirectChatItems_ :: DB.Connection -> UserId -> Int64 -> IO [CChatItem 'CTDirect]