aboutsummaryrefslogtreecommitdiff
path: root/libdino/src/service/conversation_manager.vala
blob: a757e8af5d6073cb36bf0577ab64f7b31b118496 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
using Gee;

using Xmpp;
using Dino.Entities;

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

    public signal void conversation_activated(Conversation conversation);
    public signal void conversation_deactivated(Conversation conversation);

    private StreamInteractor stream_interactor;
    private Database db;

    private HashMap<Account, HashMap<Jid, Gee.List<Conversation>>> conversations = new HashMap<Account, HashMap<Jid, Gee.List<Conversation>>>(Account.hash_func, Account.equals_func);

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

    private ConversationManager(StreamInteractor stream_interactor, Database db) {
        this.db = db;
        this.stream_interactor = stream_interactor;
        stream_interactor.add_module(this);
        stream_interactor.account_added.connect(on_account_added);
        stream_interactor.account_removed.connect(on_account_removed);
        stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new MessageListener(stream_interactor));
        stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(handle_sent_message);
        stream_interactor.get_module(Calls.IDENTITY).call_incoming.connect(handle_new_call);
        stream_interactor.get_module(Calls.IDENTITY).call_outgoing.connect(handle_new_call);
    }

    public Conversation create_conversation(Jid jid, Account account, Conversation.Type? type = null) {
        assert(conversations.has_key(account));
        Jid store_jid = type == Conversation.Type.GROUPCHAT ? jid.bare_jid : jid;

        // Do we already have a conversation for this jid?
        if (conversations[account].has_key(store_jid)) {
            foreach (var conversation in conversations[account][store_jid]) {
                if (conversation.type_ == type) {
                    return conversation;
                }
            }
        }

        // Create a new converation
        Conversation conversation = new Conversation(jid, account, type);
        // Set encryption for conversation
        if (type == Conversation.Type.CHAT ||
                (type == Conversation.Type.GROUPCHAT && stream_interactor.get_module(MucManager.IDENTITY).is_private_room(account, jid))) {
            conversation.encryption = Application.get_default().settings.get_default_encryption(account);
        } else {
            conversation.encryption = Encryption.NONE;
        }

        add_conversation(conversation);
        conversation.persist(db);
        return conversation;
    }

    public Conversation? get_conversation_for_message(Entities.Message message) {
        if (message.type_ == Entities.Message.Type.CHAT) {
            return create_conversation(message.counterpart.bare_jid, message.account, Conversation.Type.CHAT);
        } else if (message.type_ == Entities.Message.Type.GROUPCHAT) {
            return create_conversation(message.counterpart.bare_jid, message.account, Conversation.Type.GROUPCHAT);
        } else if (message.type_ == Entities.Message.Type.GROUPCHAT_PM) {
            return create_conversation(message.counterpart, message.account, Conversation.Type.GROUPCHAT_PM);
        }
        return null;
    }

    public Gee.List<Conversation> get_conversations(Jid jid, Account account) {
        Gee.List<Conversation> ret = new ArrayList<Conversation>(Conversation.equals_func);
        Conversation? bare_conversation = get_conversation(jid, account);
        if (bare_conversation != null) ret.add(bare_conversation);
        Conversation? full_conversation = get_conversation(jid.bare_jid, account);
        if (full_conversation != null) ret.add(full_conversation);
        return ret;
    }

    public Conversation? get_conversation(Jid jid, Account account, Conversation.Type? type = null) {
        if (conversations.has_key(account)) {
            if (conversations[account].has_key(jid)) {
                foreach (var conversation in conversations[account][jid]) {
                    if (type == null || conversation.type_ == type) {
                        return conversation;
                    }
                }
            }
        }
        return null;
    }

    public Conversation? approx_conversation_for_stanza(Jid from, Jid to, Account account, string msg_ty) {
        if (msg_ty == Xmpp.MessageStanza.TYPE_GROUPCHAT) {
            return get_conversation(from.bare_jid, account, Conversation.Type.GROUPCHAT);
        }

        Jid counterpart = from.equals_bare(account.bare_jid) ? to : from;

        if (msg_ty == Xmpp.MessageStanza.TYPE_CHAT && counterpart.is_full() &&
                get_conversation(counterpart.bare_jid, account, Conversation.Type.GROUPCHAT) != null) {
            var pm = get_conversation(counterpart, account, Conversation.Type.GROUPCHAT_PM);
            if (pm != null) return pm;
        }

        return get_conversation(counterpart.bare_jid, account, Conversation.Type.CHAT);
    }

    public Conversation? get_conversation_by_id(int id) {
        foreach (HashMap<Jid, Gee.List<Conversation>> hm in conversations.values) {
            foreach (Gee.List<Conversation> hm2 in hm.values) {
                foreach (Conversation conversation in hm2) {
                    if (conversation.id == id) {
                        return conversation;
                    }
                }
            }
        }
        return null;
    }

    public Gee.List<Conversation> get_active_conversations(Account? account = null) {
        Gee.List<Conversation> ret = new ArrayList<Conversation>(Conversation.equals_func);
        foreach (Account account_ in conversations.keys) {
            if (account != null && !account_.equals(account)) continue;
            foreach (Gee.List<Conversation> list in conversations[account_].values) {
                foreach (var conversation in list) {
                    if(conversation.active) ret.add(conversation);
                }
            }
        }
        return ret;
    }

    public void start_conversation(Conversation conversation) {
        if (conversation.last_active == null) {
            conversation.last_active = new DateTime.now_utc();
            if (conversation.active) conversation_activated(conversation);
        }
        if (!conversation.active) {
            conversation.active = true;
            conversation_activated(conversation);
        }
    }

    public void close_conversation(Conversation conversation) {
        if (!conversation.active) return;

        conversation.active = false;
        conversation_deactivated(conversation);
    }

    private void on_account_added(Account account) {
        conversations[account] = new HashMap<Jid, ArrayList<Conversation>>(Jid.hash_func, Jid.equals_func);
        foreach (Conversation conversation in db.get_conversations(account)) {
            add_conversation(conversation);
        }
    }

    private void on_account_removed(Account account) {
        foreach (Gee.List<Conversation> list in conversations[account].values) {
            foreach (var conversation in list) {
                if(conversation.active) conversation_deactivated(conversation);
            }
        }
        conversations.unset(account);
    }

    private class MessageListener : Dino.MessageListener {

        public string[] after_actions_const = new string[]{ "DEDUPLICATE", "FILTER_EMPTY" };
        public override string action_group { get { return "MANAGER"; } }
        public override string[] after_actions { get { return after_actions_const; } }

        private StreamInteractor stream_interactor;

        public MessageListener(StreamInteractor stream_interactor) {
            this.stream_interactor = stream_interactor;
        }

        public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) {
            conversation.last_active = message.time;

            if (stanza != null) {
                bool is_mam_message = Xmpp.MessageArchiveManagement.MessageFlag.get_flag(stanza) != null;
                bool is_recent = message.time.compare(new DateTime.now_utc().add_days(-3)) > 0;
                if (is_mam_message && !is_recent) return false;
            }
            stream_interactor.get_module(ConversationManager.IDENTITY).start_conversation(conversation);
            return false;
        }
    }

    private void handle_sent_message(Entities.Message message, Conversation conversation) {
        conversation.last_active = message.time;

        bool is_recent = message.time.compare(new DateTime.now_utc().add_hours(-24)) > 0;
        if (is_recent) {
            start_conversation(conversation);
        }
    }

    private void handle_new_call(Call call, CallState state, Conversation conversation) {
        conversation.last_active = call.time;
        start_conversation(conversation);
    }

    private void add_conversation(Conversation conversation) {
        if (!conversations[conversation.account].has_key(conversation.counterpart)) {
            conversations[conversation.account][conversation.counterpart] = new ArrayList<Conversation>(Conversation.equals_func);
        }

        conversations[conversation.account][conversation.counterpart].add(conversation);

        if (conversation.active) {
            conversation_activated(conversation);
        }
    }
}

}