WritheM / Wallace

https://plug.dj/writhem-radio
Other
4 stars 3 forks source link

url's are messed up on the relay #33

Closed pironic closed 9 years ago

pironic commented 9 years ago

slack->plug = <pironic@slack> name our new bot everyone: <http://wiki.writhem.com/pages/viewpage.action?pageId=4980797>;

plug->slack = :/ gets replaced with a face.

issue #1

ylt commented 9 years ago

Smilies:

Two issues that need fixing

Fix:

function escape(text) {
    return (text).replace(/([\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:])/g, '\\$1');
}

//and then actual replace:
var replace = new RegExp("(^|\\s)"+escape(smiley)+"($|\\s)", "g");
message.replace(replace, slacksmiley);

Links:

There's another issue in terms of links - slack wraps all links with < and >. Plug does not understand this and includes the > at the end of the link. Slack usernames are also encoded as links and there is also a bug with the current approach of replacing usernames which will be fixable in one go. Slack links also encode a name (which is useless for Plug) which can also be dealt with.

Rather than dumb matching usernames (iterating through slack users list and attempting a replace for each), a regular expression can be done to return matching <``> pairs. /<(.*?)>/g on which we run a callback func. This func can identify userids (prefixed by @) and match them up against users list to get name, else it's probably a link (which can just be dumped without the <``>).

message.replace(/<(.*?)>/g, function(match, contents) {
    var link = contents.split("|")[0]; //we don't care about name
    if(link[0] == "@") {
        var slackuser = //fetch slack user
        return "@"+slackuser.user_name;
    }
    else {
        return link;
    }
});