feat(knowledge-base): implement per-user pinning for knowledge bases

Refactored the pinning mechanism for knowledge bases to be user-specific, allowing any user with read access to pin knowledge bases independently. This change includes the introduction of a new `user_kb_pins` table to manage pin states, replacing the previous tenant-wide pinning system. Updated relevant API endpoints and service logic to support this new functionality, ensuring that the UI reflects the user's personal pin status.

Additionally, adjusted the knowledge base listing to sort by user-specific pin status, enhancing the user experience by prioritizing pinned items.

Refs: #1303
This commit is contained in:
wizardchen
2026-05-18 11:58:46 +08:00
committed by lyingbug
parent 4aedf5fd62
commit 7528175a74
10 changed files with 355 additions and 71 deletions
@@ -6,6 +6,7 @@ package interfaces
import (
"context"
"time"
"github.com/Tencent/WeKnora/internal/types"
"github.com/hibiken/asynq"
@@ -218,4 +219,20 @@ type KnowledgeBaseRepository interface {
// scope on KnowledgeBase; implementations MUST NOT add an explicit
// `deleted_at IS NULL` predicate (avoids divergence with the auto-scope).
CountByVectorStoreID(ctx context.Context, db *gorm.DB, tenantID uint64, storeID string) (int64, error)
// SetUserKBPin inserts or removes a row in user_kb_pins for the given
// (tenant, user, kb) triple. Returns the resulting pinned_at (nil when
// pinned=false) and an error. The tenant_id is captured to support
// efficient "wipe a tenant" cleanups even though (user_id, kb_id)
// alone would be unique in practice.
SetUserKBPin(
ctx context.Context, tenantID uint64, userID string, kbID string, pinned bool,
) (pinnedAt *time.Time, err error)
// ListUserKBPinIDs returns the kb_id → pinned_at map of every KB the
// given user has personally pinned in this tenant. Used by the list
// path to stamp KnowledgeBase.IsPinned / PinnedAt without a per-row
// roundtrip.
ListUserKBPinIDs(
ctx context.Context, tenantID uint64, userID string,
) (map[string]time.Time, error)
}
+12 -4
View File
@@ -91,10 +91,18 @@ type KnowledgeBase struct {
// IndexingStrategy controls which indexing pipelines are active for this knowledge base.
// Pipelines: vector search, keyword search, wiki generation, knowledge graph extraction.
IndexingStrategy IndexingStrategy `yaml:"indexing_strategy" json:"indexing_strategy" gorm:"column:indexing_strategy;type:json"`
// Whether this knowledge base is pinned to the top of the list
IsPinned bool `yaml:"is_pinned" json:"is_pinned" gorm:"default:false"`
// Time when the knowledge base was pinned (nil if not pinned)
PinnedAt *time.Time `yaml:"pinned_at" json:"pinned_at"`
// IsPinned and PinnedAt are computed per-caller from user_kb_pins
// (see migration 000050). They used to be stored on the row itself,
// which made pinning a tenant-wide ordering decision gated behind
// the kb-edit RBAC guard. The columns are still present in legacy
// schemas for rollback safety but are no longer read or written by
// the application — both fields are tagged `gorm:"-"` so GORM
// ignores them on every CRUD call and the list handler stamps them
// after enriching with the caller's pin set.
IsPinned bool `yaml:"is_pinned" json:"is_pinned" gorm:"-"`
// PinnedAt records when the current caller pinned this knowledge
// base; nil when they have not.
PinnedAt *time.Time `yaml:"pinned_at" json:"pinned_at" gorm:"-"`
// Creation time of the knowledge base
CreatedAt time.Time `yaml:"created_at" json:"created_at"`
// Last updated time of the knowledge base