Files
opencloud/services/groupware/pkg/groupware/groupware_api_mailbox.go
Pascal Bleser 3e7c4a023b Groupware: refactor jmap package, implement Email/set, EmailSubmission
* refactor the jmap package to split it into several files as the
   jmap.api.go file was becoming too unwieldy

 * refactor the Groupware handler function response to be a Response
   object, to be more future-proof and avoid adding more and more
   return parameters while handling "no content" response as well

 * more godoc for the JMAP model

 * add Email creation, updating, deleting (Email/set,
   EmailSubmission/set)

 * add endpoints
   - POST /accounts/{accountid}/messages
   - PATCH|PUT /accounts/{accountid}/messages/{messageid}
   - DELETE /accounts/{accountid}/messages/{messageid}
2026-06-11 15:01:35 +02:00

133 lines
3.4 KiB
Go

package groupware
import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/opencloud-eu/opencloud/pkg/jmap"
)
// When the request succeeds.
// swagger:response MailboxResponse200
type SwaggerGetMailboxById200 struct {
// in: body
Body struct {
*jmap.Mailbox
}
}
// swagger:route GET /accounts/{account}/mailboxes/{id} mailboxes_by_id
// Get a specific mailbox by its identifier.
//
// A Mailbox represents a named set of Emails.
// This is the primary mechanism for organising Emails within an account.
// It is analogous to a folder or a label in other systems.
//
// responses:
//
// 200: MailboxResponse200
// 400: ErrorResponse400
// 404: ErrorResponse404
// 500: ErrorResponse500
func (g Groupware) GetMailbox(w http.ResponseWriter, r *http.Request) {
mailboxId := chi.URLParam(r, UriParamMailboxId)
if mailboxId == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
g.respond(w, r, func(req Request) Response {
res, err := g.jmap.GetMailbox(req.GetAccountId(), req.session, req.ctx, req.logger, []string{mailboxId})
if err != nil {
return req.errorResponseFromJmap(err)
}
if len(res.List) == 1 {
return response(res.List[0], res.State)
} else {
return notFoundResponse(res.State)
}
})
}
// swagger:parameters mailboxes
type SwaggerMailboxesParams struct {
// The name of the mailbox, with substring matching.
// in: query
Name string `json:"name,omitempty"`
// The role of the mailbox.
// in: query
Role string `json:"role,omitempty"`
// Whether the mailbox is subscribed by the user or not.
// When omitted, the subscribed and unsubscribed mailboxes are returned.
// in: query
Subscribed bool `json:"subscribed,omitempty"`
}
// When the request succeeds.
// swagger:response MailboxesResponse200
type SwaggerMailboxesResponse200 struct {
// in: body
Body []jmap.Mailbox
}
// swagger:route GET /accounts/{account}/mailboxes mailboxes
// Get the list of all the mailboxes of an account.
//
// A Mailbox represents a named set of Emails.
// This is the primary mechanism for organising Emails within an account.
// It is analogous to a folder or a label in other systems.
//
// When none of the query parameters are specified, all the mailboxes are returned.
//
// responses:
//
// 200: MailboxesResponse200
// 400: ErrorResponse400
// 500: ErrorResponse500
func (g Groupware) GetMailboxes(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
var filter jmap.MailboxFilterCondition
hasCriteria := false
name := q.Get("name")
if name != "" {
filter.Name = name
hasCriteria = true
}
role := q.Get("role")
if role != "" {
filter.Role = role
hasCriteria = true
}
subscribed := q.Get("subscribed")
if subscribed != "" {
b, err := strconv.ParseBool(subscribed)
if err != nil {
// TODO proper response object
w.WriteHeader(http.StatusBadRequest)
return
}
filter.IsSubscribed = &b
hasCriteria = true
}
g.respond(w, r, func(req Request) Response {
if hasCriteria {
mailboxes, err := g.jmap.SearchMailboxes(req.GetAccountId(), req.session, req.ctx, req.logger, filter)
if err != nil {
return req.errorResponseFromJmap(err)
}
return response(mailboxes.Mailboxes, mailboxes.State)
} else {
mailboxes, err := g.jmap.GetAllMailboxes(req.GetAccountId(), req.session, req.ctx, req.logger)
if err != nil {
return req.errorResponseFromJmap(err)
}
return response(mailboxes.List, mailboxes.State)
}
})
}