Esempio n. 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")
Esempio n. 2
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(
                # TODO fid or handle?
                content.share_of.author.handle, )
        logger.debug("send_share - sending to recipients: %s", recipients)
        handle_send(entity, content.author.federable, recipients)
    else:
        logger.warning("send_share - No entity for %s", content)
Esempio n. 3
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.id, self.reply.fid)
     self.assertEqual(entity.target_id, self.reply.parent.fid)
     self.assertEqual(entity.actor_id, self.reply.author.fid)
     self.assertEqual(entity.created_at, self.reply.effective_modified)
Esempio n. 4
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.id, self.reply.fid)
     self.assertEqual(entity.target_id, self.reply.parent.fid)
     self.assertEqual(entity.actor_id, self.reply.author.fid)
     self.assertEqual(entity.created_at, self.reply.effective_modified)
Esempio n. 5
0
def content_xml_view(request, uuid):
    """Diaspora single post view XML representation.

    Fetched by remote servers in certain situations.
    """
    content = get_object_or_404(Content, uuid=uuid, 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")
Esempio n. 6
0
def send_content(content_id, activity_fid, 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:
        entity.activity_id = activity_fid
        if settings.DEBUG:
            # Don't send in development mode
            return
        recipients = []
        if recipient:
            recipients.append(
                recipient.get_recipient_for_visibility(content.visibility))
        else:
            if content.visibility == Visibility.PUBLIC:
                # If we have Matrix support enabled, also add the appservice
                if settings.SOCIALHOME_MATRIX_ENABLED:
                    recipients.append(
                        content.author.get_recipient_for_matrix_appservice())
                if settings.SOCIALHOME_RELAY_ID:
                    recipients.append({
                        "endpoint": settings.SOCIALHOME_RELAY_ID,
                        "fid": "",
                        "public": True,
                        "protocol": "diaspora"
                    })
            recipients.extend(
                _get_remote_followers(content.author, content.visibility))

        logger.debug("send_content - sending to recipients: %s", recipients)
        handle_send(entity,
                    content.author.federable,
                    recipients,
                    payload_logger=get_outbound_payload_logger())
    else:
        logger.warning("send_content - No entity for %s", content)
Esempio n. 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.id, self.content.fid)
        self.assertEqual(entity.actor_id, self.content.author.fid)
        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)
Esempio n. 8
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.id, self.content.fid)
        self.assertEqual(entity.actor_id, self.content.author.fid)
        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)
Esempio n. 9
0
def send_reply(content_id, activity_fid):
    """
    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)
    entity.activity_id = activity_fid
    if settings.DEBUG:
        # Don't send in development mode
        return
    recipients = []
    if not content.root_parent.author.is_local:
        recipients.append(
            content.root_parent.author.get_recipient_for_visibility(
                content.visibility))
    if content.visibility == Visibility.PUBLIC:
        recipients.extend(
            _get_remote_participants_for_content(content,
                                                 exclude=content.author.fid,
                                                 include_remote=True), )
        recipients.extend(
            _get_remote_followers(
                content.author,
                content.visibility,
                exclude=content.author.fid,
            ))
    elif content.visibility == Visibility.LIMITED:
        recipients.extend(_get_limited_recipients(content.author.fid, content))
    else:
        return
    if not recipients:
        logger.debug("send_reply - no remote recipients for content: %s",
                     content.id)
        return
    logger.debug("send_reply - sending to recipients: %s", recipients)
    handle_send(entity,
                content.author.federable,
                recipients,
                payload_logger=get_outbound_payload_logger())
Esempio n. 10
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 = [
                # TODO fid or handle?
                (recipient.handle, recipient.key, recipient.guid),
            ]
        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.federable, recipients)
    else:
        logger.warning("send_content - No entity for %s", content)
Esempio n. 11
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 = [
                # TODO fid or handle?
                parent_author.handle,
            ]
        else:
            recipients = [
                # TODO fid or handle?
                (parent_author.handle, parent_author.key, parent_author.guid),
            ]
        logger.debug("send_reply - sending to recipients: %s", recipients)
        handle_send(entity, content.author.federable, recipients)
Esempio n. 12
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)
Esempio n. 13
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)