Telegram profile pictures and display names are updated
The bridge updates the displayname and/or the avatar of the bridged Matrix user when the Telegram user changes them. This is done by checking the name and profile pic of the sender when a message is received from Telegram, and comparing to the database to see if they have changed. If they have, it will update the database and send the updates to the Matrix homeserver.
This commit is contained in:
parent
89779b4da7
commit
fc65d5f0f5
@ -200,6 +200,8 @@ async def matrix_transaction(request):
|
||||
group = TG_BOT.group(link.tg_room)
|
||||
|
||||
try:
|
||||
response = None
|
||||
|
||||
if event['type'] == 'm.room.message':
|
||||
user_id = event['user_id']
|
||||
if matrix_is_telegram(user_id):
|
||||
@ -448,6 +450,39 @@ async def register_join_matrix(chat, room_id, user_id):
|
||||
user_id, {'displayname': name})
|
||||
await matrix_post('client', 'join/{}'.format(room_id), user_id, {})
|
||||
|
||||
async def update_matrix_displayname_avatar(tg_user):
|
||||
name = tg_user['first_name']
|
||||
if 'last_name' in tg_user:
|
||||
name += ' ' + tg_user['last_name']
|
||||
name += ' (Telegram)'
|
||||
user_id = USER_ID_FORMAT.format(tg_user['id'])
|
||||
|
||||
db_user = db.session.query(db.TgUser).filter_by(tg_id=tg_user['id']).first()
|
||||
|
||||
profile_photos = await TG_BOT.get_user_profile_photos(tg_user['id'])
|
||||
pp_file_id = None
|
||||
try:
|
||||
pp_file_id = profile_photos['result']['photos'][0][-1]['file_id']
|
||||
except:
|
||||
pp_file_id = None
|
||||
|
||||
if db_user:
|
||||
if db_user.name != name:
|
||||
await matrix_put('client', 'profile/{}/displayname'.format(user_id), user_id, {'displayname': name})
|
||||
db_user.name = name
|
||||
if db_user.profile_pic_id != pp_file_id:
|
||||
pp_uri, _ = await upload_tgfile_to_matrix(pp_file_id, user_id)
|
||||
await matrix_put('client', 'profile/{}/avatar_url'.format(user_id), user_id, {'avatar_url':pp_uri})
|
||||
db_user.profile_pic_id = pp_file_id
|
||||
else:
|
||||
db_user = db.TgUser(tg_user['id'], name, pp_file_id)
|
||||
await matrix_put('client', 'profile/{}/displayname'.format(user_id), user_id, {'displayname': name})
|
||||
pp_uri, _ = await upload_tgfile_to_matrix(pp_file_id, user_id)
|
||||
await matrix_put('client', 'profile/{}/avatar_url'.format(user_id), user_id, {'avatar_url':pp_uri})
|
||||
db.session.add(db_user)
|
||||
db.session.commit()
|
||||
|
||||
|
||||
@TG_BOT.handle('sticker')
|
||||
async def aiotg_sticker(chat, sticker):
|
||||
link = db.session.query(db.ChatLink).filter_by(tg_room=chat.id).first()
|
||||
@ -455,6 +490,8 @@ async def aiotg_sticker(chat, sticker):
|
||||
print('Unknown telegram chat {}: {}'.format(chat, chat.id))
|
||||
return
|
||||
|
||||
await update_matrix_displayname_avatar(chat.sender);
|
||||
|
||||
room_id = link.matrix_room
|
||||
user_id = USER_ID_FORMAT.format(chat.sender['id'])
|
||||
txn_id = quote('{}{}'.format(chat.message['message_id'], chat.id))
|
||||
@ -501,6 +538,7 @@ async def aiotg_photo(chat, photo):
|
||||
print('Unknown telegram chat {}: {}'.format(chat, chat.id))
|
||||
return
|
||||
|
||||
await update_matrix_displayname_avatar(chat.sender);
|
||||
room_id = link.matrix_room
|
||||
user_id = USER_ID_FORMAT.format(chat.sender['id'])
|
||||
txn_id = quote('{}{}'.format(chat.message['message_id'], chat.id))
|
||||
@ -555,6 +593,7 @@ async def aiotg_message(chat, match):
|
||||
print('Unknown telegram chat {}: {}'.format(chat, chat.id))
|
||||
return
|
||||
|
||||
await update_matrix_displayname_avatar(chat.sender);
|
||||
user_id = USER_ID_FORMAT.format(chat.sender['id'])
|
||||
txn_id = quote('{}:{}'.format(chat.message['message_id'], chat.id))
|
||||
|
||||
|
@ -32,9 +32,9 @@ class TgUser(Base):
|
||||
id = sa.Column(sa.Integer, primary_key=True)
|
||||
tg_id = sa.Column(sa.BigInteger)
|
||||
name = sa.Column(sa.String)
|
||||
profile_pic_id = sa.Column(sa.String)
|
||||
profile_pic_id = sa.Column(sa.String, nullable=True)
|
||||
|
||||
def __init__(self, tg_id, name, profile_pic_id):
|
||||
def __init__(self, tg_id, name, profile_pic_id=None):
|
||||
self.tg_id = tg_id
|
||||
self.name = name
|
||||
self.profile_pic_id = profile_pic_id
|
||||
|
Loading…
Reference in New Issue
Block a user