changeset 4e4d0772ade4 in /home/hg/repos/gajim-plugins

author: lovetox <[email protected]>
branches: 
details:gajim-plugins?cmd=changeset;node=4e4d0772ade4
description: DB Migration and fixed douple entrys in DB

diffstat:

 omemo/omemo/encryption.py           |  29 +++++++++++++++++++++++++++++
 omemo/omemo/liteidentitykeystore.py |  19 +++++++++++++++++--
 2 files changed, 46 insertions(+), 2 deletions(-)

diffs (76 lines):

diff -r bb0b4f136aba -r 4e4d0772ade4 omemo/omemo/encryption.py
--- a/omemo/omemo/encryption.py Tue May 31 20:02:23 2016 +0200
+++ b/omemo/omemo/encryption.py Thu Jun 02 21:36:13 2016 +0200
@@ -91,5 +91,34 @@
                                      PRAGMA user_version=1;
                                      END TRANSACTION;
                                  """ % (create_table))
+            # Find all double entrys and delete them
+    if user_version(dbConn) < 2:
+        delete_dupes = """ DELETE FROM identities WHERE _id not in (
+                            SELECT MIN(_id)
+                            FROM identities
+                            GROUP BY
+                            recipient_id, public_key
+                            );
+                        """
+
+        dbConn.executescript(""" BEGIN TRANSACTION;
+                                     %s
+                                     PRAGMA user_version=2;
+                                     END TRANSACTION;
+                                 """ % (delete_dupes))
+
+    if user_version(dbConn) < 3:
+        # Create a UNIQUE INDEX so every public key/recipient_id tuple
+        # can only be once in the db
+        add_index = """ CREATE UNIQUE INDEX IF NOT EXISTS
+                        public_key_index
+                        ON identities (public_key, recipient_id);
+                    """
+
+        dbConn.executescript(""" BEGIN TRANSACTION;
+                                 %s
+                                 PRAGMA user_version=3;
+                                 END TRANSACTION;
+                             """ % (add_index))
 
     return dbConn
diff -r bb0b4f136aba -r 4e4d0772ade4 omemo/omemo/liteidentitykeystore.py
--- a/omemo/omemo/liteidentitykeystore.py       Tue May 31 20:02:23 2016 +0200
+++ b/omemo/omemo/liteidentitykeystore.py       Thu Jun 02 21:36:13 2016 +0200
@@ -22,6 +22,8 @@
 from axolotl.identitykeypair import IdentityKeyPair
 from axolotl.state.identitykeystore import IdentityKeyStore
 
+UNDECIDED = 2
+
 
 class LiteIdentityKeyStore(IdentityKeyStore):
     def __init__(self, dbConn):
@@ -67,10 +69,23 @@
         self.dbConn.commit()
 
     def saveIdentity(self, recipientId, identityKey):
-        q = "INSERT INTO identities (recipient_id, public_key) VALUES(?, ?)"
+        q = "INSERT INTO identities (recipient_id, public_key, trust) 
VALUES(?, ?, ?)"
         c = self.dbConn.cursor()
+
+        if not self.getIdentity(recipientId, identityKey):
+            c.execute(q, (recipientId,
+                          identityKey.getPublicKey().serialize(),
+                          UNDECIDED))
+            self.dbConn.commit()
+
+    def getIdentity(self, recipientId, identityKey):
+        q = "SELECT * FROM identities WHERE recipient_id = ? AND public_key = 
?"
+        c = self.dbConn.cursor()
+
         c.execute(q, (recipientId, identityKey.getPublicKey().serialize()))
-        self.dbConn.commit()
+        result = c.fetchone()
+
+        return result is not None
 
     def isTrustedIdentity(self, recipientId, identityKey):
         return True
_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to