migrate from github
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package domain_event
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/XingfenD/yoresee_doc/pkg/constant"
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
const (
|
||||
DocumentActionUpsert = "upsert"
|
||||
DocumentActionDelete = "delete"
|
||||
)
|
||||
|
||||
type DocumentSyncEvent struct {
|
||||
Action string `json:"action"`
|
||||
ExternalID string `json:"external_id"`
|
||||
}
|
||||
|
||||
func DocumentSyncTopic() string {
|
||||
return constant.SearchSyncTopicDefault
|
||||
}
|
||||
|
||||
func PublishDocumentUpsertEvent(ctx context.Context, externalID string) error {
|
||||
externalID = strings.TrimSpace(externalID)
|
||||
if externalID == "" {
|
||||
return fmt.Errorf("external_id is empty")
|
||||
}
|
||||
return publishJSONToRabbitMQ(ctx, DocumentSyncTopic(), DocumentSyncEvent{
|
||||
Action: DocumentActionUpsert,
|
||||
ExternalID: externalID,
|
||||
})
|
||||
}
|
||||
|
||||
func DecodeDocumentSyncEvent(data []byte) (*DocumentSyncEvent, error) {
|
||||
var evt DocumentSyncEvent
|
||||
if err := sonic.Unmarshal(data, &evt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
evt.Action = strings.TrimSpace(evt.Action)
|
||||
evt.ExternalID = strings.TrimSpace(evt.ExternalID)
|
||||
if evt.Action == "" {
|
||||
evt.Action = DocumentActionUpsert
|
||||
}
|
||||
if evt.ExternalID == "" {
|
||||
return nil, fmt.Errorf("external_id is empty")
|
||||
}
|
||||
return &evt, nil
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package domain_event
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/XingfenD/yoresee_doc/pkg/constant"
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
type NotificationCreateEvent struct {
|
||||
ReceiverExternalIDs []string `json:"receiver_external_ids"`
|
||||
Type string `json:"type"`
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
PayloadJSON string `json:"payload_json"`
|
||||
}
|
||||
|
||||
func NotificationCreateTopic() string {
|
||||
return constant.NotificationTopicDefault
|
||||
}
|
||||
|
||||
func PublishNotificationCreateEvent(ctx context.Context, evt NotificationCreateEvent) error {
|
||||
return publishJSONToRabbitMQ(ctx, NotificationCreateTopic(), evt)
|
||||
}
|
||||
|
||||
func DecodeNotificationCreateEvent(data []byte) (*NotificationCreateEvent, error) {
|
||||
var evt NotificationCreateEvent
|
||||
if err := sonic.Unmarshal(data, &evt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cleanReceiverIDs := make([]string, 0, len(evt.ReceiverExternalIDs))
|
||||
for _, receiverID := range evt.ReceiverExternalIDs {
|
||||
receiverID = strings.TrimSpace(receiverID)
|
||||
if receiverID == "" {
|
||||
continue
|
||||
}
|
||||
cleanReceiverIDs = append(cleanReceiverIDs, receiverID)
|
||||
}
|
||||
evt.ReceiverExternalIDs = cleanReceiverIDs
|
||||
return &evt, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package domain_event
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/XingfenD/yoresee_doc/internal/service/mq_service"
|
||||
"github.com/XingfenD/yoresee_doc/pkg/mq"
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
func publishJSONToRabbitMQ(ctx context.Context, topic string, payload any) error {
|
||||
data, err := sonic.Marshal(payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return mq_service.MQSvc.Publish(ctx, mq.BackendRabbitMQ, mq.PublishMessage{
|
||||
Topic: topic,
|
||||
Body: data,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user