From 19c69166f09b3dd50c42aecbf81474fa74a0f4a0 Mon Sep 17 00:00:00 2001 From: Lukas Pilz Date: Wed, 13 May 2020 14:11:18 +0200 Subject: [PATCH 1/6] Treating channel mentions similarly to user mentions --- app/importer-slack/server/importer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/app/importer-slack/server/importer.js b/app/importer-slack/server/importer.js index 258af97b076a..f8b5730c9f9a 100644 --- a/app/importer-slack/server/importer.js +++ b/app/importer-slack/server/importer.js @@ -970,6 +970,7 @@ export class SlackImporter extends Base { message = message.replace(/:uk:/g, ':gb:'); message = message.replace(/<(http[s]?:[^>|]*)>/g, '$1'); message = message.replace(/<(http[s]?:[^|]*)\|([^>]*)>/g, '[$2]($1)'); + message = message.replace(/<#([^|]*)\|([^>]*)>/g, '#$2'); for (const userReplace of Array.from(this.userTags)) { message = message.replace(userReplace.slack, userReplace.rocket); From 7d94e4a9f418d29097957f1c18ea75511610275e Mon Sep 17 00:00:00 2001 From: Lukas Pilz Date: Mon, 18 May 2020 21:46:00 +0200 Subject: [PATCH 2/6] First iteration, links not clickable client-side yet. --- app/importer-slack/server/importer.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/app/importer-slack/server/importer.js b/app/importer-slack/server/importer.js index f8b5730c9f9a..82309de1167e 100644 --- a/app/importer-slack/server/importer.js +++ b/app/importer-slack/server/importer.js @@ -16,6 +16,7 @@ import { Users, Rooms, Messages } from '../../models'; import { insertMessage, createDirectRoom } from '../../lib'; import { getValidRoomName } from '../../utils'; import { settings } from '../../settings/server'; +import { MentionsParser } from '../../mentions/lib/MentionsParser'; export class SlackImporter extends Base { constructor(info, importRecord) { @@ -242,6 +243,27 @@ export class SlackImporter extends Base { }); } + parseMentions(message){ + let mentionsParser; + mentionsParser = new MentionsParser({ + pattern: settings.get('UTF8_Names_Validation'), + useRealName: settings.get('UI_Use_Real_Name'), + me: () => 'me', + }); + + if(!message.mentions){ + message.mentions=[]; + } + message.mentions.push(...mentionsParser.getUserMentions(message.msg)); + + if(!message.channels){ + message.channels=[]; + } + message.channels.push(...mentionsParser.getChannelMentions(message.msg)); + + message.temp = message.msg; + } + processMessageSubType(message, room, msgDataDefaults, missedTypes) { const ignoreTypes = { bot_add: true, file_comment: true, file_mention: true }; @@ -274,6 +296,7 @@ export class SlackImporter extends Base { ...msgDataDefaults, msg: `_${ this.convertSlackMessageToRocketChat(message.text) }_`, }; + this.parseMentions(msgObj); insertMessage(rocketUser, msgObj, room, this._anyExistingSlackMessage); break; } @@ -304,6 +327,7 @@ export class SlackImporter extends Base { if (message.icons) { msgObj.emoji = message.icons.emoji; } + this.parseMentions(msgObj); insertMessage(botUser, msgObj, room, this._anyExistingSlackMessage); break; } @@ -457,6 +481,7 @@ export class SlackImporter extends Base { } } + this.parseMentions(msgObj); try { insertMessage(this.getRocketUserFromUserId(message.user), msgObj, room, this._anyExistingSlackMessage); } catch (e) { From a132e92213d600afa9d19ff538c1371b2d6e3572 Mon Sep 17 00:00:00 2001 From: Lukas Pilz Date: Mon, 25 May 2020 00:51:08 +0200 Subject: [PATCH 3/6] Made channel links clickable. #general import not working yet, as well as channel names with umlauts --- app/importer-slack/server/importer.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/app/importer-slack/server/importer.js b/app/importer-slack/server/importer.js index 82309de1167e..897ffa53768e 100644 --- a/app/importer-slack/server/importer.js +++ b/app/importer-slack/server/importer.js @@ -243,11 +243,11 @@ export class SlackImporter extends Base { }); } - parseMentions(message){ + parseMentions(message) { let mentionsParser; mentionsParser = new MentionsParser({ - pattern: settings.get('UTF8_Names_Validation'), - useRealName: settings.get('UI_Use_Real_Name'), + pattern: () => settings.get('UTF8_Names_Validation'), + useRealName: () => settings.get('UI_Use_Real_Name'), me: () => 'me', }); @@ -259,7 +259,17 @@ export class SlackImporter extends Base { if(!message.channels){ message.channels=[]; } - message.channels.push(...mentionsParser.getChannelMentions(message.msg)); + let channels = mentionsParser.getChannelMentions(message.msg); + channels.forEach((channel_name, index, arr) => { + let chan = channel_name.slice(1, channel_name.length); + try{ + const slackChannel = this.getSlackChannelFromName(chan); + arr[index] = Rooms.findOneById(slackChannel.rocketId); + } catch (e) { + this.logger.warn(`Failed to import room with name: ${ chan }`); + } + }); + message.channels.push(...channels); message.temp = message.msg; } From 40f9dfc23ff4abbc24e87026c6a09eb258256a37 Mon Sep 17 00:00:00 2001 From: Lukas Pilz Date: Thu, 28 May 2020 22:53:47 +0200 Subject: [PATCH 4/6] Finished implementation of clickable links. Had to add dname field to channel objects and message parser to make matching by parser possible without changing display of channels by using fname. --- app/importer-slack/server/importer.js | 37 +++++++++++++++++---------- app/mentions/lib/MentionsParser.js | 4 +-- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/app/importer-slack/server/importer.js b/app/importer-slack/server/importer.js index 897ffa53768e..4e7f2921a673 100644 --- a/app/importer-slack/server/importer.js +++ b/app/importer-slack/server/importer.js @@ -243,35 +243,46 @@ export class SlackImporter extends Base { }); } - parseMentions(message) { - let mentionsParser; - mentionsParser = new MentionsParser({ + parseMentions (message) { + const mentionsParser = new MentionsParser({ pattern: () => settings.get('UTF8_Names_Validation'), useRealName: () => settings.get('UI_Use_Real_Name'), me: () => 'me', }); - if(!message.mentions){ - message.mentions=[]; + if (!message.mentions) { + message.mentions = []; } - message.mentions.push(...mentionsParser.getUserMentions(message.msg)); + let users = mentionsParser.getUserMentions(message.msg); + users.forEach((user_id, index, arr) => { + const user = user_id.slice(1, user_id.length); + try { + if (user === 'all' || user === 'here'){ + arr[index] = user; + } else { + arr[index] = Users.findOneByUsernameIgnoringCase(user); + } + } catch (e) { + this.logger.warn(`Failed to import user mention with name: ${ user }`); + } + }); + message.mentions.push(...users); - if(!message.channels){ - message.channels=[]; + if (!message.channels) { + message.channels = []; } let channels = mentionsParser.getChannelMentions(message.msg); channels.forEach((channel_name, index, arr) => { - let chan = channel_name.slice(1, channel_name.length); - try{ + const chan = channel_name.slice(1, channel_name.length); + try { const slackChannel = this.getSlackChannelFromName(chan); arr[index] = Rooms.findOneById(slackChannel.rocketId); + arr[index].dname = chan; // Have to store name to display so parser can match it } catch (e) { - this.logger.warn(`Failed to import room with name: ${ chan }`); + this.logger.warn(`Failed to import channel mention with name: ${ chan }`); } }); message.channels.push(...channels); - - message.temp = message.msg; } processMessageSubType(message, room, msgDataDefaults, missedTypes) { diff --git a/app/mentions/lib/MentionsParser.js b/app/mentions/lib/MentionsParser.js index 6fd1a6c6dc88..136a2a7762be 100644 --- a/app/mentions/lib/MentionsParser.js +++ b/app/mentions/lib/MentionsParser.js @@ -79,11 +79,11 @@ export class MentionsParser { replaceChannels = (msg, { temp, channels }) => msg .replace(/'/g, '\'') .replace(this.channelMentionRegex, (match, prefix, mention) => { - if (!temp && !(channels && channels.find((c) => c.name === mention))) { + if (!temp && !(channels && channels.find((c) => c.dname ? c.dname === mention : c.name === mention))) { return match; } - const channel = channels && channels.find(({ name }) => name === mention); + const channel = channels && channels.find(({ name, dname }) => dname? dname === mention : name === mention); const reference = channel ? channel._id : mention; return `${ prefix }${ `#${ mention }` }`; }) From ab5164f800b2d9df8887a0c8b04a82ba0f32a47f Mon Sep 17 00:00:00 2001 From: Lukas Pilz Date: Fri, 29 May 2020 14:29:31 +0200 Subject: [PATCH 5/6] Hopefully fixed linter issues --- app/importer-slack/server/importer.js | 8 ++++---- app/mentions/lib/MentionsParser.js | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/importer-slack/server/importer.js b/app/importer-slack/server/importer.js index 4e7f2921a673..a82d2e79dd24 100644 --- a/app/importer-slack/server/importer.js +++ b/app/importer-slack/server/importer.js @@ -243,7 +243,7 @@ export class SlackImporter extends Base { }); } - parseMentions (message) { + parseMentions(message) { const mentionsParser = new MentionsParser({ pattern: () => settings.get('UTF8_Names_Validation'), useRealName: () => settings.get('UI_Use_Real_Name'), @@ -253,11 +253,11 @@ export class SlackImporter extends Base { if (!message.mentions) { message.mentions = []; } - let users = mentionsParser.getUserMentions(message.msg); + const users = mentionsParser.getUserMentions(message.msg); users.forEach((user_id, index, arr) => { const user = user_id.slice(1, user_id.length); try { - if (user === 'all' || user === 'here'){ + if (user === 'all' || user === 'here') { arr[index] = user; } else { arr[index] = Users.findOneByUsernameIgnoringCase(user); @@ -271,7 +271,7 @@ export class SlackImporter extends Base { if (!message.channels) { message.channels = []; } - let channels = mentionsParser.getChannelMentions(message.msg); + const channels = mentionsParser.getChannelMentions(message.msg); channels.forEach((channel_name, index, arr) => { const chan = channel_name.slice(1, channel_name.length); try { diff --git a/app/mentions/lib/MentionsParser.js b/app/mentions/lib/MentionsParser.js index 136a2a7762be..dc6788b95869 100644 --- a/app/mentions/lib/MentionsParser.js +++ b/app/mentions/lib/MentionsParser.js @@ -79,11 +79,11 @@ export class MentionsParser { replaceChannels = (msg, { temp, channels }) => msg .replace(/'/g, '\'') .replace(this.channelMentionRegex, (match, prefix, mention) => { - if (!temp && !(channels && channels.find((c) => c.dname ? c.dname === mention : c.name === mention))) { + if (!temp && !(channels && channels.find((c) => { return c.dname ? c.dname === mention : c.name === mention; }))) { return match; } - const channel = channels && channels.find(({ name, dname }) => dname? dname === mention : name === mention); + const channel = channels && channels.find(({ name, dname }) => { return dname ? dname === mention : name === mention; }); const reference = channel ? channel._id : mention; return `${ prefix }${ `#${ mention }` }`; }) From e42022bd4dc44ca4cc84ffe66cadf5cfe039045e Mon Sep 17 00:00:00 2001 From: Lukas Pilz Date: Fri, 29 May 2020 14:39:54 +0200 Subject: [PATCH 6/6] Changed arrow to function because eslint forbids confusing arrow and arrow with curly braces --- app/mentions/lib/MentionsParser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/mentions/lib/MentionsParser.js b/app/mentions/lib/MentionsParser.js index dc6788b95869..72e7b0da0b00 100644 --- a/app/mentions/lib/MentionsParser.js +++ b/app/mentions/lib/MentionsParser.js @@ -79,11 +79,11 @@ export class MentionsParser { replaceChannels = (msg, { temp, channels }) => msg .replace(/'/g, '\'') .replace(this.channelMentionRegex, (match, prefix, mention) => { - if (!temp && !(channels && channels.find((c) => { return c.dname ? c.dname === mention : c.name === mention; }))) { + if (!temp && !(channels && channels.find(function(c) { return c.dname ? c.dname === mention : c.name === mention; }))) { return match; } - const channel = channels && channels.find(({ name, dname }) => { return dname ? dname === mention : name === mention; }); + const channel = channels && channels.find(function({ name, dname }) { return dname ? dname === mention : name === mention; }); const reference = channel ? channel._id : mention; return `${ prefix }${ `#${ mention }` }`; })