aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service/call_store.vala
blob: fa6e63ee9aac90f0413f9acd83743f7e11c380ed (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
using Xmpp;
using Gee;
using Qlite;

using Dino.Entities;

namespace Dino {

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

        private StreamInteractor stream_interactor;
        private Database db;

        private WeakMap<int, Call> calls_by_db_id = new WeakMap<int, Call>();

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

        private CallStore(StreamInteractor stream_interactor, Database db) {
            this.stream_interactor = stream_interactor;
            this.db = db;
        }

        public void add_call(Call call, Conversation conversation) {
            call.persist(db);
            cache_call(call);
        }

        public Call? get_call_by_id(int id) {
            Call? call = calls_by_db_id[id];
            if (call != null) {
                return call;
            }

            RowOption row_option = db.call.select().with(db.call.id, "=", id).row();

            return create_call_from_row_opt(row_option);
        }

        private Call? create_call_from_row_opt(RowOption row_opt) {
            if (!row_opt.is_present()) return null;

            try {
                Call call = new Call.from_row(db, row_opt.inner);
                cache_call(call);
                return call;
            } catch (InvalidJidError e) {
                warning("Got message with invalid Jid: %s", e.message);
            }
            return null;
        }

        private void cache_call(Call call) {
            calls_by_db_id[call.id] = call;
        }
    }
}