fix: persist settings for websocket groups

This commit is contained in:
richjohnrambo
2026-09-21 11:58:36 +08:00
parent c891afba73
commit 05ad37ba80
3 changed files with 168 additions and 2 deletions
+57 -2
View File
@@ -9,7 +9,9 @@
package main
import (
"errors"
"strings"
"time"
"github.com/tinode/chat/server/auth"
"github.com/tinode/chat/server/logs"
@@ -602,9 +604,15 @@ func initTopicNewGrp(t *Topic, sreg *ClientComMessage, isChan bool) error {
Trusted: t.trusted,
}
// store.Topics.Create will add a subscription record for the topic creator
// Persist the topic and its owner subscription together with product group metadata.
stopic.GiveAccess(t.owner, userData.modeWant, userData.modeGiven)
err := store.Topics.Create(t.tenantID, stopic, t.owner, t.perUser[t.owner].private)
var err error
if !isChan && store.GroupBusiness != nil {
err = store.GroupBusiness.CreateGroup(t.tenantID, stopic, t.owner, t.perUser[t.owner].private,
newWebsocketGroupSettings(stopic, t.owner, timestamp), nil, nil, timestamp)
} else {
err = store.Topics.Create(t.tenantID, stopic, t.owner, t.perUser[t.owner].private)
}
if err != nil {
return err
}
@@ -626,6 +634,28 @@ func initTopicNewGrp(t *Topic, sreg *ClientComMessage, isChan bool) error {
return nil
}
func newWebsocketGroupSettings(topic *types.Topic, owner types.Uid, now time.Time) *types.GroupSettings {
public := groupPublicMap(topic.Public)
description, _ := imPublicString(public, "description")
return &types.GroupSettings{
Topic: topic.Id,
OwnerUserID: owner,
Name: groupName(public, topic.Id),
AvatarFileID: imAvatarFileIDFromPublic(public),
Description: description,
GroupStatus: types.GroupStatusActive,
MemberUploadFiles: true,
MemberPrivateChat: true,
MemberInvite: true,
MaxAdminCount: groupMaxAdminCount,
MemberCount: 1,
Version: 1,
CreatedBy: owner,
CreatedAt: now,
UpdatedAt: now,
}
}
// Initialize existing group topic. There is a race condition when two users attempt to load
// the same topic at the same time. It's prevented at hub level.
func initTopicGrp(t *Topic) error {
@@ -671,6 +701,11 @@ func initTopicGrp(t *Topic) error {
t.lastID = stopic.SeqId
t.delID = stopic.DelId
t.subCnt = stopic.SubCnt
if !t.isChan {
if err = ensureLegacyWebsocketGroupSettings(t.tenantID, stopic, t.owner, len(t.perUser)); err != nil {
return err
}
}
// Initialize channel for receiving session online updates.
t.supd = make(chan *sessionUpdate, 32)
@@ -680,6 +715,26 @@ func initTopicGrp(t *Topic) error {
return nil
}
func ensureLegacyWebsocketGroupSettings(tenantID types.TenantID, topic *types.Topic, owner types.Uid, memberCount int) error {
if store.GroupBusiness == nil || topic == nil || owner.IsZero() {
return nil
}
if _, err := store.GroupBusiness.GetSettings(tenantID, topic.Id); err == nil {
return nil
} else if errors.Is(err, types.ErrUnsupported) {
return nil
} else if !errors.Is(err, types.ErrNotFound) {
return err
}
settings := newWebsocketGroupSettings(topic, owner, topic.CreatedAt)
settings.MemberCount = memberCount
if !topic.UpdatedAt.IsZero() {
settings.UpdatedAt = topic.UpdatedAt
}
_, err := store.GroupBusiness.CreateSettings(tenantID, settings)
return err
}
// Initialize system topic. System topic is a singleton, always in memory.
func initTopicSys(t *Topic) error {
t.cat = types.TopicCatSys