File: example_test.go

package info (click to toggle)
golang-github-emersion-go-imap-sortthread 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 116 kB
  • sloc: makefile: 2
file content (69 lines) | stat: -rw-r--r-- 1,579 bytes parent folder | download
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
package sortthread_test

import (
	"log"
	"time"

	"github.com/emersion/go-imap"
	"github.com/emersion/go-imap-sortthread"
	"github.com/emersion/go-imap/client"
)

func ExampleSortClient() {
	// Assuming c is an IMAP client
	var c *client.Client

	// Create a new SORT client
	sc := sortthread.NewSortClient(c)

	// Check the server supports the extension
	ok, err := sc.SupportSort()
	if err != nil {
		log.Fatal(err)
	} else if !ok {
		log.Fatal("Server doesn't support SORT")
	}

	// Send a SORT command: search for the first 10 messages, sort them by
	// ascending sender and then by descending size
	sortCriteria := []sortthread.SortCriterion{
		{Field: sortthread.SortFrom},
		{Field: sortthread.SortSize, Reverse: true},
	}
	searchCriteria := imap.NewSearchCriteria()
	searchCriteria.SeqNum = new(imap.SeqSet)
	searchCriteria.SeqNum.AddRange(1, 10)
	uids, err := sc.UidSort(sortCriteria, searchCriteria)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(uids)
}

func ExampleThreadClient() {
	// Assuming c is an IMAP client
	var c *client.Client

	// Create a new THREAD client
	sc := sortthread.NewThreadClient(c)

	// Check the server supports the extension
	ok, err := sc.SupportThread()
	if err != nil {
		log.Fatal(err)
	} else if !ok {
		log.Fatal("Server doesn't support THREAD")
	}

	layoutISO := "2006-01-02"
	searchCriteria := imap.NewSearchCriteria()
	date, _ := time.Parse(layoutISO, "2019-07-05")
	searchCriteria.Since = date
	threads, err := sc.UidThread(sortthread.References, searchCriteria)
	if err != nil {
		log.Fatal(err)
	}

	log.Println(threads)
}