feat: complete current GoChat updates

This commit is contained in:
2026-09-13 00:13:55 +08:00
parent 6e62f5094f
commit 0dd188c46f
58 changed files with 2662 additions and 1503 deletions
+20 -22
View File
@@ -11,7 +11,7 @@
### 1.1 对比 Chatwoot DeviseTokenAuth
| 特性 | Chatwoot (Rails) | GoChat (Go) |
|---|---|---|
| --- | --- | --- |
| 认证库 | DeviseTokenAuth gem | 自实现 JWT middleware |
| Token存储 | 多token机制(client_id+token对) | 单JWT + Refresh token |
| Token传递 | HTTP headersaccess-token/client/uid | Authorization: Bearer <jwt> |
@@ -85,6 +85,7 @@ func AuthMiddleware() gin.HandlerFunc {
```
**认证级别分层:**
- `PublicAPI` — 无认证(WebWidget嵌入、CSAT提交、Portal文章)
- `AuthenticatedAPI` — AuthMiddleware(绝大多数API
- `AdminAPI` — AuthMiddleware + RoleCheck("administrator")(账户设置、团队管理等)
@@ -98,7 +99,7 @@ func AuthMiddleware() gin.HandlerFunc {
### 2.1 对比 Chatwoot Pundit + CustomRole
| 特性 | Chatwoot | GoChat |
|---|---|---|
| --- | --- | --- |
| 权限框架 | Pundit (Policy类) | 中间件+Policy函数 |
| 内置角色 | agent / administrator | agent / administrator |
| 自定义角色 | CustomRole (企业版, 6权限维度) | CustomRole (企业版, 同6维度) |
@@ -116,28 +117,23 @@ const (
)
// 企业版 CustomRole
// 当前对外契约为六个二值权限:key 存在即拥有完整权限,未勾选即无权限。
type CustomRole struct {
ID uint `gorm:"primaryKey"`
AccountID uint `gorm:"index"`
Name string `gorm:"size:255"`
Permissions Permissions `gorm:"type:jsonb"` // 6维度权限
Permissions []string `gorm:"type:jsonb"` // 6个二值权限 key
}
type Permissions struct {
ConversationManage PermissionLevel `json:"conversation_manage"` // full/read
ConversationDelete PermissionLevel `json:"conversation_delete"` // full/none
ContactManage PermissionLevel `json:"contact_manage"` // full/read
ReportManage PermissionLevel `json:"report_manage"` // full/none
KnowledgeBaseManage PermissionLevel `json:"knowledge_base_manage"` // full/read
AutomationManage PermissionLevel `json:"automation_manage"` // full/none
}
type PermissionLevel string
const (
PermissionFull PermissionLevel = "full"
PermissionRead PermissionLevel = "read"
PermissionNone PermissionLevel = "none"
)
// conversation_manage
// conversation_unassigned_manage
// conversation_participating_manage
// contact_manage
// report_manage
// knowledge_base_manage
//
// PermissionLevel(full/read/none) 仅保留给内置角色和旧数据兼容层,
// 不作为自定义角色前端/API 的配置粒度。
```
### 2.3 权限检查实现
@@ -230,7 +226,7 @@ type AccountUser struct {
### 4.1 对比 Chatwoot ActionCable
| 特性 | Chatwoot (ActionCable) | GoChat (Redis Pub/Sub + WebSocket) |
|---|---|---|
| --- | --- | --- |
| 连接管理 | ActionCable Server (Rails内置) | gorilla/websocket + Redis subscriber |
| Channel订阅 | `ConversationChannel`, `AccountChannel` | Redis topic: `account:{id}`, `conversation:{id}` |
| 消息推送 | `broadcast_to` | Redis PUBLISH → WebSocket send |
@@ -327,6 +323,7 @@ Dispatcher.dispatch(event_name, timestamp, event_data)
```
**12个Listener**
- ActionCableListener → WebSocket推送
- AgentBotListener → 触发Bot响应
- AutomationRuleListener → 触发自动化规则
@@ -427,7 +424,7 @@ func StartConsumer(accountID uint) {
### 5.5 对比总结
| 方面 | Chatwoot | GoChat |
|---|---|---|
| --- | --- | --- |
| 事件发布 | Dispatcher → Redis PUBLISH | Dispatcher → Redis PUBLISH(相同) |
| 事件消费 | 12个独立Listener类 | Handler函数注册表(更轻量) |
| 执行方式 | Sidekiq异步Job | goroutine异步执行 |
@@ -441,6 +438,7 @@ func StartConsumer(accountID uint) {
### 6.1 对比 Chatwoot
Chatwoot企业版SAML实现:
- `AccountSamlSettings` 模型存储IdP配置
- `DeviseSamlAuthenticatable` gem处理SAML流程
- `SamlUserBuilder` 构建User对象
@@ -542,7 +540,7 @@ func (b *SamlUserBuilder) Build(samlResponse *SamlResponse) (*User, error) {
## 8. 关键设计决策总结
| # | 决策 | 原因 | 对比Chatwoot |
|---|---|---|---|
| --- | --- | --- | --- |
| 1 | JWT替代DeviseTokenAuth | Go无对应gemJWT更标准 | 多token→单token+refresh |
| 2 | PolicyContext替代Pundit | 统一权限检查入口 | 12个Policy类→1个PolicyContext |
| 3 | Handler注册表替代Listener类 | Go无Rails autoload,函数注册更轻量 | 12个Listener→Handler map |
@@ -554,4 +552,4 @@ func (b *SamlUserBuilder) Build(samlResponse *SamlResponse) (*User, error) {
---
> **下一步:** P2B(数据库设计)和 P2C(路由设计)完成后,P2架构设计阶段完整收官。
> **下一步:** P2B(数据库设计)和 P2C(路由设计)完成后,P2架构设计阶段完整收官。