Files
go-fa-api/comments_post.go
2026-05-26 20:21:55 +02:00

63 lines
2.2 KiB
Go

package fa
import (
"context"
"fmt"
"net/url"
"git.anthrove.art/public/go-fa-api/internal/urls"
)
// PostCommentOptions configures a comment post. ParentID is the comment
// being replied to (0 for a top-level comment on the submission/journal).
type PostCommentOptions struct {
ParentID CommentID
}
// PostSubmissionComment posts a comment on a submission. Body is BBCode-
// formatted text (FA's comment markup). Returns nil on success; an
// [ErrUnauthorized] when called without auth cookies; an
// [*SystemMessageError] if FA refused the post (rate limiter, blocked
// user, etc).
//
// FA's comment form (`#add_comment_form`) posts to the submission's own
// URL with fields `action=reply`, `replyto=<parent>`, `reply=<body>`.
// There is no separate per-form CSRF key auth cookies + Cloudflare
// clearance are the only gates.
func (c *Client) PostSubmissionComment(ctx context.Context, id SubmissionID, body string, opts PostCommentOptions, reqOpts ...Option) error {
if id <= 0 {
return fmt.Errorf("fa: PostSubmissionComment: id must be > 0")
}
return c.postCommentForm(ctx, urls.Submission(int64(id)), body, opts, reqOpts)
}
// PostJournalComment posts a comment on a journal. Same form shape as
// submissions; the form action just points at /journal/{id}/.
func (c *Client) PostJournalComment(ctx context.Context, id JournalID, body string, opts PostCommentOptions, reqOpts ...Option) error {
if id <= 0 {
return fmt.Errorf("fa: PostJournalComment: id must be > 0")
}
return c.postCommentForm(ctx, urls.Journal(int64(id)), body, opts, reqOpts)
}
// postCommentForm builds the field set #add_comment_form sends. Shared
// between submission and journal comment posting because FA renders an
// identical form on both pages.
func (c *Client) postCommentForm(ctx context.Context, pageURL, body string, opts PostCommentOptions, reqOpts []Option) error {
if body == "" {
return fmt.Errorf("fa: PostComment: empty body")
}
v := url.Values{}
v.Set("f", "0")
v.Set("action", "reply")
if opts.ParentID > 0 {
v.Set("replyto", opts.ParentID.String())
} else {
v.Set("replyto", "")
}
v.Set("reply", body)
v.Set("submit", "Post Comment")
_, err := c.postForm(ctx, pageURL, v, reqOpts...)
return err
}