Beispiel #1
0
def process_entity_comment(entity: Any, profile: Profile):
    """Process an entity of type Comment."""
    fid = safe_text(entity.id)
    if not validate_against_old_content(fid, entity, profile):
        return
    try:
        parent = Content.objects.fed(entity.target_id).get()
    except Content.DoesNotExist:
        logger.warning("No target found for comment: %s", entity)
        return
    root_parent = parent
    if entity.root_target_id:
        try:
            root_parent = Content.objects.fed(entity.root_target_id).get()
        except Content.DoesNotExist:
            pass
    values = {
        "text":
        _embed_entity_images_to_post(
            entity._children, safe_text_for_markdown(entity.raw_content)),
        "author":
        profile,
        "visibility":
        parent.visibility,
        "remote_created":
        safe_make_aware(entity.created_at, "UTC"),
        "parent":
        parent,
        "root_parent":
        root_parent,
    }
    if getattr(entity, "guid", None):
        values["guid"] = safe_text(entity.guid)
    content, created = Content.objects.fed_update_or_create(fid, values)
    _process_mentions(content, entity)
    if created:
        logger.info("Saved Content from comment entity: %s", content)
    else:
        logger.info("Updated Content from comment entity: %s", content)

    # TODO we should respect the visibility of the comment instead here
    if parent.visibility == Visibility.LIMITED:
        if entity._receivers:
            receivers = get_profiles_from_receivers(entity._receivers)
            if len(receivers):
                content.limited_visibilities.set(receivers)
                logger.info("Added visibility to Comment %s to %s",
                            content.fid, receivers)
            else:
                logger.warning(
                    "No local receivers found for limited Comment %s",
                    content.fid)
        else:
            logger.warning("No receivers for limited Comment %s", content.fid)

    if parent.local:
        # We should relay this to participants we know of
        from socialhome.federate.tasks import forward_entity
        django_rq.enqueue(forward_entity, entity, root_parent.id)
Beispiel #2
0
def process_entity_post(entity: Any, profile: Profile):
    """Process an entity of type Post."""
    fid = safe_text(entity.id)
    if not validate_against_old_content(fid, entity, profile):
        return
    values = {
        "fid":
        fid,
        "text":
        _embed_entity_images_to_post(
            entity._children, safe_text_for_markdown(entity.raw_content)),
        "author":
        profile,
        "visibility":
        Visibility.PUBLIC if entity.public else Visibility.LIMITED,
        "remote_created":
        safe_make_aware(entity.created_at, "UTC"),
        "service_label":
        safe_text(entity.provider_display_name) or "",
    }
    extra_lookups = {}
    if getattr(entity, "guid", None):
        values["guid"] = safe_text(entity.guid)
        extra_lookups["guid"] = values["guid"]
    content, created = Content.objects.fed_update_or_create(
        fid, values, extra_lookups=extra_lookups)
    _process_mentions(content, entity)
    if created:
        logger.info("Saved Content: %s", content)
    else:
        logger.info("Updated Content: %s", content)
    if content.visibility == Visibility.LIMITED:
        if entity._receivers:
            receivers = get_profiles_from_receivers(entity._receivers)
            if len(receivers):
                content.limited_visibilities.set(receivers)
                logger.info("Added visibility to Post %s to %s", content.fid,
                            receivers)
            else:
                logger.warning("No local receivers found for limited Post %s",
                               content.fid)
        else:
            logger.warning("No receivers for limited Post %s", content.fid)