aboutsummaryrefslogtreecommitdiff
path: root/plugins/omemo/src/signed_pre_key_store.vala
blob: d96ded1f9ba360ee829837530f495568d23c7812 (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
using Qlite;
using Signal;

namespace Dino.Plugins.Omemo {

private class BackedSignedPreKeyStore : SimpleSignedPreKeyStore {
    private Database db;
    private int identity_id;

    public BackedSignedPreKeyStore(Database db, int identity_id) {
        this.db = db;
        this.identity_id = identity_id;
        init();
    }

    private void init() {
        try {
            foreach (Row row in db.signed_pre_key.select().with(db.signed_pre_key.identity_id, "=", identity_id)) {
                store_signed_pre_key(row[db.signed_pre_key.signed_pre_key_id], Base64.decode(row[db.signed_pre_key.record_base64]));
            }
        } catch (Error e) {
            print(@"OMEMO: Error while initializing signed pre key store: $(e.message)\n");
        }

        signed_pre_key_stored.connect(on_signed_pre_key_stored);
        signed_pre_key_deleted.connect(on_signed_pre_key_deleted);
    }

    public void on_signed_pre_key_stored(SignedPreKeyStore.Key key) {
        db.signed_pre_key.insert().or("REPLACE")
                .value(db.signed_pre_key.identity_id, identity_id)
                .value(db.signed_pre_key.signed_pre_key_id, (int) key.key_id)
                .value(db.signed_pre_key.record_base64, Base64.encode(key.record))
                .perform();
    }

    public void on_signed_pre_key_deleted(SignedPreKeyStore.Key key) {
        db.signed_pre_key.delete()
                .with(db.signed_pre_key.identity_id, "=", identity_id)
                .with(db.signed_pre_key.signed_pre_key_id, "=", (int) key.key_id)
                .perform();
    }
}

}