nomadro/apps/api/services/work_svc/routers/api.py
Eric abdcafb1eb Ship Work access gate, pads/boards upgrades, and locale hydration fix on .com.
Gate /social/work behind join/login/profile; harden Work UX and LanguageSwitcher SSR; deploy portal, admin, and work API.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-08 00:14:39 +08:00

367 lines
10 KiB
Python

from __future__ import annotations
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel, Field
from work_svc import store
router = APIRouter(tags=["work"])
class ProjectIn(BaseModel):
name: str = Field(min_length=1, max_length=200)
description: str = ""
class BoardIn(BaseModel):
project_id: str
name: str = Field(min_length=1, max_length=200)
class ListIn(BaseModel):
name: str = Field(min_length=1, max_length=120)
type: str = "active"
class CardIn(BaseModel):
list_id: str
name: str = Field(min_length=1, max_length=500)
description: str = ""
class MoveIn(BaseModel):
list_id: str
position: int = 0
class CardPatch(BaseModel):
name: str | None = None
description: str | None = None
due_date: str | None = None
clear_due: bool = False
is_due_completed: bool | None = None
cover_color: str | None = None
clear_cover: bool = False
class BoardPatch(BaseModel):
name: str | None = None
background_gradient: str | None = None
show_card_counter: bool | None = None
class AttachmentIn(BaseModel):
name: str = Field(min_length=1, max_length=200)
url: str = Field(min_length=1, max_length=2000)
class ListMoveIn(BaseModel):
position: int = 0
class LabelToggleIn(BaseModel):
label_id: str
class TaskIn(BaseModel):
name: str = Field(min_length=1, max_length=500)
class CommentIn(BaseModel):
text: str = Field(min_length=1, max_length=4000)
author: str = "guest"
class ListPatch(BaseModel):
name: str | None = None
color: str | None = None
class MemberIn(BaseModel):
name: str = Field(min_length=1, max_length=80)
class PadIn(BaseModel):
title: str = Field(min_length=1, max_length=200)
author: str = "guest"
class PadUpdate(BaseModel):
text: str
author: str = "guest"
title: str | None = None
class PadPresenceIn(BaseModel):
author: str = "guest"
sid: str = ""
class PadChatIn(BaseModel):
author: str = "guest"
text: str = Field(min_length=1, max_length=500)
@router.get("/health")
def health():
return {"ok": True, "service": "nomadro-work", "inspiredBy": ["planka", "etherpad"]}
@router.get("/projects")
def projects():
return {"ok": True, "projects": store.list_projects()}
@router.post("/projects")
def post_project(body: ProjectIn):
return {"ok": True, "project": store.create_project(body.name, body.description)}
@router.get("/boards")
def boards(project_id: str | None = None):
return {"ok": True, "boards": store.list_boards(project_id)}
@router.post("/boards")
def post_board(body: BoardIn):
board = store.create_board(body.project_id, body.name)
if not board:
raise HTTPException(400, "bad_project")
return {"ok": True, "board": board}
@router.get("/boards/{board_id}")
def board(board_id: str):
b = store.get_board(board_id)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/lists")
def post_list(board_id: str, body: ListIn):
b = store.add_list(board_id, body.name, body.type)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards")
def post_card(board_id: str, body: CardIn):
b = store.add_card(board_id, body.list_id, body.name, body.description)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards/{card_id}/move")
def post_move(board_id: str, card_id: str, body: MoveIn):
b = store.move_card(board_id, card_id, body.list_id, body.position)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.patch("/boards/{board_id}")
def patch_board(board_id: str, body: BoardPatch):
b = store.update_board(
board_id,
name=body.name,
background_gradient=body.background_gradient,
show_card_counter=body.show_card_counter,
)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.patch("/boards/{board_id}/cards/{card_id}")
def patch_card(board_id: str, card_id: str, body: CardPatch):
b = store.update_card(
board_id,
card_id,
name=body.name,
description=body.description,
due_date=body.due_date,
clear_due=body.clear_due,
is_due_completed=body.is_due_completed,
cover_color=body.cover_color,
clear_cover=body.clear_cover,
)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards/{card_id}/stopwatch/toggle")
def post_stopwatch(board_id: str, card_id: str):
b = store.toggle_stopwatch(board_id, card_id)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards/{card_id}/archive")
def post_archive(board_id: str, card_id: str):
b = store.archive_card(board_id, card_id)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards/{card_id}/attachments")
def post_attachment(board_id: str, card_id: str, body: AttachmentIn):
b = store.add_attachment(board_id, card_id, body.name, body.url)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.delete("/boards/{board_id}/attachments/{attachment_id}")
def remove_attachment(board_id: str, attachment_id: str):
b = store.delete_attachment(board_id, attachment_id)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/lists/{list_id}/move")
def post_list_move(board_id: str, list_id: str, body: ListMoveIn):
b = store.move_list(board_id, list_id, body.position)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards/{card_id}/labels/toggle")
def post_label_toggle(board_id: str, card_id: str, body: LabelToggleIn):
b = store.toggle_card_label(board_id, card_id, body.label_id)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards/{card_id}/tasks")
def post_task(board_id: str, card_id: str, body: TaskIn):
b = store.add_task(board_id, card_id, body.name)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/tasks/{task_id}/toggle")
def post_task_toggle(board_id: str, task_id: str):
b = store.toggle_task(board_id, task_id)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards/{card_id}/comments")
def post_comment(board_id: str, card_id: str, body: CommentIn):
b = store.add_comment(board_id, card_id, body.author, body.text)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.patch("/boards/{board_id}/lists/{list_id}")
def patch_list(board_id: str, list_id: str, body: ListPatch):
b = store.update_list(
board_id, list_id, name=body.name, color=body.color
)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.delete("/boards/{board_id}/lists/{list_id}")
def remove_list(board_id: str, list_id: str):
b = store.delete_list(board_id, list_id)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.delete("/boards/{board_id}/cards/{card_id}")
def remove_card(board_id: str, card_id: str):
b = store.delete_card(board_id, card_id)
if not b:
raise HTTPException(404, "not_found")
return {"ok": True, "board": b}
@router.post("/boards/{board_id}/cards/{card_id}/members/toggle")
def post_member_toggle(board_id: str, card_id: str, body: MemberIn):
b = store.toggle_card_member(board_id, card_id, body.name)
if not b:
raise HTTPException(400, "bad_request")
return {"ok": True, "board": b}
@router.get("/pads")
def pads():
store.ensure_welcome_pad()
return {"ok": True, "pads": store.list_pads()}
@router.post("/pads")
def post_pad(body: PadIn):
return {"ok": True, "pad": store.create_pad(body.title, body.author)}
@router.get("/pads/{pad_id}")
def pad(pad_id: str):
p = store.get_pad(pad_id)
if not p:
raise HTTPException(404, "not_found")
return {
"ok": True,
"pad": p,
"presence": store.pad_list_presence(pad_id),
"chat": store.pad_list_chat(pad_id),
}
@router.put("/pads/{pad_id}")
def put_pad(pad_id: str, body: PadUpdate):
p = store.update_pad(pad_id, body.text, body.author, body.title)
if not p:
raise HTTPException(404, "not_found")
return {"ok": True, "pad": p}
@router.delete("/pads/{pad_id}")
def remove_pad(pad_id: str):
if not store.delete_pad(pad_id):
raise HTTPException(404, "not_found")
return {"ok": True}
@router.post("/pads/{pad_id}/presence")
def post_pad_presence(pad_id: str, body: PadPresenceIn):
if not store.get_pad(pad_id):
raise HTTPException(404, "not_found")
users = store.pad_heartbeat(pad_id, author=body.author, sid=body.sid)
return {"ok": True, "presence": users, "color": store.author_color(body.author)}
@router.get("/pads/{pad_id}/presence")
def get_pad_presence(pad_id: str):
return {"ok": True, "presence": store.pad_list_presence(pad_id)}
@router.get("/pads/{pad_id}/chat")
def get_pad_chat(pad_id: str):
return {"ok": True, "chat": store.pad_list_chat(pad_id)}
@router.post("/pads/{pad_id}/chat")
def post_pad_chat(pad_id: str, body: PadChatIn):
if not store.get_pad(pad_id):
raise HTTPException(404, "not_found")
msg = store.pad_post_chat(pad_id, author=body.author, text=body.text)
if not msg:
raise HTTPException(400, "bad_message")
return {"ok": True, "message": msg, "chat": store.pad_list_chat(pad_id)}