| 1 | // Package slackutilsx is a utility package that doesn't promise API stability. |
| 2 | // its for experimental functionality and utilities. |
| 3 | package slackutilsx |
| 4 | |
| 5 | import ( |
| 6 | "strings" |
| 7 | "unicode/utf8" |
| 8 | ) |
| 9 | |
| 10 | // ChannelType the type of channel based on the channelID |
| 11 | type ChannelType int |
| 12 | |
| 13 | func (t ChannelType) String() string { |
| 14 | switch t { |
| 15 | case CTypeDM: |
| 16 | return "Direct" |
| 17 | case CTypeGroup: |
| 18 | return "Group" |
| 19 | case CTypeChannel: |
| 20 | return "Channel" |
| 21 | default: |
| 22 | return "Unknown" |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | const ( |
| 27 | // CTypeUnknown represents channels we cannot properly detect. |
| 28 | CTypeUnknown ChannelType = iota |
| 29 | // CTypeDM is a private channel between two slack users. |
| 30 | CTypeDM |
| 31 | // CTypeGroup is a group channel. |
| 32 | CTypeGroup |
| 33 | // CTypeChannel is a public channel. |
| 34 | CTypeChannel |
| 35 | ) |
| 36 | |
| 37 | // DetectChannelType converts a channelID to a ChannelType. |
| 38 | // channelID must not be empty. However, if it is empty, the channel type will default to Unknown. |
| 39 | func DetectChannelType(channelID string) ChannelType { |
| 40 | // intentionally ignore the error and just default to CTypeUnknown |
| 41 | switch r, _ := utf8.DecodeRuneInString(channelID); r { |
| 42 | case 'C': |
| 43 | return CTypeChannel |
| 44 | case 'G': |
| 45 | return CTypeGroup |
| 46 | case 'D': |
| 47 | return CTypeDM |
| 48 | default: |
| 49 | return CTypeUnknown |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // initialize replacer only once (if needed) |
| 54 | var escapeReplacer = strings.NewReplacer("&", "&", "<", "<", ">", ">") |
| 55 | |
| 56 | // EscapeMessage text |
| 57 | func EscapeMessage(message string) string { |
| 58 | return escapeReplacer.Replace(message) |
| 59 | } |
| 60 | |
| 61 | // Retryable errors return true. |
| 62 | type Retryable interface { |
| 63 | Retryable() bool |
| 64 | } |