mirror of
https://github.com/mautrix/telegram.git
synced 2026-05-17 07:25:46 +03:00
Implement kicking and fix invites
This commit is contained in:
@@ -95,9 +95,9 @@ does not do this automatically.
|
||||
* [x] Typing notifications
|
||||
* [ ] Pinning messages
|
||||
* [ ] Admin status
|
||||
* [ ] Membership actions
|
||||
* [ ] Inviting
|
||||
* [ ] Kicking (currently shown as leaving)
|
||||
* [x] Membership actions
|
||||
* [x] Inviting
|
||||
* [x] Kicking
|
||||
* [x] Joining/leaving
|
||||
* [x] Chat metadata changes
|
||||
* [ ] Public channel username changes
|
||||
@@ -105,7 +105,7 @@ does not do this automatically.
|
||||
* [x] Supergroup upgrade
|
||||
* Initiating chats
|
||||
* [x] Automatic portal creation for groups/channels at startup
|
||||
* [ ] Automatic portal creation for groups/channels when receiving invite/message
|
||||
* [x] Automatic portal creation for groups/channels when receiving invite/message
|
||||
* [ ] Private chat creation by inviting Telegram user to new room
|
||||
* [ ] Joining public channels/supergroups using room aliases
|
||||
* [ ] Searching for Telegram users using management commands
|
||||
|
||||
@@ -245,6 +245,10 @@ class IntentAPI:
|
||||
def send_message(self, room_id, body):
|
||||
return self.send_event(room_id, "m.room.message", body)
|
||||
|
||||
def kick(self, room_id, user_id, message):
|
||||
self._ensure_joined(room_id)
|
||||
self.client.kick_user(room_id, user_id, message)
|
||||
|
||||
def send_event(self, room_id, type, body, txn_id=None, timestamp=None):
|
||||
self._ensure_joined(room_id)
|
||||
self._ensure_has_power_level_for(room_id, type)
|
||||
|
||||
@@ -20,7 +20,7 @@ from telethon.tl.types import *
|
||||
import mimetypes
|
||||
import magic
|
||||
from .db import Portal as DBPortal, Message as DBMessage
|
||||
from . import puppet as p, formatter
|
||||
from . import puppet as p, user as u, formatter
|
||||
|
||||
config = None
|
||||
|
||||
@@ -124,9 +124,18 @@ class Portal:
|
||||
puppet.update_info(source, entity)
|
||||
puppet.intent.join_room(self.mxid)
|
||||
|
||||
def delete_telegram_user(self, user_id):
|
||||
user = u.User.get_by_tgid(user_id)
|
||||
if user:
|
||||
self.main_intent.invite(self.mxid, user.mxid)
|
||||
|
||||
def delete_telegram_user(self, user_id, kick_message=None):
|
||||
puppet = p.Puppet.get(user_id)
|
||||
puppet.intent.leave_room(self.mxid)
|
||||
user = u.User.get_by_tgid(user_id)
|
||||
if kick_message:
|
||||
self.main_intent.kick(self.mxid, puppet.mxid, kick_message)
|
||||
else:
|
||||
puppet.intent.leave_room(self.mxid)
|
||||
self.main_intent.kick(self.mxid, user.mxid, kick_message or "Left Telegram chat")
|
||||
|
||||
def update_info(self, user, entity=None):
|
||||
if self.peer_type == "user":
|
||||
@@ -333,9 +342,12 @@ class Portal:
|
||||
|
||||
def handle_telegram_action(self, source, sender, action):
|
||||
if not self.mxid:
|
||||
if isinstance(action, (MessageActionChatCreate, MessageActionChannelCreate)):
|
||||
create_and_exit = [MessageActionChatCreate, MessageActionChannelCreate]
|
||||
create_and_continue = [MessageActionChatAddUser, MessageActionChatJoinedByLink]
|
||||
if isinstance(action, create_and_exit + create_and_continue):
|
||||
self.create_room(source, invites=[source.mxid])
|
||||
return
|
||||
if isinstance(action, create_and_exit):
|
||||
return
|
||||
|
||||
if isinstance(action, MessageActionChatEditTitle):
|
||||
if self.update_title(action.title, self.main_intent):
|
||||
@@ -350,8 +362,10 @@ class Portal:
|
||||
elif isinstance(action, MessageActionChatJoinedByLink):
|
||||
self.add_telegram_user(sender.id, source)
|
||||
elif isinstance(action, MessageActionChatDeleteUser):
|
||||
# TODO show kick message if user was kicked
|
||||
self.delete_telegram_user(action.user_id)
|
||||
kick_message = None
|
||||
if sender.id != action.user_id:
|
||||
kick_message = f"Kicked by {sender.displayname}"
|
||||
self.delete_telegram_user(action.user_id, kick_message)
|
||||
elif isinstance(action, MessageActionChatMigrateTo):
|
||||
self.peer_type = "channel"
|
||||
self.migrate_and_save(action.channel_id)
|
||||
|
||||
@@ -166,9 +166,9 @@ class User:
|
||||
dialogs = self.client.get_dialogs(limit=30)
|
||||
for dialog in dialogs:
|
||||
entity = dialog.entity
|
||||
if isinstance(entity, User) \
|
||||
or (isinstance(entity, Chat) and entity.deactivated) \
|
||||
or isinstance(entity, ChannelForbidden):
|
||||
if (isinstance(entity, User)
|
||||
or (isinstance(entity, Chat) and entity.deactivated)
|
||||
or isinstance(entity, (ChannelForbidden, ChatForbidden))):
|
||||
continue
|
||||
portal = po.Portal.get_by_entity(entity)
|
||||
portal.create_room(self, entity, invites=[self.mxid])
|
||||
|
||||
Reference in New Issue
Block a user