| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | |
| 6 | "reasonix/internal/control" |
| 7 | "reasonix/internal/provider" |
| 8 | ) |
| 9 | |
| 10 | func validateSubmitAction(format, action string) error { |
| 11 | if action != "" && action != control.FinalReadinessRecoveryAction && action != control.ProtocolRecoveryAction { |
| 12 | return errors.New(`unsupported action (supported: "final_readiness_recovery", "protocol_recovery")`) |
| 13 | } |
| 14 | if action != "" && format != "" { |
| 15 | return errors.New("format is unavailable for recovery actions") |
| 16 | } |
| 17 | return nil |
| 18 | } |
| 19 | |
| 20 | func submitWithAction(ctrl control.SessionAPI, input, format, action string, recoveryID ...string) { |
| 21 | if action == control.ProtocolRecoveryAction { |
| 22 | id := "" |
| 23 | if len(recoveryID) > 0 { |
| 24 | id = recoveryID[0] |
| 25 | } |
| 26 | if recovery, ok := ctrl.(interface{ SubmitProtocolRecovery(string, string) }); ok { |
| 27 | recovery.SubmitProtocolRecovery(id, input) |
| 28 | } |
| 29 | return |
| 30 | } |
| 31 | if action == control.FinalReadinessRecoveryAction { |
| 32 | ctrl.SubmitFinalReadinessRecovery(input, input) |
| 33 | return |
| 34 | } |
| 35 | ctrl.SubmitHTTPFormat(input, format) |
| 36 | } |
| 37 | |
| 38 | func finalReadinessHistoryMessage(m provider.Message) ([]historyMessage, bool) { |
| 39 | if m.LocalOnly && len(m.ProtocolRecovery) > 0 { |
| 40 | if r, ok := provider.DecodeProtocolRecovery(m.ProtocolRecovery); ok && r.State == "pending" { |
| 41 | return []historyMessage{{Role: "protocol_recovery", ProtocolRecovery: &provider.ProtocolRecoveryAction{ID: r.ID}}}, true |
| 42 | } |
| 43 | return nil, true |
| 44 | } |
| 45 | if !m.LocalOnly || m.FinalReadinessRecovery == nil { |
| 46 | return nil, false |
| 47 | } |
| 48 | if !m.FinalReadinessRecovery.Pending { |
| 49 | return nil, true |
| 50 | } |
| 51 | return []historyMessage{{ |
| 52 | Role: "final_readiness", Missing: append([]string(nil), m.FinalReadinessRecovery.Missing...), |
| 53 | }}, true |
| 54 | } |
| 55 |