Skip to content

Commit

Permalink
Get user preferred language (#3507)
Browse files Browse the repository at this point in the history
* get user language

* add changelog

* do not override default browser setting for lang
  • Loading branch information
gmgigi96 authored Dec 1, 2022
1 parent 878df62 commit 42dfc82
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
9 changes: 9 additions & 0 deletions changelog/unreleased/get-user-language.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Enhancement: Get user preferred language

The only way for an OCIS web user to change language
was to set it into the browser settings.
In the ocs user info response, a field `language` is added,
to change their language in the UI, regardless of the
browser settings.

https://github.com/cs3org/reva/pull/3507
35 changes: 35 additions & 0 deletions internal/http/services/owncloud/ocs/handlers/cloud/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,34 @@
package user

import (
"context"
"fmt"
"net/http"

preferences "github.com/cs3org/go-cs3apis/cs3/preferences/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/config"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/conversions"
"github.com/cs3org/reva/internal/http/services/owncloud/ocs/response"
ctxpkg "github.com/cs3org/reva/pkg/ctx"
"github.com/cs3org/reva/pkg/rgrpc/todo/pool"
)

// The Handler renders the user endpoint.
type Handler struct {
gatewayAddr string
}

// Init initializes this and any contained handlers.
func (h *Handler) Init(c *config.Config) {
h.gatewayAddr = c.GatewaySvc
}

const (
languageNamespace = "core"
languageKey = "lang"
)

// GetSelf handles GET requests on /cloud/user.
func (h *Handler) GetSelf(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
Expand All @@ -47,14 +63,33 @@ func (h *Handler) GetSelf(w http.ResponseWriter, r *http.Request) {
DisplayName: u.DisplayName,
Email: u.Mail,
UserType: conversions.UserTypeString(u.Id.Type),
Language: h.getLanguage(ctx),
})
}

func (h *Handler) getLanguage(ctx context.Context) string {
gw, err := pool.GetGatewayServiceClient(pool.Endpoint(h.gatewayAddr))
if err != nil {
return ""
}
res, err := gw.GetKey(ctx, &preferences.GetKeyRequest{
Key: &preferences.PreferenceKey{
Namespace: languageNamespace,
Key: languageKey,
},
})
if err != nil || res.Status.Code != rpc.Code_CODE_OK {
return ""
}
return res.GetVal()
}

// User holds user data.
type User struct {
// TODO needs better naming, clarify if we need a userid, a username or both
ID string `json:"id" xml:"id"`
DisplayName string `json:"display-name" xml:"display-name"`
Email string `json:"email" xml:"email"`
UserType string `json:"user-type" xml:"user-type"`
Language string `json:"language,omitempty" xml:"language,omitempty"`
}
1 change: 1 addition & 0 deletions internal/http/services/owncloud/ocs/ocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (s *svc) routerInit() error {
shareesHandler := new(sharees.Handler)
capabilitiesHandler.Init(s.c)
usersHandler.Init(s.c)
userHandler.Init(s.c)
configHandler.Init(s.c)
sharesHandler.Init(s.c)
shareesHandler.Init(s.c)
Expand Down

0 comments on commit 42dfc82

Please sign in to comment.