Handle leaving Matrix rooms

Fixes #28 - Leaving group chat portals leaves the Telegram chat
Fixes #29 - Leaving private chat portals makes the bridge forget that room
This commit is contained in:
Tulir Asokan
2018-01-29 23:01:03 +02:00
parent 4593e1c857
commit b8bcd84c68
4 changed files with 24 additions and 7 deletions
+16 -3
View File
@@ -16,9 +16,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from telethon.tl.functions.messages import (GetFullChatRequest, EditChatAdminRequest,
CreateChatRequest, AddChatUserRequest,
ExportChatInviteRequest)
ExportChatInviteRequest, DeleteChatUserRequest)
from telethon.tl.functions.channels import (GetParticipantsRequest, CreateChannelRequest,
InviteToChannelRequest, ExportInviteRequest)
InviteToChannelRequest, ExportInviteRequest,
LeaveChannelRequest)
from telethon.errors.rpc_error_list import ChatAdminRequiredError, LocationInvalidError
from telethon.tl.types import *
from PIL import Image
@@ -267,6 +268,17 @@ class Portal:
file_name = f"matrix_upload{mimetypes.guess_extension(mime)}"
return file_name, None if file_name == body else body
def leave_matrix(self, user):
if self.peer_type == "user":
self.main_intent.leave_room(self.mxid)
self.delete()
del self.by_tgid[self.tgid_full]
del self.by_mxid[self.mxid]
elif self.peer_type == "chat":
user.client(DeleteChatUserRequest(chat_id=self.tgid, user_id=InputUserSelf()))
elif self.peer_type == "channel":
user.client(LeaveChannelRequest(channel=user.client.get_input_entity(self.peer)))
def handle_matrix_message(self, sender, message, event_id):
type = message["msgtype"]
if type in {"m.text", "m.emote"}:
@@ -581,7 +593,7 @@ class Portal:
existing = DBPortal.query.get(self.tgid_full)
if existing:
self.db.object_session(existing).delete(existing)
self.by_tgid[self.tgid_full] = None
del self.by_tgid[self.tgid_full]
self.tgid = new_id
self.by_tgid[self.tgid_full] = self
self.save()
@@ -592,6 +604,7 @@ class Portal:
def delete(self):
self.db.delete(self.to_db())
self.db.commit()
@classmethod
def from_db(cls, db_portal):