Files
Link-Log/backend/app/api/public.py
T
2026-08-26 12:54:38 +02:00

57 lines
1.7 KiB
Python

## Copyright © 2026 Olaf Kolkman
## SPDX-License-Identifier: GPL-3.0-or-later
from fastapi import APIRouter, Depends
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from backend.app.services.link_service import list_public_links, list_public_users
from backend.app.services.token_service import validate_token
from backend.app.services.theme_service import THEMES, get_enabled_themes
router = APIRouter()
optional_bearer = HTTPBearer(auto_error=False)
@router.get('/users')
def public_users():
return list_public_users()
@router.get('/themes')
def public_themes():
enabled = get_enabled_themes()
return [{'id': theme, **THEMES[theme]} for theme in enabled]
@router.get('/feed')
@router.get('/feed/{username}')
def public_feed(
username: str | None = None,
credentials: HTTPAuthorizationCredentials | None = Depends(optional_bearer),
):
current_user_id = None
if credentials:
token_data = validate_token(credentials.credentials)
if token_data:
current_user_id = token_data['user_id']
items = list_public_links(username)
return [
{
'id': item['id'],
'title': item['title'],
'url': item['url'],
'comment': item['comment'],
'tags': item['tags'],
'user': {
'username': item['username'],
'avatar_url': item['avatar_url'],
'bio': item['bio'],
},
'is_owner': item['user_id'] == current_user_id,
'can_edit': item['user_id'] == current_user_id,
'created_at': item['created_at'],
'mastodon_posted': bool(item['mastodon_posted']),
}
for item in items
]