Example #1
0
    async def message_processor(
        self,
        update: dict,
        update_code: int,
        update_fields: typing.List[int],
    ) -> None:
        """ Process message events. Use base fields to make a dataclass
        Params described in parent_processor
        """
        # Expanding data
        fields = ("message_id", "flags", *ADDITIONAL_FIELDS)
        data = dict(zip(fields, update_fields))
        middleware_args = []

        if self.expand_models:
            data.update(await self.expand_data(update_code, data))

        message = Message(**data)

        # Executing middleware
        async for mr in self.middleware.run_middleware(message,
                                                       MiddlewareFlags.PRE):
            if self.status.middleware_expressions:
                if mr is False:
                    return
                elif mr is not None:
                    middleware_args.append(mr)

        # Executing branch queue
        if message.dict()[
                self.branch.checkup_key.value] in await self.branch.queue:
            return await self.branch_processor(message, middleware_args)

        # Rule checkup
        for rules in self.handler.message_rules:
            rule = rules[0]  # API Complexity #FIXME
            check = await rule.check(update)

            if check is not None:
                args, kwargs = [], {}

                if isinstance(check, tuple):
                    check = await self.filter(message, check)
                    if not check:
                        continue
                    args, kwargs = check

                task = await rule.call(message, *args, **kwargs)
                await self.handler_return(task, data)
                break

        async for mr in self.middleware.run_middleware(message,
                                                       MiddlewareFlags.POST,
                                                       *middleware_args):
            logger.debug(f"POST Middleware handler returned: {mr}")
Example #2
0
    async def branch_processor(self, message: Message, middleware_args: list):
        """ Process messages for users in branch storage
        #TODO Make a simple branch extendable processor
        #TODO Move middleware_args
        """
        branch_checkup_key = message.dict()[self.branch.checkup_key.value]
        logger.debug(
            '-> BRANCHED MESSAGE FROM {} TEXT "{}"',
            branch_checkup_key,
            message.text.replace("\n", " "),
        )

        disposal, branch = await self.branch.load(branch_checkup_key)
        edited = None

        for n, member in disposal.items():
            rules = member[1]
            for rule in rules:
                if not await rule(message):
                    break
            else:
                task = types.MethodType(member[0], branch)
                args = [a for rule in rules for a in rule.context.args]
                kwargs = {
                    k: v
                    for rule in rules for k, v in rule.context.kwargs.items()
                }
                handler_return = await task(message, *middleware_args, *args,
                                            **kwargs)
                edited = await self.handler_return(handler_return,
                                                   message.dict())
                break

        # FIXME
        if edited is False and self.branch.generator is GeneratorType.DATABASE:
            if branch_checkup_key in await self.branch.queue:
                logger.debug("Updating branch context")
                await self.branch.add(branch_checkup_key, branch.key,
                                      **branch.context)

        logger.info(
            'New BRANCHED "{0}" compiled with branch <{2}> (from: {1})'.format(
                message.text.replace("\n", " "),
                branch_checkup_key,
                repr(branch.key),
            ))
Example #3
0
    async def _message_processor(
        self, update: dict, update_code: int, update_fields: list
    ):
        for rule in self.on.message_rules:
            check = await rule.check(update)

            if check is not None:
                fields, _ = rule.data["data"], rule.data["name"]
                data = dict(zip(fields, update_fields))
                args, kwargs = [], {}
                middleware_args = []

                if self._expand_models:
                    data.update(await self.expand_data(update_code, data))

                message = Message(**data)

                if isinstance(check, tuple):
                    if all([await s_rule.check(message) for s_rule in check]):
                        args = [a for rule in check for a in rule.context.args]
                        kwargs = {
                            k: v
                            for rule in check
                            for k, v in rule.context.kwargs.items()
                        }
                    else:
                        continue

                async for mr in self.middleware.run_middleware(message):
                    if mr is False:
                        return
                    elif mr is not None:
                        middleware_args.append(mr)

                if message.peer_id in await self.branch.queue:
                    await self._branched_processor(message, middleware_args)
                    return

                task = await rule.call(message, *args, **kwargs)
                await self._handler_return(task, data)
                return task