Spaces:
Sleeping

File size: 4,653 Bytes
7c87115
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""Sharing service — shared business logic for notebook sharing and collaboration."""

from typing import TypedDict, Literal, Optional

from ..core.client import NotebookLMClient
from ..core.data_types import ShareStatus, Collaborator
from .errors import ValidationError, ServiceError


class CollaboratorInfo(TypedDict):
    """Collaborator details."""
    email: str
    role: str
    is_pending: bool
    display_name: Optional[str]


class ShareStatusResult(TypedDict):
    """Result of a share status check."""
    notebook_id: str
    is_public: bool
    access_level: str
    public_link: Optional[str]
    collaborators: list[CollaboratorInfo]
    collaborator_count: int


class PublicAccessResult(TypedDict):
    """Result of a public access change."""
    notebook_id: str
    is_public: bool
    public_link: Optional[str]
    message: str


class InviteResult(TypedDict):
    """Result of a collaborator invitation."""
    notebook_id: str
    email: str
    role: str
    message: str


def _collaborator_to_dict(c: Collaborator) -> CollaboratorInfo:
    """Convert a Collaborator dataclass to a dict."""
    return {
        "email": c.email,
        "role": c.role,
        "is_pending": c.is_pending,
        "display_name": c.display_name,
    }


def get_share_status(client: NotebookLMClient, notebook_id: str) -> ShareStatusResult:
    """Get sharing status and collaborators for a notebook.

    Args:
        client: Authenticated NotebookLM client
        notebook_id: Notebook UUID

    Returns:
        ShareStatusResult with collaborators and access info

    Raises:
        ServiceError: If the API call fails
    """
    try:
        status: ShareStatus = client.get_share_status(notebook_id)
        collaborators = [_collaborator_to_dict(c) for c in status.collaborators]
        return {
            "notebook_id": notebook_id,
            "is_public": status.is_public,
            "access_level": status.access_level,
            "public_link": status.public_link,
            "collaborators": collaborators,
            "collaborator_count": len(collaborators),
        }
    except Exception as e:
        raise ServiceError(f"Failed to get share status: {e}")


def set_public_access(
    client: NotebookLMClient,
    notebook_id: str,
    is_public: bool,
) -> PublicAccessResult:
    """Enable or disable public link access.

    Args:
        client: Authenticated NotebookLM client
        notebook_id: Notebook UUID
        is_public: True to enable, False to disable

    Returns:
        PublicAccessResult with link info

    Raises:
        ServiceError: If the API call fails
    """
    try:
        result = client.set_public_access(notebook_id, is_public)
        if is_public:
            return {
                "notebook_id": notebook_id,
                "is_public": True,
                "public_link": result,
                "message": "Public link access enabled.",
            }
        else:
            return {
                "notebook_id": notebook_id,
                "is_public": False,
                "public_link": None,
                "message": "Public link access disabled.",
            }
    except Exception as e:
        raise ServiceError(f"Failed to set public access: {e}")


def invite_collaborator(
    client: NotebookLMClient,
    notebook_id: str,
    email: str,
    role: str = "viewer",
) -> InviteResult:
    """Invite a collaborator by email.

    Args:
        client: Authenticated NotebookLM client
        notebook_id: Notebook UUID
        email: Email address to invite
        role: "viewer" or "editor"

    Returns:
        InviteResult with invitation details

    Raises:
        ValidationError: If role is invalid
        ServiceError: If invitation fails
    """
    clean_role = role.lower()
    if clean_role not in ("viewer", "editor"):
        raise ValidationError(
            f"Invalid role '{role}'. Must be 'viewer' or 'editor'.",
            user_message=f"Role must be 'viewer' or 'editor' (got '{role}')",
        )

    try:
        result = client.add_collaborator(notebook_id, email, clean_role)
        if result:
            return {
                "notebook_id": notebook_id,
                "email": email,
                "role": clean_role,
                "message": f"Invited {email} as {clean_role}.",
            }
        raise ServiceError(
            "Invitation returned falsy result",
            user_message="Invitation may have failed — no confirmation from API.",
        )
    except ServiceError:
        raise
    except Exception as e:
        raise ServiceError(f"Failed to invite collaborator: {e}")