Skip to content

Commit

Permalink
Fix line lengths and add limit to .editorconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Apr 29, 2018
1 parent 73e7b8f commit 1e0f2c7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.py]
max_line_length = 99

[*.{yaml,yml,py}]
indent_style = space
9 changes: 5 additions & 4 deletions mautrix_telegram/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ async def handle_invite(self, room, user, inviter):
if user == self.az.bot_mxid:
await self.az.intent.join_room(room)
if not inviter.whitelisted:
await self.az.intent.send_notice(room, text=None,
html="You are not whitelisted to use this bridge.<br/><br/>"
"If you are the owner of this bridge, see the "
"<code>bridge.permissions</code> section in your config file.")
await self.az.intent.send_notice(
room, text=None,
html="You are not whitelisted to use this bridge.<br/><br/>"
"If you are the owner of this bridge, see the "
"<code>bridge.permissions</code> section in your config file.")
await self.az.intent.leave_room(room)
return
elif not inviter.whitelisted:
Expand Down
40 changes: 24 additions & 16 deletions mautrix_telegram/portal.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class Portal:
by_mxid = {}
by_tgid = {}

def __init__(self, tgid, peer_type, tg_receiver=None, mxid=None, username=None, megagroup=False, title=None,
about=None, photo_id=None, db_instance=None):
def __init__(self, tgid, peer_type, tg_receiver=None, mxid=None, username=None,
megagroup=False, title=None, about=None, photo_id=None, db_instance=None):
self.mxid = mxid
self.tgid = tgid
self.tg_receiver = tg_receiver or tgid
Expand Down Expand Up @@ -211,7 +211,8 @@ async def update_matrix_room(self, user, entity, direct, puppet=None,
await puppet.update_info(user, entity)
await puppet.intent.join_room(self.mxid)

async def create_matrix_room(self, user, entity=None, invites=None, update_if_exists=True, synchronous=False):
async def create_matrix_room(self, user, entity=None, invites=None, update_if_exists=True,
synchronous=False):
if self.mxid:
if update_if_exists:
if not entity:
Expand Down Expand Up @@ -487,7 +488,8 @@ async def _get_users(self, user, entity):
users, participants = [], []
offset = 0
remaining_quota = limit if limit > 0 else 1000000
query = ChannelParticipantsSearch("") if limit == -1 else ChannelParticipantsRecent()
query = (ChannelParticipantsSearch("") if limit == -1
else ChannelParticipantsRecent())
while True:
if remaining_quota <= 0:
break
Expand Down Expand Up @@ -707,10 +709,12 @@ async def _handle_matrix_text(self, sender_id, event_id, space, client, message,

lock = self.require_send_lock(sender_id)
async with lock:
response = await client.send_message(self.peer, message, entities=entities, reply_to=reply_to)
response = await client.send_message(self.peer, message, entities=entities,
reply_to=reply_to)
self._add_telegram_message_to_db(event_id, space, response)

async def _handle_matrix_file(self, type, sender_id, event_id, space, client, message, reply_to):
async def _handle_matrix_file(self, type, sender_id, event_id, space, client, message,
reply_to):
file = await self.main_intent.download_file(message["url"])

info = message.get("info", {})
Expand Down Expand Up @@ -739,7 +743,8 @@ async def _handle_matrix_file(self, type, sender_id, event_id, space, client, me
media = await client.upload_file(file, mime, attributes, file_name)
lock = self.require_send_lock(sender_id)
async with lock:
response = await client.send_media(self.peer, media, reply_to=reply_to, caption=caption)
response = await client.send_media(self.peer, media, reply_to=reply_to,
caption=caption)
self._add_telegram_message_to_db(event_id, space, response)

async def _handle_matrix_location(self, sender_id, event_id, space, client, message, reply_to):
Expand All @@ -754,8 +759,8 @@ async def _handle_matrix_location(self, sender_id, event_id, space, client, mess

lock = self.require_send_lock(sender_id)
async with lock:
response = await client.send_media(self.peer, media, reply_to=reply_to, caption=message,
entities=entities)
response = await client.send_media(self.peer, media, reply_to=reply_to,
caption=message, entities=entities)
self._add_telegram_message_to_db(event_id, space, response)

def _add_telegram_message_to_db(self, event_id, space, response):
Expand All @@ -782,9 +787,11 @@ async def handle_matrix_message(self, sender, message, event_id):
if type == "m.text" or (self.bridge_notices and type == "m.notice"):
await self._handle_matrix_text(sender_id, event_id, space, client, message, reply_to)
elif type == "m.location":
await self._handle_matrix_location(sender_id, event_id, space, client, message, reply_to)
await self._handle_matrix_location(sender_id, event_id, space, client, message,
reply_to)
elif type in ("m.sticker", "m.image", "m.file", "m.audio", "m.video"):
await self._handle_matrix_file(type, sender_id, event_id, space, client, message, reply_to)
await self._handle_matrix_file(type, sender_id, event_id, space, client, message,
reply_to)
else:
self.log.debug("Unhandled Matrix event: %s", message)

Expand Down Expand Up @@ -1444,9 +1451,9 @@ def db_instance(self):
return self._db_instance

def new_db_instance(self):
return DBPortal(tgid=self.tgid, tg_receiver=self.tg_receiver, peer_type=self.peer_type, mxid=self.mxid,
username=self.username, megagroup=self.megagroup, title=self.title, about=self.about,
photo_id=self.photo_id)
return DBPortal(tgid=self.tgid, tg_receiver=self.tg_receiver, peer_type=self.peer_type,
mxid=self.mxid, username=self.username, megagroup=self.megagroup,
title=self.title, about=self.about, photo_id=self.photo_id)

def migrate_and_save(self, new_id):
existing = DBPortal.query.get(self.tgid_full)
Expand Down Expand Up @@ -1484,8 +1491,9 @@ def delete(self):

@classmethod
def from_db(cls, db_portal):
return Portal(tgid=db_portal.tgid, tg_receiver=db_portal.tg_receiver, peer_type=db_portal.peer_type,
mxid=db_portal.mxid, username=db_portal.username, megagroup=db_portal.megagroup,
return Portal(tgid=db_portal.tgid, tg_receiver=db_portal.tg_receiver,
peer_type=db_portal.peer_type, mxid=db_portal.mxid,
username=db_portal.username, megagroup=db_portal.megagroup,
title=db_portal.title, about=db_portal.about, photo_id=db_portal.photo_id,
db_instance=db_portal)

Expand Down
7 changes: 4 additions & 3 deletions mautrix_telegram/puppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Puppet:
hs_domain = None
cache = {}

def __init__(self, id=None, username=None, displayname=None, photo_id=None, is_bot=None, db_instance=None):
def __init__(self, id=None, username=None, displayname=None, photo_id=None, is_bot=None,
db_instance=None):
self.id = id
self.mxid = self.get_mxid_from_id(self.id)

Expand Down Expand Up @@ -67,8 +68,8 @@ def new_db_instance(self):

@classmethod
def from_db(cls, db_puppet):
return Puppet(db_puppet.id, db_puppet.username, db_puppet.displayname, db_puppet.photo_id, db_puppet.is_bot,
db_instance=db_puppet)
return Puppet(db_puppet.id, db_puppet.username, db_puppet.displayname, db_puppet.photo_id,
db_puppet.is_bot, db_instance=db_puppet)

def save(self):
self.db_instance.username = self.username
Expand Down
3 changes: 2 additions & 1 deletion mautrix_telegram/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ async def sync_dialogs(self, synchronous_create=False):
portal = po.Portal.get_by_entity(entity)
self.portals[portal.tgid_full] = portal
creators.append(
portal.create_matrix_room(self, entity, invites=[self.mxid], synchronous=synchronous_create))
portal.create_matrix_room(self, entity, invites=[self.mxid],
synchronous=synchronous_create))
self.save()
await asyncio.gather(*creators, loop=self.loop)

Expand Down

0 comments on commit 1e0f2c7

Please sign in to comment.