Complete LinkSyncServer and LinkSyncExtension implementation

LinkSyncServer:
- Fix app.py imports, add CORS middleware, lifespan events
- Create api/routes.py router aggregator
- Create config/settings.py for centralized configuration
- Rewrite models/base.py with proper relationships and serialization
- Rewrite all API endpoints with real DB integration (auth, links, collections, sync, queries, tags)
- Add admin endpoints (user management, stats, audit log)
- Complete query parser with recursive descent and proper precedence
- Complete query executor with set operations and field filters
- Set up Alembic migrations with initial schema
- Create web interface (templates, CSS, JS)
- Add 42 passing tests (auth, links, collections, queries)
- Add deploy.ps1 and deploy.sh scripts
- Update README with deployment workflow

LinkSyncExtension:
- Create utils/api.js (REST client with retries, auth, error handling)
- Create utils/sync.js (3 sync modes + conflict detection)
- Create utils/collection.js (collection management)
- Create utils/query-engine.js (client-side query parser)
- Rewrite background.js (sync loop, bookmark events, message routing)
- Rewrite popup.js (tabs, settings modal, notifications, CRUD)
- Update popup.html (tabbed interface, query builder, modal)
- Update popup.css (full redesign)
- Create content/content.js (page metadata extraction)
- Create options.html/js (dedicated settings page)
- Generate icons (48x48, 96x96)
- Update manifest.json (host permissions, content scripts, options)
- Create AGENTS.md
This commit is contained in:
DavidSaylor
2026-05-19 13:21:26 -05:00
parent c5d3912070
commit 09d30427f4
54 changed files with 5918 additions and 3177 deletions

View File

