Allow upgrading Telegram groups to supergroups from Matrix

This commit is contained in:
Tulir Asokan
2017-12-03 22:34:47 +02:00
parent 58ea45638e
commit 8a082a4598
4 changed files with 39 additions and 9 deletions
+1
View File
@@ -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
+17 -1
View File
@@ -89,7 +89,8 @@ _**Telegram actions**: commands for using the bridge to interact with Telegram._
**logout** - Log out from Telegram.<br/>
**search** [_-r|--remote_] &lt;_query_&gt; - Search your contacts or the Telegram servers for users.<br/>
**create** &lt;_group/channel_&gt; [_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.<br/>
**upgrade** - Upgrade a normal Telegram group to a supergroup.
_**Temporary commands**: commands that will be replaced with more Matrix-y actions later._<br/>
**pm** &lt;_id_&gt; - 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] <query>`")
+10
View File
@@ -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.
*
+11 -8
View File
@@ -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)
}
}