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

Get user preferred language #3507

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
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
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