sylae / ligrev

XMPP MUC Utility bot
GNU General Public License v3.0
2 stars 1 forks source link

Send user classes+metadata for cadence #20

Closed cburschka closed 8 years ago

cburschka commented 8 years ago

As you mentioned in the chat.

The markup you'd want to send is basically

<span
    class="user jid-node-{username} jid-node-{domain}"
    data-jid="{jid}" data-nick="{nick}">
  {nick}
</span>

I've just tried it, and the data-* goes through XMPP just fine. If the classes are set as well, even the flair is added.

cburschka commented 8 years ago

The JS code for the classes is this:

  jidClass: function(jid) {
    return 'jid-node-' + visual.escapeClass(Strophe.unescapeNode(Strophe.getNodeFromJid(jid)).toLowerCase()) + ' '
         + 'jid-domain-' + visual.escapeClass(Strophe.getDomainFromJid(jid)) + ' '
         + 'jid-resource-' + visual.escapeClass(Strophe.getResourceFromJid(jid));
  },

  // Escape values to make a valid CSS class:
  // All values are allowed except whitespace, NUL and backslash.
  // These are replaced with \DEC, where DEC is their ASCII value.
  escapeClass: function(text) {
    return text ? text.replace(/[\s\0\\]/g, function(x) {
      return '\\' + x.charCodeAt(0);
    }) : '';
  }

In PHP, I think it would basically look like

function escape_class($string) {
    return $string ? preg_replace_callback('/[\\s\0\\\\]/', function ($x) {
      return '\\' . ord($x[0]);
    }) : '';
}

function jid_classes(XMPPJid $jid) {
  return 'jid-node-' . escape_class(strtolower($jid->node))
   .  ' jid-domain-' . escape_class($jid->domain)
    // (we don't really care about the resource)
    . ' jid-resource-' . escape_class($jid->resource);
}
cburschka commented 8 years ago

Can we have this in :tell, too? Wrapping the JID in <span data-jid=""> would allow you to do stuff like send direct messages and open the context menu - and adding the JID classes would put flairs on them as well.

cburschka commented 8 years ago

(I'll open a new issue, this looks a bit more complex.)