Post to mastodon from the profile page for logged in users

This commit is contained in:
2026-08-26 10:20:48 +02:00
parent 9deb28330a
commit 95accdf436
13 changed files with 136 additions and 5 deletions
+31 -1
View File
@@ -5,7 +5,8 @@ from fastapi import APIRouter, Header, HTTPException, status
import logging
from pydantic import BaseModel
from backend.app.services.link_service import create_link, delete_link, list_public_links, list_tags, update_link
from backend.app.services.link_service import create_link, delete_link, get_link_tags, list_public_links, list_tags, mark_mastodon_posted, update_link
from backend.app.database import get_connection
from backend.app.services.plugin_manager import plugin_manager
from backend.app.services.token_service import validate_token
@@ -47,6 +48,9 @@ def create_link_endpoint(payload: LinkCreate, authorization: str | None = Header
except ValueError as error:
raise HTTPException(status_code=422, detail=str(error)) from error
plugin_results = plugin_manager.dispatch({'type': 'link_created', **record})
mastodon_result = next((result for result in plugin_results if result.get('plugin') == 'mastodon'), None)
if mastodon_result and mastodon_result.get('status') == 'posted':
mark_mastodon_posted(record['id'], info['user_id'], mastodon_result.get('post_id'))
if any(result.get('status') == 'failed' for result in plugin_results):
logger.warning('One or more plugins failed for link_id=%s results=%s', record['id'], plugin_results)
return record
@@ -88,6 +92,32 @@ def delete_link_endpoint(
return {'status': 'deleted', 'id': link_id}
@router.post('/links/{link_id}/mastodon')
def post_link_to_mastodon(
link_id: str,
authorization: str | None = Header(default=None),
):
if not authorization or not authorization.startswith('Bearer '):
raise HTTPException(status_code=401, detail='Missing or invalid Authorization header')
info = validate_token(authorization.replace('Bearer ', '', 1))
if info is None:
raise HTTPException(status_code=401, detail='Token expired or invalid')
with get_connection() as conn:
row = conn.execute('SELECT * FROM links WHERE id = ? AND user_id = ?', (link_id, info['user_id'])).fetchone()
if row is None:
raise HTTPException(status_code=404, detail='Link not found or not owned by user')
event = dict(row)
with get_connection() as conn:
event['tags'] = get_link_tags(conn, link_id)
result = plugin_manager.post_to_mastodon({'type': 'link_created', **event})
if result.get('status') == 'posted':
mark_mastodon_posted(link_id, info['user_id'], result.get('post_id'))
return {'status': 'posted', 'post_id': result.get('post_id')}
if result.get('status') == 'skipped':
raise HTTPException(status_code=409, detail='Mastodon is not enabled or configured')
raise HTTPException(status_code=502, detail=result.get('reason', 'Mastodon post failed'))
@router.get('/links')
def list_links():
return list_public_links()