This commit is contained in:
Bryce
2025-08-06 21:30:33 -07:00
parent 3387867561
commit d021f045ae
14 changed files with 426 additions and 375 deletions

View File

@@ -16,7 +16,8 @@ def index():
folders = Folder.query.filter_by(user_id=current_user.id).all()
return render_template('index.html',
folders=folders)
folders=folders,
show_hidden=False)
@main.route('/api/folders/new', methods=['GET'])
@login_required
@@ -62,12 +63,17 @@ def add_folder():
# Default to 'destination' type for manually created folders
folder_type = 'tidy' if name.strip().lower() == 'inbox' else 'destination'
# If folder_type is 'ignore', reset emails_count to 0
if folder_type == 'ignore':
emails_count = 0
folder = Folder(
user_id=current_user.id,
name=name.strip(),
rule_text=rule_text.strip(),
priority=int(priority) if priority else 0,
folder_type=folder_type
folder_type=folder_type,
emails_count=0 if folder_type == 'ignore' else None
)
db.session.add(folder)
@@ -78,7 +84,7 @@ def add_folder():
response = make_response(render_template('partials/folders_list.html', folders=folders))
response.headers['HX-Trigger'] = 'close-modal'
response.headers["HX-Target"] = 'folder-list'
response.headers["HX-Target"] = '#folders-list'
response.status_code = 201
return response
@@ -126,9 +132,9 @@ def delete_folder(folder_id):
# Return both sections
return render_template('partials/folders_list.html', folders=folders)
@main.route('/api/folders/<folder_id>/toggle', methods=['PUT'])
@main.route('/api/folders/<folder_id>/type', methods=['PUT'])
@login_required
def toggle_folder_organize(folder_id):
def update_folder_type(folder_id):
try:
# Find the folder by ID and ensure it belongs to the current user
folder = Folder.query.filter_by(id=folder_id, user_id=current_user.id).first()
@@ -136,21 +142,30 @@ def toggle_folder_organize(folder_id):
if not folder:
return jsonify({'error': 'Folder not found'}), 404
# Toggle the organize_enabled flag
folder.organize_enabled = not folder.organize_enabled
# Get the new folder type from form data
new_folder_type = request.form.get('folder_type')
# Validate the folder type
if new_folder_type not in ['tidy', 'destination', 'ignore']:
return jsonify({'error': 'Invalid folder type'}), 400
# If changing to 'ignore', reset the emails_count to 0
if new_folder_type == 'ignore' and folder.folder_type != 'ignore':
folder.emails_count = 0
# Update the folder type
folder.folder_type = new_folder_type
db.session.commit()
# Return just the updated folder card HTML for this specific folder
# Use the appropriate template based on folder type
if folder.folder_type == 'tidy':
return render_template('partials/folder_card_tidy.html', folder=folder)
else:
return render_template('partials/folder_card_destination.html', folder=folder)
# Get updated list of folders for the current user
folders = Folder.query.filter_by(user_id=current_user.id).all()
return render_template('partials/folders_list.html', folders=folders)
except Exception as e:
# Print unhandled exceptions to the console as required
logging.exception("Error toggling folder organize flag: %s", e)
logging.exception("Error updating folder type: %s", e)
db.session.rollback()
return jsonify({'error': 'An unexpected error occurred'}), 500
@@ -219,11 +234,13 @@ def update_folder(folder_id):
folder.rule_text = rule_text.strip()
folder.priority = int(priority) if priority else 0
# Update folder type if the name changed to/from 'inbox'
if name.strip().lower() == 'inbox' and folder.folder_type != 'tidy':
folder.folder_type = 'tidy'
elif name.strip().lower() != 'inbox' and folder.folder_type != 'destination':
folder.folder_type = 'destination'
# Check if folder type is being changed to 'ignore'
old_folder_type = folder.folder_type
folder.folder_type = 'tidy' if name.strip().lower() == 'inbox' else 'destination'
# If changing to 'ignore', reset emails_count to 0
if folder.folder_type == 'ignore' and old_folder_type != 'ignore':
folder.emails_count = 0
db.session.commit()
@@ -373,6 +390,9 @@ def sync_imap_folders():
synced_count = 0
processed_emails_service = ProcessedEmailsService(current_user)
# Create a list of folders to process
folders_to_process = []
for imap_folder in unique_folders:
folder_name = imap_folder['name'].strip()
@@ -400,6 +420,7 @@ def sync_imap_folders():
)
db.session.add(new_folder)
synced_count += 1
folders_to_process.append(new_folder)
else:
# Update existing folder with email counts and recent emails
# Get all email UIDs in this folder
@@ -417,13 +438,22 @@ def sync_imap_folders():
# Get the most recent emails for this folder
recent_emails = imap_service.get_recent_emails(folder_name, 3)
existing_folder.recent_emails = recent_emails
folders_to_process.append(existing_folder)
db.session.commit()
# Get updated list of folders
folders = Folder.query.filter_by(user_id=current_user.id).all()
return render_template('partials/folders_list.html', folders=folders)
# Check if we should show the folder type selection modal
# Only show the modal if there are new folders to configure
if synced_count > 0:
# Return the folder type selection modal
response = make_response(render_template('partials/folder_type_selection_modal.html', folders=folders_to_process))
response.headers['HX-Trigger'] = 'open-modal'
return response
else:
# Just return the updated folders list
folders = Folder.query.filter_by(user_id=current_user.id).all()
return render_template('partials/folders_list.html', folders=folders)
except Exception as e:
logging.exception("Error syncing IMAP folders: %s", e)
@@ -438,6 +468,7 @@ def get_folders():
# Get filter parameter from query string
filter_type = request.args.get('filter', 'all')
folder_type = request.args.get('type', None)
show_hidden = request.args.get('show_hidden', 'off').lower() == 'on'
# Get folders for the current authenticated user
if folder_type:
@@ -454,15 +485,26 @@ def get_folders():
folders = Folder.query.filter_by(user_id=current_user.id).all()
if folder_type == 'tidy':
response = make_response(render_template('partials/folders_to_tidy_section.html', folders=folders))
response["HX-Retarget"] = "#folders-to-tidy"
response = make_response(render_template('partials/folders_to_tidy_section.html', folders=folders, show_hidden=show_hidden))
response.headers["HX-Retarget"] = "#folders-to-tidy"
return response
elif folder_type == 'destination':
response = make_response(render_template('partials/destination_folders_section.html', folders=folders))
response["HX-Retarget"] = "#destination-folders"
response = make_response(render_template('partials/destination_folders_section.html', folders=folders, show_hidden=show_hidden))
response.headers["HX-Retarget"] = "#destination-folders"
return response
elif folder_type == 'ignore':
response = make_response(render_template('partials/hidden_folders_section.html', folders=folders, show_hidden=show_hidden))
response.headers["HX-Retarget"] = "#hidden-folders"
# Show or hide the hidden folders section based on the show_hidden parameter
if show_hidden:
response.headers['HX-Trigger'] = 'show-hidden'
else:
response.headers['HX-Trigger'] = 'hide-hidden'
return response
else:
return render_template('partials/folders_list.html', folders=folders)
return render_template('partials/folders_list.html', folders=folders, show_hidden=show_hidden)
# Processed Emails API Endpoints

