From a9ea0e9f87e71c60bc570066525d3e3634fbdcc0 Mon Sep 17 00:00:00 2001 From: Marvin W Date: Sun, 12 Mar 2017 02:28:23 +0100 Subject: Split OMEMO plug-in into files, various fixes --- plugins/omemo/src/session_store.vala | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 plugins/omemo/src/session_store.vala (limited to 'plugins/omemo/src/session_store.vala') diff --git a/plugins/omemo/src/session_store.vala b/plugins/omemo/src/session_store.vala new file mode 100644 index 00000000..f70e16ea --- /dev/null +++ b/plugins/omemo/src/session_store.vala @@ -0,0 +1,58 @@ +using Signal; +using Qlite; + +namespace Dino.Plugins.Omemo { + +private class BackedSessionStore : SimpleSessionStore { + private Database db; + private int identity_id; + + public BackedSessionStore(Database db, int identity_id) { + this.db = db; + this.identity_id = identity_id; + init(); + } + + private void init() { + try { + Address addr = new Address(); + foreach (Row row in db.session.select().with(db.session.identity_id, "=", identity_id)) { + addr.name = row[db.session.address_name]; + addr.device_id = row[db.session.device_id]; + store_session(addr, Base64.decode(row[db.session.record_base64])); + } + } catch (Error e) { + print(@"OMEMO: Error while initializing session store: $(e.message)\n"); + } + + session_stored.connect(on_session_stored); + session_removed.connect(on_session_deleted); + } + + public void on_session_stored(SessionStore.Session session) { + try { + db.session.insert().or("REPLACE") + .value(db.session.identity_id, identity_id) + .value(db.session.address_name, session.name) + .value(db.session.device_id, session.device_id) + .value(db.session.record_base64, Base64.encode(session.record)) + .perform(); + } catch (Error e) { + print(@"OMEMO: Error while updating session store: $(e.message)\n"); + } + } + + public void on_session_deleted(SessionStore.Session session) { + try { + db.session.delete() + .with(db.session.identity_id, "=", identity_id) + .with(db.session.address_name, "=", session.name) + .with(db.session.device_id, "=", session.device_id) + .perform(); + } catch (Error e) { + print(@"OMEMO: Error while updating session store: $(e.message)\n"); + } + } +} + +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf