-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.go
66 lines (60 loc) · 1.26 KB
/
utils.go
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
65
66
package bot
import (
"crypto/md5"
"io"
"strings"
"github.com/gofrs/uuid/v5"
)
func UniqueObjectId(args ...string) string {
h := md5.New()
for _, s := range args {
io.WriteString(h, s)
}
sum := h.Sum(nil)
sum[6] = (sum[6] & 0x0f) | 0x30
sum[8] = (sum[8] & 0x3f) | 0x80
id, err := uuid.FromBytes(sum)
if err != nil {
panic(err)
}
return id.String()
}
func UniqueConversationId(userId, recipientId string) string {
minId, maxId := userId, recipientId
if strings.Compare(userId, recipientId) > 0 {
maxId, minId = userId, recipientId
}
h := md5.New()
io.WriteString(h, minId)
io.WriteString(h, maxId)
sum := h.Sum(nil)
sum[6] = (sum[6] & 0x0f) | 0x30
sum[8] = (sum[8] & 0x3f) | 0x80
id, _ := UuidFromBytes(sum)
return id.String()
}
func Chunked(source []interface{}, size int) [][]interface{} {
var result [][]interface{}
index := 0
for index < len(source) {
end := index + size
if end >= len(source) {
end = len(source)
}
result = append(result, source[index:end])
index += size
}
return result
}
func MakeUniqueStringSlice(ss []string) []string {
unique := make([]string, 0)
filter := make(map[string]bool)
for _, s := range ss {
if filter[s] {
continue
}
unique = append(unique, s)
filter[s] = true
}
return unique
}