From 2ce8af1608c77567293a9eb235769d6431b06731 Mon Sep 17 00:00:00 2001 From: juan Date: Mon, 25 Aug 2025 13:54:18 -0400 Subject: [PATCH] fetch email reversed and, fixed moveEmail, so it moves every message and not just the first of the thread --- lib/api_service.dart | 60 +++++++++++++++++++++++++++++++++----------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/lib/api_service.dart b/lib/api_service.dart index 4b7a087..9c447a9 100644 --- a/lib/api_service.dart +++ b/lib/api_service.dart @@ -49,7 +49,39 @@ class ApiService { return []; } } + Future> fetchEmailsFromFolderReversed( + String folder, int pagenitaion) async { + try { + var url = Uri.http('$ip:$port', 'sorted_threads_by_date_current', { + 'folder': folder, + 'limit': '50', + 'offset': pagenitaion.toString(), + }); + + var response = await http.get(url); + List allEmails = []; + if (response.statusCode == 200) { + List json = jsonDecode(response.body); + for (var item in json) { + //each item in the json is a date + if (item.length > 1 && item[0] is String && item[1] is List) { + List threadIDs = List.from(item[1]); + for (var threadId in threadIDs) { + await fetchThreads(threadId, allEmails); + } + } + } + currFolder = folder; + return allEmails; + } else { + throw Exception('Failed to load threads'); + } + } catch (e) { + print('_displayEmailsFromFolder caught error: $e'); + return []; + } + } Future fetchThreads( //populates allEmails, which is the List that contains all the emails in a thread int threadId, @@ -180,16 +212,17 @@ class ApiService { return false; } - SerializableMessage firstMail = mailsInSerializable[0]; - - Map requestBody = { - 'from': fromFolder, - 'uid': firstMail.uid.toString(), - 'to': "Deleted Crabmail", - }; + // SerializableMessage firstMail = mailsInSerializable[0]; + try { - var response = await http.post( + for (SerializableMessage mail in mailsInSerializable) { + Map requestBody = { + 'from': fromFolder, + 'uid': mail.uid.toString(), + 'to': "Deleted Crabmail", + }; + var response = await http.post( url, headers: { 'Content-Type': 'application/json', @@ -201,7 +234,7 @@ class ApiService { return true; } else { print('error ${response.statusCode} ${response.body}'); - } + }} } catch (e) { print("failed trying to post move_email, with error: $e"); } @@ -374,8 +407,8 @@ class ApiService { } catch (e) { print('_getMDContent caught error: $e'); } - print("IDS inside fetch md content $IDsString"); - + // print("IDS inside fetch md content $IDsString"); + // print("inside apiservice $MDofThread"); return MDofThread; } @@ -454,9 +487,8 @@ class ApiService { "subject": subject ?? "Untitled", "in_reply_to": "", "messages": [ - { - "message": emailContent ?? "", - "is_html": false}], + {"message": emailContent ?? "", "is_html": false} + ], "attachments": [], "inline_images": [], };