def resolve(self, value_or_update) -> custom_dataclasses.User:
     if type(value_or_update) == str or type(value_or_update) == int:
         try:
             return custom_dataclasses.User(self._db_man,
                                            int(value_or_update),
                                            resolver=self)
         except ValueError:
             self._init_thread_event_loop()
             thread_id = threading.get_ident()
             if self._threads_data[thread_id]['client']:
                 with self._threads_data[thread_id]['client'] as tg_client:
                     try:
                         peer_id = tg_client.get_peer_id(value_or_update)
                         return custom_dataclasses.User(self._db_man,
                                                        peer_id,
                                                        resolver=self)
                     except ValueError:
                         raise UserResolverError('Invalid username/user_id')
             else:
                 raise UserResolverError('Username resolution is not '
                                         'configured')
     elif type(value_or_update) == Update:
         return custom_dataclasses.User(self._db_man,
                                        value_or_update.message.from_user)
     elif type(value_or_update) == Message:
         return custom_dataclasses.User(self._db_man,
                                        value_or_update.from_user)
     else:
         raise ValueError(f'{value_or_update}\'s type is wrong')
Ejemplo n.º 2
0
 def get_message_sender(self,
                        message: Message) -> 'custom_dataclasses.User':
     if message.forward_from:
         return custom_dataclasses.User(self, message.forward_from)
     else:
         row = self._execute_get_query_for_1_row(
             queries.GET_MESSAGE_SENDER, {
                 'receiver_id': int(message.chat.id),
                 'receiver_message_id': int(message.message_id)
             })
         return custom_dataclasses.User(self, row['sender_id'])
Ejemplo n.º 3
0
    def get_users_by_role(self, role_name: str) ->\
            Iterable:
        with self._get_connection() as conn:
            cursor = conn.execute(queries.GET_USERS_BY_ROLE,
                                  {'role_name': role_name})

            return map(lambda x: custom_dataclasses.User(self, x['user_id']),
                       cursor)
Ejemplo n.º 4
0
 def get_active_users(self):
     '''
     @returns An iterable of User instances
     '''
     logger.debug('Getting active users')
     cursor = self._execute_simple_get_query(queries.GET_ACTIVE_USERS)
     return map(lambda x: custom_dataclasses.User(self, x['user_id']),
                cursor)
Ejemplo n.º 5
0
    def get_user(self, user_id):
        '''
        @returns An User object
        '''
        logging.debug(f'Getting user {user_id}')

        row = self._execute_get_query_for_1_row(
            queries.GET_USER, {'user_id': user_id},
            f'User id: {user_id} is not present in the users database')

        return custom_dataclasses.User(self, user_id,
                                       self.get_user_permissions(user_id))