diff --git a/davtelepot/__init__.py b/davtelepot/__init__.py index 170e868..eb7f02a 100644 --- a/davtelepot/__init__.py +++ b/davtelepot/__init__.py @@ -11,7 +11,7 @@ __author__ = "Davide Testa" __email__ = "davide@davte.it" __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __license__ = "GNU General Public License v3.0" -__version__ = "2.6.17" +__version__ = "2.6.18" __maintainer__ = "Davide Testa" __contact__ = "t.me/davte" diff --git a/davtelepot/authorization.py b/davtelepot/authorization.py index 53ac7f4..a5fd204 100644 --- a/davtelepot/authorization.py +++ b/davtelepot/authorization.py @@ -315,6 +315,7 @@ def get_authorization_function(bot: Bot): async def _authorization_command(bot: Bot, update: dict, user_record: OrderedDict, + language: str, mode: str = 'auth'): db = bot.db text = get_cleaned_text(bot=bot, update=update, replace=[mode]) @@ -407,6 +408,17 @@ async def _authorization_command(bot: Bot, user_record=user_record, admin_record=admin_record ) + if bot.db['user_profile_photos'].find_one(user_id=user_record['id']): + buttons.append( + make_button( + text=bot.get_message('authorization', 'auth_button', + 'profile_picture_button', + language=language), + prefix='auth:///', + delimiter='|', + data=['picture', user_record['id']] + ) + ) reply_markup = make_inline_keyboard(buttons, 1) return dict( text=result, @@ -418,6 +430,7 @@ async def _authorization_command(bot: Bot, async def _authorization_button(bot: Bot, update: dict, user_record: OrderedDict, + language: str, data: Union[str, List[Union[int, str]]]): if len(data) == 0: data = [''] @@ -435,6 +448,17 @@ async def _authorization_button(bot: Bot, user_record=other_user_record, admin_record=user_record ) + if bot.db['user_profile_photos'].find_one(user_id=other_user_record['id']): + buttons.append( + make_button( + text=bot.get_message('authorization', 'auth_button', + 'profile_picture_button', + language=language), + prefix='auth:///', + delimiter='|', + data=['picture', user_record['id']] + ) + ) reply_markup = make_inline_keyboard(buttons, 1) elif command in ['set'] and len(arguments) > 1: other_user_id, new_privileges, *_ = arguments @@ -507,7 +531,41 @@ async def _authorization_button(bot: Bot, user_record=other_user_record, admin_record=user_record ) + if bot.db['user_profile_photos'].find_one(user_id=other_user_record['id']): + buttons.append( + make_button( + text=bot.get_message('authorization', 'auth_button', + 'profile_picture_button', + language=language), + prefix='auth:///', + delimiter='|', + data=['picture', user_record['id']] + ) + ) reply_markup = make_inline_keyboard(buttons, 1) + elif command in ['picture'] and len(arguments) > 0: + photo_record = bot.db['user_profile_photos'].find_one( + user_id=arguments[0], + order_by=['-update_datetime'], + ) + other_user_record = bot.db['users'].find_one(id=arguments[0]) + if photo_record is None: + result = bot.get_message('admin', 'error', 'text', + language=language) + else: + caption, buttons = bot.Role.get_user_role_text_and_buttons( + user_record=other_user_record, + admin_record=user_record + ) + await bot.sendPhoto( + chat_id=user_record['telegram_id'], + photo=photo_record['telegram_file_id'], + caption=caption, + reply_markup=make_inline_keyboard( + buttons=buttons + ), + parse_mode='HTML' + ) if text: return dict( text=result, @@ -570,18 +628,24 @@ def init(telegram_bot: Bot, authorization_messages['auth_command']['description'] ), authorization_level='moderator') - async def authorization_command(bot, update, user_record): - return await _authorization_command(bot, update, user_record) + async def authorization_command(bot, update, user_record, language): + return await _authorization_command(bot=bot, update=update, + user_record=user_record, + language=language) @telegram_bot.button('auth:///', description=authorization_messages['auth_button']['description'], separator='|', authorization_level='moderator') - async def authorization_button(bot, update, user_record, data): - return await _authorization_button(bot, update, user_record, data) + async def authorization_button(bot, update, user_record, language, data): + return await _authorization_button(bot=bot, update=update, + user_record=user_record, + language=language, data=data) @telegram_bot.command('/ban', aliases=[], show_in_keyboard=False, description=authorization_messages['ban_command']['description'], authorization_level='moderator') - async def ban_command(bot, update, user_record): - return await _authorization_command(bot, update, user_record, mode='ban') + async def ban_command(bot, update, user_record, language): + return await _authorization_command(bot=bot, update=update, + user_record=user_record, + language=language, mode='ban') diff --git a/davtelepot/bot.py b/davtelepot/bot.py index 28ca0bc..d71a282 100644 --- a/davtelepot/bot.py +++ b/davtelepot/bot.py @@ -3162,18 +3162,25 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): """ while 1: await asyncio.sleep(interval) + users_profile_pictures_to_update = OrderedDict() + now = datetime.datetime.now() # Iterate through a copy since asyncio.sleep(0) is awaited at each # cycle iteration. for telegram_id, user in self.recent_users.copy().items(): new_record = dict() with self.db as db: user_record = db['users'].find_one(telegram_id=telegram_id) - for key in [ - 'first_name', - 'last_name', - 'username', - 'language_code' - ]: + user_picture_record = db['user_profile_photos'].find_one( + user_id=user_record['id'], + order_by=['-update_datetime'] + ) + # If user profile picture needs to be updated, add it to the OD + if (user_picture_record is None + or user_picture_record['update_datetime'] + < now - datetime.timedelta(days=1)): + users_profile_pictures_to_update[telegram_id] = user_picture_record + for key in ('first_name', 'last_name', + 'username', 'language_code', ): new_record[key] = (user[key] if key in user else None) if ( ( @@ -3206,6 +3213,37 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject): if telegram_id in self.recent_users: del self.recent_users[telegram_id] await asyncio.sleep(0) + # Update user profile pictures + for telegram_id, user_picture_record in users_profile_pictures_to_update.items(): + try: + user_profile_photos = await self.getUserProfilePhotos( + user_id=telegram_id, + offset=0, + limit=1 + ) + if (user_profile_photos is not None + and 'photos' in user_profile_photos + and len(user_profile_photos['photos'])): + current_photo = user_profile_photos['photos'][0][0] + if (user_picture_record is None + or current_photo['file_id'] + != user_picture_record['telegram_file_id']): + db['user_profile_photos'].insert(dict( + user_id=user_record['id'], + telegram_file_id=current_photo['file_id'], + update_datetime=now + )) + else: + db['user_profile_photos'].upsert( + dict( + user_id=user_record['id'], + telegram_file_id=current_photo['file_id'], + update_datetime=now + ), + ['user_id', 'telegram_file_id'] + ) + except Exception as e: + logging.error(e) def get_user_record(self, update): """Get user_record of update sender. diff --git a/davtelepot/messages.py b/davtelepot/messages.py index 0df6c4d..8d75bf9 100644 --- a/davtelepot/messages.py +++ b/davtelepot/messages.py @@ -709,17 +709,25 @@ default_authorization_messages = { } }, 'auth_button': { - 'description': { - 'en': "Edit user permissions", - 'it': "Cambia il grado di autorizzazione di un utente" + 'appointed': { + 'en': "Permission granted", + 'it': "Permesso conferito" + }, + 'back_to_user': { + 'en': "Back to user", + 'it': "Torna all'utente" }, 'confirm': { 'en': "Are you sure?", 'it': "Sicuro sicuro?" }, - 'back_to_user': { - 'en': "Back to user", - 'it': "Torna all'utente" + 'description': { + 'en': "Edit user permissions", + 'it': "Cambia il grado di autorizzazione di un utente" + }, + 'no_change': { + 'en': "No change suggested!", + 'it': "È già così!" }, 'permission_denied': { 'user': { @@ -732,14 +740,10 @@ default_authorization_messages = { 'it': "Non hai l'autorità di conferire questo permesso!" } }, - 'no_change': { - 'en': "No change suggested!", - 'it': "È già così!" + 'profile_picture_button': { + 'en': "🖼 Profile picture", + 'it': "🖼 Foto profilo", }, - 'appointed': { - 'en': "Permission granted", - 'it': "Permesso conferito" - } }, }