move and rename module
This commit is contained in:
1
src/mail/actions/__init__.py
Normal file
1
src/mail/actions/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
# Initialize the actions subpackage
|
||||
39
src/mail/actions/archive.py
Normal file
39
src/mail/actions/archive.py
Normal file
@@ -0,0 +1,39 @@
|
||||
from textual import work
|
||||
from src.services.himalaya import client as himalaya_client
|
||||
|
||||
|
||||
@work(exclusive=True)
|
||||
async def archive_current(app):
|
||||
"""Archive the current message."""
|
||||
if not app.current_message_id:
|
||||
app.show_status("No message selected to archive.", "error")
|
||||
return
|
||||
|
||||
# Store the current message ID and index
|
||||
current_message_id = app.current_message_id
|
||||
current_index = app.current_message_index
|
||||
|
||||
# Find the next message to display after archiving
|
||||
next_id, next_idx = app.message_store.find_next_valid_id(current_index)
|
||||
if next_id is None or next_idx is None:
|
||||
# If there's no next message, try to find a previous one
|
||||
next_id, next_idx = app.message_store.find_prev_valid_id(current_index)
|
||||
|
||||
# Archive the message using our Himalaya client module
|
||||
success = await himalaya_client.archive_messages([str(current_message_id)])
|
||||
|
||||
if success:
|
||||
app.show_status(f"Message {current_message_id} archived.", "success")
|
||||
app.message_store.remove_envelope(current_message_id)
|
||||
app.refresh_list_view_items()
|
||||
|
||||
# Select the next available message if it exists
|
||||
if next_id is not None and next_idx is not None:
|
||||
app.current_message_id = next_id
|
||||
app.current_message_index = next_idx
|
||||
else:
|
||||
# If there are no other messages, reset the UI
|
||||
app.current_message_id = 0
|
||||
app.show_status("No more messages available.", "warning")
|
||||
else:
|
||||
app.show_status(f"Failed to archive message {current_message_id}.", "error")
|
||||
43
src/mail/actions/delete.py
Normal file
43
src/mail/actions/delete.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from textual import work
|
||||
from src.services.himalaya import client as himalaya_client
|
||||
|
||||
|
||||
@work(exclusive=True)
|
||||
async def delete_current(app):
|
||||
"""Delete the current message."""
|
||||
if not app.current_message_id:
|
||||
app.show_status("No message selected to delete.", "error")
|
||||
return
|
||||
|
||||
# Store the current message ID and index
|
||||
current_message_id = app.current_message_id
|
||||
current_index = app.current_message_index
|
||||
|
||||
# Find the next message to display after deletion
|
||||
next_id, next_idx = app.message_store.find_next_valid_id(current_index)
|
||||
if next_id is None or next_idx is None:
|
||||
# If there's no next message, try to find a previous one
|
||||
next_id, next_idx = app.message_store.find_prev_valid_id(current_index)
|
||||
|
||||
# Delete the message using our Himalaya client module
|
||||
message, success = await himalaya_client.delete_message(current_message_id)
|
||||
|
||||
if success:
|
||||
app.show_status(f"Message {current_message_id} deleted.", "success")
|
||||
app.message_store.remove_envelope(current_message_id)
|
||||
app.refresh_list_view()
|
||||
|
||||
# Select the next available message if it exists
|
||||
if next_id is not None and next_idx is not None:
|
||||
app.current_message_id = next_id
|
||||
app.current_message_index = next_idx
|
||||
else:
|
||||
# If there are no other messages, reset the UI
|
||||
app.current_message_id = 0
|
||||
app.show_status("No more messages available.", "warning")
|
||||
else:
|
||||
app.show_status(
|
||||
f"Failed to delete message {current_message_id}: {message}", "error"
|
||||
)
|
||||
17
src/mail/actions/newest.py
Normal file
17
src/mail/actions/newest.py
Normal file
@@ -0,0 +1,17 @@
|
||||
async def action_newest(app) -> None:
|
||||
"""Show the previous email message by finding the next lower ID from the list of envelope IDs."""
|
||||
try:
|
||||
if app.reload_needed:
|
||||
await app.action_fetch_list()
|
||||
|
||||
ids = sorted(
|
||||
(int(envelope["id"]) for envelope in app.all_envelopes), reverse=True
|
||||
)
|
||||
app.current_message_id = ids[0]
|
||||
app.show_message(app.current_message_id)
|
||||
return
|
||||
|
||||
else:
|
||||
app.show_status("Failed to fetch envelope list.", severity="error")
|
||||
except Exception as e:
|
||||
app.show_status(f"Error: {e}", severity="error")
|
||||
17
src/mail/actions/next.py
Normal file
17
src/mail/actions/next.py
Normal file
@@ -0,0 +1,17 @@
|
||||
async def action_next(app) -> None:
|
||||
"""Show the next email message by finding the next higher ID from the list of envelope IDs."""
|
||||
try:
|
||||
if app.reload_needed:
|
||||
app.action_fetch_list()
|
||||
ids = sorted(int(envelope["id"]) for envelope in app.all_envelopes)
|
||||
for envelope_id in ids:
|
||||
if envelope_id > int(app.current_message_id):
|
||||
app.show_message(envelope_id)
|
||||
return
|
||||
|
||||
app.show_status("No newer messages found.", severity="warning")
|
||||
app.action_newest()
|
||||
else:
|
||||
app.show_status("Failed to fetch envelope list.", severity="error")
|
||||
except Exception as e:
|
||||
app.show_status(f"Error: {e}", severity="error")
|
||||
15
src/mail/actions/oldest.py
Normal file
15
src/mail/actions/oldest.py
Normal file
@@ -0,0 +1,15 @@
|
||||
def action_oldest(app) -> None:
|
||||
"""Show the previous email message by finding the next lower ID from the list of envelope IDs."""
|
||||
try:
|
||||
if app.reload_needed:
|
||||
app.action_fetch_list()
|
||||
|
||||
ids = sorted((int(envelope["id"]) for envelope in app.all_envelopes))
|
||||
app.current_message_id = ids[0]
|
||||
app.show_message(app.current_message_id)
|
||||
return
|
||||
|
||||
else:
|
||||
app.show_status("Failed to fetch envelope list.", severity="error")
|
||||
except Exception as e:
|
||||
app.show_status(f"Error: {e}", severity="error")
|
||||
21
src/mail/actions/open.py
Normal file
21
src/mail/actions/open.py
Normal file
@@ -0,0 +1,21 @@
|
||||
from ..screens.OpenMessage import OpenMessageScreen
|
||||
|
||||
|
||||
def action_open(app) -> None:
|
||||
"""Show the input modal for opening a specific message by ID."""
|
||||
|
||||
def check_id(message_id: str | None) -> bool:
|
||||
try:
|
||||
int(message_id)
|
||||
app.show_message(message_id)
|
||||
if message_id is not None and message_id > 0:
|
||||
app.show_message(message_id)
|
||||
except ValueError:
|
||||
app.bell()
|
||||
app.show_status(
|
||||
"Invalid message ID. Please enter an integer.", severity="error"
|
||||
)
|
||||
return True
|
||||
return False
|
||||
|
||||
app.push_screen(OpenMessageScreen(), check_id)
|
||||
20
src/mail/actions/previous.py
Normal file
20
src/mail/actions/previous.py
Normal file
@@ -0,0 +1,20 @@
|
||||
def action_previous(app) -> None:
|
||||
"""Show the previous email message by finding the next lower ID from the list of envelope IDs."""
|
||||
try:
|
||||
if app.reload_needed:
|
||||
app.action_fetch_list()
|
||||
|
||||
ids = sorted(
|
||||
(int(envelope["id"]) for envelope in app.all_envelopes), reverse=True
|
||||
)
|
||||
for envelope_id in ids:
|
||||
if envelope_id < int(app.current_message_id):
|
||||
app.current_message_id = envelope_id
|
||||
app.show_message(app.current_message_id)
|
||||
return
|
||||
app.show_status("No older messages found.", severity="warning")
|
||||
app.action_oldest()
|
||||
else:
|
||||
app.show_status("Failed to fetch envelope list.", severity="error")
|
||||
except Exception as e:
|
||||
app.show_status(f"Error: {e}", severity="error")
|
||||
14
src/mail/actions/show_message.py
Normal file
14
src/mail/actions/show_message.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import logging
|
||||
from textual.logging import TextualHandler
|
||||
|
||||
|
||||
logging.basicConfig(
|
||||
level="NOTSET",
|
||||
handlers=[TextualHandler()],
|
||||
)
|
||||
|
||||
|
||||
def show_message(app, message_id: int) -> None:
|
||||
"""Fetch and display the email message by ID."""
|
||||
logging.info("Showing message ID: " + str(message_id))
|
||||
app.current_message_id = message_id
|
||||
54
src/mail/actions/task.py
Normal file
54
src/mail/actions/task.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from textual import work
|
||||
from textual.screen import ModalScreen
|
||||
from src.services.taskwarrior import client as taskwarrior_client
|
||||
from ..screens.CreateTask import CreateTaskScreen
|
||||
|
||||
|
||||
class TaskAction:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
|
||||
|
||||
def action_create_task(app):
|
||||
"""Show the create task screen."""
|
||||
|
||||
current_message_id = app.current_message_id
|
||||
if not current_message_id:
|
||||
app.show_status("No message selected to create task from.", "error")
|
||||
return
|
||||
|
||||
# Prepare data for the create task screen
|
||||
metadata = app.message_store.get_metadata(current_message_id)
|
||||
subject = metadata.get("subject", "No subject") if metadata else "No subject"
|
||||
from_addr = metadata["from"].get("addr", "Unknown") if metadata else "Unknown"
|
||||
|
||||
# Show the create task screen with the current message data
|
||||
app.push_screen(CreateTaskScreen(subject=subject, from_addr=from_addr))
|
||||
|
||||
|
||||
@work(exclusive=True)
|
||||
async def create_task(
|
||||
subject, description=None, tags=None, project=None, due=None, priority=None
|
||||
):
|
||||
"""
|
||||
Create a task with the Taskwarrior API client.
|
||||
"""
|
||||
try:
|
||||
success, result = await taskwarrior_client.create_task(
|
||||
task_description=subject,
|
||||
tags=tags or [],
|
||||
project=project,
|
||||
due=due,
|
||||
priority=priority,
|
||||
)
|
||||
|
||||
if success:
|
||||
return True, result
|
||||
else:
|
||||
logging.error(f"Failed to create task: {result}")
|
||||
return False, result
|
||||
except Exception as e:
|
||||
logging.error(f"Exception creating task: {e}")
|
||||
return False, str(e)
|
||||
Reference in New Issue
Block a user