github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/web/settings/external_ties.go (about)

     1  package settings
     2  
     3  import (
     4  	"net/http"
     5  
     6  	csettings "github.com/cozy/cozy-stack/model/settings"
     7  	"github.com/cozy/cozy-stack/pkg/consts"
     8  	"github.com/cozy/cozy-stack/pkg/couchdb"
     9  	"github.com/cozy/cozy-stack/pkg/jsonapi"
    10  	"github.com/cozy/cozy-stack/web/middlewares"
    11  	"github.com/labstack/echo/v4"
    12  )
    13  
    14  type apiExternalTies struct {
    15  	*csettings.ExternalTies
    16  
    17  	DocID string `json:"_id,omitempty"`
    18  }
    19  
    20  func (c *apiExternalTies) ID() string                             { return c.DocID }
    21  func (c *apiExternalTies) Rev() string                            { return "" }
    22  func (c *apiExternalTies) DocType() string                        { return consts.Settings }
    23  func (c *apiExternalTies) Clone() couchdb.Doc                     { cloned := *c; return &cloned }
    24  func (c *apiExternalTies) SetID(id string)                        { c.DocID = id }
    25  func (c *apiExternalTies) SetRev(rev string)                      {}
    26  func (c *apiExternalTies) Relationships() jsonapi.RelationshipMap { return nil }
    27  func (c *apiExternalTies) Included() []jsonapi.Object             { return nil }
    28  func (c *apiExternalTies) Links() *jsonapi.LinksList {
    29  	return &jsonapi.LinksList{Self: "/settings/capabilities"}
    30  }
    31  func (c *apiExternalTies) Fetch(field string) []string { return nil }
    32  
    33  func NewExternalTies(ties *csettings.ExternalTies) jsonapi.Object {
    34  	return &apiExternalTies{
    35  		ExternalTies: ties,
    36  		DocID:        consts.ExternalTiesID,
    37  	}
    38  }
    39  
    40  func (h *HTTPHandler) getExternalTies(c echo.Context) error {
    41  	if !middlewares.IsLoggedIn(c) && !middlewares.HasWebAppToken(c) {
    42  		return middlewares.ErrForbidden
    43  	}
    44  	inst := middlewares.GetInstance(c)
    45  
    46  	ties, err := h.svc.GetExternalTies(inst)
    47  	if err != nil {
    48  		return jsonapi.InternalServerError(err)
    49  	}
    50  
    51  	doc := NewExternalTies(ties)
    52  
    53  	return jsonapi.Data(c, http.StatusOK, doc, nil)
    54  }