コード例 #1
0
    def message_list(self, sender: ServalIdentity,
                     recipient: ServalIdentity) -> List[Message]:
        """Gets all the messages sent between two identities

        Args:
            sender (ServalIdentity)
            recipient (ServalIdentity)

        Note:
            At least one of the identities needs to be local and unlocked

        Returns:
            List[Message]: List of all the messages sent between the two identities
        """
        assert isinstance(sender, ServalIdentity)
        assert isinstance(recipient, ServalIdentity)

        # TODO: Is this one- or two-way?
        result = self._low_level.message_list(sender=sender.sid,
                                              recipient=recipient.sid)

        # I would like to make a better distinction here, but unfortunately the upstream docs
        # do not specify any status codes for specific errors
        if result.status_code != 200:
            raise RhizomeHTTPStatusError(result)

        result_json = result.json()
        messages = unmarshall(json_table=result_json, object_class=Message)
        return messages
コード例 #2
0
    def conversation_list(
            self, identity: Union[ServalIdentity, str]) -> List[Conversation]:
        """Gets the list of all conversations for a given Identity

        Args:
            identity (Union[ServalIdentity, str])

        Returns:
            List[Conversation]: List of all the conversations
                                that the specified identity is taking part in
        """
        if isinstance(identity, ServalIdentity):
            identity = identity.sid

        assert isinstance(
            identity,
            str), "identity has to be either a string or ServalIdentity"

        result = self._low_level.conversation_list(identity)

        # I would like to make a better distinction here, but unfortunately the upstream docs
        # do not specify any status codes for specific errors
        if result.status_code != 200:
            raise RhizomeHTTPStatusError(result)

        conversations = unmarshall(json_table=result.json(),
                                   object_class=Conversation)
        for conversation in conversations:
            conversation._meshms = self
        return conversations
コード例 #3
0
    def get_feedlist(self, identity: Union[ServalIdentity, str]) -> List[Feed]:
        """Get a list of all followed identities

        Args:
            identity (Union[ServalIdentity, str]): Keyring identity or corresponding Signing-ID
                                                   NOTE: This is NOT the same as the SID

        Returns:
            List[Feed]: List of all feeds that the specified identity is currently following
        """
        if isinstance(identity, ServalIdentity):
            identity = identity.identity

        assert isinstance(
            identity,
            str), "identity must be either a ServalIdentity or Identity-string"

        result = self._low_level_meshmb.get_feedlist(identity=identity)

        # I would like to make a better distinction here, but unfortunately the upstream docs
        # do not specify any status codes for specific errors
        if result.status_code != 200:
            raise RhizomeHTTPStatusError(result)

        result_json = result.json()
        feeds = unmarshall(json_table=result_json,
                           object_class=Feed,
                           meshmb=self)
        return feeds
コード例 #4
0
    def get_messages(
            self, feedid: Union[ServalIdentity,
                                str]) -> List[BroadcastMessage]:
        """Get all the messages of a feed

        Args:
            feedid (Union[ServalIdentity, str]): Keyring identity or corresponding Signing-ID
                                                 NOTE: This is NOT the same as the SID

        Returns:
            List[BroadcastMessage]: All the messages sent to this feed
        """
        if isinstance(feedid, ServalIdentity):
            feedid = feedid.identity

        assert isinstance(
            feedid,
            str), "feedid must be either a ServalIdentity or Identity-string"

        result = self._low_level_meshmb.get_messages(feedid=feedid)

        # I would like to make a better distinction here, but unfortunately the upstream docs
        # do not specify any status codes for specific errors
        if result.status_code != 200:
            raise RhizomeHTTPStatusError(result)

        result_json = result.json()
        messages = unmarshall(json_table=result_json,
                              object_class=BroadcastMessage)
        return messages
コード例 #5
0
ファイル: route.py プロジェクト: umr-ds/pyserval
    def get_all(self) -> List[Peer]:
        """Get all known peers

        Returns:
            List[Peer]: List of peer-object containing metadata of all known peers
        """
        serval_response = self._route.get_all()
        response_json = serval_response.json()

        peers = unmarshall(json_table=response_json, object_class=Peer, _route=self)

        return peers
コード例 #6
0
    def message_list_newsince(self, sender: ServalIdentity,
                              recipient: ServalIdentity,
                              token: str) -> List[Message]:
        """Gets all the messages sent between two identities since the token was generated

        Args:
            sender (ServalIdentity)
            recipient (ServalIdentity)
            token (str)

        Note:
            At least one of the identities needs to be local and unlocked

        Returns:
            List[Message]: List of all the messages sent between the two identities
        """
        assert isinstance(sender, ServalIdentity)
        assert isinstance(recipient, ServalIdentity)
        assert isinstance(token, str)

        with self._low_level.message_list_newsince(
                sender=sender.sid, recipient=recipient.sid,
                token=token) as serval_stream:

            if serval_stream.status_code == 404:
                raise InvalidTokenError(token, serval_stream.reason)
            if serval_stream.status_code != 200:
                raise RhizomeHTTPStatusError(serval_stream)

            serval_reply_bytes = []
            lines = 0

            for c in serval_stream.iter_content():
                if c == b"\n":
                    lines += 1

                serval_reply_bytes.append(c)

                if (c == b"]" and lines == MESSAGELIST_HEADER_NEWLINES
                        and len(serval_reply_bytes) > MESSAGELIST_HEADER_SIZE):
                    # complete json manually
                    serval_reply_bytes += [b"\n", b"]", b"\n", b"}"]
                    break

            serval_reply = b"".join(serval_reply_bytes)
            reply_json = json.loads(serval_reply)

            messages = unmarshall(json_table=reply_json, object_class=Message)
            return messages
コード例 #7
0
    def get_identities(self, pin: str = "") -> List[ServalIdentity]:
        """List of all currently unlocked identities

        Args:
            pin (str): Passphrase to unlock identity prior to lookup

        Returns:
            List[ServalIdentity]: All currently unlocked identities
        """
        serval_response = self.low_level_keyring.get_identities(pin=pin)
        response_json = serval_response.json()

        identities = unmarshall(json_table=response_json,
                                object_class=ServalIdentity,
                                _keyring=self)

        return identities