View File

@@ -54,7 +54,8 @@
<!-- Welcome Section - Only shown when IMAP is not configured -->
{% if not current_user.imap_config %}
{% include "partials/welcome_section.html" %}
{% include "partials/welcome_section.html" %}
<section id="folders-list" class="mb-12"></section>
{% else %}
<!-- Stats Section -->
@@ -81,9 +82,21 @@
<!-- Search and Filter -->
<div class="mb-6 flex justify-between items-center">
<div class="relative w-64">
<input type="text" placeholder="Search folders..." class="input input-bordered w-full pr-10">
<i class="fas fa-search absolute right-3 top-3 text-base-content/50"></i>
<div class="flex items-center space-x-4">
<div class="relative w-64">
<input type="text" placeholder="Search folders..." class="input input-bordered w-full pr-10">
<i class="fas fa-search absolute right-3 top-3 text-base-content/50"></i>
</div>
<div class="flex items-center space-x-2">
<input type="checkbox" id="show-hidden"
name="show_hidden"
class="toggle"
hx-get="/api/folders"
hx-include="this"
hx-target="#folders-list"
hx-swap="outerHTML">
<label for="show-hidden" class="label-text cursor-pointer">Show Hidden</label>
</div>
</div>
<div class="flex space-x-2">
<button class="btn btn-sm btn-outline" hx-get="/api/folders?filter=all" hx-target="#folders-list" hx-swap="outerHTML settle:300ms">All</button>

