From 674ea9eab4127671e09955059d5eb005b86776ec Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 21 Nov 2017 14:30:59 +0200 Subject: [PATCH] Format and maybe break code --- .eslintrc.json | 37 ++++++--- src/app.js | 104 ++++++++++++-------------- src/commands.js | 24 +++--- src/index.js | 6 +- src/matrix-user.js | 21 +++--- src/portal.js | 20 ++--- src/telegram-peer.js | 153 ++++++++++++++++++------------------- src/telegram-puppet.js | 166 +++++++++++++++++++++-------------------- src/telegram-user.js | 8 +- 9 files changed, 275 insertions(+), 264 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 1508fcf4..9224c33b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,6 +1,5 @@ { "env": { - "browser": false, "node": true, "es6": true }, @@ -36,7 +35,6 @@ "object-curly-newline": [ "error", { - "minProperties": 5, "consistent": true } ], @@ -65,17 +63,29 @@ ], "max-len": [ "warn", - 80 + 120 ], - "camelcase": [ + "no-underscore-dangle": [ "error", { - "properties": "always" + "allowAfterThis": true + } + ], + "no-unused-vars": [ + "error", + { + "vars": "all", + "args": "after-used", + "varsIgnorePattern": "_" } ], "space-before-function-paren": [ "error", - "never" + { + "anonymous": "never", + "named": "never", + "asyncArrow": "always" + } ], "func-style": [ "warn", @@ -92,11 +102,11 @@ "i", "x", "y", - "$" + "$", + "_" ] } ], - "import/no-nodejs-modules": "error", "import/order": [ "warn", { @@ -123,7 +133,8 @@ "warn", { "newIsCap": true, - "capIsNew": true + "capIsNew": true, + "capIsNewExceptions": ["MTProto"] } ], "no-empty": [ @@ -151,6 +162,12 @@ "no-prototype-builtins": "off", "no-console": "off", "class-methods-use-this": "off", - "prefer-destructuring": "off" + "prefer-destructuring": "off", + "camelcase": "off", + "spaced-comment": "off", + "no-bitwise": "off", + "no-case-declarations": "off", + "no-template-curly-in-string": "off", + "no-await-in-loop": "off" } } diff --git a/src/app.js b/src/app.js index cdf4111b..5cd003aa 100644 --- a/src/app.js +++ b/src/app.js @@ -13,9 +13,7 @@ // // You should have received a copy of the GNU General Public License // along with this program. If not, see . -const {Bridge} = require("matrix-appservice-bridge") -const crypto = require("crypto") -const YAML = require("yamljs") +const { Bridge } = require("matrix-appservice-bridge") const commands = require("./commands") const MatrixUser = require("./matrix-user") const TelegramUser = require("./telegram-user") @@ -54,9 +52,10 @@ class MautrixTelegram { async run() { console.log("Appservice listening on port %s", this.config.appservice.port) await this.bridge.run(this.config.appservice.port, {}) - const userEntries = await this.bridge.getUserStore().select({ - type: "matrix", - }) + const userEntries = await this.bridge.getUserStore() + .select({ + type: "matrix", + }) for (const entry of userEntries) { const user = MatrixUser.fromEntry(this, entry) this.matrixUsersByID.set(entry.id, user) @@ -89,7 +88,8 @@ class MautrixTelegram { if (peer.type === "user") { query.receiverID = peer.receiverID } - const entries = await this.bridge.getRoomStore().select(query) + const entries = await this.bridge.getRoomStore() + .select(query) // Handle possible db query race conditions portal = this.portalsByPeerID.get(peer.id) @@ -123,10 +123,11 @@ class MautrixTelegram { } } - const entries = await this.bridge.getRoomStore().select({ - type: "portal", - roomID: id, - }) + const entries = await this.bridge.getRoomStore() + .select({ + type: "portal", + roomID: id, + }) // Handle possible db query race conditions portal = this.portalsByRoomID.get(id) @@ -151,10 +152,11 @@ class MautrixTelegram { return user } - const entries = await this.bridge.getUserStore().select({ - type: "remote", - id, - }) + const entries = await this.bridge.getUserStore() + .select({ + type: "remote", + id, + }) // Handle possible db query race conditions if (this.telegramUsersByID.has(id)) { @@ -176,10 +178,11 @@ class MautrixTelegram { return user } - const entries = this.bridge.getUserStore().select({ - type: "matrix", - id, - }) + const entries = this.bridge.getUserStore() + .select({ + type: "matrix", + id, + }) // Handle possible db query race conditions if (this.matrixUsersByID.has(id)) { @@ -197,30 +200,33 @@ class MautrixTelegram { putUser(user) { const entry = user.toEntry() - return this.bridge.getUserStore().upsert({ - type: entry.type, - id: entry.id, - }, entry) + return this.bridge.getUserStore() + .upsert({ + type: entry.type, + id: entry.id, + }, entry) } putRoom(room) { const entry = room.toEntry() - return this.bridge.getRoomStore().upsert({ - type: entry.type, - id: entry.id, - }, entry) + return this.bridge.getRoomStore() + .upsert({ + type: entry.type, + id: entry.id, + }, entry) } async handleMatrixEvent(evt) { - const asBotID = this.bridge.getBot().getUserId() + const asBotID = this.bridge.getBot() + .getUserId() if (evt.type === "m.room.member" && evt.state_key === asBotID) { if (evt.content.membership === "invite") { // Accept all invites this.botIntent.join(evt.room_id) .catch(err => { console.warn(`Failed to join room ${evt.room_id}:`, err) - if (e instanceof Error) { - console.warn(e.stack) + if (err instanceof Error) { + console.warn(err.stack) } }) } @@ -229,25 +235,27 @@ class MautrixTelegram { if (evt.sender === asBotID || evt.type !== "m.room.message" || !evt.content) { // Ignore own messages and non-message events. - return; + return } const user = await this.getMatrixUser(evt.sender) const cmdprefix = this.config.bridge.commands.prefix - if (evt.content.body.startsWith(cmdprefix + " ")) { + if (evt.content.body.startsWith(`${cmdprefix} `)) { if (!user.whitelisted) { this.botIntent.sendText(evt.room_id, "You are not authorized to use this bridge.") return } const prefixLength = cmdprefix.length + 1 - const args = evt.content.body.substr(prefixLength).split(" ") + const args = evt.content.body.substr(prefixLength) + .split(" ") const command = args.shift() - commands.run(user, command, args, reply => - this.botIntent.sendText( - evt.room_id, - reply.replace("$cmdprefix", cmdprefix)), + commands.run( + user, command, args, + reply => this.botIntent.sendText( + evt.room_id, + reply.replace("$cmdprefix", cmdprefix)), this) return } @@ -260,7 +268,6 @@ class MautrixTelegram { const portal = await this.getPortalByRoomID(evt.room_id) if (portal) { portal.handleMatrixEvent(user, evt) - return } } @@ -270,7 +277,7 @@ class MautrixTelegram { } userID = userID.toLowerCase() - const userIDCapture = /\@.+\:(.+)/.exec(userID) + const userIDCapture = /@.+:(.+)/.exec(userID) const homeserver = userIDCapture && userIDCapture.length > 1 ? userIDCapture[1] : undefined for (let whitelisted of this.config.bridge.whitelist) { whitelisted = whitelisted.toLowerCase() @@ -280,25 +287,6 @@ class MautrixTelegram { } return false } - - /*encrypt(value) { - var cipher = crypto.createCipher("aes-256-gcm", this.config.bridge.auth_key_password); - var ret = cipher.update(Buffer.from(value), "hex", "base64"); - ret += cipher.final("base64"); - - return [ret, cipher.getAuthTag().toString("base64")]; - } - - decrypt(value) { - if(!value) return value; - - var decipher = crypto.createDecipher("aes-256-gcm", this.config.bridge.auth_key_password); - decipher.setAuthTag(new Buffer(value[1], "base64")); - var ret = decipher.update(value[0], "base64", "hex"); - ret += decipher.final("hex"); - - return ret; - };*/ } module.exports = MautrixTelegram diff --git a/src/commands.js b/src/commands.js index 22871188..c3592fdc 100644 --- a/src/commands.js +++ b/src/commands.js @@ -24,13 +24,13 @@ function run(sender, command, args, reply, app) { if (command === "cancel") { reply(`${sender.commandStatus.action} cancelled.`) sender.commandStatus = undefined - return + return undefined } args.unshift(command) return sender.commandStatus.next(sender, args, reply, app) } reply("Unknown command. Try \"$cmdprefix help\" for help.") - return + return undefined } return commandFunc(sender, args, reply, app) } @@ -49,8 +49,8 @@ api - Call a Telegram API method. Args is always a JSON object. } - ///////////////////////////// - // Authentication handlers // +///////////////////////////// +// Authentication handlers // ///////////////////////////// /** @@ -88,7 +88,8 @@ commands.enterCode = async (sender, args, reply) => { reply(`Logged in successfully as @${sender.telegramPuppet.getDisplayName()}.`) sender.commandStatus = undefined } else if (data.status === "need-password") { - reply(`You have two-factor authentication enabled. Password hint: ${data.hint}\nEnter your password using "$cmdprefix "`) + reply(`You have two-factor authentication enabled. Password hint: ${data.hint} +Enter your password using "$cmdprefix "`) sender.commandStatus = { action: "Two-factor authentication", next: commands.enterPassword, @@ -114,11 +115,12 @@ commands.login = async (sender, args, reply) => { } try { - const data = await sender.sendTelegramCode(args[0]) + /*const data = */ + await sender.sendTelegramCode(args[0]) reply(`Login code sent to ${args[0]}.\nEnter the code using "$cmdprefix "`) sender.commandStatus = { action: "Phone code authentication", - next: commands.enterCode , + next: commands.enterCode, } } catch (err) { reply(`Failed to send code: ${err}`) @@ -139,13 +141,13 @@ commands.logout = async (sender, args, reply) => { } } - ////////////////////////////// - // General command handlers // +////////////////////////////// +// General command handlers // ////////////////////////////// - //////////////////////////// - // Debug command handlers // +//////////////////////////// +// Debug command handlers // //////////////////////////// commands.api = async (sender, args, reply, app) => { diff --git a/src/index.js b/src/index.js index 403c1968..bc17d61d 100755 --- a/src/index.js +++ b/src/index.js @@ -14,7 +14,7 @@ // // You should have received a copy of the GNU General Public License // along with this program. If not, see . -const {AppServiceRegistration} = require("matrix-appservice-bridge") +const { AppServiceRegistration } = require("matrix-appservice-bridge") const commander = require("commander") const YAML = require("yamljs") const fs = require("fs") @@ -41,11 +41,11 @@ if (commander.generateRegistration) { namespaces: { users: [{ exclusive: true, - regex: `@${config.bridge.username_template.replace("${ID}", ".+")}:${config.homeserver.domain}` + regex: `@${config.bridge.username_template.replace("${ID}", ".+")}:${config.homeserver.domain}`, }], aliases: [{ exclusive: true, - regex: `#${config.bridge.alias_template.replace("${NAME}", ".+")}:${config.homeserver.domain}` + regex: `#${config.bridge.alias_template.replace("${NAME}", ".+")}:${config.homeserver.domain}`, }], rooms: [], }, diff --git a/src/matrix-user.js b/src/matrix-user.js index a3f9e44d..77127282 100644 --- a/src/matrix-user.js +++ b/src/matrix-user.js @@ -97,7 +97,7 @@ class MatrixUser { async syncContacts() { const contacts = await this.telegramPuppet.client("contacts.getContacts", { - hash: md5(this.contactIDs.join(",")) + hash: md5(this.contactIDs.join(",")), }) if (contacts._ === "contacts.contactsNotModified") { return false @@ -112,7 +112,7 @@ class MatrixUser { return true } - async syncDialogs({createRooms=true} = {}) { + async syncDialogs({ createRooms = true } = {}) { const dialogs = await this.telegramPuppet.client("messages.getDialogs", {}) let changed = false for (const dialog of dialogs.chats.concat(dialogs.users)) { @@ -122,7 +122,7 @@ class MatrixUser { const peer = new TelegramPeer(dialog._, dialog.id, { receiverID: dialog._ === "user" ? this.telegramPuppet.userID - : undefined + : undefined, }) const portal = await this.app.getPortalByPeer(peer) if (await portal.updateInfo(this.telegramPuppet, dialog)) { @@ -130,7 +130,7 @@ class MatrixUser { } if (createRooms) { try { - const {roomID, created} = await portal.createMatrixRoom(this.telegramPuppet, { + const { roomID, created } = await portal.createMatrixRoom(this.telegramPuppet, { invite: [this.userID], }) if (!created) { @@ -143,7 +143,8 @@ class MatrixUser { //if (membership !== "join") { try { await intent.invite(roomID, this.userID) - } catch (_) {} + } catch (_) { + } //} } } catch (err) { @@ -159,11 +160,11 @@ class MatrixUser { if (this._telegramPuppet && this._telegramPuppet.userID) { throw new Error("You are already logged in. Please log out before logging in again.") } - switch(this.telegramPuppet.checkPhone(phoneNumber)) { - case "unregistered": - throw new Error("That number has not been registered. Please register it first.") - case "invalid": - throw new Error("Invalid phone number.") + switch (this.telegramPuppet.checkPhone(phoneNumber)) { + case "unregistered": + throw new Error("That number has not been registered. Please register it first.") + case "invalid": + throw new Error("Invalid phone number.") } try { const result = await this.telegramPuppet.sendCode(phoneNumber) diff --git a/src/portal.js b/src/portal.js index 9c628412..b0c99682 100644 --- a/src/portal.js +++ b/src/portal.js @@ -79,8 +79,8 @@ class Portal { const uploaded = await this.app.botIntent.getClient() .uploadContent({ - stream: new Buffer(file.bytes), - name: name, + stream: Buffer.from(file.bytes), + name, type: file.mimetype, }, { rawResponse: false }) @@ -106,13 +106,13 @@ class Portal { async handleMatrixEvent(sender, evt) { switch (evt.content.msgtype) { - case "m.notice": - case "m.text": - await this.loadAccessHash(sender.telegramPuppet) - sender.telegramPuppet.sendMessage(this.peer, evt.content.body) - break - default: - console.log("Unhandled event:", evt) + case "m.notice": + case "m.text": + await this.loadAccessHash(sender.telegramPuppet) + sender.telegramPuppet.sendMessage(this.peer, evt.content.body) + break + default: + console.log("Unhandled event:", evt) } } @@ -120,7 +120,7 @@ class Portal { return !!this.roomID } - async createMatrixRoom(telegramPOV, {invite = []} = {}) { + async createMatrixRoom(telegramPOV, { invite = [] } = {}) { if (this.roomID) { return { created: false, diff --git a/src/telegram-peer.js b/src/telegram-peer.js index 8f11fd14..2cee32f7 100644 --- a/src/telegram-peer.js +++ b/src/telegram-peer.js @@ -26,19 +26,19 @@ class TelegramPeer { static fromTelegramData(peer, sender, receiverID) { switch (peer._) { - case "peerChat": - return new TelegramPeer("chat", peer.chat_id) - case "peerUser": - return new TelegramPeer("user", sender, { - accessHash: peer.access_hash, - receiverID, - }) - case "peerChannel": - return new TelegramPeer("channel", peer.channel_id, { - accessHash: peer.access_hash, - }) - default: - throw new Error(`Unrecognized peer type ${peer._}`) + case "peerChat": + return new TelegramPeer("chat", peer.chat_id) + case "peerUser": + return new TelegramPeer("user", sender, { + accessHash: peer.access_hash, + receiverID, + }) + case "peerChannel": + return new TelegramPeer("channel", peer.channel_id, { + accessHash: peer.access_hash, + }) + default: + throw new Error(`Unrecognized peer type ${peer._}`) } } @@ -71,6 +71,7 @@ class TelegramPeer { } return false } + return false } async updateInfo(dialog) { @@ -92,82 +93,82 @@ class TelegramPeer { let info, users switch (this.type) { - case "user": - info = await telegramPOV.client("users.getFullUser", { - id: this.toInputObject(), - }) - users = [info.user] - info = info.user - break - case "chat": - info = await telegramPOV.client("messages.getFullChat", { - chat_id: this.id, - }) - users = info.users - info = info.chats[0] - break - case "channel": - info = await telegramPOV.client("channels.getFullChannel", { - channel: this.toInputObject(), - }) - info = info.chats[0] - const participants = await telegramPOV.client("channels.getParticipants", { - channel: this.toInputObject(), - filter: { _: "channelParticipantsRecent" }, - offset: 0, - limit: 1000, - }) - users = participants.users - break - default: - throw new Error(`Unknown peer type ${this.type}`) + case "user": + info = await telegramPOV.client("users.getFullUser", { + id: this.toInputObject(), + }) + users = [info.user] + info = info.user + break + case "chat": + info = await telegramPOV.client("messages.getFullChat", { + chat_id: this.id, + }) + users = info.users + info = info.chats[0] + break + case "channel": + info = await telegramPOV.client("channels.getFullChannel", { + channel: this.toInputObject(), + }) + info = info.chats[0] + const participants = await telegramPOV.client("channels.getParticipants", { + channel: this.toInputObject(), + filter: { _: "channelParticipantsRecent" }, + offset: 0, + limit: 1000, + }) + users = participants.users + break + default: + throw new Error(`Unknown peer type ${this.type}`) } return { - info: info, + info, users, } } toInputPeer() { switch (this.type) { - case "chat": - return { - _: "inputPeerChat", - chat_id: this.id, - } - case "user": - return { - _: "inputPeerUser", - user_id: this.id, - access_hash: this.accessHash, - } - case "channel": - return { - _: "inputPeerChannel", - channel_id: this.id, - access_hash: this.accessHash, - } - default: - throw new Error(`Unrecognized peer type ${this.type}`) + case "chat": + return { + _: "inputPeerChat", + chat_id: this.id, + } + case "user": + return { + _: "inputPeerUser", + user_id: this.id, + access_hash: this.accessHash, + } + case "channel": + return { + _: "inputPeerChannel", + channel_id: this.id, + access_hash: this.accessHash, + } + default: + throw new Error(`Unrecognized peer type ${this.type}`) } } toInputObject() { switch (this.type) { - case "user": - return { - _: "inputUser", - user_id: this.id, - access_hash: this.accessHash, - } - case "channel": - return { - _: "inputChannel", - channel_id: this.id, - access_hash: this.accessHash, - } - default: - throw new Error(`Unrecognized type ${this.type}`) + case "user": + return { + _: "inputUser", + user_id: this.id, + access_hash: this.accessHash, + } + case "channel": + return { + _: "inputChannel", + channel_id: this.id, + access_hash: this.accessHash, + } + default: + throw new Error(`Unrecognized type ${this.type}`) } } diff --git a/src/telegram-puppet.js b/src/telegram-puppet.js index 5faef4e4..b590c074 100644 --- a/src/telegram-puppet.js +++ b/src/telegram-puppet.js @@ -13,9 +13,8 @@ // // You should have received a copy of the GNU General Public License // along with this program. If not, see . -const pkg = require("../package.json") -const os = require("os") const telegram = require("telegram-mtproto") +const pkg = require("../package.json") const TelegramPeer = require("./telegram-peer") const META_FROM_FILETYPE = { @@ -37,7 +36,7 @@ const META_FROM_FILETYPE = { * TelegramPuppet represents a Telegram account being controlled from Matrix. */ class TelegramPuppet { - constructor(app, {userID, matrixUser, data, api_hash, api_id, server_config, api_config}) { + constructor(app, { userID, matrixUser, data, api_hash, api_id, server_config, api_config }) { this._client = undefined this.userID = userID this.matrixUser = matrixUser @@ -54,20 +53,20 @@ class TelegramPuppet { get: async (key) => { let value = this.data[key] // TODO test and (enable or remove) - /*if (typeof(value) === "string" && value.startsWith("b64:")) { - value = Array.from(new Buffer(value.substr("b64:".length), "base64")) - }*/ + if (typeof value === "string" && value.startsWith("b64:")) { + value = Array.from(Buffer.from(value.substr("b64:".length), "base64")) + } return value }, set: async (key, value) => { // TODO test and (enable or remove) - /*if (Array.isArray(value)) { + if (Array.isArray(value)) { console.log("Non-buffer array") - value = `b64:${new Buffer(value).toString("base64")}` + value = `b64:${Buffer.from(value).toString("base64")}` } else if (value instanceof Buffer) { console.log("Buffer array") value = `b64:${value.toString("base64")}` - }*/ + } console.warn("SET", key, "=", JSON.stringify(value)) if (this.data[key] === value) { return @@ -77,7 +76,7 @@ class TelegramPuppet { await this.matrixUser.save() }, remove: async (...keys) => { - console.warn("DEL", JSON.stringify(value)) + console.warn("DEL", JSON.stringify(keys)) keys.forEach((key) => delete this.data[key]) await this.matrixUser.save() }, @@ -91,8 +90,8 @@ class TelegramPuppet { this.apiConfig = Object.assign({}, { app_version: pkg.version, lang_code: "en", - api_id: api_id, - initConnection : 0x69796de9, + api_id, + initConnection: 0x69796de9, layer: 57, invokeWithLayer: 0xda9b0d0d, }, api_config) @@ -120,7 +119,6 @@ class TelegramPuppet { get client() { if (!this._client) { - const self = this this._client = telegram.MTProto({ api: this.apiConfig, server: this.serverConfig, @@ -212,7 +210,7 @@ class TelegramPuppet { const result = await this.client("messages.sendMessage", { peer: peer.toInputPeer(), message, - random_id: ~~(Math.random() * (1<<30)), + random_id: ~~(Math.random() * (1 << 30)), }) return result } @@ -220,8 +218,8 @@ class TelegramPuppet { async sendMedia(peer, media) { const result = await this.client("messages.sendMedia", { peer: peer.toInputPeer(), - media: media, - random_id: ~~(Math.random() * (1<<30)), + media, + random_id: ~~(Math.random() * (1 << 30)), }) // TODO use result? (maybe the ID) return result @@ -241,80 +239,83 @@ class TelegramPuppet { return } let peer, portal - switch(update._) { - case "updateUserStatus": - const user = await this.app.getTelegramUser(update.user_id) - let status - switch(update.status._) { - case "userStatusOnline": - status = "online" - break - case "userStatusOffline": - default: - status = "offline" - } - - await user.intent.getClient().setPresence({presence: status}) - break - case "updateUserTyping": - peer = new TelegramPeer("user", update.user_id, { receiverID: this.userID }) - case "updateChatUserTyping": - peer = peer || new TelegramPeer("chat", update.chat_id) - portal = await this.app.getPortalByPeer(peer) - if (portal.isMatrixRoomCreated()) { - const sender = await this.app.getTelegramUser(update.user_id) - // The Intent API currently doesn't allow you to set the - // typing timeout. Once it does, we should set it to ~5.5s - // as Telegram resends typing notifications every 5 seconds. - await sender.intent.sendTyping(portal.roomID, true/*, 5500*/) - } - break - case "updateShortMessage": - peer = new TelegramPeer("user", update.user_id, { receiverID: this.userID }) - case "updateShortChatMessage": - peer = peer || new TelegramPeer("chat", update.chat_id) - await this.handleMessage({ - from: update.user_id, - to: peer, - text: update.message, - }) - break - case "updateNewChannelMessage": - case "updateNewMessage": - // TODO handle other content types - update = update.message // Message defined at message#90dddc11 in layer 71 - await this.handleMessage({ - from: update.from_id, - to: TelegramPeer.fromTelegramData(update.to_id, update.from_id, this.userID), - text: update.message, - }) + switch (update._) { + case "updateUserStatus": + const user = await this.app.getTelegramUser(update.user_id) + let status + switch (update.status._) { + case "userStatusOnline": + status = "online" break + case "userStatusOffline": default: - console.log(`Update of type ${update._} received:\n${JSON.stringify(update, "", " ")}`) + status = "offline" + } + + await user.intent.getClient() + .setPresence({ presence: status }) + break + case "updateUserTyping": + peer = new TelegramPeer("user", update.user_id, { receiverID: this.userID }) + /* falls through */ + case "updateChatUserTyping": + peer = peer || new TelegramPeer("chat", update.chat_id) + portal = await this.app.getPortalByPeer(peer) + if (portal.isMatrixRoomCreated()) { + const sender = await this.app.getTelegramUser(update.user_id) + // The Intent API currently doesn't allow you to set the + // typing timeout. Once it does, we should set it to ~5.5s + // as Telegram resends typing notifications every 5 seconds. + await sender.intent.sendTyping(portal.roomID, true/*, 5500*/) + } + break + case "updateShortMessage": + peer = new TelegramPeer("user", update.user_id, { receiverID: this.userID }) + /* falls through */ + case "updateShortChatMessage": + peer = peer || new TelegramPeer("chat", update.chat_id) + await this.handleMessage({ + from: update.user_id, + to: peer, + text: update.message, + }) + break + case "updateNewChannelMessage": + case "updateNewMessage": + // TODO handle other content types + update = update.message // Message defined at message#90dddc11 in layer 71 + await this.handleMessage({ + from: update.from_id, + to: TelegramPeer.fromTelegramData(update.to_id, update.from_id, this.userID), + text: update.message, + }) + break + default: + console.log(`Update of type ${update._} received:\n${JSON.stringify(update, "", " ")}`) } } handleUpdate(data) { try { switch (data._) { - case "updateShort": - this.onUpdate(data.update) - break - case "updates": - for (const update of data.updates) { - this.onUpdate(update) - } - break - case "updateShortMessage": - case "updateShortChatMessage": - this.onUpdate(data) - break - default: - console.log("Unrecognized update type:", data._) + case "updateShort": + this.onUpdate(data.update) + break + case "updates": + for (const update of data.updates) { + this.onUpdate(update) + } + break + case "updateShortMessage": + case "updateShortChatMessage": + this.onUpdate(data) + break + default: + console.log("Unrecognized update type:", data._) } } catch (err) { console.error("Error handling update:", err) - console.log(e.stack) + console.log(err.stack) } } @@ -355,8 +356,9 @@ class TelegramPuppet { } setInterval(async () => { try { - const state = this.client("updates.getState", {}) // TODO use state? + /*const state = */ + this.client("updates.getState", {}) } catch (err) { console.error("Error updating state:", err) } @@ -364,12 +366,12 @@ class TelegramPuppet { } async getFile(location) { - location = Object.assign({}, location, {_: "inputFileLocation"}) + location = Object.assign({}, location, { _: "inputFileLocation" }) delete location.dc_id const file = await this.client("upload.getFile", { location, offset: 0, - limit: 100*1024*1024, + limit: 100 * 1024 * 1024, }) const meta = META_FROM_FILETYPE[file.type._] if (meta) { diff --git a/src/telegram-user.js b/src/telegram-user.js index 3930e9e4..f9c3702b 100644 --- a/src/telegram-user.js +++ b/src/telegram-user.js @@ -91,8 +91,8 @@ class TelegramUser { const userInfo = await this.intent.getProfileInfo(this.mxid, "displayname") if (userInfo.displayname !== this.getDisplayName()) { - this.intent.setDisplayName( - this.app.config.bridge.displayname_template.replace("${DISPLAYNAME}", this.getDisplayName())) + this.intent.setDisplayName(this.app.config.bridge.displayname_template + .replace("${DISPLAYNAME}", this.getDisplayName())) } if (updateAvatar && this.updateAvatar(telegramPOV, user)) { changed = true @@ -172,8 +172,8 @@ class TelegramUser { const name = `${photo.volume_id}_${photo.local_id}.${file.extension}` const uploaded = await this.uploadContent({ - stream: new Buffer(file.bytes), - name: name, + stream: Buffer.from(file.bytes), + name, type: file.mimetype, })