diff --git a/.eslintrc.json b/.eslintrc.json index 2c5d8498..ded17fd0 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -65,12 +65,6 @@ "warn", 120 ], - "no-underscore-dangle": [ - "error", - { - "allowAfterThis": true - } - ], "no-unused-vars": [ "error", { @@ -143,6 +137,10 @@ "allowEmptyCatch": true } ], + "no-cond-assign": [ + "error", + "except-parens" + ], "function-paren-newline": "off", "no-labels": "off", "no-control-regex": "off", @@ -170,6 +168,7 @@ "no-template-curly-in-string": "off", "no-await-in-loop": "off", "no-restricted-globals": "off", - "no-fallthrough": "off" + "no-fallthrough": "off", + "no-underscore-dangle": "off" } } diff --git a/README.md b/README.md index 5da867b3..1b2e6289 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ If you don't know the MXID of the puppet, you can search for users using the `se * Matrix → Telegram * [x] Plaintext messages * [x] Formatted messages - * [ ] Non-plaintext mentions + * [x] Mentions * [x] Locations * [ ] Images * [ ] Files @@ -55,7 +55,7 @@ If you don't know the MXID of the puppet, you can search for users using the `se * Telegram → Matrix * [x] Plaintext messages * [x] Formatted messages - * [ ] Non-plaintext mentions + * [x] Mentions * [x] Images * [x] Locations * [ ] Stickers (somewhat works through document upload, no preview though) diff --git a/src/app.js b/src/app.js index 93ad905e..0eb52c20 100644 --- a/src/app.js +++ b/src/app.js @@ -159,6 +159,10 @@ class MautrixTelegram { return this.config.bridge.username_template.replace("${ID}", id) } + getMXIDForTelegramUser(id) { + return `@${this.getUsernameForTelegramUser(id)}:${this.config.homeserver.domain}` + } + /** * Get the matrix.to link for the Matrix puppet of the Telegram user with the given ID. * @@ -166,7 +170,7 @@ class MautrixTelegram { * @returns {string} A matrix.to link that points to the Matrix puppet of the given user. */ getMatrixToLinkForTelegramUser(id) { - return `https://matrix.to/#/@${this.getUsernameForTelegramUser(id)}:${this.config.homeserver.domain}` + return `https://matrix.to/#/${this.getMXIDForTelegramUser(id)}` } /** diff --git a/src/formatter.js b/src/formatter.js index 67a9c89c..50efa49b 100644 --- a/src/formatter.js +++ b/src/formatter.js @@ -43,8 +43,9 @@ function addTag(tags, entity, tag, attrs, priority = 0) { * * @param {string} message The plaintext message. * @param {Array} entities The Telegram formatting entities. + * @param {MautrixTelegram} app The app main class instance to use when reformatting mentions. */ -function telegramToMatrix(message, entities) { +function telegramToMatrix(message, entities, app) { const tags = [] // Decreasing priority counter used to ensure that formattings right next to eachother don't flip like this: // *bold*_italic_ --> bolditalic @@ -52,7 +53,7 @@ function telegramToMatrix(message, entities) { // Convert Telegram formatting entities into a weird custom indexed HTML tag format thingy. for (const entity of entities) { - let url, tag + let url, tag, mxid switch (entity._) { case "messageEntityBold": tag = tag || "strong" @@ -72,9 +73,44 @@ function telegramToMatrix(message, entities) { // TODO bridge bot commands differently? addTag(tags, entity, "font", "color=\"blue\"", --pc) break + case "messageEntityMentionName": + let user = app.matrixUsersByTelegramID.get(entity.user_id) + if (!user) { + // TODO this loop step should be made useless + for (const userByMXID of app.matrixUsersByID.values()) { + if (userByMXID.telegramUserID === entity.user_id) { + user = userByMXID + app.matrixUsersByTelegramID.set(userByMXID.telegramUserID, userByMXID) + break + } + } + } + mxid = user ? + user.userID : + app.getMXIDForTelegramUser(entity.user_id) case "messageEntityMention": - // TODO bridge mentions properly? - addTag(tags, entity, "font", "color=\"red\"", --pc) + if (!mxid) { + const username = message.substr(entity.offset + 1, entity.length - 1) + for (const userByMXID of app.matrixUsersByID.values()) { + if (userByMXID._telegramPuppet && userByMXID._telegramPuppet.data.username === username) { + mxid = userByMXID.userID + break + } + } + if (!mxid) { + for (const userByID of app.telegramUsersByID.values()) { + if (userByID.username === username) { + mxid = userByID.mxid + break + } + } + } + } + + if (!mxid) { + continue + } + addTag(tags, entity, "a", `href="https://matrix.to/#/${mxid}"`) break case "messageEntityEmail": url = url || `mailto:${message.substr(entity.offset, entity.length)}` @@ -101,22 +137,125 @@ function telegramToMatrix(message, entities) { } // Formatting that is converted back to text -const paragraphs = /

(.*?)<\/p>/g -const headers = /(.*?)<\/h[0-6]>/g -const unorderedLists = /