Пример #1
0
def content_fetch_view(request, objtype, guid):
    """Diaspora content fetch view.

    Returns the signed payload for the public content. Non-public content will return 404.

    If the content is not local, redirect to content author server.

    Args:
        objtype (str) - Diaspora content type. Currently if it is `status_message`, `post` or `reshare`,
            we try to find `Content`.
        guid (str) - The object guid to look for.
    """
    if objtype not in ["status_message", "post", "reshare", "comment"]:
        raise Http404()
    content = get_object_or_404(Content,
                                guid=guid,
                                visibility=Visibility.PUBLIC)
    if not content.local:
        url = "https://%s/fetch/%s/%s" % (content.author.handle.split("@")[1],
                                          objtype, guid)
        return HttpResponseRedirect(url)
    entity = make_federable_content(content)
    message = get_full_xml_representation(entity, content.author.private_key)
    document = MagicEnvelope(message=message,
                             private_key=content.author.private_key,
                             author_handle=content.author.handle)
    return HttpResponse(document.render(),
                        content_type="application/magic-envelope+xml")
Пример #2
0
def send_content(content_id):
    """Handle sending a Content object out via the federation layer.

    Currently we only deliver public content.
    """
    try:
        content = Content.objects.get(id=content_id,
                                      visibility=Visibility.PUBLIC,
                                      content_type=ContentType.CONTENT,
                                      local=True)
    except Content.DoesNotExist:
        logger.warning("No local content found with id %s", content_id)
        return
    entity = make_federable_content(content)
    if entity:
        if settings.DEBUG:
            # Don't send in development mode
            return
        recipients = [
            (settings.SOCIALHOME_RELAY_DOMAIN, "diaspora"),
        ]
        recipients.extend(_get_remote_followers(content.author))
        handle_send(entity, content.author, recipients)
    else:
        logger.warning("send_content - No entity for %s", content)
Пример #3
0
def send_share(content_id):
    """Handle sending a share of a Content object to the federation layer.

    Currently we only deliver public shares.
    """
    try:
        content = Content.objects.get(id=content_id,
                                      visibility=Visibility.PUBLIC,
                                      content_type=ContentType.SHARE,
                                      local=True)
    except Content.DoesNotExist:
        logger.warning("No local share found with id %s", content_id)
        return
    entity = make_federable_content(content)
    if entity:
        if settings.DEBUG:
            # Don't send in development mode
            return
        recipients = _get_remote_followers(content.author)
        if not content.share_of.local:
            # Send to original author
            recipients.append((content.share_of.author.handle, None))
        handle_send(entity, content.author, recipients)
    else:
        logger.warning("send_share - No entity for %s", content)
Пример #4
0
def send_reply(content_id):
    """Handle sending a Content object that is a reply out via the federation layer.

    Currently we only deliver public content.
    """
    try:
        content = Content.objects.get(id=content_id,
                                      visibility=Visibility.PUBLIC,
                                      content_type=ContentType.REPLY,
                                      local=True)
    except Content.DoesNotExist:
        logger.warning("No content found with id %s", content_id)
        return
    entity = make_federable_content(content)
    if not entity:
        logger.warning("send_reply - No entity for %s", content)
    if settings.DEBUG:
        # Don't send in development mode
        return
    # Send directly (remote parent) or as a relayable (local parent)
    if content.parent.local:
        forward_entity(entity, content.parent.id)
    else:
        # We only need to send to the original author
        recipients = [
            (content.parent.author.handle, None),
        ]
        handle_send(entity, content.author, recipients)
Пример #5
0
 def test_reply_returns_entity(self):
     entity = make_federable_content(self.reply)
     self.assertTrue(isinstance(entity, base.Comment))
     self.assertEqual(entity.raw_content, self.reply.text)
     self.assertEqual(entity.guid, self.reply.guid)
     self.assertEqual(entity.target_guid, self.reply.parent.guid)
     self.assertEqual(entity.handle, self.reply.author.handle)
     self.assertEqual(entity.created_at, self.reply.effective_modified)
