| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strings" |
| 6 | "unicode" |
| 7 | "unicode/utf8" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | // Headlines are the one sentence a person reads first on a task's |
| 13 | // timeline (a finding, Engineering's change, Review's verdict). They are |
| 14 | // written by the specialist; the instance keeps them plain: one line, no |
| 15 | // control characters, at most wire.MaxHeadline bytes cut at a word, and |
| 16 | // never a file path (a headline with one is dropped; the detail keeps |
| 17 | // it). |
| 18 | |
| 19 | var ( |
| 20 | pathToken = regexp.MustCompile(`[/\\]|^[A-Za-z0-9_.-]*[A-Za-z_][A-Za-z0-9_-]*\.[A-Za-z][A-Za-z0-9]{0,5}$|[A-Za-z_][^\s]*:\d+$`) |
| 21 | ) |
| 22 | |
| 23 | // cleanHeadline is s as a headline, or "" when it is empty or names a |
| 24 | // path. |
| 25 | func cleanHeadline(s string) string { |
| 26 | if !utf8.ValidString(s) { |
| 27 | return "" |
| 28 | } |
| 29 | s, _, _ = strings.Cut(strings.TrimSpace(s), "\n") |
| 30 | s = strings.Join(strings.FieldsFunc(s, func(r rune) bool { return unicode.IsSpace(r) || unicode.IsControl(r) }), " ") |
| 31 | if s == "" { |
| 32 | return "" |
| 33 | } |
| 34 | for _, w := range strings.Fields(s) { |
| 35 | if pathToken.MatchString(strings.TrimRight(strings.Trim(w, "`'\"()[]"), ".,;:!?")) { |
| 36 | return "" |
| 37 | } |
| 38 | } |
| 39 | if len(s) > wire.MaxHeadline { |
| 40 | cut := s[:wire.MaxHeadline] |
| 41 | for !utf8.ValidString(cut) { |
| 42 | cut = cut[:len(cut)-1] |
| 43 | } |
| 44 | if i := strings.LastIndexByte(cut, ' '); i > wire.MaxHeadline/2 { |
| 45 | cut = cut[:i] |
| 46 | } |
| 47 | s = strings.TrimRight(cut, " ,;:") + "…" |
| 48 | } |
| 49 | return s |
| 50 | } |
| 51 | |