Exemple #1
0
 async def handle(self, context: RequestContext, responder: BaseResponder):
     """Handle status request message."""
     if not self.transport or self.transport.return_route != "all":
         raise HandlerException(
             "StatusRequest must have transport decorator with return "
             "route set to all")
     recipient_key = self.recipient_key
     manager = context.inject(InboundTransportManager)
     assert manager
     count = manager.undelivered_queue.message_count_for_key(
         recipient_key or context.message_receipt.sender_verkey)
     response = Status(message_count=count, recipient_key=recipient_key)
     response.assign_thread_from(self)
     await responder.send_reply(response)
    async def handle(self, context: RequestContext, responder: BaseResponder):
        """Handle MessageReceived message."""
        if not self.transport or self.transport.return_route != "all":
            raise HandlerException(
                "MessageReceived must have transport decorator with return "
                "route set to all")

        manager = context.inject(InboundTransportManager)
        assert manager
        queue = manager.undelivered_queue
        key = context.message_receipt.sender_verkey

        if queue.has_message_for_key(key):
            remove_message_by_tag_list(queue, key, self.message_id_list)

        response = Status(message_count=queue.message_count_for_key(key))
        response.assign_thread_from(self)
        await responder.send_reply(response)
    async def handle(self, context: RequestContext, responder: BaseResponder):
        """Handle DeliveryRequest message."""
        if not self.transport or self.transport.return_route != "all":
            raise HandlerException(
                "DeliveryRequest must have transport decorator with return "
                "route set to all")

        wire_format = context.inject(BaseWireFormat)
        manager = context.inject(InboundTransportManager)
        assert manager
        queue = manager.undelivered_queue
        key = context.message_receipt.sender_verkey
        message_attachments = []

        if queue.has_message_for_key(key):
            session = self.determine_session(manager, key)
            if session is None:
                LOGGER.warning(
                    "No session available to deliver messages as requested")
                return

            returned_count = 0
            async with context.session() as profile_session:

                for msg in get_messages_for_key(queue, key):

                    recipient_key = (msg.target_list[0].recipient_keys or
                                     context.message_receipt.recipient_verkey)
                    routing_keys = msg.target_list[0].routing_keys or []
                    sender_key = msg.target_list[0].sender_key or key

                    # Depending on send_outbound() implementation, there is a
                    # race condition with the timestamp. When ACA-Py is under
                    # load, there is a potential for this encryption to not
                    # match the actual encryption
                    # TODO: update ACA-Py to store all messages with an
                    # encrypted payload

                    msg.enc_payload = await wire_format.encode_message(
                        profile_session,
                        msg.payload,
                        recipient_key,
                        routing_keys,
                        sender_key,
                    )

                    attached_msg = Attach.data_base64(ident=json.loads(
                        msg.enc_payload)["tag"],
                                                      value=msg.enc_payload)
                    message_attachments.append(attached_msg)
                    returned_count += 1

                    if returned_count >= self.limit:
                        break

            response = Delivery(message_attachments=message_attachments)
            response.assign_thread_from(self)
            await responder.send_reply(response)

        else:
            response = Status(recipient_key=self.recipient_key,
                              message_count=0)
            response.assign_thread_from(self)
            await responder.send_reply(response)