Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Conversation proteus verification #2178

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ AFTER UPDATE ON Client
BEGIN
UPDATE Conversation SET proteus_verification_status = (CASE (new.is_verified) WHEN 1 THEN "VERIFIED" ELSE "DEGRADED" END)
WHERE qualified_id IN (
SELECT id FROM (SELECT conv.qualified_id AS id, conv.proteus_verification_status AS verificationStatus, (COUNT(*) = SUM(CASE WHEN client.is_verified = 1 THEN 1 ELSE 0 END)) AS isActuallyVerified
SELECT id FROM (SELECT
conv.qualified_id AS id,
conv.proteus_verification_status AS verificationStatus,
-- +1 is to not count a current client, for checking if conversation is verified
(COUNT(*) = 1 + SUM(CASE WHEN client.is_verified = 1 THEN 1 ELSE 0 END)) AS isActuallyVerified
FROM Conversation AS conv
LEFT JOIN Member AS mem ON conv.qualified_id = mem.conversation
LEFT JOIN Client AS client ON client.user_id = mem.user
WHERE conv.qualified_id IN (SELECT Member.conversation FROM Member
LEFT JOIN Client ON Client.user_id = Member.user
WHERE Client.id = new.id)
AND mem.user IS NOT (SELECT SelfUser.id FROM SelfUser LIMIT 1)
GROUP BY conv.qualified_id)
WHERE (CASE (verificationStatus) WHEN "VERIFIED" THEN 1 ELSE 0 END) != isActuallyVerified
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ SELECT changes();
CREATE TRIGGER addMessageAfterProteusVerificationStatusChange
AFTER UPDATE ON Conversation
WHEN new.proteus_verification_status != old.proteus_verification_status
AND (new.proteus_verification_status = "VERIFIED" OR old.proteus_verification_status = "VERIFIED")
BEGIN
INSERT OR IGNORE INTO Message(id, content_type, conversation_id, creation_date, sender_user_id, sender_client_id, status, visibility)
VALUES(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,35 @@ SELECT mem.* FROM Member AS mem
JOIN Conversation AS con ON con.qualified_id = mem.conversation
JOIN User AS usr ON usr.qualified_id = mem.user
WHERE con.type == 'ONE_ON_ONE' AND user LIKE ('%@' || :domain) AND usr.defederated == 0;

CREATE TRIGGER updateConversationProteusVerificationStatusAfterMemeberChange
AFTER INSERT ON Member
BEGIN
UPDATE Conversation
SET proteus_verification_status =
(CASE (SELECT
(COUNT(*) = 1 + SUM(CASE WHEN client.is_verified = 1 THEN 1 ELSE 0 END) AND COUNT(*) > 1)
FROM Member AS mem
LEFT JOIN Client AS client ON client.user_id = mem.user
WHERE mem.conversation = new.conversation
GROUP BY mem.conversation)
WHEN 1 THEN "VERIFIED"
ELSE "DEGRADED" END)
WHERE qualified_id = new.conversation;
END;

CREATE TRIGGER updateConversationProteusVerificationStatusAfterMemeberDelete
AFTER DELETE ON Member
BEGIN
UPDATE Conversation
SET proteus_verification_status =
(CASE (SELECT
(COUNT(*) = 1 + SUM(CASE WHEN client.is_verified = 1 THEN 1 ELSE 0 END) AND COUNT(*) > 1)
FROM Member AS mem
LEFT JOIN Client AS client ON client.user_id = mem.user
WHERE mem.conversation = old.conversation
GROUP BY mem.conversation)
WHEN 1 THEN "VERIFIED"
ELSE "DEGRADED" END)
WHERE qualified_id = old.conversation;
END;
78 changes: 78 additions & 0 deletions persistence/src/commonMain/db_user/migrations/64.sqm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
DROP TRIGGER IF EXISTS updateConversationProteusVerificationStatus;
DROP TRIGGER IF EXISTS addMessageAfterProteusVerificationStatusChange;

CREATE TRIGGER updateConversationProteusVerificationStatus
AFTER UPDATE ON Client
BEGIN
UPDATE Conversation SET proteus_verification_status = (CASE (new.is_verified) WHEN 1 THEN "VERIFIED" ELSE "DEGRADED" END)
WHERE qualified_id IN (
SELECT id FROM (SELECT
conv.qualified_id AS id,
conv.proteus_verification_status AS verificationStatus,
-- +1 is to not count a current client, for checking if conversation is verified
(COUNT(*) = 1 + SUM(CASE WHEN client.is_verified = 1 THEN 1 ELSE 0 END)) AS isActuallyVerified
FROM Conversation AS conv
LEFT JOIN Member AS mem ON conv.qualified_id = mem.conversation
LEFT JOIN Client AS client ON client.user_id = mem.user
WHERE conv.qualified_id IN (SELECT Member.conversation FROM Member
LEFT JOIN Client ON Client.user_id = Member.user
WHERE Client.id = new.id)
GROUP BY conv.qualified_id)
WHERE (CASE (verificationStatus) WHEN "VERIFIED" THEN 1 ELSE 0 END) != isActuallyVerified
);
END;

CREATE TRIGGER addMessageAfterProteusVerificationStatusChange
AFTER UPDATE ON Conversation
WHEN new.proteus_verification_status != old.proteus_verification_status
AND (new.proteus_verification_status = "VERIFIED" OR old.proteus_verification_status = "VERIFIED")
BEGIN
INSERT OR IGNORE INTO Message(id, content_type, conversation_id, creation_date, sender_user_id, sender_client_id, status, visibility)
VALUES(
(SELECT lower(hex(randomblob(4)) || '-' || lower(hex(randomblob(2))) || '-4' ||
substr(lower(hex(randomblob(2))),2) || '-a' || substr(lower(hex(randomblob(2))),2)
|| '-' || lower(hex(randomblob(6))))),
(CASE (new.proteus_verification_status)
WHEN "VERIFIED" THEN "CONVERSATION_VERIFIED_PROTEUS"
ELSE "CONVERSATION_DEGRADED_PROTEUS" END
),
new.qualified_id,
(SELECT CAST((julianday('now') - 2440587.5) * 86400 * 1000 AS INTEGER)),
(SELECT SelfUser.id FROM SelfUser LIMIT 1),
(SELECT Client.id FROM Client WHERE Client.user_id = (SELECT SelfUser.id FROM SelfUser LIMIT 1) LIMIT 1),
"SENT",
"VISIBLE"
);
END;

CREATE TRIGGER updateConversationProteusVerificationStatusAfterMemeberChange
AFTER INSERT ON Member
BEGIN
UPDATE Conversation
SET proteus_verification_status =
(CASE (SELECT
(COUNT(*) = 1 + SUM(CASE WHEN client.is_verified = 1 THEN 1 ELSE 0 END) AND COUNT(*) > 1)
FROM Member AS mem
LEFT JOIN Client AS client ON client.user_id = mem.user
WHERE mem.conversation = new.conversation
GROUP BY mem.conversation)
WHEN 1 THEN "VERIFIED"
ELSE "DEGRADED" END)
WHERE qualified_id = new.conversation;
END;

CREATE TRIGGER updateConversationProteusVerificationStatusAfterMemeberDelete
AFTER DELETE ON Member
BEGIN
UPDATE Conversation
SET proteus_verification_status =
(CASE (SELECT
(COUNT(*) = 1 + SUM(CASE WHEN client.is_verified = 1 THEN 1 ELSE 0 END) AND COUNT(*) > 1)
FROM Member AS mem
LEFT JOIN Client AS client ON client.user_id = mem.user
WHERE mem.conversation = old.conversation
GROUP BY mem.conversation)
WHEN 1 THEN "VERIFIED"
ELSE "DEGRADED" END)
WHERE qualified_id = old.conversation;
END;
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ class ConversationDAOTest : BaseDatabaseTest() {
archived = false,
archivedDateTime = null,
mlsVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED,
proteusVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED,
proteusVerificationStatus = ConversationEntity.VerificationStatus.DEGRADED,
userSupportedProtocols = if (type == ConversationEntity.Type.ONE_ON_ONE) userEntity?.supportedProtocols else null,
userActiveOneOnOneConversationId = null,
)
Expand Down Expand Up @@ -1697,7 +1697,7 @@ class ConversationDAOTest : BaseDatabaseTest() {
archived = false,
archivedInstant = null,
mlsVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED,
proteusVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED
proteusVerificationStatus = ConversationEntity.VerificationStatus.DEGRADED
)
val conversationEntity2 = ConversationEntity(
QualifiedIDEntity("2", "wire.com"),
Expand All @@ -1724,7 +1724,7 @@ class ConversationDAOTest : BaseDatabaseTest() {
archived = false,
archivedInstant = null,
mlsVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED,
proteusVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED
proteusVerificationStatus = ConversationEntity.VerificationStatus.DEGRADED
)

val conversationEntity3 = ConversationEntity(
Expand Down Expand Up @@ -1754,7 +1754,7 @@ class ConversationDAOTest : BaseDatabaseTest() {
archived = false,
archivedInstant = null,
mlsVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED,
proteusVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED
proteusVerificationStatus = ConversationEntity.VerificationStatus.DEGRADED
)

val conversationEntity4 = ConversationEntity(
Expand Down Expand Up @@ -1784,7 +1784,7 @@ class ConversationDAOTest : BaseDatabaseTest() {
archived = false,
archivedInstant = null,
mlsVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED,
proteusVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED
proteusVerificationStatus = ConversationEntity.VerificationStatus.DEGRADED
)
val conversationEntity5 = ConversationEntity(
QualifiedIDEntity("5", "wire.com"),
Expand All @@ -1805,7 +1805,7 @@ class ConversationDAOTest : BaseDatabaseTest() {
archived = false,
archivedInstant = null,
mlsVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED,
proteusVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED
proteusVerificationStatus = ConversationEntity.VerificationStatus.DEGRADED
)
val conversationEntity6 = ConversationEntity(
QualifiedIDEntity("6", "wire.com"),
Expand Down Expand Up @@ -1834,7 +1834,7 @@ class ConversationDAOTest : BaseDatabaseTest() {
archived = false,
archivedInstant = null,
mlsVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED,
proteusVerificationStatus = ConversationEntity.VerificationStatus.NOT_VERIFIED
proteusVerificationStatus = ConversationEntity.VerificationStatus.DEGRADED
)

val member1 = MemberEntity(user1.id, MemberEntity.Role.Admin)
Expand Down
Loading