-
Notifications
You must be signed in to change notification settings - Fork 2
/
rest.go
120 lines (96 loc) · 3.03 KB
/
rest.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bytes"
"context"
"fmt"
"log"
"github.com/ably-labs/Ableye/config"
"github.com/ably-labs/Ableye/text"
ably "github.com/ably/ably-go/ably"
)
// createRestClient creates a new rest client and stores it in a connection.
// A clientID is also set on the client.
func createRestClient(id connectionID) error {
newClient, err := ably.NewREST(
ably.WithKey(config.Cfg.Key),
ably.WithClientID(id.string()),
)
if err != nil {
return err
}
connection := newRestConnection(newClient, rest)
connections[id] = &connection
log.Println(createRestClientSuccess)
return nil
}
// closeRestClient closes an existing realtime client and removes the connection.
func closeRestClient(id connectionID) {
if connections[id] != nil && connections[id].restClient != nil {
//Tear down the connection in internal memory.
connections[id].restClient = nil
connections[id] = nil
log.Println(closeRestClientSuccess)
}
}
// restSetChannel sets the rest channel to the name provided in the channel name input text box.
func restSetChannel(name string, id connectionID) {
newChannel := connections[id].restClient.Channels.Get(name)
connections[id].restChannel = newChannel
log.Println(setRestChannelSuccess)
}
// restGetChannelStatus makes a request to get the channel status
func restGetChannelStatus(id connectionID) error {
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
channel := connections[id].restChannel
channelDetails, err := channel.Status(ctx)
if err != nil {
return err
}
log.Printf("received channel details: %+v", channelDetails)
connections[id].channelDetails = channelDetails
return nil
}
// publishToRestChannel publishes message name and message data to a realtime channel.
func publishToRestChannel(id connectionID, messageName string, messageData interface{}) error {
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
if err := connections[id].restChannel.Publish(ctx, messageName, messageData); err != nil {
return err
}
log.Println(publishSuccess)
return nil
}
// getRestPresence sets the presence info text box to presence information.
func getRestPresence(id connectionID, presenceInfo *text.Text) {
var buffer bytes.Buffer
log.Println(startGetPresence)
// Set timeout to be default timeout
ctx, cancel := context.WithTimeout(connections[id].context, defaultTimeout)
defer cancel()
pages, err := connections[id].restChannel.Presence.Get().Pages(ctx)
if err != nil {
log.Println(err)
return
}
for pages.Next(ctx) {
for i, msg := range pages.Items() {
if msg != nil {
buffer.WriteString(msg.ClientID)
// if not the last message, add a comma and a space.
if i != len(pages.Items())-1 {
buffer.WriteString(", ")
}
}
}
}
if err := pages.Err(); err != nil {
log.Println(err)
return
}
presence := buffer.String()
presenceInfo.SetText(fmt.Sprintf("Presence : %s", presence))
log.Println(completeGetPresence)
}