Download this file
| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | "unicode" |
| 7 | "unicode/utf8" |
| 8 | ) |
| 9 | |
| 10 | const ( |
| 11 | activityMessageMaxRunes = 240 |
| 12 | activityMessageHeader = "X-Carlos-Message" |
| 13 | ) |
| 14 | |
| 15 | type activityCopy struct { |
| 16 | Description, Action, Target, Scope, InvalidMessage string |
| 17 | } |
| 18 | |
| 19 | var activityText = activityCopy{ |
| 20 | Description: "Description", |
| 21 | Action: "Action", |
| 22 | Target: "Target", |
| 23 | Scope: "This history includes console actions and CLI actions sent through the console.", |
| 24 | InvalidMessage: "Message must be one line and 240 characters or fewer.", |
| 25 | } |
| 26 | |
| 27 | func validActivityMessage(message string) bool { |
| 28 | if !utf8.ValidString(message) || utf8.RuneCountInString(message) > activityMessageMaxRunes { |
| 29 | return false |
| 30 | } |
| 31 | for _, r := range message { |
| 32 | if unicode.IsControl(r) { |
| 33 | return false |
| 34 | } |
| 35 | } |
| 36 | return true |
| 37 | } |
| 38 | |
| 39 | func checkedActivityMessage(w http.ResponseWriter, message string) (string, bool) { |
| 40 | if !validActivityMessage(message) { |
| 41 | jsonError(w, http.StatusBadRequest, activityText.InvalidMessage) |
| 42 | return "", false |
| 43 | } |
| 44 | return strings.TrimSpace(message), true |
| 45 | } |
| 46 | |