From 515488b51d1690e1bb99d30abbf4b7a88877da71 Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Wed, 19 Aug 2026 07:59:37 +1000 Subject: [PATCH 1/2] Replace json.NewDecoder(strings.NewReader() with json.Unmarshal() --- matterclient.go | 17 ++++++++--------- messages.go | 2 +- users.go | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/matterclient.go b/matterclient.go index 259b77d..3014f2d 100644 --- a/matterclient.go +++ b/matterclient.go @@ -1080,7 +1080,7 @@ func (m *Client) WsReceiver(ctx context.Context) { } } else if userStr, ok := data["user"].(string); ok && userStr != "" { var summary UserSummary - _ = json.NewDecoder(strings.NewReader(userStr)).Decode(&summary) + _ = json.Unmarshal([]byte(userStr), &summary) if summary.Username != "" { userInfo = " [User: " + summary.Username + " (ID: " + summary.Id + ")]" } @@ -1394,7 +1394,7 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE u = userPtr } else if userStr, isStr := userVal.(string); isStr && userStr != "" { var summary UserSummary - _ = json.NewDecoder(strings.NewReader(userStr)).Decode(&summary) + _ = json.Unmarshal([]byte(userStr), &summary) // Map it back to the required model.User for the cache functions u = &model.User{ Id: summary.Id, @@ -1480,7 +1480,7 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE } else if postStr, ok := event.GetData()["post"].(string); ok && postStr != "" { // Fallback path: Driver left it as a JSON string post = &model.Post{} - _ = json.NewDecoder(strings.NewReader(postStr)).Decode(post) + _ = json.Unmarshal([]byte(postStr), post) } if post == nil || post.Id == "" { @@ -1533,10 +1533,10 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE post = postPtr } else if postStr, ok := event.GetData()["post"].(string); ok && postStr != "" { post = &model.Post{} - _ = json.NewDecoder(strings.NewReader(postStr)).Decode(post) + _ = json.Unmarshal([]byte(postStr), post) } - if m.postCache != nil && !strings.HasPrefix(post.Type, model.PostSystemMessagePrefix) { + if post != nil && post.Id != "" && m.postCache != nil && !strings.HasPrefix(post.Type, model.PostSystemMessagePrefix) { m.postCache.Add(post.Id, post) } @@ -1547,8 +1547,7 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE postID = postPtr.Id } else if postStr, ok := event.GetData()["post"].(string); ok && postStr != "" { var post model.Post - - _ = json.NewDecoder(strings.NewReader(postStr)).Decode(&post) + _ = json.Unmarshal([]byte(postStr), &post) postID = post.Id } else if id, ok := event.GetData()["post_id"].(string); ok { postID = id @@ -1565,7 +1564,7 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE channel = chPtr } else if chStr, ok := event.GetData()["channel"].(string); ok && chStr != "" { var summary ChannelSummary - _ = json.NewDecoder(strings.NewReader(chStr)).Decode(&summary) + _ = json.Unmarshal([]byte(chStr), &summary) channel = &model.Channel{ Id: summary.Id, UpdateAt: summary.UpdateAt, @@ -1695,7 +1694,7 @@ func (m *Client) syncJoinedChannelsCache(event *model.WebSocketEvent) { ID string `json:"id"` Type model.ChannelType `json:"type"` } - if err := json.NewDecoder(strings.NewReader(chStr)).Decode(&ch); err == nil { + if err := json.Unmarshal([]byte(chStr), &ch); err == nil { chID = ch.ID chType = ch.Type } diff --git a/messages.go b/messages.go index a6a154b..465044a 100644 --- a/messages.go +++ b/messages.go @@ -639,7 +639,7 @@ func (m *Client) parseActionPost(ctx context.Context, rmsg *Message) { } else if pStr, ok := rmsg.Raw.GetData()["post"].(string); ok && pStr != "" { postStr = pStr data = &model.Post{} - if err := json.NewDecoder(strings.NewReader(postStr)).Decode(data); err != nil { + if err := json.Unmarshal([]byte(postStr), data); err != nil { m.logger.Errorf("failed to unmarshal post: %v", err) return } diff --git a/users.go b/users.go index 2d91c1b..42e67ef 100644 --- a/users.go +++ b/users.go @@ -363,7 +363,7 @@ func (c *UsersCache) SetUserCustomStatus(userID string, rawJSON string) { var status CustomStatus - if err := json.NewDecoder(strings.NewReader(rawJSON)).Decode(&status); err != nil { + if err := json.Unmarshal([]byte(rawJSON), &status); err != nil { c.customStatuses[userID] = "" return } From 805af48ffb20ea58c1b65d35923020a76ef9d456 Mon Sep 17 00:00:00 2001 From: Haw Loeung Date: Wed, 19 Aug 2026 08:12:24 +1000 Subject: [PATCH 2/2] golangci-lint fixes --- matterclient.go | 8 +++++++- messages.go | 4 +++- users.go | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/matterclient.go b/matterclient.go index 3014f2d..5b01e10 100644 --- a/matterclient.go +++ b/matterclient.go @@ -1080,6 +1080,7 @@ func (m *Client) WsReceiver(ctx context.Context) { } } else if userStr, ok := data["user"].(string); ok && userStr != "" { var summary UserSummary + _ = json.Unmarshal([]byte(userStr), &summary) if summary.Username != "" { userInfo = " [User: " + summary.Username + " (ID: " + summary.Id + ")]" @@ -1394,6 +1395,7 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE u = userPtr } else if userStr, isStr := userVal.(string); isStr && userStr != "" { var summary UserSummary + _ = json.Unmarshal([]byte(userStr), &summary) // Map it back to the required model.User for the cache functions u = &model.User{ @@ -1547,6 +1549,7 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE postID = postPtr.Id } else if postStr, ok := event.GetData()["post"].(string); ok && postStr != "" { var post model.Post + _ = json.Unmarshal([]byte(postStr), &post) postID = post.Id } else if id, ok := event.GetData()["post_id"].(string); ok { @@ -1564,6 +1567,7 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE channel = chPtr } else if chStr, ok := event.GetData()["channel"].(string); ok && chStr != "" { var summary ChannelSummary + _ = json.Unmarshal([]byte(chStr), &summary) channel = &model.Channel{ Id: summary.Id, @@ -1694,7 +1698,9 @@ func (m *Client) syncJoinedChannelsCache(event *model.WebSocketEvent) { ID string `json:"id"` Type model.ChannelType `json:"type"` } - if err := json.Unmarshal([]byte(chStr), &ch); err == nil { + + err := json.Unmarshal([]byte(chStr), &ch) + if err == nil { chID = ch.ID chType = ch.Type } diff --git a/messages.go b/messages.go index 465044a..f0765d5 100644 --- a/messages.go +++ b/messages.go @@ -639,7 +639,9 @@ func (m *Client) parseActionPost(ctx context.Context, rmsg *Message) { } else if pStr, ok := rmsg.Raw.GetData()["post"].(string); ok && pStr != "" { postStr = pStr data = &model.Post{} - if err := json.Unmarshal([]byte(postStr), data); err != nil { + + err := json.Unmarshal([]byte(postStr), data) + if err != nil { m.logger.Errorf("failed to unmarshal post: %v", err) return } diff --git a/users.go b/users.go index 42e67ef..f863b48 100644 --- a/users.go +++ b/users.go @@ -363,7 +363,8 @@ func (c *UsersCache) SetUserCustomStatus(userID string, rawJSON string) { var status CustomStatus - if err := json.Unmarshal([]byte(rawJSON), &status); err != nil { + err := json.Unmarshal([]byte(rawJSON), &status) + if err != nil { c.customStatuses[userID] = "" return }