This repository has been archived by the owner on Jul 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
103 lines (91 loc) · 2.47 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"regexp"
"strconv"
"strings"
)
// Flag - Describes a flag in the database
type Flag struct {
ID int `json:"id"`
Group string `json:"group"`
Name string `json:"name"`
Flag map[string]interface{} `json:"flag"`
}
// Group - describes a group in the database
type Group struct {
ID int `json:"id"`
Name string `json:"name"`
}
// GroupHolder - Describes a group holder in the database
type GroupHolder struct {
ID int `json:"id"`
Group string `json:"group"`
User string `json:"user"`
Channel string `json:"channel"`
}
// GroupHolderFQResult - Describes a group holder received from FlagQueryResult
type GroupHolderFQResult struct {
ID int `json:"id"`
UserID int `json:"user_id"`
ChannelID int `json:"channel_id"`
GroupID int `json:"group_id"`
GroupName string `json:"group_name"`
}
// FlagQueryResult - Describes result of a flag query
type FlagQueryResult struct {
GroupHolders []*GroupHolderFQResult
FlagID int
FlagName string
Flag map[string]interface{}
}
// ChatMessage - a message received from message history
type ChatMessage struct {
Message string `json:"content"`
Pings []string `json:"pings"`
ID int `json:"id"`
Timestamp string `json:"timestamp"`
Author string `json:"author"`
AuthorID int `json:"author_id"`
}
// User - describes a user in the database
type User struct {
ID int `json:"id"`
Username string `json:"username"`
Owner bool `json:"owner"`
}
// UserStatus - describes the status of a user
type UserStatus struct {
User *User `json:"user"`
Online bool `json:"online"`
}
// Channel - describes a channel in the database
type Channel struct {
ID int `json:"id"`
Name string `json:"name"`
Topic string `json:"topic"`
}
// getPings - gets all mentioned/pinged user IDs from a message string.
func scanForPings(message string) []int {
re := regexp.MustCompile(`\<([^\<\>]*)\>`)
ids := []int{}
submatchall := re.FindAllString(message, -1)
for _, element := range submatchall {
element = strings.Trim(element, "<")
element = strings.Trim(element, ">")
element = strings.Trim(element, "@")
i, err := strconv.ParseInt(element, 10, 32)
if err == nil {
ids = append(ids, int(i))
}
}
return ids
}
// contains - check if a slice contains a value
func contains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}