fix: handle opaque attachment references
This commit is contained in:
@@ -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
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user