Add image extension if missing, fix join swallowing msgs
This commit is contained in:
parent
88769a95b7
commit
fcdc5a2fd2
@ -142,6 +142,16 @@ def matrix_is_telegram(user_id):
|
|||||||
def get_username(user_id):
|
def get_username(user_id):
|
||||||
return user_id.split(':')[0][1:]
|
return user_id.split(':')[0][1:]
|
||||||
|
|
||||||
|
mime_extensions = {
|
||||||
|
'image/jpeg': 'jpg',
|
||||||
|
'image/gif': 'gif',
|
||||||
|
'image/png': 'png',
|
||||||
|
'image/tiff': 'tif',
|
||||||
|
'image/x-tiff': 'tif',
|
||||||
|
'image/bmp': 'bmp',
|
||||||
|
'image/x-windows-bmp': 'bmp'
|
||||||
|
}
|
||||||
|
|
||||||
async def matrix_transaction(request):
|
async def matrix_transaction(request):
|
||||||
"""
|
"""
|
||||||
Handle a transaction sent by the homeserver.
|
Handle a transaction sent by the homeserver.
|
||||||
@ -225,8 +235,16 @@ async def matrix_transaction(request):
|
|||||||
elif content['msgtype'] == 'm.image':
|
elif content['msgtype'] == 'm.image':
|
||||||
try:
|
try:
|
||||||
url = urlparse(content['url'])
|
url = urlparse(content['url'])
|
||||||
|
|
||||||
|
# Append the correct extension if it's missing or wrong
|
||||||
|
ext = mime_extensions[content['info']['mimetype']]
|
||||||
|
if not content['body'].endswith(ext):
|
||||||
|
content['body'] += '.' + ext
|
||||||
|
|
||||||
|
# Download the file
|
||||||
await download_matrix_file(url, content['body'])
|
await download_matrix_file(url, content['body'])
|
||||||
with open('/tmp/{}'.format(content['body']), 'rb') as img_file:
|
with open('/tmp/{}'.format(content['body']), 'rb') as img_file:
|
||||||
|
# Create the URL and shorten it
|
||||||
url_str = MATRIX_HOST_EXT + \
|
url_str = MATRIX_HOST_EXT + \
|
||||||
'_matrix/media/r0/download/{}{}' \
|
'_matrix/media/r0/download/{}{}' \
|
||||||
.format(url.netloc, quote(url.path))
|
.format(url.netloc, quote(url.path))
|
||||||
@ -369,8 +387,8 @@ async def matrix_room(request):
|
|||||||
|
|
||||||
|
|
||||||
def send_matrix_message(room_id, user_id, txn_id, **kwargs):
|
def send_matrix_message(room_id, user_id, txn_id, **kwargs):
|
||||||
return matrix_put('client', 'rooms/{}/send/m.room.message/{}'
|
url = 'rooms/{}/send/m.room.message/{}'.format(room_id, txn_id)
|
||||||
.format(room_id, txn_id), user_id, kwargs)
|
return matrix_put('client', url, user_id, kwargs)
|
||||||
|
|
||||||
|
|
||||||
async def upload_tgfile_to_matrix(file_id, user_id):
|
async def upload_tgfile_to_matrix(file_id, user_id):
|
||||||
@ -419,7 +437,7 @@ async def aiotg_photo(chat, photo):
|
|||||||
|
|
||||||
room_id = link.matrix_room
|
room_id = link.matrix_room
|
||||||
user_id = USER_ID_FORMAT.format(chat.sender['id'])
|
user_id = USER_ID_FORMAT.format(chat.sender['id'])
|
||||||
txn_id = quote('{}:{}'.format(chat.message['message_id'], chat.id))
|
txn_id = quote('{}{}'.format(chat.message['message_id'], chat.id))
|
||||||
|
|
||||||
file_id = photo[-1]['file_id']
|
file_id = photo[-1]['file_id']
|
||||||
uri, length = await upload_tgfile_to_matrix(file_id, user_id)
|
uri, length = await upload_tgfile_to_matrix(file_id, user_id)
|
||||||
@ -513,11 +531,10 @@ async def aiotg_message(chat, match):
|
|||||||
msgtype='m.text')
|
msgtype='m.text')
|
||||||
|
|
||||||
if 'errcode' in j and j['errcode'] == 'M_FORBIDDEN':
|
if 'errcode' in j and j['errcode'] == 'M_FORBIDDEN':
|
||||||
await asyncio.sleep(0.1)
|
|
||||||
await register_join_matrix(chat, room_id, user_id)
|
await register_join_matrix(chat, room_id, user_id)
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.5)
|
||||||
await send_matrix_message(room_id, user_id, txn_id, body=message,
|
j = await send_matrix_message(room_id, user_id, txn_id + 'join',
|
||||||
msgtype='m.text')
|
body=message, msgtype='m.text')
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
Loading…
Reference in New Issue
Block a user