Add Diaspora relayable sign as parent

merge-requests/130/head
Jason Robinson 2017-05-01 21:58:00 +03:00
rodzic 7ccc070711
commit 999c27cd5c
3 zmienionych plików z 31 dodań i 20 usunięć

Wyświetl plik

@ -32,6 +32,8 @@ class DiasporaEntityMixin(BaseEntity):
class DiasporaRelayableMixin(DiasporaEntityMixin):
parent_signature = ""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._required += ["signature"]
@ -46,17 +48,21 @@ class DiasporaRelayableMixin(DiasporaEntityMixin):
def sign(self, private_key):
self.signature = create_relayable_signature(private_key, self.to_xml())
def sign_with_parent(self, private_key):
self.parent_signature = create_relayable_signature(private_key, self.to_xml())
class DiasporaComment(DiasporaRelayableMixin, Comment):
"""Diaspora comment."""
def to_xml(self):
element = etree.Element("comment")
struct_to_xml(element, [
{'guid': self.guid},
{'parent_guid': self.target_guid},
{'author_signature': self.signature},
{'text': self.raw_content},
{'diaspora_handle': self.handle},
{"guid": self.guid},
{"parent_guid": self.target_guid},
{"author_signature": self.signature},
{"parent_author_signature": self.parent_signature},
{"text": self.raw_content},
{"diaspora_handle": self.handle},
])
return element
@ -67,12 +73,12 @@ class DiasporaPost(DiasporaEntityMixin, Post):
"""Convert to XML message."""
element = etree.Element("status_message")
struct_to_xml(element, [
{'raw_message': self.raw_content},
{'guid': self.guid},
{'diaspora_handle': self.handle},
{'public': 'true' if self.public else 'false'},
{'created_at': format_dt(self.created_at)},
{'provider_display_name': self.provider_display_name},
{"raw_message": self.raw_content},
{"guid": self.guid},
{"diaspora_handle": self.handle},
{"public": "true" if self.public else "false"},
{"created_at": format_dt(self.created_at)},
{"provider_display_name": self.provider_display_name},
])
return element
@ -86,11 +92,12 @@ class DiasporaLike(DiasporaRelayableMixin, Reaction):
element = etree.Element("like")
struct_to_xml(element, [
{"target_type": "Post"},
{'guid': self.guid},
{'parent_guid': self.target_guid},
{'author_signature': self.signature},
{"guid": self.guid},
{"parent_guid": self.target_guid},
{"author_signature": self.signature},
{"parent_author_signature": self.parent_signature},
{"positive": "true"},
{'diaspora_handle': self.handle},
{"diaspora_handle": self.handle},
])
return element

Wyświetl plik

@ -206,6 +206,8 @@ def get_outbound_entity(entity, private_key):
outbound = DiasporaRetraction.from_base(entity)
if not outbound:
raise ValueError("Don't know how to convert this base entity to Diaspora protocol entities.")
if issubclass(cls, DiasporaRelayableMixin):
if issubclass(cls, DiasporaRelayableMixin) and not outbound.signature:
# Sign by author if not signed yet. We don't want to overwrite any existing signature in the case
# that this is being sent by the parent author
outbound.sign(private_key)
return outbound

Wyświetl plik

@ -34,8 +34,9 @@ class TestEntitiesConvertToXML():
result = entity.to_xml()
assert result.tag == "comment"
converted = b"<comment><guid>guid</guid><parent_guid>target_guid</parent_guid>" \
b"<author_signature>signature</author_signature><text>raw_content</text>" \
b"<diaspora_handle>handle</diaspora_handle></comment>"
b"<author_signature>signature</author_signature><parent_author_signature>" \
b"</parent_author_signature><text>raw_content</text><diaspora_handle>handle</diaspora_handle>" \
b"</comment>"
assert etree.tostring(result) == converted
def test_like_to_xml(self):
@ -43,8 +44,9 @@ class TestEntitiesConvertToXML():
result = entity.to_xml()
assert result.tag == "like"
converted = b"<like><target_type>Post</target_type><guid>guid</guid><parent_guid>target_guid</parent_guid>" \
b"<author_signature>signature</author_signature><positive>true</positive>" \
b"<diaspora_handle>handle</diaspora_handle></like>"
b"<author_signature>signature</author_signature><parent_author_signature>" \
b"</parent_author_signature><positive>true</positive><diaspora_handle>handle</diaspora_handle>" \
b"</like>"
assert etree.tostring(result) == converted
def test_request_to_xml(self):