Show Feishu app visibility and preserve sync toggle

This commit is contained in:
Codex committed 2026-10-23 11:19:15 +08:00
1 parent e5375deeb2
commit 594bb61cef
6 files changed
+40 -18

No files matched your search

@@ -26,8 +26,10 @@ var directoryRunMu sync.Mutex
var directoryRunning bool
type directorySnapshot struct {
departments map[string]model.FeishuDepartment
members map[string]model.FeishuDirectoryMember
departments map[string]model.FeishuDepartment
members map[string]model.FeishuDirectoryMember
visibleDepartments int
visibleUsers int
}
func (p *Provider) DirectorySyncStatus(c *gin.Context) {
@@ -90,6 +92,10 @@ func (p *Provider) DirectorySyncConfigure(c *gin.Context) {
}
directoryRunMu.Lock()
defer directoryRunMu.Unlock()
if directoryRunning {
c.JSON(409, gin.H{"error": "请等待当前同步完成后再修改开关"})
return
}
status := syncStatus(cfg, base)
status.Enabled = *input.Enabled
if err := store.DB.Save(&status).Error; err != nil {
@@ -167,8 +173,13 @@ func performDirectorySync(ctx context.Context) (model.FeishuDirectorySync, error
if err != nil {
return failedSync(status, err)
}
currentCfg, currentBase, err := enabled()
if err != nil || currentBase != base || currentCfg.AppID != cfg.AppID || currentCfg.TenantKey != cfg.TenantKey {
return failedSync(status, errors.New("飞书应用配置已变化,请重新同步"))
}
now = time.Now()
status.LastSuccess, status.Departments, status.Members = &now, len(snapshot.departments), len(snapshot.members)
status.VisibleDepartments, status.VisibleUsers = snapshot.visibleDepartments, snapshot.visibleUsers
status.LastError = ""
err = store.DB.Transaction(func(tx *gorm.DB) error {
for _, dept := range snapshot.departments {
@@ -183,7 +194,10 @@ func performDirectorySync(ctx context.Context) (model.FeishuDirectorySync, error
return err
}
}
return tx.Save(&status).Error
return tx.Model(&model.FeishuDirectorySync{}).Where("id = ? AND issuer = ? AND app_id = ?", 1, base, cfg.AppID).Updates(map[string]any{
"last_success": now, "last_error": "", "departments": status.Departments, "members": status.Members,
"visible_departments": status.VisibleDepartments, "visible_users": status.VisibleUsers,
}).Error
})
if err != nil {
return failedSync(status, errors.New("写入通讯录失败"))
@@ -193,7 +207,7 @@ func performDirectorySync(ctx context.Context) (model.FeishuDirectorySync, error
func failedSync(status model.FeishuDirectorySync, err error) (model.FeishuDirectorySync, error) {
status.LastError = err.Error()
_ = store.DB.Save(&status).Error
_ = store.DB.Model(&model.FeishuDirectorySync{}).Where("id = ? AND issuer = ? AND app_id = ?", 1, status.Issuer, status.AppID).Update("last_error", status.LastError).Error
return status, err
}
@@ -259,6 +273,7 @@ func readDirectory(ctx context.Context, token string) (directorySnapshot, error)
if len(scope.DepartmentIDs) == 0 && len(scope.UserIDs) == 0 {
return snapshot, errors.New("飞书应用通讯录可见范围为空;请配置成员和部门可见范围")
}
snapshot.visibleDepartments, snapshot.visibleUsers = len(scope.DepartmentIDs), len(scope.UserIDs)
queue, seen := append([]string{}, scope.DepartmentIDs...), map[string]bool{}
for len(queue) > 0 {
id := queue[0]
+11 -9
View File
@@ -41,15 +41,17 @@ type IdentityProfile struct {
// FeishuDirectorySync tracks a single application's import schedule and outcome.
type FeishuDirectorySync struct {
ID uint `gorm:"primaryKey"`
Enabled bool `json:"enabled"`
Issuer string `gorm:"size:256"`
AppID string `gorm:"size:128"`
LastAttempt *time.Time `json:"last_attempt"`
LastSuccess *time.Time `json:"last_success"`
LastError string `gorm:"size:512" json:"last_error"`
Departments int `json:"departments"`
Members int `json:"members"`
ID uint `gorm:"primaryKey"`
Enabled bool `json:"enabled"`
Issuer string `gorm:"size:256"`
AppID string `gorm:"size:128"`
LastAttempt *time.Time `json:"last_attempt"`
LastSuccess *time.Time `json:"last_success"`
LastError string `gorm:"size:512" json:"last_error"`
Departments int `json:"departments"`
Members int `json:"members"`
VisibleDepartments int `json:"visible_departments"`
VisibleUsers int `json:"visible_users"`
}
type FeishuDepartment struct {