streaming working

This commit is contained in:
tsmethurst 2021-06-18 13:06:02 +02:00 committed by tsmethurst
commit a3d3cd8d43
13 changed files with 170 additions and 93 deletions

View file

@ -1,14 +1,16 @@
package streaming
import (
"encoding/json"
"errors"
"fmt"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
)
func (p *processor) StreamStatusForAccount(s *apimodel.Status, account *gtsmodel.Account) error {
func (p *processor) StreamStatusToAccount(s *apimodel.Status, account *gtsmodel.Account) error {
l := p.log.WithFields(logrus.Fields{
"func": "StreamStatusForAccount",
"account": account.ID,
@ -19,17 +21,28 @@ func (p *processor) StreamStatusForAccount(s *apimodel.Status, account *gtsmodel
return nil
}
streams, ok := v.(*streamsForAccount)
streamsForAccount, ok := v.(*gtsmodel.StreamsForAccount)
if !ok {
return errors.New("stream map error")
}
streams.Lock()
defer streams.Unlock()
for _, stream := range streams.s {
l.Debugf("streaming status to stream id %s", stream.streamID)
if err := stream.conn.WriteJSON(s); err != nil {
return err
statusBytes, err := json.Marshal(s)
if err != nil {
return fmt.Errorf("error marshalling status to json: %s", err)
}
streamsForAccount.Lock()
defer streamsForAccount.Unlock()
for _, stream := range streamsForAccount.Streams {
stream.Lock()
defer stream.Unlock()
if stream.Connected {
l.Debugf("streaming status to stream id %s", stream.ID)
stream.Messages <- &gtsmodel.Message{
Stream: []string{stream.Type},
Event: "update",
Payload: string(statusBytes),
}
}
}