Skip to content

Commit

Permalink
Moved all/most ActivityPub code to its own library. The goal is compl…
Browse files Browse the repository at this point in the history
…etely modularize it.
  • Loading branch information
nigini committed Dec 14, 2024
1 parent 39c00e0 commit 72969fb
Show file tree
Hide file tree
Showing 44 changed files with 1,249 additions and 1,190 deletions.
Empty file added activitypub/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion app/activitypub.py → activitypub/activitypub.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from app.utils.url import check_url

if TYPE_CHECKING:
from app.actor import Actor
from activitypub.actor import Actor

RawObject = dict[str, Any]
AS_CTX = "https://www.w3.org/ns/activitystreams"
Expand Down
51 changes: 26 additions & 25 deletions app/actor.py → activitypub/actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
from sqlalchemy import select
from sqlalchemy.orm import joinedload

from app import activitypub as ap
import activitypub.models
from activitypub import activitypub as ap
from app import media
from app.config import BASE_URL
from app.config import USER_AGENT
Expand All @@ -22,7 +23,7 @@
from app.utils.datetime import now

if typing.TYPE_CHECKING:
from app.models import Actor as ActorModel
from activitypub.models import Actor as ActorModel


def _handle(raw_actor: ap.RawObject) -> str:
Expand Down Expand Up @@ -216,7 +217,7 @@ async def save_actor(db_session: AsyncSession, ap_actor: ap.RawObject) -> "Actor
if ap_type := ap_actor.get("type") not in ap.ACTOR_TYPES:
raise ValueError(f"Invalid type {ap_type} for actor {ap_actor}")

actor = models.Actor(
actor = activitypub.models.Actor(
ap_id=ap.get_id(ap_actor["id"]),
ap_actor=ap_actor,
ap_type=ap.as_list(ap_actor["type"])[0],
Expand All @@ -239,8 +240,8 @@ async def fetch_actor(

existing_actor = (
await db_session.scalars(
select(models.Actor).where(
models.Actor.ap_id == actor_id,
select(activitypub.models.Actor).where(
activitypub.models.Actor.ap_id == actor_id,
)
)
).one_or_none()
Expand Down Expand Up @@ -273,8 +274,8 @@ async def fetch_actor(
# (like Birdsite LIVE) , which mean we may already have it in DB
existing_actor_by_url = (
await db_session.scalars(
select(models.Actor).where(
models.Actor.ap_id == ap.get_id(ap_actor),
select(activitypub.models.Actor).where(
activitypub.models.Actor.ap_id == ap.get_id(ap_actor),
)
)
).one_or_none()
Expand All @@ -298,8 +299,8 @@ async def list_actors(db_session: AsyncSession, limit: int = 100) -> list["Actor
return (
(
await db_session.scalars(
select(models.Actor)
.where(models.Actor.is_deleted.is_(False))
select(activitypub.models.Actor)
.where(activitypub.models.Actor.is_deleted.is_(False))
.limit(limit)
)
)
Expand Down Expand Up @@ -350,9 +351,9 @@ async def get_actors_metadata(
follower.ap_actor_id: follower.inbox_object.ap_id
for follower in (
await db_session.scalars(
select(models.Follower)
.where(models.Follower.ap_actor_id.in_(ap_actor_ids))
.options(joinedload(models.Follower.inbox_object))
select(activitypub.models.Follower)
.where(activitypub.models.Follower.ap_actor_id.in_(ap_actor_ids))
.options(joinedload(activitypub.models.Follower.inbox_object))
)
)
.unique()
Expand All @@ -361,37 +362,37 @@ async def get_actors_metadata(
following = {
following.ap_actor_id
for following in await db_session.execute(
select(models.Following.ap_actor_id).where(
models.Following.ap_actor_id.in_(ap_actor_ids)
select(activitypub.models.Following.ap_actor_id).where(
activitypub.models.Following.ap_actor_id.in_(ap_actor_ids)
)
)
}
sent_follow_requests = {
follow_req.ap_object["object"]: follow_req.ap_id
for follow_req in await db_session.execute(
select(models.OutboxObject.ap_object, models.OutboxObject.ap_id).where(
models.OutboxObject.ap_type == "Follow",
models.OutboxObject.undone_by_outbox_object_id.is_(None),
models.OutboxObject.activity_object_ap_id.in_(ap_actor_ids),
select(activitypub.models.OutboxObject.ap_object, activitypub.models.OutboxObject.ap_id).where(
activitypub.models.OutboxObject.ap_type == "Follow",
activitypub.models.OutboxObject.undone_by_outbox_object_id.is_(None),
activitypub.models.OutboxObject.activity_object_ap_id.in_(ap_actor_ids),
)
)
}
rejected_follow_requests = {
reject.activity_object_ap_id
for reject in await db_session.execute(
select(models.InboxObject.activity_object_ap_id).where(
models.InboxObject.ap_type == "Reject",
models.InboxObject.ap_actor_id.in_(ap_actor_ids),
select(activitypub.models.InboxObject.activity_object_ap_id).where(
activitypub.models.InboxObject.ap_type == "Reject",
activitypub.models.InboxObject.ap_actor_id.in_(ap_actor_ids),
)
)
}
blocks = {
block.ap_actor_id
for block in await db_session.execute(
select(models.InboxObject.ap_actor_id).where(
models.InboxObject.ap_type == "Block",
models.InboxObject.undone_by_inbox_object_id.is_(None),
models.InboxObject.ap_actor_id.in_(ap_actor_ids),
select(activitypub.models.InboxObject.ap_actor_id).where(
activitypub.models.InboxObject.ap_type == "Block",
activitypub.models.InboxObject.undone_by_inbox_object_id.is_(None),
activitypub.models.InboxObject.ap_actor_id.in_(ap_actor_ids),
)
)
}
Expand Down
8 changes: 4 additions & 4 deletions app/ap_object.py → activitypub/ap_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from bs4 import BeautifulSoup # type: ignore
from mistletoe import markdown # type: ignore

from app import activitypub as ap
from app.actor import LOCAL_ACTOR
from app.actor import Actor
from app.actor import RemoteActor
from activitypub import activitypub as ap
from activitypub.actor import LOCAL_ACTOR
from activitypub.actor import Actor
from activitypub.actor import RemoteActor
from app.config import ID
from app.media import proxied_media_url
from app.utils.datetime import now
Expand Down
Loading

0 comments on commit 72969fb

Please sign in to comment.