Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated APIv2 to list messages #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions api/v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"

"github.com/gorilla/pat"
"github.com/ian-kent/go-log/log"
Expand Down Expand Up @@ -48,6 +50,10 @@ func createAPIv2(conf *config.Config, r *pat.Router) *APIv2 {

r.Path(conf.WebPath + "/api/v2/websocket").Methods("GET").HandlerFunc(apiv2.websocket)

r.Path(conf.WebPath + "/api/v2/list").Methods("GET").HandlerFunc(apiv2.list)
r.Path(conf.WebPath + "/api/v2/list").Methods("OPTIONS").HandlerFunc(apiv2.defaultOptions)


go func() {
for {
select {
Expand Down Expand Up @@ -76,6 +82,17 @@ type messagesResult struct {
Items []data.Message `json:"items"`
}


type messagesResultList struct {
ID string `json:"id"`
FROM string `json:"from"`
TO string `json:"to"`
DATE string `json:"date"`
SUBJECT string `json:"subject"`
}



func (apiv2 *APIv2) getStartLimit(w http.ResponseWriter, req *http.Request) (start, limit int) {
start = 0
limit = 50
Expand Down Expand Up @@ -120,6 +137,58 @@ func (apiv2 *APIv2) messages(w http.ResponseWriter, req *http.Request) {
w.Write(bytes)
}

func (apiv2 *APIv2) list(w http.ResponseWriter, req *http.Request) {
log.Println("[APIv2] GET /api/v2/list")

apiv2.defaultOptions(w, req)

start, limit := apiv2.getStartLimit(w, req)

var res []messagesResultList

messages, err := apiv2.config.Storage.List(start, limit)
if err != nil {
panic(err)
}

for _,mess := range *messages {
var resItem messagesResultList
var from *data.Path
var to []*data.Path
var contents map[string][]string

from = mess.From
to = mess.To
contents = mess.Content.Headers

resItem.ID = string(mess.ID)
resItem.FROM = strings.ToLower(from.Mailbox+"@"+from.Domain)

var to_string string
for _,to_rcpt := range to {
if to_string != "" { to_string += "," }
to_string += strings.ToLower(to_rcpt.Mailbox+"@"+to_rcpt.Domain)
}
resItem.TO = to_string
resItem.DATE = mess.Created.Format(time.RFC3339)

var subject string
for k,Header := range contents {
if(strings.HasPrefix(k,"Subject")){
subject += strings.Join(Header, " ")
}
}

resItem.SUBJECT = subject
res = append(res, resItem)
}
bytes, err := json.Marshal(res)
if( string(bytes) == "null"){ bytes = []byte("[{No emails}]") }

w.Header().Add("Content-Type", "text/json")
w.Write(bytes)
}

func (apiv2 *APIv2) search(w http.ResponseWriter, req *http.Request) {
log.Println("[APIv2] GET /api/v2/search")

Expand Down