View File

@@ -44,6 +44,22 @@
<p class="text-base-content/80">{{ folder.rule_text }}</p>
</div>
{% block additional_content %}{% endblock %}
<div class="flex justify-between items-center mt-2">
<div class="flex items-center space-x-2">
<span class="text-xs">Type:</span>
<select
class="select select-bordered select-xs"
hx-put="/api/folders/{{ folder.id }}/type"
hx-target="#folders-list"
hx-swap="innerHTML"
hx-include="this"
name="folder_type"
data-loading-disable >
<option value="tidy" {% if folder.folder_type == 'tidy' %}selected{% endif %}>Tidy</option>
<option value="destination" {% if folder.folder_type == 'destination' %}selected{% endif %}>Destination</option>
<option value="ignore" {% if folder.folder_type == 'ignore' %}selected{% endif %}>Ignore</option>
</select>
</div>
</div>
</div>
</div>

View File

@@ -1,41 +1,5 @@
<div id="folder-{{ folder.id }}" class="card bg-base-100 shadow-xl border border-base-300 hover:shadow-lg">
<div class="card-body" data-loading-states>
<div class="flex justify-between items-start mb-2">
<h3 class="text-xl font-bold truncate flex-grow">{{ folder.name }}</h3>
<div class="flex space-x-2">
<button class="btn btn-sm btn-outline"
hx-get="/api/folders/{{ folder.id }}/edit"
hx-target="#modal-holder"
hx-swap="innerHTML"
hx-trigger="click"
data-loading-disable
>
<i class="fas fa-edit" data-loading-class="!hidden"></i>
<span class="loading loading-spinner loading-xs hidden" data-loading-class-remove="hidden"></span>
</button>
<button class="btn btn-sm btn-outline btn-error fade-me-out"
hx-delete="/api/folders/{{ folder.id }}"
hx-target="#folders-list"
hx-swap="outerhtml settle:300ms"
hx-confirm="Are you sure you want to delete this folder?"
data-loading-disable
>
<i class="fas fa-trash" data-loading-class="!hidden"></i>
<span class="loading loading-spinner loading-xs hidden" data-loading-class-remove="hidden"></span>
</button>
</div>
</div>
<!-- Email count badge for destination folders -->
<div class="flex justify-between items-center mb-2">
<div class="flex space-x-1">
<span class="badge badge-primary">{{ folder.emails_count }} emails</span>
</div>
</div>
<div class="bg-base-200 rounded-box p-4 mb-4">
<p class="text-base-content/80">{{ folder.rule_text }}</p>
</div>
</div>
</div>
{% extends "partials/folder_card_base.html" %}
{% block badges %}
<span class="badge badge-primary">{{ folder.emails_count }} emails</span>
{% endblock %}

View File

@@ -0,0 +1,6 @@
{% extends "partials/folder_card_base.html" %}
{% block badges %}
<span class="badge badge-secondary">Ignored</span>
<span class="badge badge-outline">{{ folder.emails_count }} emails</span>
{% endblock %}

View File

