49 lines
1.4 KiB
Python
49 lines
1.4 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
|
|
|
|
router = APIRouter()
|
|
optional_bearer = HTTPBearer(auto_error=False)
|
|
|
|
|
|
@router.get('/users')
|
|
def public_users():
|
|
return list_public_users()
|
|
|
|
|
|
@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'],
|
|
}
|
|
for item in items
|
|
]
|