2023-03-17 10:59:29 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
from app import models
|
|
|
|
from app.database import AsyncSession
|
2023-03-17 18:46:31 +01:00
|
|
|
from app.activitypub import ME
|
2023-03-17 10:59:29 +01:00
|
|
|
from loguru import logger
|
|
|
|
from uuid import uuid4
|
2023-03-17 18:46:31 +01:00
|
|
|
from app.config import MANUALLY_APPROVES_FOLLOWERS
|
|
|
|
from app.config import BASE_URL
|
2023-03-17 10:59:29 +01:00
|
|
|
|
2023-03-17 18:46:31 +01:00
|
|
|
import app.activitypub as ap
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
|
|
def allocate_outbox_id() -> str:
|
|
|
|
return str(uuid.uuid4())
|
|
|
|
|
|
|
|
|
|
|
|
def build_object_id(id) -> str:
|
|
|
|
return f"{BASE_URL}/tail/{id}"
|
2023-03-17 10:59:29 +01:00
|
|
|
|
|
|
|
async def save_incoming(
|
|
|
|
db_session: AsyncSession,
|
|
|
|
payload: dict,
|
|
|
|
) -> models.IncomingActivity | None:
|
|
|
|
ap_id: str
|
|
|
|
if "@context" not in payload:
|
|
|
|
logger.warning(f"invalid object: {payload}")
|
|
|
|
return None
|
|
|
|
|
|
|
|
if "id" in payload:
|
|
|
|
ap_id = payload["id"]
|
|
|
|
else:
|
|
|
|
ap_id = str(uuid4())
|
|
|
|
|
|
|
|
incoming_activity = models.IncomingActivity(
|
|
|
|
ap_id=ap_id,
|
|
|
|
ap_object=payload,
|
|
|
|
)
|
2023-03-17 18:46:31 +01:00
|
|
|
await process_incoming(db_session, payload)
|
2023-03-18 09:12:44 +01:00
|
|
|
if db_session.add(incoming_activity):
|
|
|
|
return incoming_activity
|
|
|
|
|
2023-03-17 10:59:29 +01:00
|
|
|
await db_session.commit()
|
|
|
|
await db_session.refresh(incoming_activity)
|
|
|
|
return incoming_activity
|
2023-03-17 18:46:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def process_incoming(
|
|
|
|
db_session: AsyncSession,
|
|
|
|
ap_object: dict,
|
|
|
|
) -> bool:
|
|
|
|
|
|
|
|
if "Follow" == ap_object["type"]:
|
|
|
|
await _handle_follow(db_session, ap_object)
|
2023-03-18 09:12:44 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2023-03-17 18:46:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def _handle_follow(
|
|
|
|
db_session: AsyncSession,
|
|
|
|
ap_object: dict,
|
2023-03-18 09:12:44 +01:00
|
|
|
) -> bool:
|
2023-03-17 18:46:31 +01:00
|
|
|
if ME["id"] != ap_object["object"]:
|
|
|
|
# await db_session.delete(ap_object)
|
|
|
|
logger.warning("no match follow object!" + ap_object["object"])
|
2023-03-18 09:12:44 +01:00
|
|
|
return False
|
2023-03-17 18:46:31 +01:00
|
|
|
|
|
|
|
if MANUALLY_APPROVES_FOLLOWERS:
|
|
|
|
# TODO
|
2023-03-18 09:12:44 +01:00
|
|
|
return False
|
2023-03-17 18:46:31 +01:00
|
|
|
|
|
|
|
await _send_accept(db_session, ap_object)
|
2023-03-18 09:12:44 +01:00
|
|
|
return True
|
2023-03-17 18:46:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
async def _send_accept(
|
|
|
|
db_session: AsyncSession,
|
|
|
|
ap_object: dict,
|
|
|
|
) -> None :
|
|
|
|
|
|
|
|
reply_id = allocate_outbox_id()
|
2023-03-18 04:57:20 +01:00
|
|
|
|
|
|
|
url = ap_object["actor"] + "/inbox"
|
2023-03-17 18:46:31 +01:00
|
|
|
out = {
|
|
|
|
"@context": ap.AS_CTX,
|
|
|
|
"id": build_object_id(reply_id),
|
|
|
|
"type": "Accept",
|
|
|
|
"actor": ME["id"],
|
|
|
|
"object": ap_object["id"],
|
|
|
|
}
|
2023-03-18 04:57:20 +01:00
|
|
|
#TODO outcoming
|
|
|
|
await ap.post(url, out)
|