diff --git a/matterclient.go b/matterclient.go index 259b77d..5b01e10 100644 --- a/matterclient.go +++ b/matterclient.go @@ -1080,7 +1080,8 @@ 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 +1395,8 @@ 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 +1482,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 +1535,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) } @@ -1548,7 +1550,7 @@ func (m *Client) maintainUsersCache(ctx context.Context, event *model.WebSocketE } 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 +1567,8 @@ 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 +1698,9 @@ 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 { + + err := json.Unmarshal([]byte(chStr), &ch) + if err == nil { chID = ch.ID chType = ch.Type } diff --git a/messages.go b/messages.go index a6a154b..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.NewDecoder(strings.NewReader(postStr)).Decode(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 2d91c1b..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.NewDecoder(strings.NewReader(rawJSON)).Decode(&status); err != nil { + err := json.Unmarshal([]byte(rawJSON), &status) + if err != nil { c.customStatuses[userID] = "" return }