feat(session): add admin API session bucket and stabilize sidebar filter

Let tenant admins list and open API-key sessions from the Web console while
keeping per-key isolation for everyone else. Pin the session source filter so
it no longer jumps when switching buckets, and resolve session origin on read
so deep links stay in sync with the active folder after refresh.
This commit is contained in:
wizardchen
2026-07-20 18:23:35 +08:00
committed by lyingbug
parent eab91d2f7f
commit 76a4ffe525
18 changed files with 317 additions and 65 deletions
@@ -72,6 +72,25 @@ func (r *sessionRepository) GetByID(ctx context.Context, tenantID uint64, id str
return &session, nil
}
// GetIMPlatform returns the IM platform bound to a session, or "" when none.
// It intentionally ignores soft-deleted mappings' visibility rules used by
// QueryPaged: any mapping (active or cleared) marks the session as IM-origin.
func (r *sessionRepository) GetIMPlatform(
ctx context.Context, tenantID uint64, sessionID string,
) (string, error) {
var platform string
err := r.db.WithContext(ctx).
Table("im_channel_sessions AS ics").
Joins("JOIN sessions AS s ON s.id = ics.session_id").
Where("ics.session_id = ? AND s.tenant_id = ?", sessionID, tenantID).
Limit(1).
Pluck("ics.platform", &platform).Error
if err != nil {
return "", err
}
return platform, nil
}
// GetByTenantID retrieves all sessions for a tenant
func (r *sessionRepository) GetByTenantID(ctx context.Context, tenantID uint64, userID string) ([]*types.Session, error) {
var sessions []*types.Session
@@ -169,6 +188,12 @@ func (r *sessionRepository) QueryPaged(
switch lower {
case "":
return db
case types.SessionSourceAPI:
// Tenant-wide view of API-key sessions. Their owner id is
// "api_tenant_key:<tenantID>:<keyID>", so a prefix match selects
// every key's sessions. The service layer already enforced Admin+
// and cleared the per-user scope for this source.
return db.Where("s.user_id LIKE ?", types.SessionOwnerAPITenantKeyPrefix+"%")
case "web":
// User web chats only — exclude embed-widget sessions (same IM-null row).
return db.Where(