fix: handle opaque attachment references

This commit is contained in:
richjohnrambo
2026-09-30 10:50:42 +08:00
parent 9f30995787
commit 06e08a65e4
4 changed files with 88 additions and 14 deletions
+30
View File
@@ -0,0 +1,30 @@
package store
import (
"testing"
"github.com/tinode/chat/server/store/types"
)
func TestFileLinkUserIDAllowsOpaqueTopicReferences(t *testing.T) {
tests := []struct {
name string
topic string
isUser bool
wantUID types.Uid
}{
{name: "cultivate reference", topic: "cvpbzxw1aio4ni8x0"},
{name: "short opaque reference", topic: "x"},
{name: "empty reference", topic: ""},
{name: "user reference", topic: types.Uid(42).UserId(), isUser: true, wantUID: types.Uid(42)},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
gotUID, gotIsUser := fileLinkUserID(test.topic)
if gotIsUser != test.isUser || gotUID != test.wantUID {
t.Fatalf("fileLinkUserID(%q) = (%v, %v), want (%v, %v)", test.topic, gotUID, gotIsUser, test.wantUID, test.isUser)
}
})
}
}
+10 -2
View File
@@ -4684,8 +4684,8 @@ func (fileMapper) LinkAttachments(tenantID types.TenantID, topic string, msgId t
return err
}
userId := types.ZeroUid
if topic != "" && types.GetTopicCat(topic) == types.TopicCatMe {
userId = types.ParseUserId(topic)
if parsedUserID, ok := fileLinkUserID(topic); ok {
userId = parsedUserID
topic = ""
}
return tenantAdp.TenantFileLinkAttachments(tenantID, topic, userId, msgId, fids)
@@ -4693,6 +4693,14 @@ func (fileMapper) LinkAttachments(tenantID types.TenantID, topic string, msgId t
return nil
}
// File link topics may also be opaque retention keys, such as cultivate's cvp prefix.
func fileLinkUserID(topic string) (types.Uid, bool) {
if !strings.HasPrefix(topic, "usr") {
return types.ZeroUid, false
}
return types.ParseUserId(topic), true
}
func (fileMapper) UserHasAccess(tenantID types.TenantID, fid string, uid types.Uid) (bool, error) {
if fid == "" || uid.IsZero() {
return false, types.ErrMalformed