inital commit
This commit is contained in:
62
comments_post.go
Normal file
62
comments_post.go
Normal file
@@ -0,0 +1,62 @@
|
||||
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) error {
|
||||
if id <= 0 {
|
||||
return fmt.Errorf("fa: PostSubmissionComment: id must be > 0")
|
||||
}
|
||||
return c.postCommentForm(ctx, urls.Submission(int64(id)), body, opts)
|
||||
}
|
||||
|
||||
// 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) error {
|
||||
if id <= 0 {
|
||||
return fmt.Errorf("fa: PostJournalComment: id must be > 0")
|
||||
}
|
||||
return c.postCommentForm(ctx, urls.Journal(int64(id)), body, opts)
|
||||
}
|
||||
|
||||
// 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) 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)
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user