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:
@@ -1,233 +1,258 @@
|
||||
"""
|
||||
LinkSyncServer - Collection CRUD Endpoints with SQLAlchemy
|
||||
LinkSyncServer - Collection CRUD Endpoints
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Query, Request
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import func, and_, or_, exists
|
||||
from typing import List, Optional
|
||||
import uuid
|
||||
import logging
|
||||
import uuid
|
||||
from typing import List, Optional
|
||||
|
||||
from models.base import Base, Bookmark, Collection, AuditLog, get_engine, sessionmaker
|
||||
from fastapi import APIRouter, HTTPException, Query, Request, status
|
||||
from pydantic import BaseModel, Field
|
||||
import os
|
||||
from sqlalchemy import and_, or_
|
||||
|
||||
from models.base import AuditLog, Bookmark, Collection, CollectionBookmark, get_session
|
||||
from queries.executor import execute_query
|
||||
|
||||
router = APIRouter(prefix="/api/collections", tags=["Collections"])
|
||||
|
||||
# Logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CollectionCreate(BaseModel):
|
||||
name: str = Field(..., description="Collection name")
|
||||
description: Optional[str] = Field(None, max_length=1024, description="Collection description")
|
||||
query_type: str = Field(default="static", description="Static or dynamic collection")
|
||||
description: Optional[str] = Field(None, max_length=1024)
|
||||
query_type: str = Field(default="static", description="static or dynamic")
|
||||
query_expression: Optional[dict] = Field(None, description="Query expression for dynamic collections")
|
||||
is_public: bool = Field(default=False, description="Is collection public")
|
||||
tags: Optional[List[str]] = Field(default_factory=list, description="Collection tags")
|
||||
is_public: bool = Field(default=False)
|
||||
link_ids: Optional[List[str]] = Field(default_factory=list, description="Link IDs for static collections")
|
||||
|
||||
|
||||
class CollectionUpdate(BaseModel):
|
||||
name: Optional[str] = Field(None, max_length=255)
|
||||
name: Optional[str] = Field(None, max_length=200)
|
||||
description: Optional[str] = Field(None, max_length=1024)
|
||||
query_type: Optional[str] = Field(None)
|
||||
query_expression: Optional[dict] = Field(None)
|
||||
query_type: Optional[str] = None
|
||||
query_expression: Optional[dict] = None
|
||||
is_public: Optional[bool] = None
|
||||
tags: Optional[List[str]] = Field(None)
|
||||
|
||||
|
||||
class CollectionResponse(BaseModel):
|
||||
id: str
|
||||
name: str
|
||||
description: Optional[str]
|
||||
query_type: str
|
||||
query_expression: Optional[dict]
|
||||
is_public: bool
|
||||
created_at: str
|
||||
updated_at: str
|
||||
tags: List[str]
|
||||
|
||||
|
||||
def get_db():
|
||||
"""Get database session."""
|
||||
db_session = sessionmaker(get_engine())()
|
||||
return db_session
|
||||
|
||||
|
||||
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")}
|
||||
from config.settings import settings
|
||||
payload = jwt.decode(token, settings.SECRET_KEY, algorithms=["HS256"])
|
||||
return payload.get("sub")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return {"username": "guest"}
|
||||
return None
|
||||
|
||||
|
||||
class CollectionManager:
|
||||
"""Collection management helper."""
|
||||
|
||||
@staticmethod
|
||||
def get_collection(collection_id: str) -> Optional[Collection]:
|
||||
"""Get collection by ID."""
|
||||
db = get_db()
|
||||
try:
|
||||
collection = db.query(Collection).filter(Collection.id == collection_id).first()
|
||||
return collection
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def create_collection(data: CollectionCreate, request: Request) -> Collection:
|
||||
"""Create new collection."""
|
||||
db = get_db()
|
||||
|
||||
def log_audit(db, action, entity_type, entity_id, user_id, old_value=None, new_value=None):
|
||||
try:
|
||||
audit = AuditLog(
|
||||
action=action,
|
||||
entity_type=entity_type,
|
||||
entity_id=entity_id,
|
||||
old_value=old_value,
|
||||
new_value=new_value,
|
||||
user_id=user_id,
|
||||
)
|
||||
db.add(audit)
|
||||
db.commit()
|
||||
except Exception:
|
||||
db.rollback()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[dict])
|
||||
async def list_collections(
|
||||
limit: int = Query(20, le=100, ge=1),
|
||||
offset: int = Query(0, ge=0),
|
||||
request: Request = None,
|
||||
):
|
||||
db = get_session()
|
||||
try:
|
||||
user_id = get_current_user_id(request) if request else None
|
||||
query = db.query(Collection)
|
||||
if user_id:
|
||||
query = query.filter(
|
||||
or_(Collection.created_by == user_id, Collection.is_public == True)
|
||||
)
|
||||
else:
|
||||
query = query.filter(Collection.is_public == True)
|
||||
collections = query.order_by(Collection.created_at.desc()).offset(offset).limit(limit).all()
|
||||
return [c.to_dict() for c in collections]
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.get("/{collection_id}", response_model=dict)
|
||||
async def get_collection(collection_id: str):
|
||||
db = get_session()
|
||||
try:
|
||||
collection = db.query(Collection).filter(Collection.id == collection_id).first()
|
||||
if not collection:
|
||||
raise HTTPException(status_code=404, detail="Collection not found")
|
||||
result = collection.to_dict()
|
||||
if collection.query_type == "static":
|
||||
links = (
|
||||
db.query(CollectionBookmark)
|
||||
.filter(CollectionBookmark.collection_id == collection_id)
|
||||
.all()
|
||||
)
|
||||
result["link_ids"] = [lb.bookmark_id for lb in links]
|
||||
return result
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.post("/", response_model=dict, status_code=status.HTTP_201_CREATED)
|
||||
async def create_collection(data: CollectionCreate, request: Request):
|
||||
db = get_session()
|
||||
try:
|
||||
user_id = get_current_user_id(request)
|
||||
if not user_id:
|
||||
raise HTTPException(status_code=401, detail="Authentication required")
|
||||
|
||||
collection = Collection(
|
||||
id=str(uuid.uuid4()),
|
||||
name=data.name,
|
||||
description=data.description,
|
||||
query_type=data.query_type,
|
||||
query_expression=data.query_expression,
|
||||
is_public=data.is_public,
|
||||
tags=TagCollection(tags=data.tags or []),
|
||||
created_by=user_id,
|
||||
)
|
||||
|
||||
db.add(collection)
|
||||
db.flush()
|
||||
|
||||
if data.query_type == "static" and data.link_ids:
|
||||
for link_id in data.link_ids:
|
||||
cb = CollectionBookmark(collection_id=collection.id, bookmark_id=link_id)
|
||||
db.add(cb)
|
||||
|
||||
db.commit()
|
||||
db.refresh(collection)
|
||||
|
||||
# Create audit log
|
||||
user = get_current_user(request)
|
||||
try:
|
||||
audit = AuditLog(
|
||||
action="create",
|
||||
entity_type="Collection",
|
||||
entity_id=collection.id,
|
||||
old_value=None,
|
||||
new_value=collection.dict(),
|
||||
user_id=user.get("id")
|
||||
)
|
||||
db.add(audit)
|
||||
db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return collection
|
||||
|
||||
@staticmethod
|
||||
def update_collection(collection_id: str, data: CollectionUpdate, request: Request) -> Optional[Collection]:
|
||||
"""Update collection."""
|
||||
db = get_db()
|
||||
|
||||
log_audit(db, "create", "Collection", collection.id, user_id, new_value=collection.to_dict())
|
||||
return collection.to_dict()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.put("/{collection_id}", response_model=dict)
|
||||
async def update_collection(collection_id: str, data: CollectionUpdate, request: Request):
|
||||
db = get_session()
|
||||
try:
|
||||
collection = db.query(Collection).filter(Collection.id == collection_id).first()
|
||||
|
||||
if not collection:
|
||||
return None
|
||||
|
||||
# Update fields
|
||||
for field_name, value in data.dict().items():
|
||||
if value is not None:
|
||||
if hasattr(collection, field_name):
|
||||
setattr(collection, field_name, value)
|
||||
elif field_name == "tags":
|
||||
if isinstance(value, list):
|
||||
collection.tags.add(*value)
|
||||
else:
|
||||
collection.tags.update(str(value))
|
||||
|
||||
raise HTTPException(status_code=404, detail="Collection not found")
|
||||
|
||||
old_value = collection.to_dict()
|
||||
update_data = data.model_dump(exclude_unset=True)
|
||||
for field, value in update_data.items():
|
||||
setattr(collection, field, value)
|
||||
|
||||
db.commit()
|
||||
db.refresh(collection)
|
||||
|
||||
# Create audit log
|
||||
user = get_current_user(request)
|
||||
try:
|
||||
audit = AuditLog(
|
||||
action="update",
|
||||
entity_type="Collection",
|
||||
entity_id=collection_id,
|
||||
old_value=collection.dict(),
|
||||
new_value=collection.dict(),
|
||||
user_id=user.get("id")
|
||||
)
|
||||
db.add(audit)
|
||||
db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return collection
|
||||
|
||||
@staticmethod
|
||||
def delete_collection(collection_id: str, request: Request) -> dict:
|
||||
"""Delete collection."""
|
||||
db = get_db()
|
||||
|
||||
user_id = get_current_user_id(request)
|
||||
log_audit(db, "update", "Collection", collection_id, user_id, old_value=old_value, new_value=collection.to_dict())
|
||||
return collection.to_dict()
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.delete("/{collection_id}", response_model=dict)
|
||||
async def delete_collection(collection_id: str, request: Request):
|
||||
db = get_session()
|
||||
try:
|
||||
collection = db.query(Collection).filter(Collection.id == collection_id).first()
|
||||
|
||||
if not collection:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Collection not found")
|
||||
|
||||
raise HTTPException(status_code=404, detail="Collection not found")
|
||||
|
||||
old_value = collection.to_dict()
|
||||
if collection.query_type == "static":
|
||||
db.query(CollectionBookmark).filter(
|
||||
CollectionBookmark.collection_id == collection_id
|
||||
).delete()
|
||||
db.delete(collection)
|
||||
db.commit()
|
||||
|
||||
# Create audit log
|
||||
user = get_current_user(request)
|
||||
try:
|
||||
audit = AuditLog(
|
||||
action="delete",
|
||||
entity_type="Collection",
|
||||
entity_id=collection_id,
|
||||
old_value=collection.dict(),
|
||||
new_value=None,
|
||||
user_id=user.get("id")
|
||||
)
|
||||
db.add(audit)
|
||||
db.commit()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
user_id = get_current_user_id(request)
|
||||
log_audit(db, "delete", "Collection", collection_id, user_id, old_value=old_value)
|
||||
return {"message": "Collection deleted successfully", "deleted_id": collection_id}
|
||||
|
||||
@staticmethod
|
||||
def get_collection_tags(collection_id: str) -> List[str]:
|
||||
"""Get collection tags."""
|
||||
db = get_db()
|
||||
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.post("/{collection_id}/refresh", response_model=dict)
|
||||
async def refresh_collection(collection_id: str):
|
||||
db = get_session()
|
||||
try:
|
||||
collection = db.query(Collection).filter(Collection.id == collection_id).first()
|
||||
|
||||
if not collection:
|
||||
return []
|
||||
|
||||
return list(collection.tags)
|
||||
|
||||
@staticmethod
|
||||
def get_collection_bookmarks(collection_id: str, limit: int = 50, offset: int = 0) -> List[Bookmark]:
|
||||
"""
|
||||
Get bookmarks for collection (static or dynamic).
|
||||
|
||||
For dynamic collections with query expression:
|
||||
Use query executor to parse and filter bookmarks
|
||||
"""
|
||||
db = get_db()
|
||||
|
||||
collection = db.query(Collection).filter(Collection.id == collection_id).first()
|
||||
|
||||
if not collection:
|
||||
return []
|
||||
|
||||
if collection.query_type == "static":
|
||||
# Static collection: get all bookmarks
|
||||
bookmarks = db.query(Bookmark).filter(Bookmark.collection_id == collection_id).limit(limit).offset(offset).all()
|
||||
raise HTTPException(status_code=404, detail="Collection not found")
|
||||
if collection.query_type != "dynamic":
|
||||
raise HTTPException(status_code=400, detail="Only dynamic collections can be refreshed")
|
||||
|
||||
if collection.query_expression:
|
||||
bookmarks = execute_query(collection.query_expression)
|
||||
else:
|
||||
# Dynamic collection: query expression
|
||||
# TODO: Use query executor to parse expression (executor module)
|
||||
bookmarks = db.query(Bookmark).limit(limit).offset(offset).all()
|
||||
|
||||
return bookmarks
|
||||
bookmarks = []
|
||||
|
||||
return {
|
||||
"collection_id": collection_id,
|
||||
"matched_count": len(bookmarks),
|
||||
"bookmarks": bookmarks,
|
||||
}
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.post("/{collection_id}/add-links", response_model=dict)
|
||||
async def add_links_to_collection(collection_id: str, link_ids: List[str]):
|
||||
db = get_session()
|
||||
try:
|
||||
collection = db.query(Collection).filter(Collection.id == collection_id).first()
|
||||
if not collection:
|
||||
raise HTTPException(status_code=404, detail="Collection not found")
|
||||
if collection.query_type != "static":
|
||||
raise HTTPException(status_code=400, detail="Can only add links to static collections")
|
||||
|
||||
existing = {
|
||||
cb.bookmark_id
|
||||
for cb in db.query(CollectionBookmark)
|
||||
.filter(CollectionBookmark.collection_id == collection_id)
|
||||
.all()
|
||||
}
|
||||
added = 0
|
||||
for link_id in link_ids:
|
||||
if link_id not in existing:
|
||||
db.add(CollectionBookmark(collection_id=collection_id, bookmark_id=link_id))
|
||||
added += 1
|
||||
|
||||
db.commit()
|
||||
return {"message": f"Added {added} links", "added_count": added}
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
@router.delete("/{collection_id}/remove-links", response_model=dict)
|
||||
async def remove_links_from_collection(collection_id: str, link_ids: List[str]):
|
||||
db = get_session()
|
||||
try:
|
||||
collection = db.query(Collection).filter(Collection.id == collection_id).first()
|
||||
if not collection:
|
||||
raise HTTPException(status_code=404, detail="Collection not found")
|
||||
|
||||
removed = (
|
||||
db.query(CollectionBookmark)
|
||||
.filter(
|
||||
CollectionBookmark.collection_id == collection_id,
|
||||
CollectionBookmark.bookmark_id.in_(link_ids),
|
||||
)
|
||||
.delete(synchronize_session=False)
|
||||
)
|
||||
db.commit()
|
||||
return {"message": f"Removed {removed} links", "removed_count": removed}
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
Reference in New Issue
Block a user