How to use the federation.entities.activitypub.entities.ActivitypubImage function in federation

To help you get started, we’ve selected a few federation examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jaywink / federation / federation / entities / activitypub / entities.py View on Github external
def pre_send(self) -> None:
        """
        Attach any embedded images from raw_content.
        """
        super().pre_send()
        if self._media_type != "text/markdown":
            return
        regex = r"!\[([\w ]*)\]\((https?://[\w\d\-\./]+\.[\w]*((?<=jpg)|(?<=gif)|(?<=png)|(?<=jpeg)))\)"
        matches = re.finditer(regex, self.raw_content, re.MULTILINE | re.IGNORECASE)
        for match in matches:
            groups = match.groups()
            self._children.append(
                ActivitypubImage(
                    url=groups[1],
                    name=groups[0] or "",
                    inline=True,
                )
github jaywink / federation / federation / entities / activitypub / mappers.py View on Github external
def extract_attachments(payload: Dict) -> List[Image]:
    """
    Extract images from attachments.

    There could be other attachments, but currently we only extract images.
    """
    attachments = []
    for item in payload.get('attachment', []):
        # noinspection PyProtectedMember
        if item.get("type") in ("Document", "Image") and item.get("mediaType") in Image._valid_media_types:
            if item.get('pyfed:inlineImage', False):
                # Skip this image as it's indicated to be inline in content and source already
                continue
            attachments.append(
                ActivitypubImage(
                    url=item.get('url'),
                    name=item.get('name') or "",
                    media_type=item.get("mediaType"),
                )
            )
    return attachments
github jaywink / federation / federation / entities / activitypub / entities.py View on Github external
"outbox": f"{with_slash(self.id)}outbox/",
            "publicKey": {
                "id": f"{self.id}#main-key",
                "owner": self.id,
                "publicKeyPem": self.public_key,
            },
            "type": self._type,
            "url": self.url,
        }
        if self.username:
            as2['preferredUsername'] = self.username
        if self.raw_content:
            as2['summary'] = self.raw_content
        if self.image_urls.get('large'):
            try:
                profile_icon = ActivitypubImage(url=self.image_urls.get('large'))
                if profile_icon.media_type:
                    as2['icon'] = profile_icon.to_as2()
            except Exception as ex:
                logger.warning("ActivitypubProfile.to_as2 - failed to set profile icon: %s", ex)

        if self.guid or self.handle:
            as2["@context"].append(CONTEXT_DIASPORA)
            if self.guid:
                as2["diaspora:guid"] = self.guid
            if self.handle:
                as2["diaspora:handle"] = self.handle

        return as2
github jaywink / federation / federation / entities / activitypub / mappers.py View on Github external
from federation.entities.base import Follow, Profile, Accept, Post, Comment, Retraction, Share, Image
from federation.entities.mixins import BaseEntity
from federation.types import UserType, ReceiverVariant

logger = logging.getLogger("federation")


MAPPINGS = {
    "Accept": ActivitypubAccept,
    "Announce": ActivitypubShare,
    "Application": ActivitypubProfile,
    "Article": ActivitypubPost,
    "Delete": ActivitypubRetraction,
    "Follow": ActivitypubFollow,  # Technically not correct, but for now we support only following profiles
    "Group": ActivitypubProfile,
    "Image": ActivitypubImage,
    "Note": ActivitypubPost,
    "Organization": ActivitypubProfile,
    "Page": ActivitypubPost,
    "Person": ActivitypubProfile,
    "Service": ActivitypubProfile,
}

OBJECTS = (
    "Application",
    "Article",
    "Group",
    "Image",
    "Note",
    "Organization",
    "Page",
    "Person",