mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-07 16:16:37 +08:00
feat(message_gateway): expose PushRegistry contract
This commit is contained in:
@@ -0,0 +1,26 @@
|
|||||||
|
// Copyright 2026 Arctel.net
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
// Package contracts defines unified service interfaces and DTOs for cross-plugin communication.
|
||||||
|
package contracts
|
||||||
|
|
||||||
|
import "context"
|
||||||
|
|
||||||
|
type PushNotificationTemplate struct {
|
||||||
|
Title string
|
||||||
|
Content string
|
||||||
|
Level string
|
||||||
|
Ext map[string]any
|
||||||
|
}
|
||||||
|
|
||||||
|
type PushEventMeta struct {
|
||||||
|
Key string
|
||||||
|
Name string
|
||||||
|
Description string
|
||||||
|
DefaultTemplate PushNotificationTemplate
|
||||||
|
}
|
||||||
|
|
||||||
|
type PushRegistry interface {
|
||||||
|
RegisterBuiltInEvent(meta PushEventMeta)
|
||||||
|
SyncEvents(ctx context.Context) error
|
||||||
|
}
|
||||||
@@ -231,8 +231,9 @@ func (p *Plugin) Apply(ctx *core.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
// 9. Register built-in domain events
|
// 9. Register built-in domain events and provide PushRegistry
|
||||||
service.RegisterCustomEvents()
|
service.RegisterCustomEvents()
|
||||||
|
core.Provide[contracts.PushRegistry](ctx, service.PushRegistryAdapter{})
|
||||||
|
|
||||||
// 10. Register Settings Schemas
|
// 10. Register Settings Schemas
|
||||||
ctx.Settings().Register(extpoints.SettingSchema{
|
ctx.Settings().Register(extpoints.SettingSchema{
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
// Copyright 2026 Arctel.net
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package message_gateway_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"Wavelet/core"
|
||||||
|
"Wavelet/core/contracts"
|
||||||
|
"Wavelet/plugins/domain/message_gateway"
|
||||||
|
"Wavelet/plugins/domain/message_gateway/service"
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestPushRegistry(t *testing.T) {
|
||||||
|
ctx := core.NewContext(context.Background())
|
||||||
|
require.NoError(t, message_gateway.New().Apply(ctx))
|
||||||
|
|
||||||
|
registry, err := core.Inject[contracts.PushRegistry](ctx)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NotNil(t, registry)
|
||||||
|
|
||||||
|
const key = "test.push_registry.probe"
|
||||||
|
registry.RegisterBuiltInEvent(contracts.PushEventMeta{
|
||||||
|
Key: key,
|
||||||
|
Name: "Push Registry Probe",
|
||||||
|
Description: "observability probe for contracts.PushRegistry",
|
||||||
|
DefaultTemplate: contracts.PushNotificationTemplate{
|
||||||
|
Title: "Probe Title",
|
||||||
|
Content: "Probe Content",
|
||||||
|
Level: "INFO",
|
||||||
|
Ext: map[string]any{"source": "test"},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
found := false
|
||||||
|
for _, ev := range service.GetBuiltInEvents() {
|
||||||
|
if ev.Key != key {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
found = true
|
||||||
|
assert.Equal(t, "Push Registry Probe", ev.Name)
|
||||||
|
assert.Equal(t, "observability probe for contracts.PushRegistry", ev.Description)
|
||||||
|
assert.Equal(t, "Probe Title", ev.DefaultTemplate.Title)
|
||||||
|
assert.Equal(t, "Probe Content", ev.DefaultTemplate.Content)
|
||||||
|
assert.Equal(t, "INFO", ev.DefaultTemplate.Level)
|
||||||
|
assert.Equal(t, map[string]any{"source": "test"}, ev.DefaultTemplate.Ext)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
require.True(t, found, "registered key %q should be visible via GetBuiltInEvents", key)
|
||||||
|
}
|
||||||
@@ -49,6 +49,31 @@ func GetBuiltInEvents() []model.EventMetadata {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PushRegistryAdapter adapts contracts.PushRegistry onto the built-in event store.
|
||||||
|
type PushRegistryAdapter struct{}
|
||||||
|
|
||||||
|
func (PushRegistryAdapter) RegisterBuiltInEvent(meta contracts.PushEventMeta) {
|
||||||
|
RegisterBuiltInEvent(eventMetadataFromContract(meta))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (PushRegistryAdapter) SyncEvents(ctx context.Context) error {
|
||||||
|
return SyncEvents(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func eventMetadataFromContract(meta contracts.PushEventMeta) model.EventMetadata {
|
||||||
|
return model.EventMetadata{
|
||||||
|
Key: meta.Key,
|
||||||
|
Name: meta.Name,
|
||||||
|
Description: meta.Description,
|
||||||
|
DefaultTemplate: model.NotificationMessage{
|
||||||
|
Title: meta.DefaultTemplate.Title,
|
||||||
|
Content: meta.DefaultTemplate.Content,
|
||||||
|
Level: meta.DefaultTemplate.Level,
|
||||||
|
Ext: meta.DefaultTemplate.Ext,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SyncBuiltInEvents seeds a database row for every registered built-in event.
|
// SyncBuiltInEvents seeds a database row for every registered built-in event.
|
||||||
func SyncBuiltInEvents(ctx context.Context) error {
|
func SyncBuiltInEvents(ctx context.Context) error {
|
||||||
for _, meta := range GetBuiltInEvents() {
|
for _, meta := range GetBuiltInEvents() {
|
||||||
|
|||||||
Reference in New Issue
Block a user