feat: distribute new local posts to followers via websocket
This commit is contained in:
parent
5c6da51234
commit
97b05d787f
6 changed files with 107 additions and 75 deletions
|
@ -1,64 +0,0 @@
|
|||
package streaming
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type EventType int8
|
||||
|
||||
const (
|
||||
EventTypeError EventType = 1
|
||||
EventTypePost EventType = 2
|
||||
|
||||
EventTypeSubscribe EventType = 126
|
||||
EventTypeUnsubscribe EventType = 127
|
||||
)
|
||||
|
||||
func (et EventType) Valid() bool {
|
||||
switch et {
|
||||
case EventTypeError:
|
||||
return true
|
||||
case EventTypePost:
|
||||
return true
|
||||
case EventTypeSubscribe:
|
||||
return true
|
||||
case EventTypeUnsubscribe:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Returns true if this event can be subscribed to/unsubscribed from
|
||||
func (et EventType) ValidReceive() bool {
|
||||
if !et.Valid() {
|
||||
return false
|
||||
}
|
||||
|
||||
switch et {
|
||||
case EventTypeError, EventTypeSubscribe, EventTypeUnsubscribe:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
Type EventType `json:"t"`
|
||||
Data any `json:"d"`
|
||||
}
|
||||
|
||||
func newEvent(mt EventType, data any) *Event {
|
||||
return &Event{
|
||||
Type: mt,
|
||||
Data: data,
|
||||
}
|
||||
}
|
||||
|
||||
type ErrorEvent struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type IncomingEvent struct {
|
||||
Type EventType `json:"t"`
|
||||
Data json.RawMessage `json:"d"` // this is a RawMessage so we can easily unmarshal it later
|
||||
}
|
|
@ -41,7 +41,10 @@ func (app *App) Streaming(w http.ResponseWriter, r *http.Request) error {
|
|||
ctx, cancel := context.WithCancel(context.Background())
|
||||
socket, ok := app.Processor.SocketHolder.SocketsFor(token.UserID).NewSocket(ctx, cancel)
|
||||
if !ok {
|
||||
err := conn.WriteJSON(newEvent(EventTypeError, ErrorEvent{Code: api.ErrTooManyStreams, Message: "Too many streams open"}))
|
||||
err := conn.WriteJSON(streaming.Event{
|
||||
Type: streaming.EventTypeError,
|
||||
Data: streaming.ErrorEvent{Code: api.ErrTooManyStreams, Message: "Too many streams open"},
|
||||
})
|
||||
if err != nil {
|
||||
log.Err(err).Msg("writing stream rejection message to socket")
|
||||
}
|
||||
|
@ -100,11 +103,24 @@ func (app *App) readStream(conn *websocket.Conn, socket *streaming.Socket) {
|
|||
}
|
||||
|
||||
if !et.ValidReceive() {
|
||||
log.Debug().Int("event", int(et)).Msg("invalid event for subscription")
|
||||
|
||||
// if it's not a valid event, ignore silently
|
||||
continue
|
||||
}
|
||||
|
||||
socket.SetEvent(et, e.Type != streaming.EventTypeUnsubscribe)
|
||||
socket.SetEvent(et, e.Type == streaming.EventTypeSubscribe)
|
||||
log.Debug().
|
||||
Int("event", int(et)).
|
||||
Bool("subscribed", e.Type == streaming.EventTypeSubscribe).
|
||||
Msg("toggled subscription status for event")
|
||||
}
|
||||
|
||||
if socket.WillAcceptEvent(streaming.EventTypeEcho) {
|
||||
conn.WriteJSON(streaming.Event{
|
||||
Type: streaming.EventTypeEcho,
|
||||
Data: e,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue