aboutsummaryrefslogtreecommitdiff
path: root/plugins/omemo/src/logic/session_store.vala
blob: 2c7ef6426274570e0f06f8a9ef795226a906bd84 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 {
            foreach (Row row in db.session.select().with(db.session.identity_id, "=", identity_id)) {
                Address addr = new Address(row[db.session.address_name], row[db.session.device_id]);
                store_session(addr, Base64.decode(row[db.session.record_base64]));
                addr.device_id = 0;
            }
        } catch (Error e) {
            print("Error while initializing session store: %s", e.message);
        }

        session_stored.connect(on_session_stored);
        session_removed.connect(on_session_deleted);
    }

    public void on_session_stored(SessionStore.Session session) {
        db.session.upsert()
                .value(db.session.identity_id, identity_id, true)
                .value(db.session.address_name, session.name, true)
                .value(db.session.device_id, session.device_id, true)
                .value(db.session.record_base64, Base64.encode(session.record))
                .perform();
    }

    public void on_session_deleted(SessionStore.Session session) {
        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();
    }
}

}