Skip to content

Commit

Permalink
traslate sse notifications
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <jkoberg@owncloud.com>
  • Loading branch information
kobergj committed Jun 26, 2023
1 parent d155130 commit fa9492c
Show file tree
Hide file tree
Showing 29 changed files with 2,290 additions and 1 deletion.
3 changes: 3 additions & 0 deletions services/userlog/pkg/command/server.go
Expand Up @@ -16,6 +16,7 @@ import (
ogrpc "github.com/owncloud/ocis/v2/ocis-pkg/service/grpc"
"github.com/owncloud/ocis/v2/ocis-pkg/version"
ehsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/eventhistory/v0"
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
"github.com/owncloud/ocis/v2/services/userlog/pkg/config"
"github.com/owncloud/ocis/v2/services/userlog/pkg/config/parser"
"github.com/owncloud/ocis/v2/services/userlog/pkg/logging"
Expand Down Expand Up @@ -102,6 +103,7 @@ func Server(cfg *config.Config) *cli.Command {
}

hClient := ehsvc.NewEventHistoryService("com.owncloud.api.eventhistory", ogrpc.DefaultClient())
vClient := settingssvc.NewValueService("com.owncloud.api.settings", ogrpc.DefaultClient())

{
server, err := http.Server(
Expand All @@ -113,6 +115,7 @@ func Server(cfg *config.Config) *cli.Command {
http.Consumer(consumer),
http.GatewaySelector(gatewaySelector),
http.History(hClient),
http.Value(vClient),
http.RegisteredEvents(_registeredEvents),
)

Expand Down
9 changes: 9 additions & 0 deletions services/userlog/pkg/server/http/option.go
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
ehsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/eventhistory/v0"
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
"github.com/owncloud/ocis/v2/services/userlog/pkg/config"
"github.com/owncloud/ocis/v2/services/userlog/pkg/metrics"
"github.com/urfave/cli/v2"
Expand All @@ -29,6 +30,7 @@ type Options struct {
Consumer events.Consumer
GatewaySelector pool.Selectable[gateway.GatewayAPIClient]
HistoryClient ehsvc.EventHistoryService
ValueClient settingssvc.ValueService
RegisteredEvents []events.Unmarshaller
}

Expand Down Expand Up @@ -119,3 +121,10 @@ func RegisteredEvents(evs []events.Unmarshaller) Option {
o.RegisteredEvents = evs
}
}

// Value provides a function to configure the value service client
func Value(vs settingssvc.ValueService) Option {
return func(o *Options) {
o.ValueClient = vs
}
}
1 change: 1 addition & 0 deletions services/userlog/pkg/server/http/server.go
Expand Up @@ -76,6 +76,7 @@ func Server(opts ...Option) (http.Service, error) {
svc.Config(options.Config),
svc.HistoryClient(options.HistoryClient),
svc.GatewaySelector(options.GatewaySelector),
svc.ValueClient(options.ValueClient),
svc.RegisteredEvents(options.RegisteredEvents),
)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions services/userlog/pkg/service/options.go
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/go-chi/chi/v5"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
ehsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/eventhistory/v0"
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
"github.com/owncloud/ocis/v2/services/userlog/pkg/config"
"go-micro.dev/v4/store"
)
Expand All @@ -23,6 +24,7 @@ type Options struct {
Config *config.Config
HistoryClient ehsvc.EventHistoryService
GatewaySelector pool.Selectable[gateway.GatewayAPIClient]
ValueClient settingssvc.ValueService
RegisteredEvents []events.Unmarshaller
}

Expand Down Expand Up @@ -81,3 +83,9 @@ func RegisteredEvents(e []events.Unmarshaller) Option {
o.RegisteredEvents = e
}
}

func ValueClient(vs settingssvc.ValueService) Option {
return func(o *Options) {
o.ValueClient = vs
}
}
26 changes: 25 additions & 1 deletion services/userlog/pkg/service/service.go
Expand Up @@ -18,14 +18,20 @@ import (
"github.com/cs3org/reva/v2/pkg/utils"
"github.com/go-chi/chi/v5"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/owncloud/ocis/v2/ocis-pkg/middleware"
ehmsg "github.com/owncloud/ocis/v2/protogen/gen/ocis/messages/eventhistory/v0"
ehsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/eventhistory/v0"
settingssvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/settings/v0"
"github.com/owncloud/ocis/v2/services/settings/pkg/store/defaults"
"github.com/owncloud/ocis/v2/services/userlog/pkg/config"
"github.com/r3labs/sse/v2"
micrometadata "go-micro.dev/v4/metadata"
"go-micro.dev/v4/store"
"google.golang.org/grpc/metadata"
)

var _defaultLocale = "en"

// UserlogService is the service responsible for user activities
type UserlogService struct {
log log.Logger
Expand All @@ -34,6 +40,7 @@ type UserlogService struct {
cfg *config.Config
historyClient ehsvc.EventHistoryService
gatewaySelector pool.Selectable[gateway.GatewayAPIClient]
valueClient settingssvc.ValueService
sse *sse.Server
registeredEvents map[string]events.Unmarshaller
translationPath string
Expand Down Expand Up @@ -62,6 +69,7 @@ func NewUserlogService(opts ...Option) (*UserlogService, error) {
cfg: o.Config,
historyClient: o.HistoryClient,
gatewaySelector: o.GatewaySelector,
valueClient: o.ValueClient,
sse: sse.New(),
registeredEvents: make(map[string]events.Unmarshaller),
}
Expand Down Expand Up @@ -224,7 +232,7 @@ func (ul *UserlogService) DeleteEvents(userid string, evids []string) error {
}

func (ul *UserlogService) addEventToUser(userid string, event events.Event) error {
loc := "en" // TODO: where to get the locale from?
loc := getUserLang(context.Background(), userid, ul.valueClient)
ev, _ := NewConverter(loc, ul.gatewaySelector, ul.cfg.MachineAuthAPIKey, ul.cfg.Service.Name, ul.cfg.TranslationPath).ConvertEvent(event.ID, event.Event)
b, _ := json.Marshal(ev)

Expand Down Expand Up @@ -542,3 +550,19 @@ func editor(perms *storageprovider.ResourcePermissions) bool {
func manager(perms *storageprovider.ResourcePermissions) bool {
return perms.DenyGrant
}

func getUserLang(ctx context.Context, userid string, vs settingssvc.ValueService) string {
granteeCtx := micrometadata.Set(ctx, middleware.AccountID, userid)
if resp, err := vs.GetValueByUniqueIdentifiers(granteeCtx,
&settingssvc.GetValueByUniqueIdentifiersRequest{
AccountUuid: userid,
SettingId: defaults.SettingUUIDProfileLanguage,
},
); err == nil {
val := resp.GetValue().GetValue().GetListValue().GetValues()
if len(val) > 0 && val[0] != nil {
return val[0].GetStringValue()
}
}
return _defaultLocale
}
2 changes: 2 additions & 0 deletions vendor/github.com/r3labs/sse/v2/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions vendor/github.com/r3labs/sse/v2/.golangci.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions vendor/github.com/r3labs/sse/v2/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fa9492c

Please sign in to comment.