File: slackutilsx.go

package info (click to toggle)
golang-github-slack-go-slack 0.11.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,720 kB
  • sloc: makefile: 54
file content (64 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Package slackutilsx is a utility package that doesn't promise API stability.
// its for experimental functionality and utilities.
package slackutilsx

import (
	"strings"
	"unicode/utf8"
)

// ChannelType the type of channel based on the channelID
type ChannelType int

func (t ChannelType) String() string {
	switch t {
	case CTypeDM:
		return "Direct"
	case CTypeGroup:
		return "Group"
	case CTypeChannel:
		return "Channel"
	default:
		return "Unknown"
	}
}

const (
	// CTypeUnknown represents channels we cannot properly detect.
	CTypeUnknown ChannelType = iota
	// CTypeDM is a private channel between two slack users.
	CTypeDM
	// CTypeGroup is a group channel.
	CTypeGroup
	// CTypeChannel is a public channel.
	CTypeChannel
)

// DetectChannelType converts a channelID to a ChannelType.
// channelID must not be empty. However, if it is empty, the channel type will default to Unknown.
func DetectChannelType(channelID string) ChannelType {
	// intentionally ignore the error and just default to CTypeUnknown
	switch r, _ := utf8.DecodeRuneInString(channelID); r {
	case 'C':
		return CTypeChannel
	case 'G':
		return CTypeGroup
	case 'D':
		return CTypeDM
	default:
		return CTypeUnknown
	}
}

// initialize replacer only once (if needed)
var escapeReplacer = strings.NewReplacer("&", "&amp;", "<", "&lt;", ">", "&gt;")

// EscapeMessage text
func EscapeMessage(message string) string {
	return escapeReplacer.Replace(message)
}

// Retryable errors return true.
type Retryable interface {
	Retryable() bool
}