2020-07-11 13:49:07 -04:00
|
|
|
package reddit
|
2020-04-29 15:59:18 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-11 14:11:41 -04:00
|
|
|
"net/http"
|
2020-04-29 15:59:18 -04:00
|
|
|
"net/url"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CommentService handles communication with the comment
|
2020-06-22 21:52:34 -04:00
|
|
|
// related methods of the Reddit API.
|
2020-07-13 23:05:24 -04:00
|
|
|
//
|
|
|
|
// Reddit API docs: https://www.reddit.com/dev/api/#section_links_and_comments
|
2020-07-21 23:05:24 -04:00
|
|
|
type CommentService struct {
|
|
|
|
*PostAndCommentService
|
|
|
|
client *Client
|
|
|
|
}
|
2020-04-29 15:59:18 -04:00
|
|
|
|
2020-07-13 23:05:24 -04:00
|
|
|
// Submit submits a comment as a reply to a post, comment, or message.
|
|
|
|
// parentID is the full ID of the thing being replied to.
|
|
|
|
func (s *CommentService) Submit(ctx context.Context, parentID string, text string) (*Comment, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
path := "api/comment"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("return_rtjson", "true")
|
2020-07-13 23:05:24 -04:00
|
|
|
form.Set("parent", parentID)
|
2020-04-29 15:59:18 -04:00
|
|
|
form.Set("text", text)
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-04-29 15:59:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(Comment)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-07-13 23:05:24 -04:00
|
|
|
// Edit edits a comment.
|
2020-06-27 23:53:59 -04:00
|
|
|
func (s *CommentService) Edit(ctx context.Context, id string, text string) (*Comment, *Response, error) {
|
2020-04-29 15:59:18 -04:00
|
|
|
path := "api/editusertext"
|
|
|
|
|
|
|
|
form := url.Values{}
|
|
|
|
form.Set("api_type", "json")
|
|
|
|
form.Set("return_rtjson", "true")
|
|
|
|
form.Set("thing_id", id)
|
|
|
|
form.Set("text", text)
|
|
|
|
|
2020-07-11 14:11:41 -04:00
|
|
|
req, err := s.client.NewRequestWithForm(http.MethodPost, path, form)
|
2020-04-29 15:59:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
root := new(Comment)
|
|
|
|
resp, err := s.client.Do(ctx, req, root)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return root, resp, nil
|
|
|
|
}
|