@@ -1,82 +1,20 @@
<div id="folder-{{ folder.id }}" class="card bg-base-100 shadow-xl border border-base-300 hover:shadow-lg">
<div class="card-body" data-loading-states>
<div class="flex justify-between items-start mb-2">
<h3 class="text-xl font-bold truncate flex-grow">{{ folder.name }}</h3>
<div class="flex space-x-2">
<button class="btn btn-sm btn-outline"
hx-get="/api/folders/{{ folder.id }}/edit"
hx-target="#modal-holder"
hx-swap="innerHTML"
hx-trigger="click"
data-loading-disable
>
<i class="fas fa-edit" data-loading-class="!hidden"></i>
<span class="loading loading-spinner loading-xs hidden" data-loading-class-remove="hidden"></span>
</button>
<button class="btn btn-sm btn-outline btn-error fade-me-out"
hx-delete="/api/folders/{{ folder.id }}"
hx-target="#folders-to-tidy-list"
hx-swap="innerHTML swap:1s"
hx-confirm="Are you sure you want to delete this folder?"
data-loading-disable
>
<i class="fas fa-trash" data-loading-class="!hidden"></i>
<span class="loading loading-spinner loading-xs hidden" data-loading-class-remove="hidden"></span>
</button>
</div>
</div>
<!-- Email count badges placed below title but in a separate row -->
<div class="flex justify-between items-center mb-2">
<div class="flex space-x-1">
<span class="badge badge-outline">{{ folder.total_count }} total</span>
{% if folder.pending_count > 0 %}
<button
class="badge badge-warning cursor-pointer"
hx-get="/api/folders/{{ folder.id }}/pending-emails"
hx-target="#modal-holder"
hx-swap="innerHTML"
hx-trigger="click"
title="{{ folder.pending_count }} pending emails"
>
{{ folder.pending_count }} pending
</button>
{% else %}
<span class="badge badge-secondary cursor-pointer" x-tooltip.raw.html="{% if folder.recent_emails %}<table class='text-xs'><tr><th class='text-left pr-2'>Subject</th><th class='text-left'>Date</th></tr>{% for email in folder.recent_emails %}<tr><td class='text-left pr-2 truncate max-w-[150px]'>{{ email.subject }}</td><td class='text-left'>{{ email.date[:10] if email.date else 'N/A' }}</td></tr>{% endfor %}</table>{% else %}No recent emails{% endif %}">{{ folder.pending_count }} pending</span>
{% endif %}
<span class="badge badge-success">{{ folder.total_count - folder.pending_count }} processed</span>
</div>
{% if folder.priority == 1 %}
<span class="badge badge-error">High Priority</span>
{% elif folder.priority == -1 %}
<span class="badge badge-info">Low Priority</span>
{% else %}
<span class="badge badge-primary">Normal Priority</span>
{% endif %}
</div>
<div class="bg-base-200 rounded-box p-4 mb-4">
<p class="text-base-content/80">{{ folder.rule_text }}</p>
</div>
<div class="flex justify-between items-center mt-2">
<div class="flex items-center space-x-2">
<span class="text-xs">Organize:</span>
<input
type="checkbox"
class="toggle toggle-sm toggle-success"
{% if folder.organize_enabled %}checked="checked"{% endif %}
hx-put="/api/folders/{{ folder.id }}/toggle"
hx-target="#folder-{{ folder.id }}"
hx-swap="outerHTML"
hx-trigger="click"
data-loading-disable
aria-label="Toggle organize enabled">
</input>
<span class="loading loading-spinner loading-xs hidden" data-loading-class-remove="hidden"></span>
</div>
</div>
</div>
</div>
{% extends "partials/folder_card_base.html" %}
{% block badges %}
<span class="badge badge-outline">{{ folder.total_count }} total</span>
{% if folder.pending_count > 0 %}
<button
class="badge badge-warning cursor-pointer"
hx-get="/api/folders/{{ folder.id }}/pending-emails"
hx-target="#modal-holder"
hx-swap="innerHTML"
hx-trigger="click"
title="{{ folder.pending_count }} pending emails"
>
{{ folder.pending_count }} pending
</button>
{% else %}
<span class="badge badge-secondary cursor-pointer" x-tooltip.raw.html="{% if folder.recent_emails %}<table class='text-xs'><tr><th class='text-left pr-2'>Subject</th><th class='text-left'>Date</th></tr>{% for email in folder.recent_emails %}<tr><td class='text-left pr-2 truncate max-w-[150px]'>{{ email.subject }}</td><td class='text-left'>{{ email.date[:10] if email.date else 'N/A' }}</td></tr>{% endfor %}</table>{% else %}No recent emails{% endif %}">{{ folder.pending_count }} pending</span>
{% endif %}
<span class="badge badge-success">{{ folder.total_count - folder.pending_count }} processed</span>
{% endblock %}

