feat: publish text message for group announcements
This commit is contained in:
@@ -5598,6 +5598,11 @@ func serveIMGroupAnnouncements(wrt http.ResponseWriter, req *http.Request, topic
|
||||
writeIMRelationError(wrt, req, err)
|
||||
return
|
||||
}
|
||||
if err = publishGroupAnnouncementTextMessage(req.Context(), principal.TenantID, topic, principal.Uid, announcement); err != nil {
|
||||
logs.Warn.Println("im group announcement: failed to publish text message", principal.TenantID, topic, announcement.AnnouncementID, err)
|
||||
writeIMRelationError(wrt, req, err)
|
||||
return
|
||||
}
|
||||
dispatchIMGroupEvents(principal.TenantID)
|
||||
writeIMSuccess(wrt, req, groupAnnouncementPayloadWithPermissions(topic, announcement, selfSub))
|
||||
case strings.HasPrefix(tail, "announcements/") && strings.HasSuffix(tail, "/read") && req.Method == http.MethodPost:
|
||||
@@ -5620,6 +5625,53 @@ func serveIMGroupAnnouncements(wrt http.ResponseWriter, req *http.Request, topic
|
||||
}
|
||||
}
|
||||
|
||||
func publishGroupAnnouncementTextMessage(ctx context.Context, tenantID types.TenantID, topic string, author types.Uid, ann *types.GroupAnnouncement) error {
|
||||
if ann == nil || strings.TrimSpace(topic) == "" || author.IsZero() {
|
||||
return types.ErrMalformed
|
||||
}
|
||||
content := strings.TrimSpace(ann.Content)
|
||||
if content == "" {
|
||||
return types.ErrMalformed
|
||||
}
|
||||
if store.Messages == nil || store.Topics == nil {
|
||||
return types.ErrMalformed
|
||||
}
|
||||
topicRecord, err := store.Topics.Get(tenantID, topic)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if topicRecord == nil {
|
||||
return types.ErrNotFound
|
||||
}
|
||||
now := ann.PublishedAt
|
||||
if now.IsZero() {
|
||||
now = time.Now().UTC().Round(time.Millisecond)
|
||||
}
|
||||
msg := &types.Message{
|
||||
ObjHeader: types.ObjHeader{CreatedAt: now, UpdatedAt: now},
|
||||
TenantID: tenantID,
|
||||
SeqId: topicRecord.SeqId + 1,
|
||||
Topic: topic,
|
||||
From: author.String(),
|
||||
Head: types.KVMap{
|
||||
"mime": "application/json",
|
||||
"x-im-schema": 1,
|
||||
"x-im-type": "text",
|
||||
"x-im-client-mid": "group_announcement_" + strings.TrimSpace(ann.AnnouncementID),
|
||||
},
|
||||
Content: map[string]any{"text": "群公告\n" + content},
|
||||
}
|
||||
saved, err := saveMessageMaybeKafka(ctx, tenantID, msg, nil, true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if saved != nil && saved.SeqId > 0 {
|
||||
msg.SeqId = saved.SeqId
|
||||
}
|
||||
broadcastHTTPForwardMessage(tenantID, msg)
|
||||
return nil
|
||||
}
|
||||
|
||||
func serveIMGroupGovernance(wrt http.ResponseWriter, req *http.Request, topic string) {
|
||||
principal, selfSub, ok := requireIMGroupMember(wrt, req, topic, true)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user