Fix streamed messages ending up in wrong timeline(s) (#325)

* define timeline consts

* remove double stream of status

* change test stream creation up a bit

* stream messages more selectively

* add test for streaming new status creation via clientAPI

* tidy code + comments a bit

* tidy up tests

* make sure new status isn't streamed to public
This commit is contained in:
tobi
2021-11-22 19:03:21 +01:00
committed by GitHub
parent a7882fabc7
commit 3caae376e7
10 changed files with 208 additions and 50 deletions

View File

@@ -33,5 +33,5 @@ func (p *processor) StreamNotificationToAccount(n *apimodel.Notification, accoun
return fmt.Errorf("error marshalling notification to json: %s", err)
}
return p.streamToAccount(string(bytes), stream.EventTypeNotification, account.ID)
return p.streamToAccount(string(bytes), stream.EventTypeNotification, []string{stream.TimelineNotifications, stream.TimelineHome}, account.ID)
}

View File

@@ -30,11 +30,11 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/stream"
)
func (p *processor) OpenStreamForAccount(ctx context.Context, account *gtsmodel.Account, streamType string) (*stream.Stream, gtserror.WithCode) {
func (p *processor) OpenStreamForAccount(ctx context.Context, account *gtsmodel.Account, streamTimeline string) (*stream.Stream, gtserror.WithCode) {
l := logrus.WithFields(logrus.Fields{
"func": "OpenStreamForAccount",
"account": account.ID,
"streamType": streamType,
"streamType": streamTimeline,
})
l.Debug("received open stream request")
@@ -46,7 +46,7 @@ func (p *processor) OpenStreamForAccount(ctx context.Context, account *gtsmodel.
thisStream := &stream.Stream{
ID: streamID,
Type: streamType,
Timeline: streamTimeline,
Messages: make(chan *stream.Message, 100),
Hangup: make(chan interface{}, 1),
Connected: true,

View File

@@ -37,7 +37,7 @@ func (p *processor) StreamDelete(statusID string) error {
// stream the delete to every account
for _, accountID := range accountIDs {
if err := p.streamToAccount(statusID, stream.EventTypeDelete, accountID); err != nil {
if err := p.streamToAccount(statusID, stream.EventTypeDelete, stream.AllStatusTimelines, accountID); err != nil {
errs = append(errs, err.Error())
}
}

View File

@@ -35,9 +35,9 @@ type Processor interface {
// AuthorizeStreamingRequest returns an oauth2 token info in response to an access token query from the streaming API
AuthorizeStreamingRequest(ctx context.Context, accessToken string) (*gtsmodel.Account, error)
// OpenStreamForAccount returns a new Stream for the given account, which will contain a channel for passing messages back to the caller.
OpenStreamForAccount(ctx context.Context, account *gtsmodel.Account, streamType string) (*stream.Stream, gtserror.WithCode)
OpenStreamForAccount(ctx context.Context, account *gtsmodel.Account, timeline string) (*stream.Stream, gtserror.WithCode)
// StreamUpdateToAccount streams the given update to any open, appropriate streams belonging to the given account.
StreamUpdateToAccount(s *apimodel.Status, account *gtsmodel.Account) error
StreamUpdateToAccount(s *apimodel.Status, account *gtsmodel.Account, timeline string) error
// StreamNotificationToAccount streams the given notification to any open, appropriate streams belonging to the given account.
StreamNotificationToAccount(n *apimodel.Notification, account *gtsmodel.Account) error
// StreamDelete streams the delete of the given statusID to *ALL* open streams.

View File

@@ -25,7 +25,7 @@ import (
)
// streamToAccount streams the given payload with the given event type to any streams currently open for the given account ID.
func (p *processor) streamToAccount(payload string, event stream.EventType, accountID string) error {
func (p *processor) streamToAccount(payload string, event string, timelines []string, accountID string) error {
v, ok := p.streamMap.Load(accountID)
if !ok {
// no open connections so nothing to stream
@@ -42,11 +42,17 @@ func (p *processor) streamToAccount(payload string, event stream.EventType, acco
for _, s := range streamsForAccount.Streams {
s.Lock()
defer s.Unlock()
if s.Connected {
s.Messages <- &stream.Message{
Stream: []string{s.Type},
Event: string(event),
Payload: payload,
if !s.Connected {
continue
}
for _, t := range timelines {
if s.Timeline == string(t) {
s.Messages <- &stream.Message{
Stream: []string{string(t)},
Event: string(event),
Payload: payload,
}
}
}
}

View File

@@ -27,11 +27,11 @@ import (
"github.com/superseriousbusiness/gotosocial/internal/stream"
)
func (p *processor) StreamUpdateToAccount(s *apimodel.Status, account *gtsmodel.Account) error {
func (p *processor) StreamUpdateToAccount(s *apimodel.Status, account *gtsmodel.Account, timeline string) error {
bytes, err := json.Marshal(s)
if err != nil {
return fmt.Errorf("error marshalling status to json: %s", err)
}
return p.streamToAccount(string(bytes), stream.EventTypeUpdate, account.ID)
return p.streamToAccount(string(bytes), stream.EventTypeUpdate, []string{timeline}, account.ID)
}