Use interfaces and callbacks

This commit is contained in:
Karmanyaah Malhotra 2021-02-21 22:53:43 -05:00
parent 784cfe93c1
commit eeb2f88b97

View File

@ -47,29 +47,46 @@ type LikeEvent struct {
Message Message Message Message
} }
type EventType = int
const (
EventMessage EventType = iota
EventLike
)
type Handler interface {
HandleError(error)
}
type HandlerText interface {
HandleTextMessage(Message)
}
type HandlerLike interface {
HandleLike(Message)
}
//PushSubscription manages real time subscription //PushSubscription manages real time subscription
type PushSubscription struct { type PushSubscription struct {
channel chan wray.Message channel chan wray.Message
//Channel to return things fayeClient *wray.FayeClient
MessageChannel chan Message handlers []Handler
LikeChannel chan LikeEvent
fayeClient *wray.FayeClient
} }
//NewPushSubscription creates and returns a push subscription object //NewPushSubscription creates and returns a push subscription object
func NewPushSubscription(context context.Context) PushSubscription { func NewPushSubscription(context context.Context) PushSubscription {
r := PushSubscription{ r := PushSubscription{
channel: make(chan wray.Message), channel: make(chan wray.Message),
MessageChannel: make(chan Message),
LikeChannel: make(chan LikeEvent),
} }
return r return r
} }
func (r *PushSubscription) AddHandler(h Handler) {
r.handlers = append(r.handlers, h)
}
//Listen connects to GroupMe. Runs in Goroutine. //Listen connects to GroupMe. Runs in Goroutine.
func (r *PushSubscription) Listen(context context.Context) { func (r *PushSubscription) StartListening(context context.Context) {
r.fayeClient = wray.NewFayeClient(pushServer) r.fayeClient = wray.NewFayeClient(pushServer)
r.fayeClient.SetLogger(fayeLogger{}) r.fayeClient.SetLogger(fayeLogger{})
@ -79,48 +96,55 @@ func (r *PushSubscription) Listen(context context.Context) {
go r.fayeClient.Listen() go r.fayeClient.Listen()
for { go func() {
msg := <-r.channel for {
data := msg.Data() msg := <-r.channel
content, _ := data["subject"] data := msg.Data()
contentType := data["type"].(string) content, _ := data["subject"]
contentType := data["type"].(string)
switch contentType { switch contentType {
case "line.create": case "line.create":
b, _ := json.Marshal(content) b, _ := json.Marshal(content)
out := Message{} out := Message{}
json.Unmarshal(b, &out) json.Unmarshal(b, &out)
//fmt.Printf("%+v\n", out) //TODO //fmt.Printf("%+v\n", out) //TODO
for _, h := range r.handlers {
if h, ok := h.(HandlerText); ok {
h.HandleTextMessage(out)
}
}
r.MessageChannel <- out
break
case "like.create":
b, _ := json.Marshal(content.(map[string]interface{})["line"])
out := Message{}
//log.Println(string(b))
err := json.Unmarshal(b, &out)
if err != nil {
log.Println(err)
}
outt := LikeEvent{Message: out}
//fmt.Printf("Like on %+v \n", outt.Message)
r.LikeChannel <- outt
break
case "ping":
break
default: //TODO: see if any other types are returned
if len(contentType) == 0 || content == nil {
break break
case "like.create":
b, _ := json.Marshal(content.(map[string]interface{})["line"])
out := Message{}
//log.Println(string(b))
err := json.Unmarshal(b, &out)
if err != nil {
log.Println(err)
}
for _, h := range r.handlers {
if h, ok := h.(HandlerLike); ok {
h.HandleLike(out)
}
}
break
case "ping":
break
default: //TODO: see if any other types are returned
if len(contentType) == 0 || content == nil {
break
}
log.Println(contentType)
log.Fatalln(data)
} }
log.Println(contentType)
log.Fatalln(data)
} }
}()
}
} }
//SubscribeToUser to users //SubscribeToUser to users