Пример #1
0
async def host_data(message: Message, user_data: Tuple[Campus, Peer, User]):
    await dp.current_state(user=message.from_user.id
                           ).set_state(States.THROTTLER)
    *_, user = user_data
    host = message.text[1:].lower().strip()
    message = await message.answer(Config.local.wait.get(user.language))
    await message.bot.send_chat_action(user.id, 'typing')
    text, peer = await text_compile.host_data_compile(user=user, host=host)
    keyboard = None
    if peer:
        friends = await UserPeer.get_friends(user_id=user.id)
        observables = await UserPeer.get_observables(user_id=user.id)
        keyboard = peer_keyboard(peers=[peer],
                                 friends=friends,
                                 observables=observables,
                                 payload='pagination')
        keyboard = pagination_keyboard(action='host_pagination',
                                       count=1,
                                       content=host,
                                       limit=0,
                                       stop=3,
                                       keyboard=keyboard)
    await dp.current_state(user=user.id).set_state(States.GRANTED)
    with suppress(MessageToDeleteNotFound, MessageCantBeDeleted):
        await message.delete()
    await bot.send_message(user.id,
                           text,
                           reply_markup=keyboard,
                           disable_web_page_preview=not user.show_avatar)
Пример #2
0
async def friends_data(message: Message, user_data: Tuple[Campus, Peer, User]):
    await dp.current_state(user=message.from_user.id).set_state(States.THROTTLER)
    *_, user = user_data
    message = await message.answer(Config.local.wait.get(user.language))
    texts = ['']
    friends = await UserPeer.get_friends(user_id=user.id)
    friends_count = await UserPeer.get_friends_count(user_id=user.id)
    observables = await UserPeer.get_observables(user_id=user.id)
    for i, friend in enumerate(friends[:10], 1):
        await message.bot.send_chat_action(user.id, 'typing')
        texts[0] = Config.local.friends_list.get(user.language, from_=1, to=i, friends_count=friends_count)
        peer, text = await text_compile.peer_data_compile(user=user, login=friend.login, is_single=False)
        texts.append(text)
        try:
            with suppress(MessageNotModified, MessageToEditNotFound):
                await message.edit_text('\n\n'.join(texts), disable_web_page_preview=True)
        except RetryAfter as e:
            await message.answer(str(e))
            await asyncio.sleep(e.timeout)
    text = '\n\n'.join(texts)
    await Cache().set(key=f'Friends:{user.id}:1', value=texts)
    keyboard = peer_keyboard(peers=friends[:10], friends=friends[:10], observables=observables,
                             payload='' if friends_count < 11 else 'pagination')
    if friends_count > 10:
        keyboard = pagination_keyboard(action='friends_pagination', count=friends_count, content=1,
                                       limit=10, stop=3, keyboard=keyboard)
    with suppress(MessageToDeleteNotFound, MessageCantBeDeleted):
        await message.delete()
    await dp.current_state(user=user.id).set_state(States.GRANTED)
    await bot.send_message(user.id, text, reply_markup=keyboard,
                           disable_web_page_preview=not (friends_count == 1 and user.show_avatar))
async def host_pagination(callback_query: CallbackQuery,
                          user_data: Tuple[Campus, Peer, User]):
    await dp.current_state(user=callback_query.from_user.id
                           ).set_state(States.THROTTLER)
    *_, user = user_data
    host, page = callback_query.data.split('.')[1:]
    page = int(page)
    await callback_query.answer(text=Config.local.wait.get(user.language))
    text, peer = await text_compile.host_data_compile(user=user,
                                                      host=host,
                                                      page=page)
    keyboard = None
    if peer:
        friends = await UserPeer.get_friends(user_id=user.id)
        observables = await UserPeer.get_observables(user_id=user.id)
        keyboard = peer_keyboard(peers=[peer],
                                 friends=friends,
                                 observables=observables,
                                 payload='pagination')
    keyboard = pagination_keyboard(action='host_pagination',
                                   count=1,
                                   content=host,
                                   limit=0,
                                   stop=3,
                                   page=page,
                                   keyboard=keyboard)
    await dp.current_state(user=user.id).set_state(States.GRANTED)
    with suppress(MessageNotModified, MessageToEditNotFound):
        await callback_query.message.edit_text(
            text,
            reply_markup=keyboard,
            disable_web_page_preview=not user.show_avatar)