@@ -1,348 +1,61 @@
"""
LinkSyncServer - Link CRUD Endpoints with SQLAlchemy
LinkSyncServer - Link CRUD Endpoints
"""
from fastapi import APIRouter, Depends, HTTPException, status, Query, Request
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy import func, or_
from typing import List, Optional
import uuid
import logging
import hashlib
import uuid
from typing import List, Optional
from models.base import Base, Bookmark, User, AuditLog, get_engine, create_engine
from fastapi import APIRouter, HTTPException, Query, Request, status
from pydantic import BaseModel, Field
import os
from sqlalchemy import or_
from config.settings import settings
from models.base import AuditLog, Bookmark, User, get_session
router = APIRouter(prefix="/api/links", tags=["Links"])
# Logging
logger = logging.getLogger(__name__)
class BookmarkCreate(BaseModel):
url: str = Field(..., description="Bookmark URL")
title: str = Field(..., min_length=1, max_length=255, description="Bookmark title")
description: Optional[str] = Field(None, max_length=500, description="Optional description")
notes: Optional[str] = Field(None, max_length=2000, description="Optional notes")
tags: Optional[List[str]] = Field(default_factory=list, description="List of tag names")
favicon_url: Optional[str] = Field(None, max_length=512, description="Favicon URL")
path: Optional[str] = Field(None, max_length=512, description="Folder path")
visit_count: int = Field(ge=0, description="Visit counter")
is_bookmarked: bool = Field(default=False, description="Bookmark flag")
description: Optional[str] = Field(None, max_length=500)
notes: Optional[str] = Field(None, max_length=2000)
tags: Optional[List[str]] = Field(default_factory=list)
favicon_url: Optional[str] = Field(None, max_length=512)
path: Optional[str] = Field(None, max_length=512)
visit_count: int = Field(0, ge=0)
is_bookmarked: bool = Field(default=False)
class BookmarkUpdate(BaseModel):
url: Optional[str] = Field(None, description="New URL")
url: Optional[str] = None
title: Optional[str] = Field(None, min_length=1, max_length=255)
description: Optional[str] = Field(None, max_length=500)
notes: Optional[str] = Field(None, max_length=2000)
tags: Optional[List[str]] = Field(None)
tags: Optional[List[str]] = None
favicon_url: Optional[str] = Field(None, max_length=512)
path: Optional[str] = Field(None, max_length=512)
visit_count: Optional[int] = Field(None, ge=0)
is_bookmarked: Optional[bool] = None
class BookmarkResponse(BaseModel):
id: str
url: str
title: str
description: Optional[str]
notes: Optional[str]
tags: List[str]
favicon_url: Optional[str]
path: Optional[str]
created_at: str
updated_at: str
visit_count: int
is_bookmarked: bool
source_set_id: Optional[str]
user_id: Optional[str]
def get_db_session():
"""Get database session."""
try:
return sessionmaker(get_engine())()
except Exception:
return None
def get_current_user(request: Request):
"""Get current authenticated user."""
SECRET_KEY = os.environ.get("SECRET_KEY")
auth_header = request.headers.get("Authorization") or request.headers.get("authorization")
if auth_header and auth_header.startswith("Bearer "):
def get_current_user_id(request: Request) -> Optional[str]:
auth_header = request.headers.get("Authorization", "")
if auth_header.startswith("Bearer "):
token = auth_header[7:]
try:
import jwt
payload = jwt.decode(token, SECRET_KEY, algorithms=["HS256"])
return {"username": payload.get("sub"), "id": payload.get("sub")}
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
return payload.get("sub")
except Exception:
pass
return {"username": "guest"}
return None
@router.get("/", response_model=List[BookmarkResponse])
async def list_bookmarks(
limit: int = Query(20, le=100, ge=1, description="Number of results per page"),
offset: int = Query(0, ge=0, description="Offset for pagination"),
search: Optional[str] = Query(None, description="Search query"),
tags_filter: Optional[List[str]] = Query(None, description="Filter by tags"),
path_filter: Optional[str] = Query(None, description="Filter by folder path")
):
"""List all bookmarks with optional filters."""
db = get_db_session()
if not db:
return []
query = Bookmark.query
# Search filter
if search:
query = query.filter((Bookmark.title.contains(search)) |
(Bookmark.description.contains(search)) |
(Bookmark.url.contains(search)))
# Tag filter
if tags_filter:
or_clause = or_(*[Bookmark.tags.contains(tag) for tag in tags_filter])
query = query.filter(or_clause)
# Path filter
if path_filter:
query = query.filter(Bookmark.path.contains(path_filter))
bookmarks = query.limit(limit).offset(offset).all()
return bookmarks
@router.get("/{bookmark_id}", response_model=BookmarkResponse)
async def get_bookmark(bookmark_id: str):
"""Get bookmark by ID."""
db = get_db_session()
if not db:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Bookmark not found")
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Bookmark not found")
return bookmark
@router.post("/", response_model=BookmarkResponse, status_code=status.HTTP_201_CREATED)
async def create_bookmark(data: BookmarkCreate, request: Request):
"""Create new bookmark."""
db = get_db_session()
if not db:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Database unavailable")
bookmark = Bookmark(
url=data.url,
title=data.title,
description=data.description,
notes=data.notes,
tags=data.tags or [],
favicon_url=data.favicon_url,
path=data.path,
visit_count=data.visit_count,
is_bookmarked=data.is_bookmarked
)
bookmark_id = f"{data.url[:20]}-{uuid.uuid4()[:8]}"
bookmark = db.add(bookmark)
db.commit()
db.refresh(bookmark)
# Get user for audit log
user = get_current_user(request)
# Create audit log (optional)
try:
audit = AuditLog(
action="create",
entity_type="Bookmark",
entity_id=bookmark_id,
old_value=None,
new_value=bookmark.dict(),
user_id=user.get("id")
)
db.add(audit)
db.commit()
except Exception:
pass
return bookmark
@router.put("/{bookmark_id}", response_model=BookmarkResponse)
async def update_bookmark(
bookmark_id: str,
data: BookmarkUpdate,
request: Request
):
"""Update bookmark."""
db = get_db_session()
if not db:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Database unavailable")
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Bookmark not found")
# Update fields
for field_name, value in data.dict().items():
if value is not None:
setattr(bookmark, field_name, value)
db.commit()
db.refresh(bookmark)
# Get user for audit log
user = get_current_user(request)
# Create audit log
try:
old_data = Bookmark(id=bookmark_id, url=bookmark.url, title=bookmark.title).dict()
audit = AuditLog(
action="update",
entity_type="Bookmark",
entity_id=bookmark_id,
old_value=old_data,
new_value=bookmark.dict(),
user_id=user.get("id")
)
db.add(audit)
db.commit()
except Exception:
pass
return bookmark
@router.delete("/{bookmark_id}", response_model=dict)
async def delete_bookmark(bookmark_id: str, request: Request):
"""Delete bookmark."""
db = get_db_session()
if not db:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Database unavailable")
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Bookmark not found")
db.delete(bookmark)
db.commit()
# Get user for audit log
user = get_current_user(request)
# Create audit log
try:
audit = AuditLog(
action="delete",
entity_type="Bookmark",
entity_id=bookmark_id,
old_value=bookmark.dict(),
new_value=None,
user_id=user.get("id")
)
db.add(audit)
db.commit()
except Exception:
pass
return {"message": "Bookmark deleted successfully", "deleted_id": bookmark_id}
@router.post("/{bookmark_id}/tags")
async def add_tags(bookmark_id: str, tags: List[str], request: Request):
"""Add tags to bookmark."""
db = get_db_session()
if not db:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Database unavailable")
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Bookmark not found")
for tag in tags:
if tag.lower() not in [t.lower() for t in bookmark.tags]:
bookmark.tags.append(tag)
db.commit()
db.refresh(bookmark)
return bookmark
@router.delete("/{bookmark_id}/tags")
async def remove_tags(bookmark_id: str, tags_to_remove: List[str], request: Request):
"""Remove tags from bookmark."""
db = get_db_session()
if not db:
raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail="Database unavailable")
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Bookmark not found")
bookmark.tags = [t for t in bookmark.tags if t.lower() not in [tag.lower() for tag in tags_to_remove]]
db.commit()
db.refresh(bookmark)
return bookmark
@router.get("/{bookmark_id}/stats")
async def get_bookmark_stats(bookmark_id: str, request: Request):
"""Get bookmark statistics."""
db = get_db_session()
if not db:
return {}
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Bookmark not found")
# Get visit count
visits = db.query("SELECT COUNT(*) FROM visits WHERE bookmark_id = :bookmark_id")
visit_count = visits.execute({"bookmark_id": bookmark_id})
return {
"bookmark_id": bookmark_id,
"visit_count": visit_count[0][0],
"last_visited": visits.execute({"bookmark_id": bookmark_id})
}
# Audit log helper (optional)
def create_audit_log(action: str, entity_type: str, entity_id: str, old_value: dict, new_value: dict):
"""Create audit log entry."""
db = get_db_session()
if not db:
return
def log_audit(db, action: str, entity_type: str, entity_id: str, user_id: Optional[str], old_value=None, new_value=None):
try:
audit = AuditLog(
action=action,
@@ -350,9 +63,162 @@ def create_audit_log(action: str, entity_type: str, entity_id: str, old_value: d
entity_id=entity_id,
old_value=old_value,
new_value=new_value,
ip_address=request.client.host if hasattr(request, 'client') and hasattr(request.client, 'host') else None
user_id=user_id,
)
db.add(audit)
db.commit()
except Exception:
pass
db.rollback()
@router.get("/", response_model=List[dict])
async def list_bookmarks(
limit: int = Query(20, le=100, ge=1),
offset: int = Query(0, ge=0),
search: Optional[str] = Query(None),
tags_filter: Optional[List[str]] = Query(None),
path_filter: Optional[str] = Query(None),
):
db = get_session()
try:
query = db.query(Bookmark)
if search:
query = query.filter(
or_(
Bookmark.title.ilike(f"%{search}%"),
Bookmark.description.ilike(f"%{search}%"),
Bookmark.url.ilike(f"%{search}%"),
)
)
if tags_filter:
for tag in tags_filter:
query = query.filter(Bookmark.tags.contains(tag))
if path_filter:
query = query.filter(Bookmark.path.ilike(f"%{path_filter}%"))
bookmarks = query.order_by(Bookmark.created_at.desc()).offset(offset).limit(limit).all()
return [b.to_dict() for b in bookmarks]
finally:
db.close()
@router.get("/{bookmark_id}", response_model=dict)
async def get_bookmark(bookmark_id: str):
db = get_session()
try:
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=404, detail="Bookmark not found")
return bookmark.to_dict()
finally:
db.close()
@router.post("/", response_model=dict, status_code=status.HTTP_201_CREATED)
async def create_bookmark(data: BookmarkCreate, request: Request):
db = get_session()
try:
user_id = get_current_user_id(request)
bookmark = Bookmark(
id=str(uuid.uuid4()),
url=data.url,
title=data.title,
description=data.description,
notes=data.notes,
tags=data.tags or [],
favicon_url=data.favicon_url,
path=data.path,
visit_count=data.visit_count,
is_bookmarked=data.is_bookmarked,
user_id=user_id,
)
db.add(bookmark)
db.commit()
db.refresh(bookmark)
log_audit(db, "create", "Bookmark", bookmark.id, user_id, new_value=bookmark.to_dict())
return bookmark.to_dict()
finally:
db.close()
@router.put("/{bookmark_id}", response_model=dict)
async def update_bookmark(bookmark_id: str, data: BookmarkUpdate, request: Request):
db = get_session()
try:
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=404, detail="Bookmark not found")
old_value = bookmark.to_dict()
update_data = data.model_dump(exclude_unset=True)
for field, value in update_data.items():
setattr(bookmark, field, value)
db.commit()
db.refresh(bookmark)
user_id = get_current_user_id(request)
log_audit(db, "update", "Bookmark", bookmark_id, user_id, old_value=old_value, new_value=bookmark.to_dict())
return bookmark.to_dict()
finally:
db.close()
@router.delete("/{bookmark_id}", response_model=dict)
async def delete_bookmark(bookmark_id: str, request: Request):
db = get_session()
try:
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=404, detail="Bookmark not found")
old_value = bookmark.to_dict()
db.delete(bookmark)
db.commit()
user_id = get_current_user_id(request)
log_audit(db, "delete", "Bookmark", bookmark_id, user_id, old_value=old_value)
return {"message": "Bookmark deleted successfully", "deleted_id": bookmark_id}
finally:
db.close()
class TagList(BaseModel):
tags: List[str]
@router.post("/{bookmark_id}/tags", response_model=dict)
async def add_tags(bookmark_id: str, data: TagList, request: Request):
db = get_session()
try:
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=404, detail="Bookmark not found")
current_tags = list(bookmark.tags or [])
current_lower = [t.lower() for t in current_tags]
for tag in data.tags:
if tag.lower() not in current_lower:
current_tags.append(tag)
current_lower.append(tag.lower())
bookmark.tags = current_tags
db.commit()
db.refresh(bookmark)
return bookmark.to_dict()
finally:
db.close()
@router.delete("/{bookmark_id}/tags", response_model=dict)
async def remove_tags(bookmark_id: str, data: TagList, request: Request):
db = get_session()
try:
bookmark = db.query(Bookmark).filter(Bookmark.id == bookmark_id).first()
if not bookmark:
raise HTTPException(status_code=404, detail="Bookmark not found")
remove_lower = [t.lower() for t in data.tags]
bookmark.tags = [t for t in (bookmark.tags or []) if t.lower() not in remove_lower]
db.commit()
db.refresh(bookmark)
return bookmark.to_dict()
finally:
db.close()