release / release (release) Canceled after 0s
- mcp_service.free_daily_limit: per service, per user per day (NULL/0 = unlimited) - sys_config free_global_daily_limit: applies to every free service - effective limit = smaller of the two, so a service without its own value still inherits the global one - admin: Pricing tab input, System Settings > Business > Free Call Limits - submission review pages and endpoints (scaffolding, not yet published end to end) - allow LAN IPs in http_utils, add docker-compose.yml
145 lines
4.8 KiB
Python
145 lines
4.8 KiB
Python
"""User submission controller for MCP/Skill/Plugin marketplace."""
|
|
import logging
|
|
import os
|
|
import uuid
|
|
from fastapi import APIRouter, Depends, Request, UploadFile, File, Form, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from services.common.database import get_db
|
|
from services.common.utils.response_utils import ResponseUtils
|
|
from services.common.utils.validation_utils import ValidationUtils
|
|
from services.admin_service.utils.user_utils import UserUtils
|
|
from services.common import error_msg
|
|
from services.common.models.submission import Submission
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter()
|
|
|
|
UPLOAD_DIR = "/tmp/submission_uploads"
|
|
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
|
|
|
|
|
@router.post("/submission", summary="User submit MCP/Skill/Plugin")
|
|
async def create_submission(
|
|
request: Request,
|
|
type: str = Form(...),
|
|
name: str = Form(...),
|
|
description: str = Form(None),
|
|
file: UploadFile = File(None),
|
|
db: Session = Depends(get_db),
|
|
):
|
|
try:
|
|
user_id = UserUtils.get_request_user_id(request)
|
|
except Exception:
|
|
user_id = None
|
|
if not user_id:
|
|
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
|
|
|
if type not in ("mcp", "skill", "plugin"):
|
|
return ResponseUtils.error(error_msg=error_msg.INVALID_PARAMETER)
|
|
|
|
file_path = None
|
|
if file:
|
|
safe_name = f"{uuid.uuid4()}_{file.filename}"
|
|
file_path = os.path.join(UPLOAD_DIR, safe_name)
|
|
with open(file_path, "wb") as f:
|
|
f.write(await file.read())
|
|
|
|
sub = Submission(
|
|
user_id=user_id,
|
|
type=type,
|
|
name=name,
|
|
description=description,
|
|
file_path=file_path,
|
|
status="pending",
|
|
)
|
|
db.add(sub)
|
|
db.commit()
|
|
db.refresh(sub)
|
|
return ResponseUtils.success(data={"id": sub.id, "status": sub.status})
|
|
|
|
|
|
@router.get("/submissions", summary="List user submissions")
|
|
def list_submissions(request: Request, db: Session = Depends(get_db)):
|
|
try:
|
|
user_id = UserUtils.get_request_user_id(request)
|
|
except Exception:
|
|
user_id = None
|
|
if not user_id:
|
|
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
|
|
|
subs = db.query(Submission).filter(Submission.user_id == user_id).order_by(Submission.created_at.desc()).all()
|
|
data = [
|
|
{
|
|
"id": s.id,
|
|
"type": s.type,
|
|
"name": s.name,
|
|
"description": s.description,
|
|
"status": s.status,
|
|
"created_at": s.created_at.isoformat() if s.created_at else None,
|
|
"review_note": s.review_note,
|
|
}
|
|
for s in subs
|
|
]
|
|
return ResponseUtils.success(data=data)
|
|
|
|
|
|
# Admin endpoints
|
|
@router.get("/admin/submissions", summary="Admin list submissions")
|
|
def admin_list_submissions(request: Request, status: str = None, db: Session = Depends(get_db)):
|
|
if not UserUtils.is_admin(request):
|
|
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
|
|
|
q = db.query(Submission)
|
|
if status:
|
|
q = q.filter(Submission.status == status)
|
|
subs = q.order_by(Submission.created_at.desc()).all()
|
|
data = [
|
|
{
|
|
"id": s.id,
|
|
"user_id": s.user_id,
|
|
"type": s.type,
|
|
"name": s.name,
|
|
"description": s.description,
|
|
"file_path": s.file_path,
|
|
"status": s.status,
|
|
"created_at": s.created_at.isoformat() if s.created_at else None,
|
|
"review_note": s.review_note,
|
|
}
|
|
for s in subs
|
|
]
|
|
return ResponseUtils.success(data=data)
|
|
|
|
|
|
@router.put("/admin/submission/{sub_id}/approve", summary="Approve submission")
|
|
def approve_submission(sub_id: str, request: Request, db: Session = Depends(get_db)):
|
|
if not UserUtils.is_admin(request):
|
|
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
|
|
|
sub = db.query(Submission).filter(Submission.id == sub_id).first()
|
|
if not sub:
|
|
return ResponseUtils.error(error_msg=error_msg.NOT_FOUND)
|
|
sub.status = "approved"
|
|
sub.reviewer_id = UserUtils.get_request_user_id(request)
|
|
from datetime import datetime
|
|
sub.reviewed_at = datetime.utcnow()
|
|
db.commit()
|
|
return ResponseUtils.success()
|
|
|
|
|
|
@router.put("/admin/submission/{sub_id}/reject", summary="Reject submission")
|
|
def reject_submission(sub_id: str, request: Request, body: dict = None, db: Session = Depends(get_db)):
|
|
if not UserUtils.is_admin(request):
|
|
return ResponseUtils.error(error_msg=error_msg.NO_PERMISSION)
|
|
|
|
sub = db.query(Submission).filter(Submission.id == sub_id).first()
|
|
if not sub:
|
|
return ResponseUtils.error(error_msg=error_msg.NOT_FOUND)
|
|
sub.status = "rejected"
|
|
sub.reviewer_id = UserUtils.get_request_user_id(request)
|
|
from datetime import datetime
|
|
sub.reviewed_at = datetime.utcnow()
|
|
if body:
|
|
sub.review_note = body.get("note")
|
|
db.commit()
|
|
return ResponseUtils.success()
|