[feat] support unlisted visibility note
All checks were successful
/ run-pytest (push) Successful in 1m28s

This commit is contained in:
SouthFox 2024-03-04 20:57:57 +08:00
parent eff8338f80
commit c97581278d
4 changed files with 43 additions and 6 deletions

View file

@ -435,6 +435,9 @@ async def _send_create(
if visibility == ap.VisibilityEnum.PUBLIC: if visibility == ap.VisibilityEnum.PUBLIC:
to = [ap.AS_PUBLIC] to = [ap.AS_PUBLIC]
cc = [f"{BASE_URL}/followers"] cc = [f"{BASE_URL}/followers"]
elif visibility == ap.VisibilityEnum.UNLISTED:
to = [f"{BASE_URL}/followers"]
cc = [ap.AS_PUBLIC]
else: else:
raise ValueError(f"Unsupport visibility {visibility}") raise ValueError(f"Unsupport visibility {visibility}")

View file

@ -126,9 +126,8 @@ async def outbox(
db_session: AsyncSession = Depends(get_db_session), db_session: AsyncSession = Depends(get_db_session),
) -> Response: ) -> Response:
"""ActivityPub outbox endpoint, now only process client post request.""" """ActivityPub outbox endpoint, now only process client post request."""
payload = await request.body() payload = await request.json()
content = payload.decode("utf-8") logger.info(payload)
logger.info(content)
post_token = request.headers.get("Authorization") post_token = request.headers.get("Authorization")
def _check_post_token() -> bool: def _check_post_token() -> bool:
@ -144,13 +143,13 @@ async def outbox(
return Response(status_code=406) return Response(status_code=406)
logger.info("True token") logger.info("True token")
content = to_html(content).replace("\n", "") note_content = to_html(payload["content"]).replace("\n", "")
await _send_create( await _send_create(
db_session, db_session,
"Note", "Note",
content, note_content,
VisibilityEnum.PUBLIC payload["visibility"]
) )
return Response(status_code=200) return Response(status_code=200)

View file

@ -10,3 +10,5 @@ debug = true
# In-mem DB # In-mem DB
sqlalchemy_database = "file:pytest?mode=memory&cache=shared&uri=true" sqlalchemy_database = "file:pytest?mode=memory&cache=shared&uri=true"
post_token = "test-token"

View file

@ -155,3 +155,36 @@ async def test_outbox_send_create_activity(
outbox_object = db.execute(select(models.OutboxObject)).scalar_one() outbox_object = db.execute(select(models.OutboxObject)).scalar_one()
assert outbox_object.ap_type == "Note" assert outbox_object.ap_type == "Note"
assert outbox_object.ap_object["content"] == content assert outbox_object.ap_object["content"] == content
@pytest.mark.asyncio
async def test_outbox_send_unlisted_note(
db: Session,
async_db_session,
client: TestClient,
respx_mock: respx.MockRouter,
) -> None:
# build test actor
ra = build_remote_actor()
remote_ap_id = ra.ap_id # type: ignore
# mock request
respx_mock.get(remote_ap_id).mock(
return_value=httpx.Response(200,json=ra.ap_actor))
respx_mock.post(remote_ap_id + "/inbox").mock(
return_value=httpx.Response(202))
from app.activitypub import VisibilityEnum
response = client.post(
"/outbox",
headers={"Authorization": "Basic test-token"},
content='{"visibility": "unlisted","content": "note content"}'
)
assert response.status_code == 200
# And the Follow activity was created in the outbox
outbox_object = db.execute(select(models.OutboxObject)).scalar_one()
assert outbox_object.ap_type == "Note"
assert outbox_object.visibility == VisibilityEnum.UNLISTED