async def back_to_peer(callback_query: CallbackQuery,
                       user_data: Tuple[Campus, Peer, User]):
    *_, user = user_data
    await dp.current_state(user=user.id).set_state(States.THROTTLER)
    login = callback_query.data.split('.')[-1]
    with suppress(MessageNotModified, MessageToEditNotFound):
        message = await callback_query.message.edit_text(
            Config.local.wait.get(user.language))
    await message.bot.send_chat_action(user.id, 'typing')
    peer, text = await text_compile.peer_data_compile(user=user,
                                                      login=login,
                                                      is_single=True)
    friends = await UserPeer.get_friends(user_id=user.id)
    observables = await UserPeer.get_observables(user_id=user.id)
    keyboard = peer_keyboard(peers=[peer],
                             friends=friends,
                             observables=observables,
                             payload='alone_peer')
    keyboard = alone_peer_keyboard(user=user,
                                   login=peer.login,
                                   keyboard=keyboard)
    await dp.current_state(user=user.id).set_state(States.GRANTED)
    with suppress(MessageToDeleteNotFound, MessageCantBeDeleted):
        await message.delete()
    await bot.send_message(user.id,
                           text,
                           reply_markup=keyboard,
                           disable_web_page_preview=not user.show_avatar)
Пример #5
0
async def peers_data(message: Message, user_data: Tuple[Campus, Peer, User]):
    await dp.current_state(user=message.from_user.id
                           ).set_state(States.THROTTLER)
    *_, user = user_data
    peer_logins, bad_logins = await text_compile.logins_separation(
        message_text=message.text)
    message = await message.answer(Config.local.wait.get(user.language))
    texts = []
    peers = []
    for login in peer_logins:
        await message.bot.send_chat_action(user.id, 'typing')
        peer, text = await text_compile.peer_data_compile(user=user,
                                                          login=login,
                                                          is_single=False)
        if peer:
            peers.append(peer)
        texts.append(text)
        with suppress(MessageNotModified, MessageToEditNotFound):
            await message.edit_text('\n\n'.join(texts),
                                    disable_web_page_preview=True)
    for login in bad_logins:
        text = Config.local.not_found.get(user.language,
                                          login=login.replace("<", "&lt"))
        texts.append(text)
    friends = await UserPeer.get_friends(user_id=user.id)
    observables = await UserPeer.get_observables(user_id=user.id)
    if len(peers) == 1:
        keyboard = peer_keyboard(peers=peers,
                                 friends=friends,
                                 observables=observables,
                                 payload='alone_peer')
        keyboard = alone_peer_keyboard(user=user,
                                       login=peers[0].login,
                                       keyboard=keyboard)
    else:
        keyboard = peer_keyboard(peers=peers,
                                 friends=friends,
                                 observables=observables)
    await dp.current_state(user=user.id).set_state(States.GRANTED)
    with suppress(MessageToDeleteNotFound, MessageCantBeDeleted):
        await message.delete()
    await bot.send_message(user.id,
                           '\n\n'.join(texts),
                           reply_markup=keyboard,
                           disable_web_page_preview=len(peers) > 1
                           or not user.show_avatar)
