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 <noreply@anthropic.com>
This commit is contained in:
MarekWo
2026-03-01 17:26:37 +01:00
parent 5df10f0ab9
commit d89e276054
2 changed files with 48 additions and 0 deletions
+17
View File
@@ -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
# ================================================================
+31
View File
@@ -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"""