""" Webmention database query functions Provides database query functions for webmention management. Per display-moderation.md design specification. Functions: - get_webmentions_for_note: Get approved webmentions for public display - list_webmentions: List webmentions for admin with filters and pagination - get_webmention: Get single webmention by ID - count_webmentions: Count webmentions (optionally filtered) - update_webmention_status: Update moderation status - delete_webmentions: Delete webmentions by IDs - get_reply_context: Get cached reply context for a URL """ import logging import math from datetime import datetime from pathlib import Path from typing import List, Dict, Any, Optional from flask import current_app from starpunk.database import get_db logger = logging.getLogger(__name__) def get_webmentions_for_note(note_id: int) -> Dict[str, List[Dict[str, Any]]]: """ Get approved webmentions for a note, grouped by type for public display. Per display-moderation.md: - Only returns validation_status='valid' AND moderation_status='approved' - Groups by mention_type (reply, like, repost, bookmark, mention) - Sorts within groups by received_at ASC (oldest first) Args: note_id: Note database ID Returns: Dict with keys: 'replies', 'likes', 'reposts', 'bookmarks', 'mentions' Each value is a list of webmention dicts """ db = get_db() # Query for approved webmentions only query = """ SELECT * FROM webmentions WHERE target_note_id = ? AND validation_status = 'valid' AND moderation_status = 'approved' ORDER BY CASE mention_type WHEN 'reply' THEN 1 WHEN 'like' THEN 2 WHEN 'repost' THEN 3 WHEN 'bookmark' THEN 4 WHEN 'mention' THEN 5 END, received_at ASC """ rows = db.execute(query, (note_id,)).fetchall() # Group by type grouped = { 'replies': [], 'likes': [], 'reposts': [], 'bookmarks': [], 'mentions': [] } for row in rows: wm = dict(row) # Parse received_at timestamp if isinstance(wm.get('received_at'), str): wm['received_at'] = datetime.fromisoformat(wm['received_at'].replace('Z', '+00:00')) mention_type = wm.get('mention_type', 'mention') if mention_type == 'reply': grouped['replies'].append(wm) elif mention_type == 'like': grouped['likes'].append(wm) elif mention_type == 'repost': grouped['reposts'].append(wm) elif mention_type == 'bookmark': grouped['bookmarks'].append(wm) else: grouped['mentions'].append(wm) return grouped def list_webmentions( status_filter: Optional[str] = None, page: int = 1, per_page: int = 25 ) -> tuple[List[Dict[str, Any]], int]: """ List webmentions for admin with pagination and filtering. Args: status_filter: Filter by moderation_status ('pending', 'approved', 'hidden') page: Page number (1-indexed) per_page: Items per page (default: 25) Returns: Tuple of (webmentions list, total_pages) """ db = get_db() # Build query base_query = """ SELECT w.*, n.slug as target_slug FROM webmentions w LEFT JOIN notes n ON w.target_note_id = n.id """ count_query = "SELECT COUNT(*) FROM webmentions" params = [] where_clause = "" if status_filter: where_clause = " WHERE w.moderation_status = ?" count_query += " WHERE moderation_status = ?" params.append(status_filter) # Count total total_count = db.execute(count_query, params).fetchone()[0] total_pages = math.ceil(total_count / per_page) if total_count > 0 else 1 # Get paginated results offset = (page - 1) * per_page query = base_query + where_clause + " ORDER BY w.received_at DESC LIMIT ? OFFSET ?" params.extend([per_page, offset]) rows = db.execute(query, params).fetchall() # Convert to dicts webmentions = [] for row in rows: wm = dict(row) # Parse timestamps if isinstance(wm.get('received_at'), str): wm['received_at'] = datetime.fromisoformat(wm['received_at'].replace('Z', '+00:00')) if wm.get('validated_at') and isinstance(wm['validated_at'], str): wm['validated_at'] = datetime.fromisoformat(wm['validated_at'].replace('Z', '+00:00')) webmentions.append(wm) return webmentions, total_pages def get_webmention(webmention_id: int) -> Optional[Dict[str, Any]]: """ Get single webmention by ID. Args: webmention_id: Webmention database ID Returns: Webmention dict or None if not found """ db = get_db() query = """ SELECT w.*, n.slug as target_slug FROM webmentions w LEFT JOIN notes n ON w.target_note_id = n.id WHERE w.id = ? """ row = db.execute(query, (webmention_id,)).fetchone() if not row: return None wm = dict(row) # Parse timestamps if isinstance(wm.get('received_at'), str): wm['received_at'] = datetime.fromisoformat(wm['received_at'].replace('Z', '+00:00')) if wm.get('validated_at') and isinstance(wm['validated_at'], str): wm['validated_at'] = datetime.fromisoformat(wm['validated_at'].replace('Z', '+00:00')) return wm def count_webmentions(status_filter: Optional[str] = None) -> int: """ Count webmentions, optionally filtered by status. Args: status_filter: Filter by moderation_status Returns: Count of webmentions """ db = get_db() if status_filter: query = "SELECT COUNT(*) FROM webmentions WHERE moderation_status = ?" result = db.execute(query, (status_filter,)).fetchone() else: query = "SELECT COUNT(*) FROM webmentions" result = db.execute(query).fetchone() return result[0] if result else 0 def update_webmention_status(webmention_ids: List[int], status: str) -> int: """ Update moderation status for multiple webmentions. Args: webmention_ids: List of webmention IDs status: New moderation_status ('approved', 'hidden', 'pending') Returns: Number of webmentions updated """ if not webmention_ids: return 0 if status not in ('approved', 'hidden', 'pending'): raise ValueError(f"Invalid status: {status}") db = get_db() # Use parameterized query with placeholders placeholders = ','.join('?' * len(webmention_ids)) query = f""" UPDATE webmentions SET moderation_status = ?, updated_at = CURRENT_TIMESTAMP WHERE id IN ({placeholders}) """ # Notes whose displayed mentions change (valid ones switching between # approved and not) changed_notes = _visibility_changes( db, webmention_ids, "validation_status = 'valid' AND (moderation_status = 'approved') != (? = 'approved')", [status], ) params = [status] + webmention_ids cursor = db.execute(query, params) db.commit() _notify_responses_changed(changed_notes) return cursor.rowcount def delete_webmentions(webmention_ids: List[int]) -> int: """ Delete webmentions by IDs. Args: webmention_ids: List of webmention IDs to delete Returns: Number of webmentions deleted """ if not webmention_ids: return 0 db = get_db() changed_notes = _visibility_changes( db, webmention_ids, "validation_status = 'valid' AND moderation_status = 'approved'", [], ) placeholders = ','.join('?' * len(webmention_ids)) query = f"DELETE FROM webmentions WHERE id IN ({placeholders})" cursor = db.execute(query, webmention_ids) db.commit() _notify_responses_changed(changed_notes) return cursor.rowcount def _visibility_changes(db, webmention_ids: List[int], condition: str, params: list) -> List[int]: """Target note IDs of the given webmentions that match condition""" placeholders = ','.join('?' * len(webmention_ids)) rows = db.execute( f"SELECT DISTINCT target_note_id FROM webmentions " f"WHERE id IN ({placeholders}) AND {condition}", list(webmention_ids) + params, ).fetchall() return [row[0] for row in rows] def _notify_responses_changed(note_ids: List[int]) -> None: """Re-send webmentions for notes whose shown responses changed (best effort)""" if not note_ids: return try: from starpunk.webmention_sender import note_responses_changed note_responses_changed(Path(current_app.config['DATABASE_PATH']), note_ids) except Exception as e: current_app.logger.warning(f"Failed to re-send webmentions after moderation: {e}") def get_reply_context(url: str) -> Optional[Dict[str, Any]]: """ Get cached reply context for a URL. Args: url: The in_reply_to URL Returns: Reply context dict or None if not found """ db = get_db() query = "SELECT * FROM reply_contexts WHERE url = ?" row = db.execute(query, (url,)).fetchone() if not row: return None context = dict(row) # Parse timestamps if context.get('fetched_at') and isinstance(context['fetched_at'], str): context['fetched_at'] = datetime.fromisoformat(context['fetched_at'].replace('Z', '+00:00')) if context.get('created_at') and isinstance(context['created_at'], str): context['created_at'] = datetime.fromisoformat(context['created_at'].replace('Z', '+00:00')) if context.get('published_at') and isinstance(context['published_at'], str): context['published_at'] = datetime.fromisoformat(context['published_at'].replace('Z', '+00:00')) return context