WIP/following actor

This commit is contained in:
SouthFox 2023-03-27 18:18:04 +08:00
parent 89394d50c6
commit e8ec99c77a

View file

@ -70,7 +70,11 @@ async def process_incoming(
) -> bool:
actor = await fetch_actor(db_session, ap_object["actor"])
def build_object(object) -> InboxObject:
def build_object(
object,
relates_to_inbox_object_id = None,
relates_to_outbox_object_id = None,
) -> InboxObject:
inbox_object = models.InboxObject(
actor_id=actor.id,
server=urlparse(object["id"]).hostname,
@ -81,8 +85,9 @@ async def process_incoming(
ap_published_at=now(),
ap_object=object,
visibility=handle_visibility(object),
activity_object_ap_id=object["actor"]
#TODO relates
activity_object_ap_id=object["actor"],
relates_to_inbox_object_id=relates_to_inbox_object_id,
relates_to_outbox_object_id=relates_to_outbox_object_id,
)
return inbox_object
@ -100,6 +105,33 @@ async def process_incoming(
#db_session.add(inbox_object)
if await _handle_undo(db_session, ap_object):
return True
elif ap_object["type"] in ["Accept", "Rejact"]:
relate_following_object = (await db_session.execute(
select(models.OutboxObject)
.where(models.OutboxObject.ap_id == ap_object["object"])
.options(
joinedload(models.OutboxObject.relates_to_inbox_object).options(
joinedload(models.InboxObject.actor)
)
)
)
).unique().scalar_one_or_none()
if "Accept" == ap_object["type"]:
inbox_object = build_object(ap_object)
db_session.add(inbox_object)
await db_session.flush()
await db_session.refresh(inbox_object)
following = models.Following(
actor_id=relate_following_object.relates_to_actor.id, #type: ignore
outbox_object_id=relate_following_object.id, #type: ignore
ap_actor_id=ap_object["actor"], #type: ignore
)
db_session.add(following)
await db_session.flush()
await db_session.refresh(following)
return False
@ -205,15 +237,21 @@ async def _send_follow(
):
actor = await fetch_actor(db_session, actor_url)
follow_id = allocate_outbox_id()
follow_id = build_object_id(allocate_outbox_id())
out = {
"@context": ap.AS_CTX,
"id": build_object_id(follow_id),
"id": follow_id,
"type": "Follow",
"actor": ME["id"],
"object": actor.ap_id,
}
await save_to_outbox(
db_session,
follow_id,
out,
relates_to_actor_id=actor.id, # type: ignore
)
await ap.post(
actor.inbox_url,
@ -221,6 +259,37 @@ async def _send_follow(
)
async def save_to_inbox(
db_session : AsyncSession,
inbox_id : str,
ap_object : dict,
relates_to_inbox_object_id: int | None = None,
relates_to_outbox_object_id: int | None = None,
relates_to_actor_id: int | None = None,
) -> InboxObject:
ap_type = ap_object["type"]
ap_id = ap_object["id"]
visibility = handle_visibility(ap_object)
inbox_object = OutboxObject(
public_id=inbox_id,
ap_object=ap_object,
ap_id=ap_id,
ap_type=ap_type,
visibility =visibility,
relates_to_inbox_object_id=relates_to_inbox_object_id,
relates_to_outbox_object_id=relates_to_outbox_object_id,
relates_to_actor_id=relates_to_actor_id,
)
db_session.add(inbox_object)
await db_session.flush()
await db_session.refresh(inbox_object)
return inbox_object
async def save_to_outbox(
db_session : AsyncSession,
outbox_id : str,
@ -229,10 +298,16 @@ async def save_to_outbox(
relates_to_outbox_object_id: int | None = None,
relates_to_actor_id: int | None = None,
) -> OutboxObject:
ap_type = ap_object["type"]
ap_id = ap_object["id"]
visibility = handle_visibility(ap_object)
outbox_object = OutboxObject(
public_id=outbox_id,
ap_object=ap_object,
ap_id=ap_id,
ap_type=ap_type,
visibility =visibility,
relates_to_inbox_object_id=relates_to_inbox_object_id,
relates_to_outbox_object_id=relates_to_outbox_object_id,
relates_to_actor_id=relates_to_actor_id,