From aa9c1859b5daaecbabe1e36366293574592225a2 Mon Sep 17 00:00:00 2001 From: Rasmus Lindroth Date: Fri, 30 Dec 2022 10:45:58 +0100 Subject: [PATCH] bug fix for conversation/direct --- streaming.go | 13 +++++++++++++ streaming_test.go | 7 +++++++ streaming_ws.go | 11 +++++++++++ streaming_ws_test.go | 18 ++++++++++++++---- 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/streaming.go b/streaming.go index b109f6c..9ebf997 100644 --- a/streaming.go +++ b/streaming.go @@ -39,6 +39,13 @@ type DeleteEvent struct{ ID ID } func (e *DeleteEvent) event() {} +// ConversationEvent is a struct for passing conversationevent to app. +type ConversationEvent struct { + Conversation *Conversation `json:"conversation"` +} + +func (e *ConversationEvent) event() {} + // ErrorEvent is a struct for passing errors to app. type ErrorEvent struct{ err error } @@ -100,6 +107,12 @@ func handleReader(q chan Event, r io.Reader) error { if err == nil { q <- &NotificationEvent{¬ification} } + case "conversation": + var conversation Conversation + err = json.Unmarshal([]byte(token[1]), &conversation) + if err == nil { + q <- &ConversationEvent{&conversation} + } case "delete": q <- &DeleteEvent{ID: ID(strings.TrimSpace(token[1]))} } diff --git a/streaming_test.go b/streaming_test.go index a9e90d9..0a65cec 100644 --- a/streaming_test.go +++ b/streaming_test.go @@ -30,6 +30,8 @@ event: delete data: 1234567 event: status.update data: {"content": "foo"} +event: conversation +data: {"id":"819516","unread":true,"accounts":[{"id":"108892712797543112","username":"a","acct":"a@pl.nulled.red","display_name":"a","locked":false,"bot":true,"discoverable":false,"group":false,"created_at":"2022-08-27T00:00:00.000Z","note":"a (pleroma edition)","url":"https://pl.nulled.red/users/a","avatar":"https://files.mastodon.social/cache/accounts/avatars/108/892/712/797/543/112/original/975674b2caa61034.png","avatar_static":"https://files.mastodon.social/cache/accounts/avatars/108/892/712/797/543/112/original/975674b2caa61034.png","header":"https://files.mastodon.social/cache/accounts/headers/108/892/712/797/543/112/original/f61d0382356caa0e.png","header_static":"https://files.mastodon.social/cache/accounts/headers/108/892/712/797/543/112/original/f61d0382356caa0e.png","followers_count":0,"following_count":0,"statuses_count":362,"last_status_at":"2022-11-13","emojis":[],"fields":[]}],"last_status":{"id":"109346889330629417","created_at":"2022-11-15T08:31:57.476Z","in_reply_to_id":null,"in_reply_to_account_id":null,"sensitive":false,"spoiler_text":"","visibility":"direct","language":null,"uri":"https://pl.nulled.red/objects/c869c5be-c184-4706-a45d-3459d9aa711c","url":"https://pl.nulled.red/objects/c869c5be-c184-4706-a45d-3459d9aa711c","replies_count":0,"reblogs_count":0,"favourites_count":0,"edited_at":null,"favourited":false,"reblogged":false,"muted":false,"bookmarked":false,"content":"test @trwnh","filtered":[],"reblog":null,"account":{"id":"108892712797543112","username":"a","acct":"a@pl.nulled.red","display_name":"a","locked":false,"bot":true,"discoverable":false,"group":false,"created_at":"2022-08-27T00:00:00.000Z","note":"a (pleroma edition)","url":"https://pl.nulled.red/users/a","avatar":"https://files.mastodon.social/cache/accounts/avatars/108/892/712/797/543/112/original/975674b2caa61034.png","avatar_static":"https://files.mastodon.social/cache/accounts/avatars/108/892/712/797/543/112/original/975674b2caa61034.png","header":"https://files.mastodon.social/cache/accounts/headers/108/892/712/797/543/112/original/f61d0382356caa0e.png","header_static":"https://files.mastodon.social/cache/accounts/headers/108/892/712/797/543/112/original/f61d0382356caa0e.png","followers_count":0,"following_count":0,"statuses_count":362,"last_status_at":"2022-11-13","emojis":[],"fields":[]},"media_attachments":[],"mentions":[{"id":"14715","username":"trwnh","url":"https://mastodon.social/@trwnh","acct":"trwnh"}],"tags":[],"emojis":[],"card":null,"poll":null}} :thump `, largeContent)) var wg sync.WaitGroup @@ -61,6 +63,11 @@ data: {"content": "foo"} } else { t.Fatalf("bad update content: %q", event.Status.Content) } + case *ConversationEvent: + passNotification = true + if event.Conversation.ID != "819516" { + t.Fatalf("want %q but %q", "819516", event.Conversation.ID) + } case *NotificationEvent: passNotification = true if event.Notification.Type != "mention" { diff --git a/streaming_ws.go b/streaming_ws.go index 5658bbf..2dff42e 100644 --- a/streaming_ws.go +++ b/streaming_ws.go @@ -56,6 +56,11 @@ func (c *WSClient) StreamingWSList(ctx context.Context, id ID) (chan Event, erro return c.streamingWS(ctx, "list", string(id)) } +// StreamingWSDirect return channel to read events on a direct messages using WebSocket. +func (c *WSClient) StreamingWSDirect(ctx context.Context) (chan Event, error) { + return c.streamingWS(ctx, "direct", "") +} + func (c *WSClient) streamingWS(ctx context.Context, stream, tag string) (chan Event, error) { params := url.Values{} params.Set("access_token", c.client.Config.AccessToken) @@ -139,6 +144,12 @@ func (c *WSClient) handleWS(ctx context.Context, rawurl string, q chan Event) er if err == nil { q <- &NotificationEvent{Notification: ¬ification} } + case "conversation": + var conversation Conversation + err = json.Unmarshal([]byte(s.Payload.(string)), &conversation) + if err == nil { + q <- &ConversationEvent{Conversation: &conversation} + } case "delete": if f, ok := s.Payload.(float64); ok { q <- &DeleteEvent{ID: ID(fmt.Sprint(int64(f)))} diff --git a/streaming_ws_test.go b/streaming_ws_test.go index 7e78890..e6b6661 100644 --- a/streaming_ws_test.go +++ b/streaming_ws_test.go @@ -101,6 +101,13 @@ func wsMock(w http.ResponseWriter, r *http.Request) { return } + err = conn.WriteMessage(websocket.TextMessage, + []byte(`{"event":"conversation","payload":"{\"id\":819516}"}`)) + if err != nil { + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + return + } + err = conn.WriteMessage(websocket.TextMessage, []byte(`{"event":"update","payload":""}`)) if err != nil { @@ -119,8 +126,8 @@ func wsTest(t *testing.T, q chan Event, cancel func()) { for e := range q { events = append(events, e) } - if len(events) != 7 { - t.Fatalf("result should be seven: %d", len(events)) + if len(events) != 8 { + t.Fatalf("result should be 8: %d", len(events)) } if events[0].(*UpdateEvent).Status.Content != "foo" { t.Fatalf("want %q but %q", "foo", events[0].(*UpdateEvent).Status.Content) @@ -134,8 +141,8 @@ func wsTest(t *testing.T, q chan Event, cancel func()) { if events[3].(*DeleteEvent).ID != "1234567" { t.Fatalf("want %q but %q", "1234567", events[3].(*DeleteEvent).ID) } - if errorEvent, ok := events[4].(*ErrorEvent); !ok { - t.Fatalf("should be fail: %v", errorEvent.err) + if events[4].(*ConversationEvent).Conversation.ID != "819516" { + t.Fatalf("want %q but %q", "819516", events[4].(*ConversationEvent).Conversation.ID) } if errorEvent, ok := events[5].(*ErrorEvent); !ok { t.Fatalf("should be fail: %v", errorEvent.err) @@ -143,6 +150,9 @@ func wsTest(t *testing.T, q chan Event, cancel func()) { if errorEvent, ok := events[6].(*ErrorEvent); !ok { t.Fatalf("should be fail: %v", errorEvent.err) } + if errorEvent, ok := events[7].(*ErrorEvent); !ok { + t.Fatalf("should be fail: %v", errorEvent.err) + } } func TestStreamingWS(t *testing.T) {