forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.go
88 lines (70 loc) · 1.94 KB
/
helpers.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
package messaging
import (
"fmt"
"strings"
"time"
)
// Timeout channel for non-subscribe requests
func Timeout() <-chan time.Time {
return Timeouts(GetNonSubscribeTimeout())
}
// SubscribeTimeout: Timeout channel for subscribe requests
func SubscribeTimeout() <-chan time.Time {
return Timeouts(GetSubscribeTimeout())
}
// Timeouts: Timeout channel with custon timeout value
func Timeouts(seconds uint16) <-chan time.Time {
return time.After(time.Second * time.Duration(seconds))
}
// PresenceEvent: Type for presence events
type PresenceEvent struct {
Action string `json:"action"`
Uuid string `json:"uuid"`
Timestamp float64 `json:"timestamp"`
Occupancy int `json:"occupancy"`
}
// PresenceResonse: Type for presence response
type PresenceResonse struct {
Action string `json:"action"`
Status int `json:"status"`
Service string `json:"service"`
Message string `json:"message"`
}
func stringPresenceOrSubscribe(channel string) string {
const (
subscribeMessage string = "Subscription to"
presenceMessage string = "Presence notifications for"
)
if strings.HasSuffix(channel, presenceSuffix) {
return presenceMessage
}
return subscribeMessage
}
func splitItems(items string) []string {
if items == "" {
return []string{}
}
return strings.Split(items, ",")
}
func addPnpresToString(items string) string {
var presenceItems []string
itemsSlice := splitItems(items)
for _, v := range itemsSlice {
presenceItems = append(presenceItems,
fmt.Sprintf("%s%s", v, presenceSuffix))
}
return strings.Join(presenceItems, ",")
}
func removePnpres(initial string) string {
return strings.TrimSuffix(initial, presenceSuffix)
}
// Check does passed in string contain at least one non preesnce name
func hasNonPresenceChannels(channelsString string) bool {
channels := strings.Split(channelsString, ",")
for _, channel := range channels {
if !strings.HasSuffix(channel, presenceSuffix) {
return true
}
}
return false
}