Skip to content

Commit

Permalink
DistinctByParticipants SQL bug fix
Browse files Browse the repository at this point in the history
The previous SQL returned any conversation which is the superset of the
given participants. The fix is to add one more checking, which verifies each
conversation size is exactly the same as the number of given
participants.

Connects #201
  • Loading branch information
howawong authored and carmenlau committed Mar 1, 2018
1 parent a5da579 commit 34c90c1
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions chat/conversation_handlers.py
Expand Up @@ -37,20 +37,22 @@ def __validate_conversation(participants):
first_row = None
with db.conn() as conn:
result = conn.execute("""
SELECT c._id FROM
%(schema_name)s.conversation AS c
WHERE
c.distinct_by_participants = TRUE
AND
(
SELECT COUNT(DISTINCT uc.user)
FROM %(schema_name)s.user_conversation
AS uc
WHERE
uc.conversation = c._id
AND
uc.user IN %(user_ids)s
) = %(count)s
SELECT c._id
FROM %(schema_name)s.conversation AS c
WHERE c._id IN (
SELECT uc.conversation
FROM %(schema_name)s.user_conversation AS uc
WHERE uc.conversation IN (
SELECT uc.conversation
FROM %(schema_name)s.user_conversation
AS uc
WHERE uc.user IN %(user_ids)s
GROUP BY uc.conversation
HAVING
COUNT(DISTINCT uc.user) = %(count)s)
GROUP BY uc.conversation
HAVING COUNT(DISTINCT uc.user) = %(count)s)
ORDER BY c._created_at DESC
""", {'schema_name': AsIs(_get_schema_name()),
'user_ids': tuple(participants),
'count': len(participants)})
Expand Down

0 comments on commit 34c90c1

Please sign in to comment.