From d89e27605483459e5b24e3c9fb8a2285de80b25e Mon Sep 17 00:00:00 2001 From: MarekWo Date: Sun, 1 Mar 2026 17:26:37 +0100 Subject: [PATCH] feat(api): add advertisement history API endpoint (Task 2.8) Add GET /api/advertisements with optional pubkey filter and limit. Enriches results with contact name lookup from cache. Co-Authored-By: Claude Opus 4.6 --- app/database.py | 17 +++++++++++++++++ app/routes/api.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/app/database.py b/app/database.py index 77600d6..db2f46a 100644 --- a/app/database.py +++ b/app/database.py @@ -414,6 +414,23 @@ class Database: kwargs.get('raw_payload')) ) + def get_advertisements(self, limit: int = 100, public_key: str = None) -> list: + with self._connect() as conn: + if public_key: + rows = conn.execute( + """SELECT * FROM advertisements + WHERE public_key = ? + ORDER BY timestamp DESC LIMIT ?""", + (public_key.lower(), limit) + ).fetchall() + else: + rows = conn.execute( + """SELECT * FROM advertisements + ORDER BY timestamp DESC LIMIT ?""", + (limit,) + ).fetchall() + return [dict(r) for r in rows] + # ================================================================ # Read Status # ================================================================ diff --git a/app/routes/api.py b/app/routes/api.py index 45b9d40..65933f4 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -3275,6 +3275,37 @@ def add_console_history(): }), 500 +@api_bp.route('/advertisements', methods=['GET']) +def get_advertisements(): + """Get advertisement history, optionally filtered by public key.""" + db = _get_db() + if not db: + return jsonify({'success': False, 'error': 'Database not available'}), 503 + + try: + limit = request.args.get('limit', 100, type=int) + public_key = request.args.get('pubkey', None) + + limit = max(1, min(limit, 1000)) + + adverts = db.get_advertisements(limit=limit, public_key=public_key) + + # Enrich with contact name lookup + names = get_all_names() + for adv in adverts: + pk = adv.get('public_key', '') + adv['contact_name'] = names.get(pk, adv.get('name', '')) + + return jsonify({ + 'success': True, + 'advertisements': adverts, + 'count': len(adverts) + }) + except Exception as e: + logger.error(f"Error fetching advertisements: {e}") + return jsonify({'success': False, 'error': str(e)}), 500 + + @api_bp.route('/console/history', methods=['DELETE']) def clear_console_history(): """Clear console command history"""