aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service/entity_info.vala
blob: 8efea7e5f0bf6d0cb63c10d4a5c2240604705e51 (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
using Gee;
using Dino.Entities;
using Xmpp;
using Xmpp.Xep;
using Xmpp.Xep.ServiceDiscovery;

namespace Dino {
public class EntityInfo : StreamInteractionModule, Object {
    public static ModuleIdentity<EntityInfo> IDENTITY = new ModuleIdentity<EntityInfo>("entity_info");
    public string id { get { return IDENTITY.id; } }

    private StreamInteractor stream_interactor;
    private Database db;
    private EntityCapabilitiesStorage entity_capabilities_storage;


    private HashMap<Jid, string> entity_caps_hashes = new HashMap<Jid, string>(Jid.hash_func, Jid.equals_func);

    public static void start(StreamInteractor stream_interactor, Database db) {
        EntityInfo m = new EntityInfo(stream_interactor, db);
        stream_interactor.add_module(m);
    }

    private EntityInfo(StreamInteractor stream_interactor, Database db) {
        this.stream_interactor = stream_interactor;
        this.db = db;
        this.entity_capabilities_storage = new EntityCapabilitiesStorage(db);

        stream_interactor.account_added.connect(on_account_added);
        stream_interactor.module_manager.initialize_account_modules.connect(initialize_modules);
    }

    public Identity? get_identity(Account account, Jid jid) {
        string? caps_hash = entity_caps_hashes[jid];
        if (caps_hash == null) return null;
        return entity_capabilities_storage.get_identities(caps_hash);
    }

    private void on_received_available_presence(Account account, Presence.Stanza presence) {
        bool is_gc = stream_interactor.get_module(MucManager.IDENTITY).is_groupchat(presence.from.bare_jid, account);
        if (is_gc) return;

        string? caps_hash = EntityCapabilities.get_caps_hash(presence);
        if (caps_hash == null) return;

        db.entity.upsert()
                .value(db.entity.account_id, account.id, true)
                .value(db.entity.jid_id, db.get_jid_id(presence.from), true)
                .value(db.entity.resource, presence.from.resourcepart, true)
                .value(db.entity.last_seen, (long)(new DateTime.now_local()).to_unix())
                .value(db.entity.caps_hash, caps_hash)
                .perform();

        if (caps_hash != null) {
            entity_caps_hashes[presence.from] = caps_hash;
        }
    }

    private void on_account_added(Account account) {
        stream_interactor.module_manager.get_module(account, Presence.Module.IDENTITY).received_available.connect((stream, presence) => on_received_available_presence(account, presence));
    }

    private void initialize_modules(Account account, ArrayList<XmppStreamModule> modules) {
        modules.add(new Xep.EntityCapabilities.Module(entity_capabilities_storage));
    }
}
}