Пример #6
0
def content_xml_view(request, guid):
    """Diaspora single post view XML representation.

    Fetched by remote servers in certain situations.
    """
    content = get_object_or_404(Content,
                                guid=guid,
                                visibility=Visibility.PUBLIC,
                                local=True)
    entity = make_federable_content(content)
    xml = get_full_xml_representation(entity, content.author.private_key)
    return HttpResponse(xml, content_type="application/xml")
Пример #7
0
    def test_content_returns_entity(self):
        entity = make_federable_content(self.content)
        self.assertTrue(isinstance(entity, base.Post))
        self.assertEqual(entity.raw_content, self.content.text)
        self.assertEqual(entity.guid, self.content.guid)
        self.assertEqual(entity.handle, self.content.author.handle)
        self.assertEqual(entity.public, True)
        self.assertEqual(entity.provider_display_name, "Socialhome")
        self.assertEqual(entity.created_at, self.content.effective_modified)

        self.content.visibility = Visibility.LIMITED
        entity = make_federable_content(self.content)
        self.assertEqual(entity.public, False)

        self.content.visibility = Visibility.SELF
        entity = make_federable_content(self.content)
        self.assertEqual(entity.public, False)

        self.content.visibility = Visibility.SITE
        entity = make_federable_content(self.content)
        self.assertEqual(entity.public, False)
Пример #8
0
def send_content(content_id, recipient_id=None):
    """
    Handle sending a Content object out via the federation layer.
    """
    try:
        content = Content.objects.get(
            id=content_id,
            visibility__in=(Visibility.PUBLIC, Visibility.LIMITED),
            content_type=ContentType.CONTENT,
            local=True,
        )
    except Content.DoesNotExist:
        logger.warning("No local content found with id %s", content_id)
        return
    if recipient_id:
        try:
            recipient = Profile.objects.get(id=recipient_id, user__isnull=True)
        except Profile.DoesNotExist:
            logger.warning("No remote recipient found with id %s", recipient_id)
            return
    else:
        recipient = None
    entity = make_federable_content(content)
    if entity:
        if settings.DEBUG:
            # Don't send in development mode
            return
        if recipient:
            recipients = [
                (generate_diaspora_profile_id(recipient.handle, recipient.guid), recipient.key),
            ]
        else:
            recipients = [settings.SOCIALHOME_RELAY_ID]
            recipients.extend(_get_remote_followers(content.author))

        logger.debug("send_content - sending to recipients: %s", recipients)
        handle_send(entity, content.author, recipients)
    else:
        logger.warning("send_content - No entity for %s", content)
Пример #9
0
def send_reply(content_id):
    """
    Handle sending a Content object that is a reply out via the federation layer.
    """
    try:
        content = Content.objects.get(
            id=content_id,
            visibility__in=(Visibility.PUBLIC, Visibility.LIMITED),
            content_type=ContentType.REPLY,
            local=True,
        )
    except Content.DoesNotExist:
        logger.warning("No content found with id %s", content_id)
        return
    entity = make_federable_content(content)
    if not entity:
        logger.warning("send_reply - No entity for %s", content)
    if settings.DEBUG:
        # Don't send in development mode
        return
    # Send directly (remote parent) or as a relayable (local parent)
    if content.parent.local:
        forward_entity(entity, content.parent.id)
    else:
        # We only need to send to the original author
        parent_author = content.parent.author
        if content.visibility == Visibility.PUBLIC:
            recipients = [
                generate_diaspora_profile_id(parent_author.handle, parent_author.guid),
            ]
        else:
            recipients = [
                (generate_diaspora_profile_id(parent_author.handle, parent_author.guid), parent_author.key),
            ]
        logger.debug("send_reply - sending to recipients: %s", recipients)
        handle_send(entity, content.author, recipients)
Пример #10
0
 def test_returns_none_on_exception(self, mock_post):
     mock_post = Mock()
     mock_post.parent = False
     entity = make_federable_content(mock_post)
     self.assertIsNone(entity)