Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement bridging of plain text matrix mentions #99

Merged
merged 12 commits into from
Oct 17, 2018

Conversation

Cadair
Copy link
Collaborator

@Cadair Cadair commented Oct 7, 2018

The Slack API removed the ability to just send slack @Cadair text and have slack convert it into a mention. This is an attempt to do this substitution automatically in the bridge.

I would like to remove the need for the @ here as well, which I have an idea how to do, but I am a little concerned about the performance impacts of all this.

This now correctly bridges plain text matrix mentions as slack mentions. This means that all matrix clients should give the correct behaviour on slack, making the bridge truly transparent. 🎉

This also builds on #98 because of many many conflicts.

@Cadair Cadair changed the title Re-implement support for @username style mentions Implement explicit support for @username style mentions Oct 7, 2018
@Cadair Cadair changed the title Implement explicit support for @username style mentions Implement bridging of plain text matrix mentions Oct 8, 2018
@Cadair
Copy link
Collaborator Author

Cadair commented Oct 8, 2018

My concern with this as implemented is the potential effect on performance. This

const firstwords = makeFirstWordMap(displaymap);
// Now parse the message to find the intersection of every word in the
// message with every first word of all nicks.
const match_words = new Set(Object.keys(firstwords));
const string_words = new Set(string.split(" "));
const matches = [...string_words].filter(x => match_words.has(x));

block of code is being executed for each message, irrespective of if there is a match or not (this was not the case when matching @), which means that this:
function getDisplayMap(main, room_id) {
const store = main.getUserStore();
return main.listGhostUsers(room_id)
.then(users => Promise.all(users.map(user => store.select({id: user}))))
.then(users => {
const displaymap = {};
users.forEach(user => {
if (user && user[0]) {
displaymap[user[0].display_name] = user[0].id.split("_")[2].split(":")[0];
}
});
return displaymap;
});
}

function, which calls out to room state and the user store is also being hit on each message.

I don't know enough about the way the bridge lib handles caching of both the room state store or the user store to know if this is an issue.

@Cadair
Copy link
Collaborator Author

Cadair commented Oct 8, 2018

This does also have the downside of being unable to handle the situation where you have two users on slack with the same display name. I guess the only way to semi-guard against this is to do some crazy regex on nicks of the form Cadair (@slack_cadair_UDAS26GDC:testmatrix.home.cadair.com) but I don't really fancy that frankly.

@turt2live
Copy link
Member

(merged develop to make review easier - don't mind me)

Copy link
Member

@turt2live turt2live left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks largely okay to me, but @Half-Shot probably knows what is going on here more than I do

Copy link
Contributor

@Half-Shot Half-Shot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the vars and one name change, I'm happy :)

@@ -3,7 +3,7 @@
var url = require('url');
var https = require('https');
var rp = require('request-promise');
const Slackdown = require('Slackdown');
const slackdown = require('Slackdown');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but why? :(

substitutions.matrixToSlack(message, this._main).then(body => {
var uri = (this._slack_bot_token) ? "https://slack.com/api/chat.postMessage" : this._slack_webhook_uri;

var sendMessageParams = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟

sendMessageParams.headers = {
Authorization: 'Bearer ' + this._slack_bot_token
substitutions.matrixToSlack(message, this._main).then(body => {
var uri = (this._slack_bot_token) ? "https://slack.com/api/chat.postMessage" : this._slack_webhook_uri;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟

// Note: This slower plainTextSlackMentions call is only used if there is not a pill in the message,
// meaning if we can we use the much simpler pill subs rather than this.
return plainTextSlackMentions(main, string, event.room_id).then(string => {
var ret = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟

link_names: 1 //This no longer works for nicks but is needed to make @channel work.
};
if (event.content.msgtype == "m.image" && event.content.url.indexOf("mxc://") === 0) {
var url = main.getUrlForMxc(event.content.url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟

expect(new_string).toBe("<@U2222>");
});
it("replace shortest non-unique", () => {
var new_string = subsitutions.replacementFromDisplayMap("@Stuart", displaymap);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟

expect(new_string).toBe("<@U1111>");
});
it("replace longest non-unique", () => {
var new_string = subsitutions.replacementFromDisplayMap("@Stuart Mumford", displaymap);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟

})

describe ("Make First Word Map", () => {
var displaymap = {Stuart: "U1111", Cadair: "U2222", "Stuart Mumford": "U3333"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟


describe ("Make First Word Map", () => {
var displaymap = {Stuart: "U1111", Cadair: "U2222", "Stuart Mumford": "U3333"}
var expectedfirstwords = {Stuart: [{Stuart: "U1111"}, {"Stuart Mumford": "U3333"}],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟

var expectedfirstwords = {Stuart: [{Stuart: "U1111"}, {"Stuart Mumford": "U3333"}],
Cadair: [{Cadair: "U2222"}]};
it("Make the First Word Map", () => {
var firstwords = subsitutions.makeFirstWordMap(displaymap);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😟

@Half-Shot
Copy link
Contributor

Thank you!

@Half-Shot Half-Shot merged commit bf8443d into matrix-org:develop Oct 17, 2018
@Cadair Cadair deleted the plaintext_nicks branch October 18, 2018 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants