aboutsummaryrefslogtreecommitdiff
path: root/xmpp-vala/src/module/xep/0444_reactions.vala
blob: 877d3070ae35583bb2d15c38c1c1e7a87f4930b1 (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
using Gee;

namespace Xmpp.Xep.Reactions {

public const string NS_URI = "urn:xmpp:reactions:0";

public class Module : XmppStreamModule {
    public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "reactions");

    public signal void received_reactions(XmppStream stream, Jid from_jid, string message_id, Gee.List<string> reactions, MessageStanza stanza);

    private ReceivedPipelineListener received_pipeline_listener = new ReceivedPipelineListener();

    public async void send_reaction(XmppStream stream, Jid jid, string stanza_type, string message_id, Gee.List<string> reactions) throws IOError {
        StanzaNode reactions_node = new StanzaNode.build("reactions", NS_URI).add_self_xmlns();
        reactions_node.put_attribute("id", message_id);
        foreach (string reaction in reactions) {
            StanzaNode reaction_node = new StanzaNode.build("reaction", NS_URI);
            reaction_node.put_node(new StanzaNode.text(reaction));
            reactions_node.put_node(reaction_node);
        }

        MessageStanza message = new MessageStanza() { to=jid, type_=stanza_type };
        message.stanza.put_node(reactions_node);

        MessageProcessingHints.set_message_hint(message, MessageProcessingHints.HINT_STORE);

        yield stream.get_module(MessageModule.IDENTITY).send_message(stream, message);
    }

    public override void attach(XmppStream stream) {
        stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI);
        stream.get_module(MessageModule.IDENTITY).received_pipeline.connect(received_pipeline_listener);
    }

    public override void detach(XmppStream stream) {
        stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI);
        stream.get_module(MessageModule.IDENTITY).received_pipeline.disconnect(received_pipeline_listener);
    }

    public override string get_ns() { return NS_URI; }
    public override string get_id() { return IDENTITY.id; }
}

public class ReceivedPipelineListener : StanzaListener<MessageStanza> {

    private const string[] after_actions_const = {"EXTRACT_MESSAGE_2"};

    public override string action_group { get { return ""; } }
    public override string[] after_actions { get { return after_actions_const; } }

    public override async bool run(XmppStream stream, MessageStanza message) {
        StanzaNode? reactions_node = message.stanza.get_subnode("reactions", NS_URI);
        if (reactions_node == null) return false;

        string? id_attribute = reactions_node.get_attribute("id");
        if (id_attribute == null) return false;

        Gee.List<string> reactions = new ArrayList<string>();
        foreach (StanzaNode reaction_node in reactions_node.get_subnodes("reaction", NS_URI)) {
            string? reaction = reaction_node.get_string_content();
            if (reaction == null) return false;

            if (!reactions.contains(reaction)) {
                reactions.add(reaction);
            }
        }
        stream.get_module(Module.IDENTITY).received_reactions(stream, message.from, id_attribute, reactions, message);

        return true;
    }
}

}