Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tags = {word.strip("#").lower() for word in self.raw_content.split() if word.startswith("#") and len(word) > 1}
return sorted(tags)
def extract_mentions(self):
matches = re.findall(r'@{([\S ][^{}]+)}', self.raw_content)
if not matches:
return
for mention in matches:
splits = mention.split(";")
if len(splits) == 1:
self._mentions.add(splits[0].strip(' }'))
elif len(splits) == 2:
self._mentions.add(splits[1].strip(' }'))
class OptionalRawContentMixin(RawContentMixin):
"""A version of the RawContentMixin where `raw_content` is not required."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._required.remove("raw_content")
class EntityTypeMixin(BaseEntity):
"""
Provides a field for entity type.
"""
entity_type = ""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._required += ["entity_type"]
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._required += ["url"]
self._required.remove("id")
self._required.remove("actor_id")
if self.url and not self.media_type:
self.media_type = self.get_media_type()
def get_media_type(self) -> str:
media_type = fetch_content_type(self.url)
if media_type in self._valid_media_types:
return media_type
return ""
class Comment(RawContentMixin, ParticipationMixin, CreatedAtMixin, RootTargetIDMixin, BaseEntity):
"""Represents a comment, linked to another object."""
participation = "comment"
url = ""
_allowed_children = (Image,)
_default_activity = ActivityType.CREATE
class Follow(CreatedAtMixin, TargetIDMixin, BaseEntity):
"""Represents a handle following or unfollowing another handle."""
following = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._required += ["following"]
# ID not required for follow
_allowed_children = (Image,)
_default_activity = ActivityType.CREATE
class Follow(CreatedAtMixin, TargetIDMixin, BaseEntity):
"""Represents a handle following or unfollowing another handle."""
following = True
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._required += ["following"]
# ID not required for follow
self._required.remove('id')
class Post(RawContentMixin, PublicMixin, CreatedAtMixin, ProviderDisplayNameMixin, BaseEntity):
"""Reflects a post, status message, etc, which will be composed from the message or to the message."""
location = ""
url = ""
_allowed_children = (Image,)
_default_activity = ActivityType.CREATE
class Reaction(ParticipationMixin, CreatedAtMixin, BaseEntity):
"""Represents a reaction to another object, for example a like."""
participation = "reaction"
reaction = ""
_default_activity = ActivityType.CREATE
_reaction_valid_values = ["like"]
import re
from federation.entities.base import Image
from federation.entities.mixins import BaseEntity, RawContentMixin
from federation.entities.utils import get_base_attributes
class AttachImagesMixin(RawContentMixin):
def pre_send(self) -> None:
"""
Attach any embedded images from raw_content.
"""
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(
Image(
url=groups[1],
name=groups[0] or "",
inline=True,
)
from federation.entities.activitypub.constants import (
CONTEXTS_DEFAULT, CONTEXT_MANUALLY_APPROVES_FOLLOWERS, CONTEXT_SENSITIVE, CONTEXT_HASHTAG,
CONTEXT_LD_SIGNATURES, CONTEXT_DIASPORA)
from federation.entities.activitypub.enums import ActorType, ObjectType, ActivityType
from federation.entities.base import Profile, Post, Follow, Accept, Comment, Retraction, Share, Image
from federation.entities.mixins import RawContentMixin, BaseEntity, PublicMixin
from federation.entities.utils import get_base_attributes
from federation.outbound import handle_send
from federation.types import UserType
from federation.utils.django import get_configuration
from federation.utils.text import with_slash, validate_handle
logger = logging.getLogger("federation")
class AttachImagesMixin(RawContentMixin):
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,
class ActivitypubEntityMixin(BaseEntity):
_type = None
@classmethod
def from_base(cls, entity):
# noinspection PyArgumentList
return cls(**get_base_attributes(entity))
def to_string(self):
# noinspection PyUnresolvedReferences
return str(self.to_as2())
class CleanContentMixin(RawContentMixin):
def post_receive(self) -> None:
"""
Make linkified tags normal tags.
"""
super().post_receive()
def remove_tag_links(attrs, new=False):
rel = (None, "rel")
if attrs.get(rel) == "tag":
return
return attrs
self.raw_content = bleach.linkify(
self.raw_content,
callbacks=[remove_tag_links],
parse_email=False,
class ActivitypubEntityMixin(BaseEntity):
_type = None
@classmethod
def from_base(cls, entity):
# noinspection PyArgumentList
return cls(**get_base_attributes(entity))
def to_string(self):
# noinspection PyUnresolvedReferences
return str(self.to_as2())
class CleanContentMixin(RawContentMixin):
def post_receive(self) -> None:
"""
Make linkified tags normal tags.
"""
def cleaner(match):
return f"#{match.groups()[0]}"
self.raw_content = re.sub(
r'\[#([\w\-_]+)\]\(http?s://[a-zA-Z0-9/._-]+\)',
cleaner,
self.raw_content,
re.MULTILINE,
)