-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathactions.py
More file actions
319 lines (282 loc) · 9.69 KB
/
Copy pathactions.py
File metadata and controls
319 lines (282 loc) · 9.69 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"""
Actions API Routes
------------------
POST endpoints for triggering operations.
"""
import logging
from functools import partial
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, Request, status
from app.api.deps import SessionContext, get_session_context
from app.models import (
ScanRequest,
MarkReadRequest,
DeleteScanRequest,
UnsubscribeRequest,
DeleteEmailsRequest,
DeleteBulkRequest,
DownloadEmailsRequest,
CreateLabelRequest,
ApplyLabelRequest,
RemoveLabelRequest,
ArchiveRequest,
MarkImportantRequest,
)
from app.services import (
scan_emails,
get_gmail_service,
sign_out,
unsubscribe_single,
mark_emails_as_read,
scan_senders_for_delete,
delete_emails_by_sender,
delete_emails_bulk_background,
download_emails_background,
create_label,
delete_label,
apply_label_to_senders_background,
remove_label_from_senders_background,
archive_emails_background,
mark_important_background,
)
router = APIRouter(prefix="/api", tags=["Actions"])
logger = logging.getLogger(__name__)
@router.post("/scan")
async def api_scan(
request: ScanRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Start email scan for unsubscribe links."""
filters_dict = (
request.filters.model_dump(exclude_none=True) if request.filters else None
)
background_tasks.add_task(scan_emails, ctx.session, request.limit, filters_dict)
return {"status": "started"}
def _get_request_host_and_scheme(request: Request) -> tuple[str, str]:
forwarded_host = request.headers.get("x-forwarded-host")
forwarded_proto = request.headers.get("x-forwarded-proto")
host = forwarded_host or request.url.hostname or "localhost"
if host and ":" in host and not host.startswith("["):
host = host.split(":", 1)[0]
scheme = (forwarded_proto or request.url.scheme or "http").split(",")[0].strip()
if scheme not in ("http", "https"):
scheme = "http"
return host, scheme
@router.post("/sign-in")
async def api_sign_in(
request: Request,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Trigger OAuth sign-in flow."""
host, scheme = _get_request_host_and_scheme(request)
ctx.session.oauth_host = host
ctx.session.oauth_scheme = scheme
background_tasks.add_task(get_gmail_service, ctx.session, ctx.token_json, host, scheme)
return {"status": "signing_in"}
@router.post("/sign-out")
async def api_sign_out(ctx: SessionContext = Depends(get_session_context)):
"""Sign out and clear credentials."""
try:
return sign_out(ctx.session)
except Exception as e:
logger.exception("Error during sign-out")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to sign out",
) from e
@router.post("/unsubscribe")
async def api_unsubscribe(request: UnsubscribeRequest):
"""Unsubscribe from a single sender."""
try:
return unsubscribe_single(request.domain, request.link)
except Exception as e:
logger.exception("Error during unsubscribe")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to unsubscribe",
) from e
@router.post("/mark-read")
async def api_mark_read(
request: MarkReadRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Mark emails as read."""
filters_dict = (
request.filters.model_dump(exclude_none=True) if request.filters else None
)
background_tasks.add_task(
mark_emails_as_read, ctx.session, request.count, filters_dict
)
return {"status": "started"}
@router.post("/delete-scan")
async def api_delete_scan(
request: DeleteScanRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Scan senders for bulk delete."""
filters_dict = (
request.filters.model_dump(exclude_none=True) if request.filters else None
)
background_tasks.add_task(
scan_senders_for_delete, ctx.session, request.limit, filters_dict
)
return {"status": "started"}
@router.post("/delete-emails")
async def api_delete_emails(
request: DeleteEmailsRequest,
ctx: SessionContext = Depends(get_session_context),
):
"""Delete emails from a specific sender."""
if not request.sender or not request.sender.strip():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Sender email is required",
)
try:
return delete_emails_by_sender(ctx.session, request.sender)
except Exception as e:
logger.exception("Error deleting emails")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to delete emails",
) from e
@router.post("/delete-emails-bulk")
async def api_delete_emails_bulk(
request: DeleteBulkRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Delete emails from multiple senders (background task with progress)."""
background_tasks.add_task(
delete_emails_bulk_background, ctx.session, request.senders
)
return {"status": "started"}
@router.post("/download-emails")
async def api_download_emails(
request: DownloadEmailsRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Start downloading email metadata for selected senders."""
# Note: Empty list is allowed - service function will handle it gracefully
background_tasks.add_task(download_emails_background, ctx.session, request.senders)
return {"status": "started"}
# ----- Label Management Endpoints -----
@router.post("/labels")
async def api_create_label(
request: CreateLabelRequest,
ctx: SessionContext = Depends(get_session_context),
):
"""Create a new Gmail label."""
try:
return create_label(ctx.session, request.name)
except Exception as e:
logger.exception("Error creating label")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to create label",
) from e
@router.delete("/labels/{label_id}")
async def api_delete_label(
label_id: str, ctx: SessionContext = Depends(get_session_context)
):
"""Delete a Gmail label."""
if not label_id or not label_id.strip():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Label ID is required",
)
try:
return delete_label(ctx.session, label_id)
except Exception as e:
logger.exception("Error deleting label")
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to delete label",
) from e
@router.post("/apply-label")
async def api_apply_label(
request: ApplyLabelRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Apply a label to emails from selected senders."""
if not request.label_id or not request.label_id.strip():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Label ID is required",
)
if not request.senders:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="At least one sender is required",
)
background_tasks.add_task(
apply_label_to_senders_background,
ctx.session,
request.label_id,
request.senders,
)
return {"status": "started"}
@router.post("/remove-label")
async def api_remove_label(
request: RemoveLabelRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Remove a label from emails from selected senders."""
if not request.label_id or not request.label_id.strip():
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Label ID is required",
)
if not request.senders:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="At least one sender is required",
)
background_tasks.add_task(
remove_label_from_senders_background,
ctx.session,
request.label_id,
request.senders,
)
return {"status": "started"}
@router.post("/archive")
async def api_archive(
request: ArchiveRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Archive emails from selected senders (remove from inbox)."""
if not request.senders:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="At least one sender is required",
)
background_tasks.add_task(archive_emails_background, ctx.session, request.senders)
return {"status": "started"}
@router.post("/mark-important")
async def api_mark_important(
request: MarkImportantRequest,
background_tasks: BackgroundTasks,
ctx: SessionContext = Depends(get_session_context),
):
"""Mark/unmark emails from selected senders as important."""
if not request.senders:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="At least one sender is required",
)
background_tasks.add_task(
partial(
mark_important_background,
ctx.session,
request.senders,
important=request.important,
)
)
return {"status": "started"}