feat: add im kafka admin integration
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
// Package queues
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2026 HotGo
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
package queues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/imkafka"
|
||||
"hotgo/internal/library/queue"
|
||||
)
|
||||
|
||||
func init() {
|
||||
queue.RegisterConsumer(ImAdminCommandResult)
|
||||
}
|
||||
|
||||
var ImAdminCommandResult = &qImAdminCommandResult{}
|
||||
|
||||
type qImAdminCommandResult struct{}
|
||||
|
||||
func (q *qImAdminCommandResult) GetTopic() string {
|
||||
return consts.QueueImAdminCommandResultTopic
|
||||
}
|
||||
|
||||
func (q *qImAdminCommandResult) Handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
var envelope imkafka.Envelope
|
||||
if err = json.Unmarshal(mqMsg.Body, &envelope); err == nil && envelope.EventID != "" && len(envelope.Payload) > 0 {
|
||||
var result imkafka.CommandResult
|
||||
if err = json.Unmarshal(envelope.Payload, &result); err != nil {
|
||||
return err
|
||||
}
|
||||
if result.CommandEventID == "" {
|
||||
result.CommandEventID = envelope.ParentEventID
|
||||
}
|
||||
if result.TenantCode == "" {
|
||||
result.TenantCode = envelope.TenantCode
|
||||
}
|
||||
if result.EventID == "" {
|
||||
result.EventID = envelope.EventID
|
||||
}
|
||||
return imkafka.ApplyCommandResult(ctx, result)
|
||||
}
|
||||
|
||||
var result imkafka.CommandResult
|
||||
if err = json.Unmarshal(mqMsg.Body, &result); err != nil {
|
||||
return err
|
||||
}
|
||||
return imkafka.ApplyCommandResult(ctx, result)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package queues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/imkafka"
|
||||
"hotgo/internal/library/queue"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
func init() {
|
||||
queue.RegisterConsumer(ImAuditEvent)
|
||||
}
|
||||
|
||||
var ImAuditEvent = &qImAuditEvent{}
|
||||
|
||||
type qImAuditEvent struct{}
|
||||
|
||||
func (q *qImAuditEvent) GetTopic() string {
|
||||
return consts.QueueImAuditEventTopic
|
||||
}
|
||||
|
||||
func (q *qImAuditEvent) Handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
var envelope imkafka.Envelope
|
||||
if err = json.Unmarshal(mqMsg.Body, &envelope); err != nil {
|
||||
return err
|
||||
}
|
||||
payload := map[string]any{}
|
||||
if len(envelope.Payload) > 0 {
|
||||
_ = json.Unmarshal(envelope.Payload, &payload)
|
||||
}
|
||||
_, err = g.DB().Model("im_audit_events").Safe().Ctx(ctx).Data(g.Map{
|
||||
"event_id": envelope.EventID,
|
||||
"tenant_code": envelope.TenantCode,
|
||||
"event_type": envelope.EventType,
|
||||
"biz_type": envelope.BizType,
|
||||
"biz_id": envelope.BizID,
|
||||
"actor_type": firstString(payload["actor_type"]),
|
||||
"actor_id": firstString(payload["actor_id"]),
|
||||
"action": firstString(payload["action"]),
|
||||
"resource": firstString(payload["resource"]),
|
||||
"resource_id": firstString(payload["resource_id"]),
|
||||
"result": firstString(payload["result"]),
|
||||
"reason": firstString(payload["reason"]),
|
||||
"payload": string(envelope.Payload),
|
||||
"envelope": string(mqMsg.Body),
|
||||
"occurred_at": envelope.OccurredAt,
|
||||
"received_at": gtime.Now(),
|
||||
}).InsertIgnore()
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package queues
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/library/imkafka"
|
||||
"hotgo/internal/library/queue"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
)
|
||||
|
||||
func init() {
|
||||
queue.RegisterConsumer(ImDeadLetter)
|
||||
}
|
||||
|
||||
var ImDeadLetter = &qImDeadLetter{}
|
||||
|
||||
type qImDeadLetter struct{}
|
||||
|
||||
func (q *qImDeadLetter) GetTopic() string {
|
||||
return consts.QueueImDeadLetterTopic
|
||||
}
|
||||
|
||||
func (q *qImDeadLetter) Handle(ctx context.Context, mqMsg queue.MqMsg) (err error) {
|
||||
var envelope imkafka.Envelope
|
||||
if err = json.Unmarshal(mqMsg.Body, &envelope); err != nil {
|
||||
return err
|
||||
}
|
||||
payload := map[string]any{}
|
||||
if len(envelope.Payload) > 0 {
|
||||
_ = json.Unmarshal(envelope.Payload, &payload)
|
||||
}
|
||||
_, err = g.DB().Model("im_kafka_dead_letter_events").Safe().Ctx(ctx).Data(g.Map{
|
||||
"event_id": envelope.EventID,
|
||||
"parent_event_id": envelope.ParentEventID,
|
||||
"tenant_code": envelope.TenantCode,
|
||||
"event_type": envelope.EventType,
|
||||
"biz_type": envelope.BizType,
|
||||
"biz_id": envelope.BizID,
|
||||
"source_service": envelope.SourceService,
|
||||
"target_service": envelope.TargetService,
|
||||
"dead_id": firstString(payload["dead_id"], payload["deadId"], payload["DeadID"]),
|
||||
"reason": firstString(payload["reason"], payload["Reason"], payload["last_reason"], payload["lastReason"], payload["LastReason"]),
|
||||
"message": firstString(payload["message"], payload["Message"], payload["last_error"], payload["lastError"], payload["LastError"]),
|
||||
"payload": string(envelope.Payload),
|
||||
"envelope": string(mqMsg.Body),
|
||||
"received_at": gtime.Now(),
|
||||
}).InsertIgnore()
|
||||
return err
|
||||
}
|
||||
|
||||
func stringValue(v any) string {
|
||||
if v == nil {
|
||||
return ""
|
||||
}
|
||||
switch value := v.(type) {
|
||||
case string:
|
||||
return value
|
||||
default:
|
||||
b, _ := json.Marshal(value)
|
||||
return string(b)
|
||||
}
|
||||
}
|
||||
|
||||
func firstString(values ...any) string {
|
||||
for _, v := range values {
|
||||
if text := stringValue(v); text != "" && text != "null" {
|
||||
return text
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
Reference in New Issue
Block a user