diff options
author | fiaxh <git@lightrise.org> | 2022-10-11 13:37:48 +0200 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2022-10-11 13:37:48 +0200 |
commit | 80258a874ddfeb87b4b71f5791eab94a2465de6d (patch) | |
tree | 80ef37c3e3c3138d79b3321ee22d185d5a94098e /xmpp-vala/src/module | |
parent | 6c6e7e3aa7935ec513b7e5ea9b53a92b741ecf92 (diff) | |
download | dino-80258a874ddfeb87b4b71f5791eab94a2465de6d.tar.gz dino-80258a874ddfeb87b4b71f5791eab94a2465de6d.zip |
Add support for reactions
Diffstat (limited to 'xmpp-vala/src/module')
-rw-r--r-- | xmpp-vala/src/module/xep/0421_occupant_ids.vala | 45 | ||||
-rw-r--r-- | xmpp-vala/src/module/xep/0444_reactions.vala | 74 |
2 files changed, 119 insertions, 0 deletions
diff --git a/xmpp-vala/src/module/xep/0421_occupant_ids.vala b/xmpp-vala/src/module/xep/0421_occupant_ids.vala new file mode 100644 index 00000000..ce9f2471 --- /dev/null +++ b/xmpp-vala/src/module/xep/0421_occupant_ids.vala @@ -0,0 +1,45 @@ +namespace Xmpp.Xep.OccupantIds { + +public const string NS_URI = "urn:xmpp:occupant-id:0"; + +public static string? get_occupant_id(StanzaNode stanza) { + StanzaNode? node = stanza.get_subnode("occupant-id", NS_URI); + if (node == null) return null; + + return node.get_attribute("id"); +} + +public class Module : XmppStreamModule { + public static ModuleIdentity<Module> IDENTITY = new ModuleIdentity<Module>(NS_URI, "0421_occupant_ids"); + + public signal void received_occupant_id(XmppStream stream, Jid jid, string occupant_id); + public signal void received_own_occupant_id(XmppStream stream, Jid jid, string occupant_id); + + public override void attach(XmppStream stream) { + stream.get_module(Presence.Module.IDENTITY).received_available.connect(parse_occupant_id_from_presence); + } + + public override void detach(XmppStream stream) { + stream.get_module(Presence.Module.IDENTITY).received_available.disconnect(parse_occupant_id_from_presence); + } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return IDENTITY.id; } + + public void parse_occupant_id_from_presence(XmppStream stream, Presence.Stanza presence) { + string? occupant_id = get_occupant_id(presence.stanza); + if (occupant_id == null) return; + + received_occupant_id(stream, presence.from, occupant_id); + + StanzaNode? x_node = presence.stanza.get_subnode("x", "http://jabber.org/protocol/muc#user"); + if (x_node == null) return; + foreach (StanzaNode status_node in x_node.get_subnodes("status", "http://jabber.org/protocol/muc#user")) { + if (int.parse(status_node.get_attribute("code")) == 110) { + received_own_occupant_id(stream, presence.from, occupant_id); + } + } + } +} + +} diff --git a/xmpp-vala/src/module/xep/0444_reactions.vala b/xmpp-vala/src/module/xep/0444_reactions.vala new file mode 100644 index 00000000..90d922d1 --- /dev/null +++ b/xmpp-vala/src/module/xep/0444_reactions.vala @@ -0,0 +1,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 void send_reaction(XmppStream stream, Jid jid, string stanza_type, string message_id, Gee.List<string> reactions) { + StanzaNode reactions_node = new StanzaNode.build("reactions", NS_URI).add_self_xmlns(); + reactions_node.put_attribute("to", 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); + + stream.get_module(MessageModule.IDENTITY).send_message.begin(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? to_attribute = reactions_node.get_attribute("to"); + if (to_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, to_attribute, reactions, message); + + return false; + } +} + +} |