2c7265efc4
build / build (push) Successful in 58s
主人要求:去掉入口,统一成 dws 导入与 PP 导入;其他来源连数据一起下架,之后用 dws 重新获取。
界面(web):
- 「数据源配置」只剩两块:**dws 导入(钉钉)**(按当前钉钉账号列出可访问的知识库与钉盘空间)
与 **PP 导入**(PP 资料下载中心)。上一步加的「显示 AList/钉钉知识库来源」折叠开关删除。
- 来源卡片新增管理员可见的「去掉来源」(带确认弹窗,说明是归档下架、数据保留可恢复)。
- 侧栏文案改成"只留两个入口";发现面板标题改为「dws 导入(钉钉)」。
后端(归档语义,可逆):
- 迁移 000047:source_connections 增 archived_at / archived_by + 只覆盖未归档行的部分索引。
- store:ArchiveSourceConnection / RestoreSourceConnection(幂等;归档同时停用并清空
next_refresh_at,避免被手工改回后立刻带着旧调度跑)。列表、目录、计划刷新(claimDue)、
手动扫描(triggerBlock/inspect)四类查询一律排除归档行。
- 注册路径(dws 重新获取):RegisterDingtalkSpace 的 ON CONFLICT 清掉归档标记 ——
重新登记同一个空间**原地复活同一行**,不产生重复,历史快照仍属于同一行。
- HTTP:POST /api/v1/source-connections/{id}/archive|restore(管理员专属,写意图头
source-archive / source-restore + Idempotency-Key;404 区分"不存在"与"已是目标状态");
openapi 同步两条契约。
验证:
- 集成测试(真 postgres):归档从列表消失但 source_items 一行不少、重复归档不漂移归档时间与人、
未知 ID 报 ErrSourceConnectionNotFound、归档来源不能再被扫描、恢复后回到列表但保持停用;
dws 重新登记同一空间复活同一行。
- httpapi:写意图/幂等键门禁、200/403/404/500、错误体不回显内部细节、classifyRole 管理员专属。
- 迁移护栏:000047 结构 + 四类 SQL 必须含 archived_at is null(漏一条就是入口没去掉)。
- make verify 15/15。
96 lines
3.7 KiB
Go
96 lines
3.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
)
|
|
|
|
// DingTalk space registration: a knowledge-base space discovered through the
|
|
// login sidecar becomes a source_connections row with a DETERMINISTIC id,
|
|
// derived the same way the catalog importer derives stable ids
|
|
// ('sc-' + sha256('dingtalk-'+root_key)[:32]). Re-registering the same space
|
|
// is an idempotent re-sync of name/root, never a duplicate row. New rows are
|
|
// enabled but carry no schedule (next_refresh_at stays NULL): automatic scans
|
|
// begin only after an admin sets an interval — same posture as the existing
|
|
// imported DingTalk sources.
|
|
|
|
// RegisterDingtalkSpaceInput describes one space resolved by the sidecar.
|
|
type RegisterDingtalkSpaceInput struct {
|
|
RootKey string // sidecar inventory key, e.g. "atom" or "space-1f2e3d4c5b"
|
|
Name string // live space name from DingTalk spaceProfile
|
|
SpaceID string // alidocs space id from the URL
|
|
RootID string // root dentry uuid
|
|
Now time.Time
|
|
}
|
|
|
|
func (in RegisterDingtalkSpaceInput) ConnectionID() string {
|
|
sum := sha256.Sum256([]byte("dingtalk-" + in.RootKey))
|
|
return "sc-" + hex.EncodeToString(sum[:])[:32]
|
|
}
|
|
|
|
func (in RegisterDingtalkSpaceInput) validate() error {
|
|
if in.Now.IsZero() {
|
|
return errors.New("dingtalk space registration: now is required")
|
|
}
|
|
if !validShortID(in.RootKey) || strings.TrimSpace(in.Name) == "" || len(in.Name) > 255 {
|
|
return errors.New("dingtalk space registration: invalid key or name")
|
|
}
|
|
for _, id := range []string{in.SpaceID, in.RootID} {
|
|
if id == "" || len(id) > 128 || strings.ContainsAny(id, " \t\r\n/") {
|
|
return errors.New("dingtalk space registration: invalid identifier")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) RegisterDingtalkSpace(ctx context.Context, in RegisterDingtalkSpaceInput) (string, error) {
|
|
if err := in.validate(); err != nil {
|
|
return "", err
|
|
}
|
|
connectionID := in.ConnectionID()
|
|
var existingName, existingRoot string
|
|
err := s.db.QueryRow(ctx, `SELECT name, base_url FROM source_connections WHERE id = $1`, connectionID).Scan(&existingName, &existingRoot)
|
|
switch {
|
|
case errors.Is(err, pgx.ErrNoRows):
|
|
// fresh registration below
|
|
case err != nil:
|
|
return "", fmt.Errorf("load dingtalk connection: %w", err)
|
|
default:
|
|
// A different space resolved under the same key must not silently
|
|
// rewrite the existing connection.
|
|
if !strings.HasPrefix(existingRoot, "spaces/"+in.RootID+"?") {
|
|
return "", fmt.Errorf("dingtalk space registration: connection %s already serves another root", connectionID)
|
|
}
|
|
}
|
|
// base_url stores a stable, non-secret locator (the root dentry uuid plus
|
|
// the space id) so re-registration can verify identity and the UI can
|
|
// explain what a DingTalk connection points at.
|
|
locator := "spaces/" + in.RootID + "?space=" + in.SpaceID
|
|
_, err = s.db.Exec(ctx, `
|
|
INSERT INTO source_connections (
|
|
id, source_type, name, base_url, secret_ref, enabled,
|
|
schedule, last_health, created_at, updated_at, updated_by
|
|
) VALUES ($1, 'dingtalk', $2, $3, '', TRUE, '{}'::jsonb, '{}'::jsonb, $4, $4, $5)
|
|
ON CONFLICT (id) DO UPDATE
|
|
SET name = EXCLUDED.name, base_url = EXCLUDED.base_url,
|
|
-- 归档过的来源被重新登记(dws 重新获取)时原地复活:清掉归档标记,不建重复行 ——
|
|
-- 目录快照与历史仍属于同一行。
|
|
archived_at = NULL, archived_by = '',
|
|
updated_at = EXCLUDED.updated_at, updated_by = EXCLUDED.updated_by`,
|
|
connectionID, in.Name, locator, in.Now.UTC(), "dingtalk-registration")
|
|
if err != nil {
|
|
if isUniqueViolation(err) {
|
|
return "", fmt.Errorf("dingtalk space registration: name %q already used by another connection", in.Name)
|
|
}
|
|
return "", fmt.Errorf("register dingtalk space: %w", err)
|
|
}
|
|
return connectionID, nil
|
|
}
|