Files
ai_academy/internal/im/wecompush/client.go
T
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

98 lines
3.2 KiB
Go

// Package wecompush resolves per-tenant WeCom proactive-push clients from
// the tenant's IM channel credentials.
//
// It exists so the application service layer never imports internal/im
// (directly or via the wecom adapter): internal/im's in-package tests
// import internal/application/service, so a service → im edge creates an
// import cycle in the im test binary. The container injects
// TenantClient into service.NewTrainingScheduler instead.
package wecompush
import (
"context"
"errors"
"fmt"
"academy/internal/im"
"academy/internal/im/wecom"
"academy/internal/types"
"gorm.io/gorm"
)
// TenantClient builds a wecom.ProactiveClient from the tenant's enabled
// webhook-mode wecom IM channel. Only webhook (self-built app) channels can
// call message/send; websocket smart-bot credentials cannot and are
// ignored.
func TenantClient(ctx context.Context, db *gorm.DB, tenantID uint64) (*wecom.ProactiveClient, error) {
var channel im.IMChannel
err := db.WithContext(ctx).
Where("tenant_id = ? AND platform = ? AND mode = ? AND enabled = ? AND deleted_at IS NULL",
tenantID, im.PlatformWeCom, "webhook", true).
Order("created_at").
First(&channel).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errors.New("未配置企微自建应用渠道(IM 集成中创建 webhook 模式的企业微信渠道)")
}
return nil, fmt.Errorf("load wecom channel: %w", err)
}
creds, err := im.ParseCredentials(channel.Credentials)
if err != nil {
return nil, fmt.Errorf("parse wecom credentials: %w", err)
}
corpAgentID := 0
if v, ok := creds["corp_agent_id"]; ok {
switch val := v.(type) {
case float64:
corpAgentID = int(val)
case int:
corpAgentID = val
}
}
return wecom.NewProactiveClient(
im.GetString(creds, "corp_id"),
im.GetString(creds, "agent_secret"),
corpAgentID,
im.GetString(creds, "api_base_url"),
)
}
// TenantWecomUserIDs maps platform user IDs to WeCom userids via
// org_employees.wecom_userid; users without a binding are absent from the
// result.
func TenantWecomUserIDs(ctx context.Context, db *gorm.DB, tenantID uint64, userIDs []string) (map[string]string, error) {
out := map[string]string{}
if len(userIDs) == 0 {
return out, nil
}
var employees []types.OrgEmployee
if err := db.WithContext(ctx).
Select("user_id", "wecom_userid").
Where("tenant_id = ? AND user_id IN ? AND deleted_at IS NULL", tenantID, userIDs).
Find(&employees).Error; err != nil {
return nil, fmt.Errorf("load wecom userids: %w", err)
}
for _, e := range employees {
if e.WecomUserid != "" {
out[e.UserID] = e.WecomUserid
}
}
return out, nil
}
// Backend adapts this package to the service layer's WecomPushBackend
// interface (structural typing: *wecom.ProactiveClient satisfies
// service.WecomPushSender).
type Backend struct{}
// ClientForTenant resolves the tenant's push client.
func (Backend) ClientForTenant(ctx context.Context, db *gorm.DB, tenantID uint64) (*wecom.ProactiveClient, error) {
return TenantClient(ctx, db, tenantID)
}
// TenantWecomUserIDs resolves the userid bindings.
func (Backend) TenantWecomUserIDs(ctx context.Context, db *gorm.DB, tenantID uint64, userIDs []string) (map[string]string, error) {
return TenantWecomUserIDs(ctx, db, tenantID, userIDs)
}