From 4daf030d7e6240f2218dce6e24eb4d6624d82398 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Wed, 7 Aug 2024 08:05:46 -0700 Subject: [PATCH] /api/v1/streaming/public/local --- api/v1/streaming/public/local/client.go | 125 ++++++++++++++++++++++++ api/v1/streaming/public/local/dial.go | 18 +++- api/v1/streaming/public/local/errors.go | 11 +++ api/v1/streaming/public/local/event.go | 21 +++- 4 files changed, 171 insertions(+), 4 deletions(-) create mode 100644 api/v1/streaming/public/local/errors.go diff --git a/api/v1/streaming/public/local/client.go b/api/v1/streaming/public/local/client.go index 18659c3..3205fef 100644 --- a/api/v1/streaming/public/local/client.go +++ b/api/v1/streaming/public/local/client.go @@ -1,7 +1,132 @@ package local import ( + "encoding/json" + "fmt" + "github.com/reiver/go-httpsse" + "github.com/reiver/go-opt" ) +// Client represenrs a client for interacting with the "/api/v1/streaming/public/local" API end-point. +// +// That API end-point will return a series of events. +// +// Client will let you iterate through those events. type Client = httpsse.Client + +type internalClient struct { + sseclient httpsse.Client + data []string + err error +} + +var _ Client = &internalClient{} + +func (receiver *internalClient) Close() error { + if nil == receiver { + return errNilReceiver + } + + var sseclient httpsse.Client = receiver.sseclient + + if nil == sseclient { + return errNilHTTPSSEClient + } + + return sseclient.Close() +} + +func (receiver *internalClient) Decode(dst interface{}) error { + + if nil == dst { + return errNilDestination + } + + var event *Event + { + var casted bool + event, casted = dst.(*Event) + if !casted { + + } + } + + if len(receiver.data) <= 0 { + + } + + var datum string + { + datum, receiver.data = receiver.data[0], receiver.data[1:] + } +fmt.Printf("DATUM:\n%s \n", datum) + + if len(datum) <= 0 { + + } + + switch datum[0] { + case '0','1','2','3','4','5','6','7','8','9': + event.Name="delete" + event.Status.ID = opt.Something(datum) + default: + event.Name="update" + + var bytes []byte = []byte(datum) + + { + err := json.Unmarshal(bytes, &(event.Status)) + if nil != err { + + return err + } + } + } + + + return nil +} + +func (receiver *internalClient) Err() error { + if nil == receiver { + return errNilReceiver + } + + if nil != receiver.err { + return receiver.err + } + + return receiver.sseclient.Err() +} + +func (receiver *internalClient) Next() bool { + if nil == receiver { + return false + } + + if 0 < len(receiver.data) { + return true + } + + more := receiver.sseclient.Next() + if !more { + return false + } + + var event httpsse.Event + { + err := receiver.sseclient.Decode(&event) + if nil != err { + receiver.err = err + return false + } + } + + receiver.data = append(receiver.data, event.EventData()...) + +//@TODO: what if event.EventData() returns an empty array? +// which could happen. + + return true +} diff --git a/api/v1/streaming/public/local/dial.go b/api/v1/streaming/public/local/dial.go index c7eddc0..effbbfe 100644 --- a/api/v1/streaming/public/local/dial.go +++ b/api/v1/streaming/public/local/dial.go @@ -14,9 +14,23 @@ func DialHost(host string) (Client, error) { Path:Path, } - return httpsse.DialURL(urloc.String()) + sseclient, err := httpsse.DialURL(urloc.String()) + if nil != err { + return nil, err + } + + return &internalClient{ + sseclient:sseclient, + }, nil } func Dial(req *http.Request) (Client, error) { - return httpsse.Dial(req) + sseclient, err :=httpsse.Dial(req) + if nil != err { + return nil, err + } + + return &internalClient{ + sseclient:sseclient, + }, nil } diff --git a/api/v1/streaming/public/local/errors.go b/api/v1/streaming/public/local/errors.go new file mode 100644 index 0000000..0590ee4 --- /dev/null +++ b/api/v1/streaming/public/local/errors.go @@ -0,0 +1,11 @@ +package local + +import ( + "github.com/reiver/go-erorr" +) + +const ( + errNilDestination = erorr.Error("mstdn: nil destination") + errNilHTTPSSEClient = erorr.Error("mstdn: nil http-sse-client") + errNilReceiver = erorr.Error("mstdn: nil receiver") +) diff --git a/api/v1/streaming/public/local/event.go b/api/v1/streaming/public/local/event.go index d93fd3e..4b5d9a2 100644 --- a/api/v1/streaming/public/local/event.go +++ b/api/v1/streaming/public/local/event.go @@ -1,7 +1,24 @@ package local import ( - "github.com/reiver/go-httpsse" + "github.com/reiver/go-mstdn/ent" ) -type Event = httpsse.Event +// The "/api/v1/streaming/public/local" API end-points returns a series of events. +// Event is used to represent one of those events. +// +// And event has a 'name'. +// Currently the possible names are: +// +// • "delete" +// • "update" +// +// And event also has a status. +// +// If the event is an "update", then all of (or most of) the 'status' should be filled in. +// +// If the event is a "delete", then only the 'ID' will be filled in. +type Event struct { + Name string + Status ent.Status +}