aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service/entity_capabilities_storage.vala
blob: c6d0bbbd8af2f4c2b7a0465e0528334c1e5cf6a8 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using Gee;
using Qlite;
using Xmpp;
using Xmpp.Xep.ServiceDiscovery;

namespace Dino {

public class EntityCapabilitiesStorage : Xep.EntityCapabilities.Storage, Object {

    private Database db;
    private HashMap<string, Gee.List<string>> features_cache = new HashMap<string, Gee.List<string>>();
    private HashMap<string, Identity> identity_cache = new HashMap<string, Identity>();

    public EntityCapabilitiesStorage(Database db) {
        this.db = db;
    }

    public void store_features(string entity, Gee.List<string> features) {
        if (features_cache.has_key(entity)) return;

        foreach (string feature in features) {
            db.entity_feature.insert()
                    .value(db.entity_feature.entity, entity)
                    .value(db.entity_feature.feature, feature)
                    .perform();
        }
    }

    public void store_identities(string entity, Gee.Set<Identity> identities) {
        foreach (Identity identity in identities) {
            if (identity.category == Identity.CATEGORY_CLIENT) {
                db.entity_identity.insert()
                        .value(db.entity_identity.entity, entity)
                        .value(db.entity_identity.category, identity.category)
                        .value(db.entity_identity.type, identity.type_)
                        .value(db.entity_identity.entity_name, identity.name)
                        .perform();
                return;
            }
        }
    }

    public Gee.List<string> get_features(string entity) {
        Gee.List<string>? features = features_cache[entity];
        if (features != null) {
            return features;
        }

        features = new ArrayList<string>();
        foreach (Row row in db.entity_feature.select({db.entity_feature.feature}).with(db.entity_feature.entity, "=", entity)) {
            features.add(row[db.entity_feature.feature]);
        }
        features_cache[entity] = features;
        return features;
    }

    public Identity? get_identities(string entity) {
        Identity? identity = identity_cache[entity];
        if (identity != null) {
            return identity;
        }

        RowOption row = db.entity_identity.select().with(db.entity_identity.entity, "=", entity).single().row();
        if (row.is_present()) {
            identity = new Identity(row[db.entity_identity.category], row[db.entity_identity.type], row[db.entity_identity.entity_name]);
        }
        identity_cache[entity] = identity;
        return identity;
    }
}
}