feat(favorites): implement user favorites and recents functionality
Introduced a new API client for managing user favorites, allowing users to list, add, and remove starred resources. Enhanced the sidebar to display favorites and recents, improving user navigation. Updated internationalization files to include relevant labels and tooltips for the new features across multiple languages. Refs: #1303
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
)
|
||||
|
||||
// UserResourceFavoriteRepository is the storage contract for per-user
|
||||
// favorited resources. Implementations are expected to be tenant-aware
|
||||
// (queries always include tenant_id) so a user switching tenants sees
|
||||
// only that tenant's favorites — matches the PK shape in migration 000047.
|
||||
type UserResourceFavoriteRepository interface {
|
||||
// List returns favorites for (userID, tenantID, resourceType), newest first.
|
||||
List(ctx context.Context, userID string, tenantID uint64, resourceType string) ([]*types.UserResourceFavorite, error)
|
||||
// Add upserts a favorite (no-op if already present). Returns whether
|
||||
// a new row was actually inserted so the service layer can decide
|
||||
// whether to emit an audit event.
|
||||
Add(ctx context.Context, userID string, tenantID uint64, resourceType, resourceID string) (created bool, err error)
|
||||
// Remove deletes a favorite; returns whether anything was deleted.
|
||||
Remove(ctx context.Context, userID string, tenantID uint64, resourceType, resourceID string) (removed bool, err error)
|
||||
// IsFavorite exists for the rare RPC where the caller wants a single
|
||||
// boolean rather than the full list (e.g. a future GET-by-id endpoint).
|
||||
IsFavorite(ctx context.Context, userID string, tenantID uint64, resourceType, resourceID string) (bool, error)
|
||||
}
|
||||
|
||||
// UserResourceFavoriteService wraps the repository with input validation
|
||||
// (resource type allowlist, non-empty id) so the handler stays thin.
|
||||
type UserResourceFavoriteService interface {
|
||||
List(ctx context.Context, userID string, tenantID uint64, resourceType string) ([]*types.UserResourceFavorite, error)
|
||||
Add(ctx context.Context, userID string, tenantID uint64, resourceType, resourceID string) error
|
||||
Remove(ctx context.Context, userID string, tenantID uint64, resourceType, resourceID string) error
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package types
|
||||
|
||||
import "time"
|
||||
|
||||
// Resource type constants for UserResourceFavorite. Extensible — adding a
|
||||
// new favoritable resource is just a new constant string + a frontend hook,
|
||||
// no schema change required.
|
||||
const (
|
||||
ResourceTypeKB = "kb"
|
||||
ResourceTypeAgent = "agent"
|
||||
)
|
||||
|
||||
// UserResourceFavorite is a per-(user, tenant) star on a single resource.
|
||||
// See migration 000047 for the schema rationale (composite key, no FK,
|
||||
// tenant-scoped on purpose).
|
||||
type UserResourceFavorite struct {
|
||||
UserID string `json:"user_id" gorm:"type:varchar(36);primaryKey"`
|
||||
TenantID uint64 `json:"tenant_id" gorm:"primaryKey"`
|
||||
ResourceType string `json:"resource_type" gorm:"type:varchar(16);primaryKey"`
|
||||
ResourceID string `json:"resource_id" gorm:"type:varchar(64);primaryKey"`
|
||||
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
||||
}
|
||||
|
||||
// TableName pins the table to the migration's exact name so GORM's
|
||||
// pluraliser doesn't drift if we ever rename the struct.
|
||||
func (UserResourceFavorite) TableName() string {
|
||||
return "user_resource_favorites"
|
||||
}
|
||||
|
||||
// IsValidFavoriteResourceType returns true for resource types this product
|
||||
// currently supports favoriting. The handler validates against this list
|
||||
// to keep clients from inserting arbitrary strings (which would balloon
|
||||
// the table and break the frontend's segmented view).
|
||||
func IsValidFavoriteResourceType(t string) bool {
|
||||
switch t {
|
||||
case ResourceTypeKB, ResourceTypeAgent:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user