Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /ParisNeo_lollms /backend /routers /notes.py
| # backend/routers/notes.py | |
| from typing import List, Optional | |
| from fastapi import APIRouter, Depends, HTTPException, status | |
| from sqlalchemy.orm import Session, joinedload | |
| from pydantic import BaseModel, Field, EmailStr | |
| from backend.db import get_db | |
| from backend.db.models.user import User as DBUser | |
| from backend.db.models.note import Note as DBNote, NoteGroup as DBNoteGroup | |
| from backend.session import get_current_active_user | |
| from backend.models import UserAuthDetails | |
| from backend.models.personality import PersonalitySendRequest | |
| notes_router = APIRouter( | |
| prefix="/api/notes", | |
| tags=["Notes"], | |
| dependencies=[Depends(get_current_active_user)] | |
| ) | |
| # --- Pydantic Models --- | |
| class NoteGroupCreate(BaseModel): | |
| name: str = Field(..., min_length=1) | |
| parent_id: Optional[str] = None | |
| class NoteGroupUpdate(BaseModel): | |
| name: Optional[str] = None | |
| parent_id: Optional[str] = None | |
| class NoteGroupPublic(BaseModel): | |
| id: str | |
| name: str | |
| parent_id: Optional[str] | |
| created_at: str | |
| updated_at: str | |
| class Config: | |
| from_attributes = True | |
| class NoteCreate(BaseModel): | |
| title: str = Field(..., min_length=1) | |
| content: str = "" | |
| group_id: Optional[str] = None | |
| class NoteUpdate(BaseModel): | |
| title: Optional[str] = None | |
| content: Optional[str] = None | |
| group_id: Optional[str] = None | |
| class NotePublic(BaseModel): | |
| id: str | |
| title: str | |
| content: str | |
| group_id: Optional[str] | |
| created_at: str | |
| updated_at: str | |
| class Config: | |
| from_attributes = True | |
| # --- Group Endpoints --- | |
| def get_note_groups( | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| groups = db.query(DBNoteGroup).filter(DBNoteGroup.owner_user_id == current_user.id).all() | |
| return [ | |
| NoteGroupPublic( | |
| id=g.id, name=g.name, parent_id=g.parent_id, | |
| created_at=g.created_at.isoformat(), updated_at=g.updated_at.isoformat() | |
| ) for g in groups | |
| ] | |
| def create_note_group( | |
| group_data: NoteGroupCreate, | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| new_group = DBNoteGroup( | |
| name=group_data.name, | |
| parent_id=group_data.parent_id, | |
| owner_user_id=current_user.id | |
| ) | |
| db.add(new_group) | |
| db.commit() | |
| db.refresh(new_group) | |
| return NoteGroupPublic( | |
| id=new_group.id, name=new_group.name, parent_id=new_group.parent_id, | |
| created_at=new_group.created_at.isoformat(), updated_at=new_group.updated_at.isoformat() | |
| ) | |
| def update_note_group( | |
| group_id: str, | |
| group_data: NoteGroupUpdate, | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| group = db.query(DBNoteGroup).filter(DBNoteGroup.id == group_id, DBNoteGroup.owner_user_id == current_user.id).first() | |
| if not group: | |
| raise HTTPException(status_code=404, detail="Group not found.") | |
| if group_data.name is not None: | |
| group.name = group_data.name | |
| if group_data.parent_id is not None: | |
| if group_data.parent_id == group.id: | |
| raise HTTPException(status_code=400, detail="Cannot set parent to self.") | |
| group.parent_id = group_data.parent_id | |
| db.commit() | |
| db.refresh(group) | |
| return NoteGroupPublic( | |
| id=group.id, name=group.name, parent_id=group.parent_id, | |
| created_at=group.created_at.isoformat(), updated_at=group.updated_at.isoformat() | |
| ) | |
| def delete_note_group( | |
| group_id: str, | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| group = db.query(DBNoteGroup).filter(DBNoteGroup.id == group_id, DBNoteGroup.owner_user_id == current_user.id).first() | |
| if not group: | |
| raise HTTPException(status_code=404, detail="Group not found.") | |
| # Logic for orphans: if group is deleted, children groups become top-level, notes become ungrouped | |
| # SQLAlchemy cascade might handle notes if configured, but re-parenting groups is manual | |
| children = db.query(DBNoteGroup).filter(DBNoteGroup.parent_id == group_id).all() | |
| for child in children: | |
| child.parent_id = group.parent_id # Move up one level | |
| notes = db.query(DBNote).filter(DBNote.group_id == group_id).all() | |
| for note in notes: | |
| note.group_id = None # Ungroup notes | |
| db.delete(group) | |
| db.commit() | |
| # --- Note Endpoints --- | |
| def get_notes( | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| notes = db.query(DBNote).filter(DBNote.owner_user_id == current_user.id).order_by(DBNote.updated_at.desc()).all() | |
| return [ | |
| NotePublic( | |
| id=n.id, title=n.title, content=n.content, group_id=n.group_id, | |
| created_at=n.created_at.isoformat(), updated_at=n.updated_at.isoformat() | |
| ) for n in notes | |
| ] | |
| def create_note( | |
| note_data: NoteCreate, | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| new_note = DBNote( | |
| title=note_data.title, | |
| content=note_data.content, | |
| group_id=note_data.group_id, | |
| owner_user_id=current_user.id | |
| ) | |
| db.add(new_note) | |
| db.commit() | |
| db.refresh(new_note) | |
| return NotePublic( | |
| id=new_note.id, title=new_note.title, content=new_note.content, group_id=new_note.group_id, | |
| created_at=new_note.created_at.isoformat(), updated_at=new_note.updated_at.isoformat() | |
| ) | |
| def update_note( | |
| note_id: str, | |
| note_data: NoteUpdate, | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| note = db.query(DBNote).filter(DBNote.id == note_id, DBNote.owner_user_id == current_user.id).first() | |
| if not note: | |
| raise HTTPException(status_code=404, detail="Note not found.") | |
| if note_data.title is not None: note.title = note_data.title | |
| if note_data.content is not None: note.content = note_data.content | |
| if note_data.group_id is not None: note.group_id = note_data.group_id | |
| db.commit() | |
| db.refresh(note) | |
| return NotePublic( | |
| id=note.id, title=note.title, content=note.content, group_id=note.group_id, | |
| created_at=note.created_at.isoformat(), updated_at=note.updated_at.isoformat() | |
| ) | |
| async def share_note( | |
| note_id: str, | |
| payload: PersonalitySendRequest, | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| note = db.query(DBNote).filter(DBNote.id == note_id, DBNote.owner_user_id == current_user.id).first() | |
| if not note: | |
| raise HTTPException(status_code=404, detail="Note not found") | |
| target_user = db.query(DBUser).filter(DBUser.username == payload.target_username).first() | |
| if not target_user: | |
| raise HTTPException(status_code=404, detail="Target user not found") | |
| # Tag the sender's copy | |
| share_tag = f" [Shared with {target_user.username}]" | |
| if share_tag not in note.title: | |
| note.title += share_tag | |
| # Create the receiver's copy | |
| new_note = DBNote( | |
| title=f"{note.title.replace(share_tag, '')} (from {current_user.username})", | |
| content=note.content, | |
| owner_user_id=target_user.id | |
| ) | |
| db.add(new_note) | |
| try: | |
| db.commit() | |
| from backend.ws_manager import manager | |
| # 1. Notify Receiver via Toast | |
| manager.send_personal_message_sync({ | |
| "type": "notification", | |
| "data": {"message": f"📝 {current_user.username} shared a note: {new_note.title}", "type": "info", "duration": 5000} | |
| }, target_user.id) | |
| # 2. Force Refresh Receiver's UI | |
| manager.send_personal_message_sync({"type": "data_zone_processed", "data": {"zone": "note"}}, target_user.id) | |
| return {"message": f"Note shared with {payload.target_username}"} | |
| except Exception as e: | |
| db.rollback() | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| def delete_note( | |
| note_id: str, | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| note = db.query(DBNote).filter(DBNote.id == note_id, DBNote.owner_user_id == current_user.id).first() | |
| if not note: | |
| raise HTTPException(status_code=404, detail="Note not found.") | |
| db.delete(note) | |
| db.commit() | |
| class EmailNotesRequest(BaseModel): | |
| note_ids: List[str] | |
| recipient_email: EmailStr | |
| async def email_notes( | |
| payload: EmailNotesRequest, | |
| current_user: UserAuthDetails = Depends(get_current_active_user), | |
| db: Session = Depends(get_db) | |
| ): | |
| from backend.security import send_generic_email | |
| from backend.settings import settings | |
| from backend.routers.files import md2_to_html # Use existing renderer | |
| notes = db.query(DBNote).filter( | |
| DBNote.id.in_(payload.note_ids), | |
| DBNote.owner_user_id == current_user.id | |
| ).all() | |
| if not notes: | |
| raise HTTPException(status_code=404, detail="No valid notes found to email.") | |
| subject = f"Notes shared by {current_user.username}" | |
| if len(notes) == 1: | |
| subject = f"Note: {notes[0].title}" | |
| # 1. Generate HTML Content | |
| html_body = f"<div style='font-family: sans-serif;'>" | |
| html_body += f"<h2 style='color: #2563eb;'>Notes Shared via LoLLMs</h2>" | |
| for note in notes: | |
| html_content = md2_to_html(note.content or "") | |
| html_body += f"<div style='border-top: 2px solid #e5e7eb; margin-top: 20px; padding-top: 10px;'>" | |
| html_body += f"<h3 style='margin-bottom: 5px;'>{note.title}</h3>" | |
| html_body += f"<div style='color: #374151;'>{html_content}</div>" | |
| html_body += f"</div>" | |
| html_body += f"<p style='font-size: 12px; color: #9ca3af; margin-top: 30px;'>Sent from {current_user.username}'s workspace.</p>" | |
| html_body += "</div>" | |
| # SECURITY POLICY: We never send user-generated notes directly from the LoLLMs server. | |
| # This prevents the platform from being used for phishing, harassment, or as an open relay. | |
| # We strictly provide the formatted content for the user to send from their own local client. | |
| # Create a plain-text version for the URL 'body' param | |
| text_body = f"Notes Shared by {current_user.username}\n\n" | |
| for note in notes: | |
| text_body += f"--- {note.title} ---\n{note.content}\n\n" | |
| return { | |
| "manual_mode": True, # Force local handling | |
| "subject": subject, | |
| "body": text_body, | |
| "html": html_body, | |
| "recipient": payload.recipient_email | |
| } | |
Xet Storage Details
- Size:
- 11.3 kB
- Xet hash:
- d803390112caf879b3d6bbd83146848709deb59580893228a5c403f3d4bfe2df
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.