async def friends_list(callback_query: CallbackQuery,
                       user_data: Tuple[Campus, Peer, User]):
    await dp.current_state(user=callback_query.from_user.id
                           ).set_state(States.THROTTLER)
    *_, user = user_data
    peer_id, login = callback_query.data.split('.')[1:-1]
    alert_text = Config.local.remove_friend.get(user.language, login=login)
    await callback_query.answer(alert_text, show_alert=True)
    await UserPeer.remove_friend(user_id=user.id, peer_id=int(peer_id))
    friends_count = await UserPeer.get_friends_count(user_id=user.id)
    friends = await UserPeer.get_friends(user_id=user.id)
    observables = await UserPeer.get_observables(user_id=user.id)
    current_page = 1
    raws = [
        button.callback_data
        for row in callback_query.message.reply_markup.inline_keyboard
        for button in row
    ][-1].split('.')
    if friends_count + 1 > 10 and raws[0] == 'friends_pagination':
        current_page = int(raws[1])
    await callback_query.message.bot.send_chat_action(user.id, 'typing')
    text, page = await text_compile.friends_list_normalization(
        user=user,
        current_page=current_page,
        removable=login,
        friends=friends,
        friends_count=friends_count)
    count = len(friends[(page - 1) * 10:])
    friends = paginate(friends, page=page - 1, limit=10)
    payload = ''
    if friends_count > 11:
        payload = 'pagination'
    if friends_count == 1:
        payload = 'alone_peer'
    keyboard = peer_keyboard(peers=friends,
                             friends=friends,
                             observables=observables,
                             payload=payload)
    if friends_count + 1 > 10:
        keyboard = pagination_keyboard(action='friends_pagination',
                                       count=count,
                                       content=current_page,
                                       limit=10,
                                       stop=3,
                                       page=page - 1,
                                       keyboard=keyboard)
    if friends_count == 1:
        keyboard = alone_peer_keyboard(user=user,
                                       login=friends[0].login,
                                       keyboard=keyboard)
    with suppress(MessageToDeleteNotFound, MessageCantBeDeleted):
        await callback_query.message.delete()
    await dp.current_state(user=user.id).set_state(States.GRANTED)
    await bot.send_message(
        user.id,
        text,
        reply_markup=keyboard,
        disable_web_page_preview=not (friends_count == 1 and user.show_avatar))
Пример #7
0
async def friend_data(message: Message, user_data: Tuple[Campus, Peer, User]):
    await dp.current_state(user=message.from_user.id).set_state(States.THROTTLER)
    *_, user = user_data
    keyboard = None
    friends = await UserPeer.get_friends(user_id=user.id)
    if not friends:
        text = Config.local.no_friends.get(user.language)
    else:
        observables = await UserPeer.get_observables(user_id=user.id)
        peer, text = await text_compile.peer_data_compile(user=user, login=friends[0].login, is_single=True)
        keyboard = peer_keyboard(peers=[peer], friends=friends, observables=observables, payload='alone_peer')
        keyboard = alone_peer_keyboard(user=user, login=peer.login, keyboard=keyboard)
        text = '\n\n'.join((Config.local.friends_list.get(user.language, from_=1, to=1, friends_count=1), text))
    await dp.current_state(user=user.id).set_state(States.GRANTED)
    await message.answer(text, reply_markup=keyboard, disable_web_page_preview=not user.show_avatar)
Пример #8
0
async def peer_data(message: Message, user_data: Tuple[Campus, Peer, User]):
    await dp.current_state(user=message.from_user.id
                           ).set_state(States.THROTTLER)
    *_, user = user_data
    await message.bot.send_chat_action(user.id, 'typing')
    login = message.text.lower().strip()
    keyboard = None
    peer, text = await text_compile.peer_data_compile(user=user,
                                                      login=login,
                                                      is_single=True)
    if peer:
        friends = await UserPeer.get_friends(user_id=user.id)
        observables = await UserPeer.get_observables(user_id=user.id)
        keyboard = peer_keyboard(peers=[peer],
                                 friends=friends,
                                 observables=observables,
                                 payload='alone_peer')
        keyboard = alone_peer_keyboard(user=user,
                                       login=peer.login,
                                       keyboard=keyboard)
    await dp.current_state(user=user.id).set_state(States.GRANTED)
    await message.answer(text,
                         reply_markup=keyboard,
                         disable_web_page_preview=not user.show_avatar)