diff --git a/README.md b/README.md index 8d8db555..3a139d7e 100644 --- a/README.md +++ b/README.md @@ -100,4 +100,5 @@ does not do this automatically. * Misc * [ ] Use optional bot to relay messages for unauthenticated Matrix users * [x] Properly handle upgrading groups to supergroups + * [x] Allow upgrading group to supergroup from Matrix * [ ] Handle public channel username changes diff --git a/src/commands.js b/src/commands.js index 5f8f62e6..61c19102 100644 --- a/src/commands.js +++ b/src/commands.js @@ -89,7 +89,8 @@ _**Telegram actions**: commands for using the bridge to interact with Telegram._ **logout** - Log out from Telegram.
**search** [_-r|--remote_] <_query_> - Search your contacts or the Telegram servers for users.
**create** <_group/channel_> [_room ID_] - Create a Telegram chat of the given type for a Matrix room. - If the room ID is not specified, a chat for the current room is created. + If the room ID is not specified, a chat for the current room is created.
+**upgrade** - Upgrade a normal Telegram group to a supergroup. _**Temporary commands**: commands that will be replaced with more Matrix-y actions later._
**pm** <_id_> - Open a private chat with the given Telegram user ID. @@ -287,6 +288,21 @@ commands.create = async (sender, args, reply, { app, roomID }) => { } } +commands.upgrade = async (sender, args, reply, { app, roomID }) => { + if (!sender._telegramPuppet) { + reply("This command requires you to be logged in.") + return + } + + const portal = await app.getPortalByRoomID(roomID) + if (!portal) { + reply("This is not a portal room.") + return + } + + await portal.upgradeTelegramChat(sender.telegramPuppet) +} + commands.search = async (sender, args, reply, { app }) => { if (args.length < 1) { reply("**Usage:** `$cmdprefix search [-r|--remote] `") diff --git a/src/portal.js b/src/portal.js index dd1384d1..fe66d4a9 100644 --- a/src/portal.js +++ b/src/portal.js @@ -541,6 +541,16 @@ class Portal { await this.save() } + async upgradeTelegramChat(telegramPOV) { + if (this.peer.type !== "chat") { + throw new Error("Can't upgrade non-chat portal.") + } + const updates = await telegramPOV.client("messages.migrateChat", { + chat_id: this.id, + }) + await telegramPOV.handleUpdate(updates) + } + /** * Create a Matrix room for this portal. * diff --git a/src/telegram-puppet.js b/src/telegram-puppet.js index fd4788c2..d235f639 100644 --- a/src/telegram-puppet.js +++ b/src/telegram-puppet.js @@ -328,6 +328,7 @@ class TelegramPuppet { to = TelegramPeer.fromTelegramData(update.to_id, update.from_id, this.userID) break case "updateReadMessages": + case "updateReadHistoryOutbox": case "updateDeleteMessages": case "updateRestoreMessages": this.pts = update.pts @@ -379,38 +380,40 @@ class TelegramPuppet { }) } - handleUpdate(data) { + async handleUpdate(data) { try { switch (data._) { case "updateShort": this.date = data.date - this.onUpdate(data.update) + await this.onUpdate(data.update) break case "updates": this.date = data.date + const updateHandlers = [] for (const update of data.updates) { - this.onUpdate(update) + updateHandlers.push(this.onUpdate(update)) } + await Promise.all(updateHandlers) break case "updateShortMessage": case "updateShortChatMessage": - this.onUpdate(data) + await this.onUpdate(data) break case "updatesTooLong": console.log("Handling updatesTooLong") - this.client("updates.getDifference", { + const dat = await this.client("updates.getDifference", { pts: this.pts, date: this.date, qts: -1, - }).then(dat => console.log("getDifference", dat), - err => console.error("getDifferenceFail", err)) + }) + console.log("updatesTooLong data:", dat) break default: console.log("Unrecognized update type:", data._) } } catch (err) { console.error("Error handling update:", err) - console.log(err.stack) + console.error(err.stack) } }