Files
pcli/client/comments.go
T
Steve Cliff b07572fed5 Released v1
2026-02-12 10:37:19 +00:00

115 lines
2.5 KiB
Go

package client
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"git.franklin.lab/steve.cliff/pcli/model"
)
func (c *Client) ListComments(ctx context.Context, cardId string, limit int) ([]model.Comment, error) {
var all []model.Comment
var beforeId string
for {
path := fmt.Sprintf("/api/cards/%s/comments", cardId)
if beforeId != "" {
path = fmt.Sprintf("%s?beforeId=%s", path, beforeId)
}
fallback := fmt.Sprintf("/api/cards/%s/comment-actions", cardId)
if beforeId != "" {
fallback = fmt.Sprintf("%s?beforeId=%s", fallback, beforeId)
}
c.Logger.Debug("Fetching comments page",
slog.String("cardId", cardId),
slog.String("beforeId", beforeId),
)
data, err := c.DoNoBodyWithFallback(ctx, "GET", path, fallback)
if err != nil {
return nil, err
}
var response struct {
Items []model.Comment `json:"items"`
}
if err := json.Unmarshal(data, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal comments response: %w", err)
}
if len(response.Items) == 0 {
break
}
all = append(all, response.Items...)
if limit > 0 && len(all) >= limit {
all = all[:limit]
break
}
beforeId = response.Items[len(response.Items)-1].ID
}
return all, nil
}
func (c *Client) CreateComment(ctx context.Context, cardId, text string) (*model.Comment, error) {
fields := map[string]any{
"text": text,
}
data, err := c.DoWithFallback(ctx, "POST",
fmt.Sprintf("/api/cards/%s/comments", cardId),
fmt.Sprintf("/api/cards/%s/comment-actions", cardId),
fields)
if err != nil {
return nil, err
}
var response struct {
Item model.Comment `json:"item"`
}
if err := json.Unmarshal(data, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal comment response: %w", err)
}
return &response.Item, nil
}
func (c *Client) UpdateComment(ctx context.Context, id, text string) (*model.Comment, error) {
fields := map[string]any{
"text": text,
}
data, err := c.DoWithFallback(ctx, "PATCH",
fmt.Sprintf("/api/comments/%s", id),
fmt.Sprintf("/api/comment-actions/%s", id),
fields)
if err != nil {
return nil, err
}
var response struct {
Item model.Comment `json:"item"`
}
if err := json.Unmarshal(data, &response); err != nil {
return nil, fmt.Errorf("failed to unmarshal comment response: %w", err)
}
return &response.Item, nil
}
func (c *Client) DeleteComment(ctx context.Context, id string) error {
_, err := c.DoNoBodyWithFallback(ctx, "DELETE",
fmt.Sprintf("/api/comments/%s", id),
fmt.Sprintf("/api/comment-actions/%s", id))
return err
}