| 1 | package bot |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "strings" |
| 6 | |
| 7 | "reasonix/internal/event" |
| 8 | ) |
| 9 | |
| 10 | func isWriteAccessApproval(a event.Approval) bool { |
| 11 | return strings.EqualFold(strings.TrimSpace(a.Kind), event.ApprovalKindWriteAccess) || a.WriteAccess != nil |
| 12 | } |
| 13 | |
| 14 | func writeAccessKeyboard(id string) *InlineKeyboard { |
| 15 | return &InlineKeyboard{Rows: []InlineKeyboardRow{{ |
| 16 | Buttons: []InlineKeyboardButton{ |
| 17 | {ID: "allow_once", Label: "允许一次", Style: 1, CallbackID: "/approve " + id}, |
| 18 | {ID: "allow_session", Label: "本会话允许", Style: 0, CallbackID: "/approve-session " + id}, |
| 19 | {ID: "deny", Label: "拒绝", Style: 2, CallbackID: "/deny " + id}, |
| 20 | }, |
| 21 | }}} |
| 22 | } |
| 23 | |
| 24 | func writeAccessCard(a event.Approval, chatType ChatType, userID string) *InteractiveCard { |
| 25 | return &InteractiveCard{ |
| 26 | Header: "需要扩展写入范围", |
| 27 | Elements: []InteractiveCardElement{ |
| 28 | {Tag: "markdown", Content: renderWriteAccessText(a)}, |
| 29 | {Tag: "action", Extra: map[string]any{ |
| 30 | "actions": []map[string]any{ |
| 31 | {"tag": "button", "text": map[string]string{"tag": "plain_text", "content": "允许一次"}, "type": "primary", "value": cardActionValue("/approve "+a.ID, chatType, userID)}, |
| 32 | {"tag": "button", "text": map[string]string{"tag": "plain_text", "content": "本会话允许"}, "type": "default", "value": cardActionValue("/approve-session "+a.ID, chatType, userID)}, |
| 33 | {"tag": "button", "text": map[string]string{"tag": "plain_text", "content": "拒绝"}, "type": "danger", "value": cardActionValue("/deny "+a.ID, chatType, userID)}, |
| 34 | }, |
| 35 | }}, |
| 36 | }, |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func renderWriteAccessText(a event.Approval) string { |
| 41 | var b strings.Builder |
| 42 | b.WriteString("⚠️ 需要扩展写入范围\n") |
| 43 | fmt.Fprintf(&b, "工具: %s\n操作: %s\n", a.Tool, a.Subject) |
| 44 | if wa := a.WriteAccess; wa != nil { |
| 45 | dirs := wa.DisplayDirectories |
| 46 | if len(dirs) == 0 { |
| 47 | dirs = wa.Directories |
| 48 | } |
| 49 | if len(dirs) > 0 { |
| 50 | fmt.Fprintf(&b, "目录: %s\n", strings.Join(dirs, ", ")) |
| 51 | } |
| 52 | if wa.Justification != "" { |
| 53 | fmt.Fprintf(&b, "原因: %s\n", wa.Justification) |
| 54 | } |
| 55 | if wa.BroadHomeAccess { |
| 56 | b.WriteString("警告: 这将授权写入整个用户主目录。Reasonix 会话和运行时状态仍受保护。\n") |
| 57 | } |
| 58 | if wa.OrdinaryPermissionNeeded { |
| 59 | b.WriteString("此选择也会授权当前匹配操作。\n") |
| 60 | } |
| 61 | } |
| 62 | fmt.Fprintf(&b, "\nID: `%s`\n回复 1 允许一次,2 本会话允许,3 拒绝。", a.ID) |
| 63 | return b.String() |
| 64 | } |
| 65 |