[feat] show note in index

This commit is contained in:
SouthFox 2023-07-30 20:48:50 +08:00
parent 68ae04e72a
commit abdf1fea0f
6 changed files with 81 additions and 5 deletions

18
app/ap_object.py Normal file
View file

@ -0,0 +1,18 @@
#!/usr/bin/env python3
from app import activitypub as ap
from functools import cached_property
class BaseObject:
@property
def ap_object(self) -> ap.RawObject:
raise NotImplementedError
@cached_property
def content(self) -> str | None:
content = self.ap_object.get("content")
if not content:
return None
return content

View file

@ -9,6 +9,7 @@ from fastapi import Depends
from fastapi import Request
from fastapi import Response
from fastapi.exceptions import HTTPException
from fastapi.templating import Jinja2Templates
from starlette.responses import JSONResponse
from loguru import logger
@ -43,6 +44,8 @@ app = FastAPI(
dependencies=[Depends(_check_0rtt_early_data)]
)
templates = Jinja2Templates(directory="templates")
logger.remove()
logger.add(sys.stdout, level="DEBUG" if DEBUG else "INFO")
logger.add("output.log", level="DEBUG")
@ -71,13 +74,29 @@ def is_ap_requested(req: Request) -> bool:
@app.get("/")
async def index(
request: Request
request: Request,
db_session: AsyncSession = Depends(get_db_session),
):
"""Return index page."""
if is_ap_requested(request):
return ActivityPubResponse(ME)
return ME
statues = (
await db_session.scalars(
select(models.OutboxObject).where(
models.OutboxObject.ap_type == "Note",
models.OutboxObject.is_deleted.is_(False),
)
)
).all()
return templates.TemplateResponse(
"index.html",
{
"request": request,
"statues": statues,
}
)
@app.post("/inbox")

View file

@ -8,6 +8,7 @@ from app import activitypub as ap
from app.database import Base
from app.database import metadata_obj
from app.activitypub import BaseActor
from app.ap_object import BaseObject
from sqlalchemy import Column
from sqlalchemy import Boolean
@ -42,7 +43,7 @@ class Actor(Base, BaseActor):
is_blocked = Column(Boolean, nullable=False, default=False, server_default="0")
is_deleted = Column(Boolean, nullable=False, default=False, server_default="0")
class InboxObject(Base):
class InboxObject(Base, BaseObject):
__tablename__ = "inbox"
id = Column(Integer, primary_key=True, index=True)
@ -89,7 +90,7 @@ class InboxObject(Base):
uselist=False,
)
class OutboxObject(Base):
class OutboxObject(Base, BaseObject):
__tablename__ = "outbox"
id = Column(Integer, primary_key=True, index=True)

11
templates/index.html Normal file
View file

@ -0,0 +1,11 @@
<html>
<head>
<title>Index</title>
</head>
<body>
<h1>Test</h1>
{% for status in statues recursive%}
{{ status.content | safe }}
{% endfor %}
</body>
</html>

25
templates/utils.html Normal file
View file

@ -0,0 +1,25 @@
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Untitled</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
<!-- Place favicon.ico in the root directory -->
</head>
<body>
<!--[if lt IE 8]>
<p class="browserupgrade">
You are using an <strong>outdated</strong> browser. Please
<a href="http://browsehappy.com/">upgrade your browser</a> to improve
your experience.
</p>
<![endif]-->
</body>
</html>

View file

@ -13,10 +13,12 @@ _ACCEPTED_AP_HEADERS = [
'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
]
def test_index__html(client: TestClient):
response = client.get("/")
assert response.status_code == 200
assert response.headers["content-type"] == "application/json"
assert response.headers["content-type"].startswith("text/html")
@pytest.mark.parametrize("accept", _ACCEPTED_AP_HEADERS)
def test_index__ap(db: Session, client: TestClient, accept: str):