feat: text query (#2735)

* feat: change mail template to new query side

* feat: adminapi message text

* feat: adminapi message text

* feat: adminapi message text

* feat: message texts

* feat: admin texts

* feat: tests

* feat: tests

* feat: custom login text on adminapi

* feat: custom login text

* feat: custom login text

* feat: message text prepare test

* feat: login text texts

* feat: custom login text

* merge main

* fix go.sum

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
Fabi
2021-12-16 15:21:37 +01:00
committed by GitHub
parent a43e1fc34a
commit c5d6325897
53 changed files with 2250 additions and 2196 deletions

View File

@@ -1,133 +0,0 @@
package handler
import (
"github.com/caos/logging"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v1"
es_models "github.com/caos/zitadel/internal/eventstore/v1/models"
"github.com/caos/zitadel/internal/eventstore/v1/query"
"github.com/caos/zitadel/internal/eventstore/v1/spooler"
iam_es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
iam_model "github.com/caos/zitadel/internal/iam/repository/view/model"
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
)
type CustomText struct {
handler
subscription *v1.Subscription
}
func newCustomText(handler handler) *CustomText {
h := &CustomText{
handler: handler,
}
h.subscribe()
return h
}
func (m *CustomText) subscribe() {
m.subscription = m.es.Subscribe(m.AggregateTypes()...)
go func() {
for event := range m.subscription.Events {
query.ReduceEvent(m, event)
}
}()
}
const (
customTextTable = "notification.custom_texts"
)
func (m *CustomText) ViewModel() string {
return customTextTable
}
func (m *CustomText) Subscription() *v1.Subscription {
return m.subscription
}
func (_ *CustomText) AggregateTypes() []es_models.AggregateType {
return []es_models.AggregateType{model.OrgAggregate, iam_es_model.IAMAggregate}
}
func (p *CustomText) CurrentSequence() (uint64, error) {
sequence, err := p.view.GetLatestCustomTextSequence()
if err != nil {
return 0, err
}
return sequence.CurrentSequence, nil
}
func (m *CustomText) EventQuery() (*es_models.SearchQuery, error) {
sequence, err := m.view.GetLatestCustomTextSequence()
if err != nil {
return nil, err
}
return es_models.NewSearchQuery().
AggregateTypeFilter(m.AggregateTypes()...).
LatestSequenceFilter(sequence.CurrentSequence), nil
}
func (m *CustomText) Reduce(event *es_models.Event) (err error) {
switch event.AggregateType {
case model.OrgAggregate, iam_es_model.IAMAggregate:
err = m.processCustomText(event)
}
return err
}
func (m *CustomText) processCustomText(event *es_models.Event) (err error) {
customText := new(iam_model.CustomTextView)
switch event.Type {
case iam_es_model.CustomTextSet, model.CustomTextSet:
text := new(iam_model.CustomTextView)
err = text.SetData(event)
if err != nil {
return err
}
customText, err = m.view.CustomTextByIDs(event.AggregateID, text.Template, text.Key, text.Language)
if err != nil && !caos_errs.IsNotFound(err) {
return err
}
if caos_errs.IsNotFound(err) {
err = nil
customText = new(iam_model.CustomTextView)
customText.Language = text.Language
customText.Template = text.Template
customText.CreationDate = event.CreationDate
}
err = customText.AppendEvent(event)
case iam_es_model.CustomTextRemoved, model.CustomTextRemoved:
text := new(iam_model.CustomTextView)
err = text.SetData(event)
if err != nil {
return err
}
return m.view.DeleteCustomText(event.AggregateID, text.Template, text.Language, text.Key, event)
case iam_es_model.CustomTextMessageRemoved, model.CustomTextMessageRemoved:
text := new(iam_model.CustomTextView)
err = text.SetData(event)
if err != nil {
return err
}
return m.view.DeleteCustomTextTemplate(event.AggregateID, text.Template, text.Language, event)
default:
return m.view.ProcessedCustomTextSequence(event)
}
if err != nil {
return err
}
return m.view.PutCustomText(customText, event)
}
func (m *CustomText) OnError(event *es_models.Event, err error) error {
logging.LogWithFields("SPOOL-3m912", "id", event.AggregateID).WithError(err).Warn("something went wrong in custom text handler")
return spooler.HandleError(event, err, m.view.GetLatestCustomTextFailedEvent, m.view.ProcessedCustomTextFailedEvent, m.view.ProcessedCustomTextSequence, m.errorCountUntilSkip)
}
func (o *CustomText) OnSuccess() error {
return spooler.HandleSuccess(o.view.UpdateCustomTextSpoolerRunTimestamp)
}

View File

@@ -5,13 +5,14 @@ import (
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/query"
"github.com/caos/zitadel/internal/command"
sd "github.com/caos/zitadel/internal/config/systemdefaults"
"github.com/caos/zitadel/internal/config/types"
"github.com/caos/zitadel/internal/crypto"
v1 "github.com/caos/zitadel/internal/eventstore/v1"
"github.com/caos/zitadel/internal/eventstore/v1/query"
queryv1 "github.com/caos/zitadel/internal/eventstore/v1/query"
"github.com/caos/zitadel/internal/notification/repository/eventsourcing/view"
)
@@ -34,20 +35,20 @@ func (h *handler) Eventstore() v1.Eventstore {
return h.es
}
func Register(configs Configs, bulkLimit, errorCount uint64, view *view.View, es v1.Eventstore, command *command.Commands, systemDefaults sd.SystemDefaults, dir http.FileSystem, apiDomain string) []query.Handler {
func Register(configs Configs, bulkLimit, errorCount uint64, view *view.View, es v1.Eventstore, command *command.Commands, queries *query.Queries, systemDefaults sd.SystemDefaults, dir http.FileSystem, apiDomain string) []queryv1.Handler {
aesCrypto, err := crypto.NewAESCrypto(systemDefaults.UserVerificationKey)
if err != nil {
logging.Log("HANDL-s90ew").WithError(err).Debug("error create new aes crypto")
}
return []query.Handler{
return []queryv1.Handler{
newNotifyUser(
handler{view, bulkLimit, configs.cycleDuration("User"), errorCount, es},
systemDefaults.IamID,
),
newCustomText(handler{view, bulkLimit, configs.cycleDuration("CustomText"), errorCount, es}),
newNotification(
handler{view, bulkLimit, configs.cycleDuration("Notification"), errorCount, es},
command,
queries,
systemDefaults,
aesCrypto,
dir,

View File

@@ -7,8 +7,6 @@ import (
"time"
"github.com/caos/logging"
"golang.org/x/text/language"
"github.com/caos/zitadel/internal/api/authz"
"github.com/caos/zitadel/internal/command"
sd "github.com/caos/zitadel/internal/config/systemdefaults"
@@ -17,12 +15,13 @@ import (
"github.com/caos/zitadel/internal/errors"
v1 "github.com/caos/zitadel/internal/eventstore/v1"
"github.com/caos/zitadel/internal/eventstore/v1/models"
"github.com/caos/zitadel/internal/eventstore/v1/query"
queryv1 "github.com/caos/zitadel/internal/eventstore/v1/query"
"github.com/caos/zitadel/internal/eventstore/v1/spooler"
"github.com/caos/zitadel/internal/i18n"
iam_model "github.com/caos/zitadel/internal/iam/model"
iam_es_model "github.com/caos/zitadel/internal/iam/repository/view/model"
"github.com/caos/zitadel/internal/notification/types"
"github.com/caos/zitadel/internal/query"
user_repo "github.com/caos/zitadel/internal/repository/user"
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
"github.com/caos/zitadel/internal/user/repository/view"
@@ -30,14 +29,10 @@ import (
)
const (
notificationTable = "notification.notifications"
NotifyUserID = "NOTIFICATION"
labelPolicyTableOrg = "management.label_policies"
labelPolicyTableDef = "adminapi.label_policies"
mailTemplateTableOrg = "management.mail_templates"
mailTemplateTableDef = "adminapi.mail_templates"
messageTextTableOrg = "management.message_texts"
messageTextTableDef = "adminapi.message_texts"
notificationTable = "notification.notifications"
NotifyUserID = "NOTIFICATION"
labelPolicyTableOrg = "management.label_policies"
labelPolicyTableDef = "adminapi.label_policies"
)
type Notification struct {
@@ -48,11 +43,13 @@ type Notification struct {
statikDir http.FileSystem
subscription *v1.Subscription
apiDomain string
queries *query.Queries
}
func newNotification(
handler handler,
command *command.Commands,
query *query.Queries,
defaults sd.SystemDefaults,
aesCrypto crypto.EncryptionAlgorithm,
statikDir http.FileSystem,
@@ -65,6 +62,7 @@ func newNotification(
statikDir: statikDir,
AesCrypto: aesCrypto,
apiDomain: apiDomain,
queries: query,
}
h.subscribe()
@@ -76,7 +74,7 @@ func (k *Notification) subscribe() {
k.subscription = k.es.Subscribe(k.AggregateTypes()...)
go func() {
for event := range k.subscription.Events {
query.ReduceEvent(k, event)
queryv1.ReduceEvent(k, event)
}
}()
}
@@ -424,21 +422,20 @@ func (n *Notification) getLabelPolicy(ctx context.Context) (*iam_model.LabelPoli
}
// Read organization specific template
func (n *Notification) getMailTemplate(ctx context.Context) (*iam_model.MailTemplateView, error) {
func (n *Notification) getMailTemplate(ctx context.Context) (*query.MailTemplate, error) {
// read from Org
template, err := n.view.MailTemplateByAggregateID(authz.GetCtxData(ctx).OrgID, mailTemplateTableOrg)
template, err := n.queries.MailTemplateByOrg(ctx, authz.GetCtxData(ctx).OrgID)
if errors.IsNotFound(err) {
// read from default
template, err = n.view.MailTemplateByAggregateID(n.systemDefaults.IamID, mailTemplateTableDef)
template, err = n.queries.DefaultMailTemplate(ctx)
if err != nil {
return nil, err
}
template.Default = true
}
if err != nil {
return nil, err
}
return iam_es_model.MailTemplateViewToModel(template), err
return template, err
}
func (n *Notification) getTranslatorWithOrgTexts(orgID, textType string) (*i18n.Translator, error) {
@@ -446,52 +443,27 @@ func (n *Notification) getTranslatorWithOrgTexts(orgID, textType string) (*i18n.
if err != nil {
return nil, err
}
allCustomTexts, err := n.view.CustomTextsByAggregateIDAndTemplate(domain.IAMID, textType)
ctx := context.TODO()
allCustomTexts, err := n.queries.CustomTextListByTemplate(ctx, domain.IAMID, textType)
if err != nil {
return translator, nil
}
customTexts, err := n.view.CustomTextsByAggregateIDAndTemplate(orgID, textType)
customTexts, err := n.queries.CustomTextListByTemplate(ctx, orgID, textType)
if err != nil {
return translator, nil
}
allCustomTexts = append(allCustomTexts, customTexts...)
allCustomTexts.CustomTexts = append(allCustomTexts.CustomTexts, customTexts.CustomTexts...)
for _, text := range allCustomTexts {
for _, text := range allCustomTexts.CustomTexts {
msg := i18n.Message{
ID: text.Template + "." + text.Key,
Text: text.Text,
}
translator.AddMessages(language.Make(text.Language), msg)
translator.AddMessages(text.Language, msg)
}
return translator, nil
}
// Read organization specific texts
func (n *Notification) getMessageText(user *model.NotifyUser, textType, lang string) (*iam_model.MessageTextView, error) {
langTag := language.Make(lang)
if langTag == language.Und {
langTag = language.English
}
langBase, _ := langTag.Base()
defaultMessageText, err := n.view.MessageTextByIDs(n.systemDefaults.IamID, textType, langBase.String(), messageTextTableDef)
if err != nil {
return nil, err
}
defaultMessageText.Default = true
// read from Org
orgMessageText, err := n.view.MessageTextByIDs(user.ResourceOwner, textType, langBase.String(), messageTextTableOrg)
if errors.IsNotFound(err) {
return iam_es_model.MessageTextViewToModel(defaultMessageText), nil
}
if err != nil {
return nil, err
}
mergedText := mergeMessageTexts(defaultMessageText, orgMessageText)
return iam_es_model.MessageTextViewToModel(mergedText), err
}
func (n *Notification) getUserByID(userID string) (*model.NotifyUser, error) {
user, usrErr := n.view.NotifyUserByID(userID)
if usrErr != nil && !errors.IsNotFound(usrErr) {
@@ -515,28 +487,3 @@ func (n *Notification) getUserByID(userID string) (*model.NotifyUser, error) {
}
return &userCopy, nil
}
func mergeMessageTexts(defaultText *iam_es_model.MessageTextView, orgText *iam_es_model.MessageTextView) *iam_es_model.MessageTextView {
if orgText.Subject == "" {
orgText.Subject = defaultText.Subject
}
if orgText.Title == "" {
orgText.Title = defaultText.Title
}
if orgText.PreHeader == "" {
orgText.PreHeader = defaultText.PreHeader
}
if orgText.Text == "" {
orgText.Text = defaultText.Text
}
if orgText.Greeting == "" {
orgText.Greeting = defaultText.Greeting
}
if orgText.ButtonText == "" {
orgText.ButtonText = defaultText.ButtonText
}
if orgText.FooterText == "" {
orgText.FooterText = defaultText.FooterText
}
return orgText
}