| 1 |
using Gee; |
| 2 |
|
| 3 |
namespace Xmpp.Xep.Reactions { |
| 4 |
|
| 5 |
public const string NS_URI = "urn:xmpp:reactions:0"; |
| 6 |
|
| 7 |
public class Module : XmppStreamModule { |
| 8 |
public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "reactions"); |
| 9 |
|
| 10 |
public signal void received_reactions(XmppStream stream, Jid from_jid, string message_id, Gee.List<string> reactions, MessageStanza stanza); |
| 11 |
|
| 12 |
private ReceivedPipelineListener received_pipeline_listener = new ReceivedPipelineListener(); |
| 13 |
|
| 14 |
public async void send_reaction(XmppStream stream, Jid jid, string stanza_type, string message_id, Gee.List<string> reactions) throws IOError { |
| 15 |
StanzaNode reactions_node = new StanzaNode.build("reactions", NS_URI).add_self_xmlns(); |
| 16 |
reactions_node.put_attribute("id", message_id); |
| 17 |
foreach (string reaction in reactions) { |
| 18 |
StanzaNode reaction_node = new StanzaNode.build("reaction", NS_URI); |
| 19 |
reaction_node.put_node(new StanzaNode.text(reaction)); |
| 20 |
reactions_node.put_node(reaction_node); |
| 21 |
} |
| 22 |
|
| 23 |
MessageStanza message = new MessageStanza() { to=jid, type_=stanza_type }; |
| 24 |
message.stanza.put_node(reactions_node); |
| 25 |
|
| 26 |
MessageProcessingHints.set_message_hint(message, MessageProcessingHints.HINT_STORE); |
| 27 |
|
| 28 |
yield stream.get_module(MessageModule.IDENTITY).send_message(stream, message); |
| 29 |
} |
| 30 |
|
| 31 |
public override void attach(XmppStream stream) { |
| 32 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).add_feature(stream, NS_URI); |
| 33 |
stream.get_module(MessageModule.IDENTITY).received_pipeline.connect(received_pipeline_listener); |
| 34 |
} |
| 35 |
|
| 36 |
public override void detach(XmppStream stream) { |
| 37 |
stream.get_module(ServiceDiscovery.Module.IDENTITY).remove_feature(stream, NS_URI); |
| 38 |
stream.get_module(MessageModule.IDENTITY).received_pipeline.disconnect(received_pipeline_listener); |
| 39 |
} |
| 40 |
|
| 41 |
public override string get_ns() { return NS_URI; } |
| 42 |
public override string get_id() { return IDENTITY.id; } |
| 43 |
} |
| 44 |
|
| 45 |
public class ReceivedPipelineListener : StanzaListener<MessageStanza> { |
| 46 |
|
| 47 |
private const string[] after_actions_const = {"EXTRACT_MESSAGE_2"}; |
| 48 |
|
| 49 |
public override string action_group { get { return ""; } } |
| 50 |
public override string[] after_actions { get { return after_actions_const; } } |
| 51 |
|
| 52 |
public override async bool run(XmppStream stream, MessageStanza message) { |
| 53 |
StanzaNode? reactions_node = message.stanza.get_subnode("reactions", NS_URI); |
| 54 |
if (reactions_node == null) return false; |
| 55 |
|
| 56 |
string? id_attribute = reactions_node.get_attribute("id"); |
| 57 |
if (id_attribute == null) return false; |
| 58 |
|
| 59 |
Gee.List<string> reactions = new ArrayList<string>(); |
| 60 |
foreach (StanzaNode reaction_node in reactions_node.get_subnodes("reaction", NS_URI)) { |
| 61 |
string? reaction = reaction_node.get_string_content(); |
| 62 |
if (reaction == null) return false; |
| 63 |
|
| 64 |
if (!reactions.contains(reaction)) { |
| 65 |
reactions.add(reaction); |
| 66 |
} |
| 67 |
} |
| 68 |
stream.get_module(Module.IDENTITY).received_reactions(stream, message.from, id_attribute, reactions, message); |
| 69 |
|
| 70 |
return true; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |