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

     1  package settings
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/cozy/cozy-stack/model/instance/lifecycle"
     8  	"github.com/cozy/cozy-stack/pkg/consts"
     9  	"github.com/cozy/cozy-stack/pkg/couchdb"
    10  	"github.com/cozy/cozy-stack/pkg/jsonapi"
    11  	"github.com/cozy/cozy-stack/web/auth"
    12  	"github.com/cozy/cozy-stack/web/middlewares"
    13  	"github.com/labstack/echo/v4"
    14  	"github.com/mssola/user_agent"
    15  )
    16  
    17  type apiContext struct {
    18  	doc map[string]interface{}
    19  }
    20  
    21  func (c *apiContext) ID() string                             { return consts.ContextSettingsID }
    22  func (c *apiContext) Rev() string                            { return "" }
    23  func (c *apiContext) DocType() string                        { return consts.Settings }
    24  func (c *apiContext) Fetch(field string) []string            { return nil }
    25  func (c *apiContext) Clone() couchdb.Doc                     { return c }
    26  func (c *apiContext) SetID(id string)                        {}
    27  func (c *apiContext) SetRev(rev string)                      {}
    28  func (c *apiContext) Relationships() jsonapi.RelationshipMap { return nil }
    29  func (c *apiContext) Included() []jsonapi.Object             { return nil }
    30  func (c *apiContext) Links() *jsonapi.LinksList {
    31  	return &jsonapi.LinksList{Self: "/settings/context"}
    32  }
    33  
    34  func (c *apiContext) MarshalJSON() ([]byte, error) {
    35  	return json.Marshal(c.doc)
    36  }
    37  
    38  func (h *HTTPHandler) onboarded(c echo.Context) error {
    39  	i := middlewares.GetInstance(c)
    40  	if !middlewares.IsLoggedIn(c) {
    41  		return c.Redirect(http.StatusSeeOther, i.PageURL("/auth/login", nil))
    42  	}
    43  	return finishOnboarding(c, "", true)
    44  }
    45  
    46  func finishOnboarding(c echo.Context, redirection string, acceptHTML bool) error {
    47  	i := middlewares.GetInstance(c)
    48  	if !i.OnboardingFinished {
    49  		t := true
    50  		err := lifecycle.Patch(i, &lifecycle.Options{OnboardingFinished: &t})
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  	redirect := i.OnboardedRedirection().String()
    56  	if redirection != "" {
    57  		if u, err := auth.AppRedirection(i, redirection); err == nil {
    58  			redirect = u.String()
    59  		}
    60  	}
    61  
    62  	rawUserAgent := c.Request().UserAgent()
    63  	ua := user_agent.New(rawUserAgent)
    64  	if ua.Mobile() {
    65  		redirect = i.PageURL("/settings/install_flagship_app", nil)
    66  	}
    67  
    68  	if acceptHTML {
    69  		return c.Redirect(http.StatusSeeOther, redirect)
    70  	}
    71  	return c.JSON(http.StatusOK, echo.Map{"redirect": redirect})
    72  }
    73  
    74  func (h *HTTPHandler) context(c echo.Context) error {
    75  	// Any request with a token can ask for the context (no permissions are required)
    76  	if _, err := middlewares.GetPermission(c); err != nil {
    77  		return echo.NewHTTPError(http.StatusForbidden)
    78  	}
    79  
    80  	i := middlewares.GetInstance(c)
    81  	doc := &apiContext{i.GetContextWithSponsorships()}
    82  	return jsonapi.Data(c, http.StatusOK, doc, nil)
    83  }