97 lines
2.6 KiB
Python
97 lines
2.6 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import json
|
||
|
|
from enum import Enum
|
||
|
|
from typing import ClassVar, Literal, Union
|
||
|
|
from uuid import UUID
|
||
|
|
|
||
|
|
from pydantic import BaseModel, ConfigDict, Field
|
||
|
|
|
||
|
|
|
||
|
|
class AttachmentType(str, Enum):
|
||
|
|
DOCUMENT = "document"
|
||
|
|
REMINDER = "reminder"
|
||
|
|
|
||
|
|
|
||
|
|
class ScheduleItemMetadataAttachment(BaseModel):
|
||
|
|
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||
|
|
|
||
|
|
name: str
|
||
|
|
type: AttachmentType
|
||
|
|
visible_to: list[UUID] = Field(default_factory=list)
|
||
|
|
url: str | None = None
|
||
|
|
note: str | None = None
|
||
|
|
content: str | None = None
|
||
|
|
|
||
|
|
|
||
|
|
class ScheduleItemMetadata(BaseModel):
|
||
|
|
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||
|
|
|
||
|
|
color: str | None = Field(default=None, pattern=r"^#[0-9A-Fa-f]{6}$")
|
||
|
|
location: str | None = None
|
||
|
|
notes: str | None = None
|
||
|
|
attachments: list[ScheduleItemMetadataAttachment] = Field(default_factory=list)
|
||
|
|
reminder_minutes: int | None = Field(default=None, ge=0, le=10080)
|
||
|
|
version: Literal[1] = 1
|
||
|
|
|
||
|
|
|
||
|
|
class ScheduleItemStatus(str, Enum):
|
||
|
|
ACTIVE = "active"
|
||
|
|
COMPLETED = "completed"
|
||
|
|
CANCELED = "canceled"
|
||
|
|
ARCHIVED = "archived"
|
||
|
|
|
||
|
|
|
||
|
|
class ScheduleItemSourceType(str, Enum):
|
||
|
|
MANUAL = "manual"
|
||
|
|
IMPORTED = "imported"
|
||
|
|
AGENT_GENERATED = "agent_generated"
|
||
|
|
|
||
|
|
|
||
|
|
class CalendarInviteContent(BaseModel):
|
||
|
|
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||
|
|
|
||
|
|
type: Literal["invite"]
|
||
|
|
permission: int = Field(..., description="权限: 1=view, 4=edit, 8=invite")
|
||
|
|
action: Literal["pending"] = "pending"
|
||
|
|
|
||
|
|
|
||
|
|
class CalendarUpdateContent(BaseModel):
|
||
|
|
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||
|
|
|
||
|
|
type: Literal["update"]
|
||
|
|
title: str = Field(..., description="事件标题")
|
||
|
|
action: Literal["updated"] = "updated"
|
||
|
|
|
||
|
|
|
||
|
|
class CalendarDeleteContent(BaseModel):
|
||
|
|
model_config: ClassVar[ConfigDict] = ConfigDict(extra="forbid")
|
||
|
|
|
||
|
|
type: Literal["delete"]
|
||
|
|
title: str = Field(..., description="事件标题")
|
||
|
|
action: Literal["deleted"] = "deleted"
|
||
|
|
|
||
|
|
|
||
|
|
CalendarContent = Union[
|
||
|
|
CalendarInviteContent,
|
||
|
|
CalendarUpdateContent,
|
||
|
|
CalendarDeleteContent,
|
||
|
|
]
|
||
|
|
|
||
|
|
|
||
|
|
def parse_calendar_content(content: str | None) -> CalendarContent | None:
|
||
|
|
if not content:
|
||
|
|
return None
|
||
|
|
try:
|
||
|
|
data = json.loads(content)
|
||
|
|
content_type = data.get("type")
|
||
|
|
if content_type == "invite":
|
||
|
|
return CalendarInviteContent(**data)
|
||
|
|
if content_type == "update":
|
||
|
|
return CalendarUpdateContent(**data)
|
||
|
|
if content_type == "delete":
|
||
|
|
return CalendarDeleteContent(**data)
|
||
|
|
raise ValueError(f"Unknown calendar content type: {content_type}")
|
||
|
|
except Exception:
|
||
|
|
return None
|