1
+ package commands
2
+
3
+ import (
4
+ "context"
5
+ "fmt"
6
+ "strconv"
7
+
8
+ "github.com/gptscript-ai/tools/outlook/mail/pkg/client"
9
+ "github.com/gptscript-ai/tools/outlook/mail/pkg/global"
10
+ "github.com/gptscript-ai/tools/outlook/mail/pkg/graph"
11
+ "github.com/gptscript-ai/tools/outlook/mail/pkg/util"
12
+ md "github.com/JohannesKaufmann/html-to-markdown"
13
+ )
14
+
15
+
16
+
17
+ func ListGroupThreads (ctx context.Context , groupID , start , end , limit string ) error {
18
+ var (
19
+ limitInt int = 100
20
+ err error
21
+ )
22
+ if limit != "" {
23
+ limitInt , err = strconv .Atoi (limit )
24
+ if err != nil {
25
+ return fmt .Errorf ("failed to parse limit: %w" , err )
26
+ }
27
+ if limitInt < 1 {
28
+ return fmt .Errorf ("limit must be a positive integer" )
29
+ }
30
+ }
31
+
32
+ if groupID == "" {
33
+ return fmt .Errorf ("group ID is required" )
34
+ }
35
+
36
+ c , err := client .NewClient (global .ReadOnlyScopes )
37
+ if err != nil {
38
+ return fmt .Errorf ("failed to create client: %w" , err )
39
+ }
40
+
41
+ threads , err := graph .ListGroupThreads (ctx , c , groupID , start , end , limitInt )
42
+ if err != nil {
43
+ return fmt .Errorf ("failed to list group threads: %w" , err )
44
+ }
45
+
46
+ for _ , thread := range threads {
47
+ threadID := util .Deref (thread .GetId ())
48
+
49
+ fmt .Printf ("📩 Thread ID: %s\n " , threadID )
50
+ if thread .GetTopic () != nil {
51
+ fmt .Printf ("📌 Subject: %s\n " , util .Deref (thread .GetTopic ()))
52
+ } else {
53
+ fmt .Println ("📌 Subject: (No Subject)" )
54
+ }
55
+ fmt .Printf ("📅 Last Delivered: %s\n " , thread .GetLastDeliveredDateTime ().String ())
56
+
57
+ // Print unique senders
58
+ senders := thread .GetUniqueSenders ()
59
+ fmt .Print ("👥 Unique Senders: " )
60
+ for _ , sender := range senders {
61
+ fmt .Printf ("%s, " , sender )
62
+ }
63
+ fmt .Println ()
64
+
65
+ // Fetch posts (individual emails/messages) inside the thread and then print them
66
+ posts , err := graph .ListThreadMessages (ctx , c , groupID , threadID )
67
+ if err != nil {
68
+ return fmt .Errorf ("failed to list thread messages: %w" , err )
69
+ }
70
+
71
+ fmt .Println ("\n ✉️ Messages:" )
72
+ for i , post := range posts {
73
+ messageID := util .Deref (post .GetId ())
74
+ fmt .Printf ("📧 Message %d, ID: %s\n " , i + 1 , messageID )
75
+
76
+ // Check if sender information is available
77
+ if post .GetFrom () != nil && post .GetFrom ().GetEmailAddress () != nil {
78
+ fmt .Printf ("👤 From: %s <%s>\n " ,
79
+ util .Deref (post .GetFrom ().GetEmailAddress ().GetName ()),
80
+ util .Deref (post .GetFrom ().GetEmailAddress ().GetAddress ()),
81
+ )
82
+ } else {
83
+ fmt .Println ("👤 Sender: Unknown" )
84
+ }
85
+
86
+ fmt .Printf ("📅 Sent: %s\n " , post .GetReceivedDateTime ().String ())
87
+
88
+ // Print message body if available
89
+ if post .GetBody () != nil && post .GetBody ().GetContent () != nil {
90
+ fmt .Println ("📝 Message Body:" )
91
+ converter := md .NewConverter ("" , true , nil )
92
+ bodyHTML := util .Deref (post .GetBody ().GetContent ())
93
+ bodyMarkdown , err := converter .ConvertString (bodyHTML )
94
+ if err != nil {
95
+ return fmt .Errorf ("failed to convert email body HTML to markdown: %w" , err )
96
+ }
97
+ fmt .Println (bodyMarkdown )
98
+
99
+ } else {
100
+ fmt .Println ("📭 (No content in this message)" )
101
+ }
102
+ fmt .Println ()
103
+ }
104
+
105
+ fmt .Println ("\n " )
106
+ }
107
+ return nil
108
+ }
0 commit comments