View File

@@ -0,0 +1,52 @@
<div id="folder-type-modal" class="modal-box" @click.away="$refs.modal.close()">
<h3 class="font-bold text-lg mb-4">Configure Folder Types</h3>
<p class="mb-4">Select the processing type for each folder. Inbox will default to Tidy, while Archive/Spam/Drafts will default to Ignore.</p>
<div class="overflow-x-auto">
<table class="table">
<thead>
<tr>
<th>Folder Name</th>
<th>Processing Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
{% for folder in folders %}
<tr>
<td>{{ folder.name }}</td>
<td>
<select class="select select-bordered select-sm"
name="folder_type_{{ folder.id }}"
hx-put="/api/folders/{{ folder.id }}/type"
hx-target="#modal-holder"
hx-swap="outerHTML">
<option value="tidy" {% if folder.folder_type == 'tidy' %}selected{% endif %}>Tidy</option>
<option value="destination" {% if folder.folder_type == 'destination' %}selected{% endif %}>Destination</option>
<option value="ignore" {% if folder.folder_type == 'ignore' %}selected{% endif %}>Ignore</option>
</select>
</td>
<td>
{% if folder.folder_type == 'tidy' %}
Processed by AI to organize emails
{% elif folder.folder_type == 'destination' %}
Target for organized emails
{% else %}
Ignored during processing
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="modal-action">
<button type="button" class="btn btn-outline" hx-get="/api/imap/sync" hx-target="#folders-list" hx-swap="outerHTML">
Skip and Use Defaults
</button>
<button type="button" class="btn btn-primary" hx-post="/api/imap/sync" hx-target="#folders-list" hx-swap="outerHTML">
Save and Continue
</button>
</div>
</div>

View File

@@ -1,4 +1,7 @@
<section id="folders-list" class="mb-12">
{% include 'partials/folders_to_tidy_section.html' %}
{% include 'partials/destination_folders_section.html' %}
{% if show_hidden %}
{% include 'partials/hidden_folders_section.html' %}
{% endif %}
</section>

View File

@@ -0,0 +1,27 @@
<div class="hidden-folders-section mb-8" id="hidden-folders">
<div class="section-header bg-base-200 p-4 rounded-t-lg border-b border-base-300">
<div class="flex flex-col md:flex-row md:items-center md:justify-between">
<div>
<h2 class="text-2xl font-bold text-base-content">
<i class="fas fa-eye-slash mr-3"></i>
Hidden Folders
</h2>
<p class="text-base-content/70 mt-1">Folders that are ignored during email processing</p>
</div>
</div>
</div>
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 p-6">
{% for folder in folders|selectattr('folder_type', 'equalto', 'ignore') %}
{% include 'partials/folder_card_ignore.html' %}
{% else %}
<div class="col-span-full text-center py-12 bg-base-100 rounded-box shadow-lg border border-dashed border-base-300">
<div class="text-5xl mb-4 text-secondary">
<i class="fas fa-folder-slash"></i>
</div>
<h3 class="text-2xl font-bold mb-2">No hidden folders</h3>
<p class="mb-6 text-base-content/70">Folders that are set to ignore will appear here.</p>
</div>
{% endfor %}
</div>
</div>

View File

@@ -80,8 +80,8 @@
{% if success %}
<div class="mt-4 pt-4 border-t border-base-300" data-loading-states>
<button class="btn btn-success w-full" hx-post="/api/imap/sync" hx-target="#folders-list" hx-swap="outerHTML settle:300ms" data-loading-disable>
<span data-loading-class="!hidden"><i class="fas fa-sync mr-2"></i>Sync Folders</span>
<button class="btn btn-success w-full" hx-post="/api/imap/sync" hx-target="#modal-holder" hx-swap="innerHTML" data-loading-disable>
<span data-loading-class="!hidden"><i class="fas fa-sync mr-2"></i>Configure Folder Types</span>
<span class="loading loading-spinner loading-xs hidden" data-loading-class-remove="hidden"></span>
</button>
</div>