Refactor more code

This commit is contained in:
Tulir Asokan
2018-02-23 21:24:18 +02:00
parent 9e5843a0dc
commit 9e5cb84140
3 changed files with 30 additions and 17 deletions
-1
View File
@@ -31,4 +31,3 @@ class Context:
yield self.config yield self.config
yield self.loop yield self.loop
yield self.bot yield self.bot
# yield self.mx
+22 -14
View File
@@ -332,16 +332,19 @@ class Portal:
user.register_portal(self) user.register_portal(self)
await self.main_intent.invite(self.mxid, user.mxid) await self.main_intent.invite(self.mxid, user.mxid)
async def delete_telegram_user(self, user_id, kick_message=None): async def delete_telegram_user(self, user_id, sender):
puppet = p.Puppet.get(user_id) puppet = p.Puppet.get(user_id)
user = u.User.get_by_tgid(user_id) user = u.User.get_by_tgid(user_id)
if kick_message: kick_message = (f"Kicked by {sender.displayname}"
if sender and sender.tgid != user.tgid
else "Left Telegram chat")
if sender and sender.tgid != user.tgid:
await self.main_intent.kick(self.mxid, puppet.mxid, kick_message) await self.main_intent.kick(self.mxid, puppet.mxid, kick_message)
else: else:
await puppet.intent.leave_room(self.mxid) await puppet.intent.leave_room(self.mxid)
if user: if user:
user.unregister_portal(self) user.unregister_portal(self)
await self.main_intent.kick(self.mxid, user.mxid, kick_message or "Left Telegram chat") await self.main_intent.kick(self.mxid, user.mxid, kick_message)
async def update_info(self, user, entity=None): async def update_info(self, user, entity=None):
if self.peer_type == "user": if self.peer_type == "user":
@@ -367,7 +370,7 @@ class Portal:
if changed: if changed:
self.save() self.save()
async def update_username(self, username): async def update_username(self, username, save=False):
if self.username != username: if self.username != username:
if self.username: if self.username:
await self.main_intent.remove_room_alias(self._get_alias_localpart()) await self.main_intent.remove_room_alias(self._get_alias_localpart())
@@ -377,20 +380,27 @@ class Portal:
await self.main_intent.set_join_rule(self.mxid, "public") await self.main_intent.set_join_rule(self.mxid, "public")
else: else:
await self.main_intent.set_join_rule(self.mxid, "invite") await self.main_intent.set_join_rule(self.mxid, "invite")
if save:
self.save()
return True return True
return False return False
async def update_about(self, about): async def update_about(self, about, save=False):
if self.about != about: if self.about != about:
self.about = about self.about = about
await self.main_intent.set_room_topic(self.mxid, self.about) await self.main_intent.set_room_topic(self.mxid, self.about)
if save:
self.save()
return True return True
return False return False
async def update_title(self, title): async def update_title(self, title, save=False):
if self.title != title: if self.title != title:
self.title = title self.title = title
await self.main_intent.set_room_name(self.mxid, self.title) await self.main_intent.set_room_name(self.mxid, self.title)
if save:
self.save()
return True return True
return False return False
@@ -399,7 +409,7 @@ class Portal:
return max(photo.sizes, key=(lambda photo2: ( return max(photo.sizes, key=(lambda photo2: (
len(photo2.bytes) if isinstance(photo2, PhotoCachedSize) else photo2.size))) len(photo2.bytes) if isinstance(photo2, PhotoCachedSize) else photo2.size)))
async def update_avatar(self, user, photo): async def update_avatar(self, user, photo, save=False):
photo_id = f"{photo.volume_id}-{photo.local_id}" photo_id = f"{photo.volume_id}-{photo.local_id}"
if self.photo_id != photo_id: if self.photo_id != photo_id:
file = await util.transfer_file_to_matrix(self.db, user.client, self.main_intent, file = await util.transfer_file_to_matrix(self.db, user.client, self.main_intent,
@@ -407,6 +417,8 @@ class Portal:
if file: if file:
await self.main_intent.set_room_avatar(self.mxid, file.mxc) await self.main_intent.set_room_avatar(self.mxid, file.mxc)
self.photo_id = photo_id self.photo_id = photo_id
if save:
self.save()
return True return True
return False return False
@@ -974,21 +986,17 @@ class Portal:
# TODO figure out how to see changes to about text / channel username # TODO figure out how to see changes to about text / channel username
if isinstance(action, MessageActionChatEditTitle): if isinstance(action, MessageActionChatEditTitle):
if await self.update_title(action.title): await self.update_title(action.title, save=True)
self.save()
elif isinstance(action, MessageActionChatEditPhoto): elif isinstance(action, MessageActionChatEditPhoto):
largest_size = self._get_largest_photo_size(action.photo) largest_size = self._get_largest_photo_size(action.photo)
if await self.update_avatar(source, largest_size.location): self.update_avatar(source, largest_size.location, save=True)
self.save()
elif isinstance(action, MessageActionChatAddUser): elif isinstance(action, MessageActionChatAddUser):
for user_id in action.users: for user_id in action.users:
await self.add_telegram_user(user_id, source) await self.add_telegram_user(user_id, source)
elif isinstance(action, MessageActionChatJoinedByLink): elif isinstance(action, MessageActionChatJoinedByLink):
await self.add_telegram_user(sender.id, source) await self.add_telegram_user(sender.id, source)
elif isinstance(action, MessageActionChatDeleteUser): elif isinstance(action, MessageActionChatDeleteUser):
kick_message = (f"Kicked by {sender.displayname}" await self.delete_telegram_user(action.user_id, sender)
if sender.id != action.user_id else None)
await self.delete_telegram_user(action.user_id, kick_message)
elif isinstance(action, MessageActionChatMigrateTo): elif isinstance(action, MessageActionChatMigrateTo):
self.peer_type = "channel" self.peer_type = "channel"
self.migrate_and_save(action.channel_id) self.migrate_and_save(action.channel_id)
+8 -2
View File
@@ -20,6 +20,7 @@ import logging
import magic import magic
from PIL import Image from PIL import Image
from sqlalchemy.exc import IntegrityError
from telethon.tl.types import (Document, FileLocation, InputFileLocation, from telethon.tl.types import (Document, FileLocation, InputFileLocation,
InputDocumentFileLocation) InputDocumentFileLocation)
@@ -69,7 +70,12 @@ async def transfer_file_to_matrix(db, client, intent, location):
db_file = DBTelegramFile(id=id, mxc=uploaded["content_uri"], db_file = DBTelegramFile(id=id, mxc=uploaded["content_uri"],
mime_type=mime_type, was_converted=image_converted, mime_type=mime_type, was_converted=image_converted,
timestamp=int(time.time())) timestamp=int(time.time()))
db.add(db_file) try:
db.commit() db.add(db_file)
db.commit()
except IntegrityError:
log.exception("Integrity error while saving transferred file data. "
"This was probably caused by two simultaneous transfers of the same file, "
"and should not cause any problems.")
return db_file return db_file