-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_updates_and_deletes.go
129 lines (104 loc) · 2.85 KB
/
message_updates_and_deletes.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
121
122
123
124
125
126
127
128
129
package main
import (
"fmt"
"time"
pubnub "github.com/pubnub/go"
)
type Lists struct {
message_list map[string]interface{}
deleted_ids map[string]interface{}
message_ids map[string]interface{}
}
var my_channel string
func displayMessages(message interface{}, channel string, lists *Lists) {
if _, ok := lists.message_list[channel]; !ok {
lists.message_list[channel] = map[string]interface{}{}
}
if _, ok := lists.message_ids[channel]; !ok {
lists.message_ids[channel] = map[string]interface{}{}
}
if _, ok := lists.deleted_ids[channel]; !ok {
lists.deleted_ids[channel] = map[string]interface{}{}
}
messageObj := map[string]interface{}{}
if m, ok := message.(map[string]interface{}); ok {
if message_id, ok := m["message_id"].(string); ok {
if m_ids, ok := lists.message_ids[channel].(map[string]interface{}); ok {
if _, ok := m_ids[message_id]; !ok {
lists.message_ids[channel] = message_id
if m_id, ok := m["message_id"].(string); ok {
messageObj[m_id] = message
}
lists.message_list[channel] = messageObj
} else {
if del, ok := m["deleted"].(bool); ok {
if del {
lists.deleted_ids[channel] = m["message_id"]
delete(lists.message_list, channel)
} else {
if m_id, ok := m["message_id"].(string); ok {
messageObj[m_id] = message
}
lists.message_list[channel] = messageObj
}
}
}
}
}
}
}
func main() {
config := pubnub.NewConfig()
config.PublishKey = "pub-c-1bd448ed-05ba-4dbc-81a5-7d6ff5c6e2bb"
config.SubscribeKey = "sub-c-b9ab9508-43cf-11e8-9967-869954283fb4"
pn := pubnub.NewPubNub(config)
my_channel = "jasdeep-status"
lists := Lists{
map[string]interface{}{},
map[string]interface{}{},
map[string]interface{}{},
}
listener := pubnub.NewListener()
go func() {
for {
select {
case status := <-listener.Status:
fmt.Println(status)
case message := <-listener.Message:
displayMessages(message.Message, message.Channel, &lists)
case <-listener.Presence:
}
}
}()
pn.AddListener(listener)
pn.Subscribe().
Channels([]string{my_channel}).
Execute()
data := map[string]interface{}{}
data["message_id"] = "10001"
data["channel"] = "jasdeep-status"
data["original_timetoken"] = time.Now().Unix()
data["user"] = "jasdeep"
data["status"] = "Writing up design patterns..."
data["usecase"] = "update"
data["deleted"] = false
data["is_update"] = true
res, status, err := pn.Publish().
Message(data).
Channel("jasdeep-status").
Execute()
fmt.Println(res, status, err)
data["is_update"] = false
data["deleted"] = true
res, status, err = pn.Publish().
Message(data).
Channel("jasdeep-status-UPDATES").
Execute()
fmt.Println(res, status, err)
data["is_update"] = true
res, status, err = pn.Publish().
Message(data).
Channel("jasdeep-status").
Execute()
fmt.Println(res, status, err)
}