mirror of
https://github.com/writeas/go-writeas.git
synced 2025-07-27 17:18:46 +00:00
Support claiming anonymous posts
This commit is contained in:
parent
ba05f3ff99
commit
4846285bde
44
post.go
44
post.go
@ -28,11 +28,16 @@ type (
|
||||
Collection *Collection `json:"collection,omitempty"`
|
||||
}
|
||||
|
||||
// OwnedPostParams are, together, fields only the original post author knows.
|
||||
OwnedPostParams struct {
|
||||
ID string `json:"-"`
|
||||
Token string `json:"token,omitempty"`
|
||||
}
|
||||
|
||||
// PostParams holds values for creating or updating a post.
|
||||
PostParams struct {
|
||||
// Parameters only for updating
|
||||
ID string `json:"-"`
|
||||
Token string `json:"token,omitempty"`
|
||||
OwnedPostParams
|
||||
|
||||
// Parameters for creating or updating
|
||||
Title string `json:"title,omitempty"`
|
||||
@ -43,6 +48,13 @@ type (
|
||||
|
||||
Crosspost []map[string]string `json:"crosspost,omitempty"`
|
||||
}
|
||||
|
||||
ClaimPostResult struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Code int `json:"code,omitempty"`
|
||||
ErrorMessage string `json:"error_msg,omitempty"`
|
||||
Post *Post `json:"post,omitempty"`
|
||||
}
|
||||
)
|
||||
|
||||
// GetPost retrieves a published post, returning the Post and any error (in
|
||||
@ -145,3 +157,31 @@ func (c *Client) DeletePost(sp *PostParams) error {
|
||||
}
|
||||
return fmt.Errorf("Problem getting post: %s. %v\n", status, err)
|
||||
}
|
||||
|
||||
// ClaimPosts associates anonymous posts with a user / account.
|
||||
// https://writeas.github.io/docs/#claim-posts.
|
||||
func (c *Client) ClaimPosts(sp *[]OwnedPostParams) (*[]ClaimPostResult, error) {
|
||||
p := &[]ClaimPostResult{}
|
||||
env, err := c.put("/posts/claim", sp, p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ok bool
|
||||
if p, ok = env.Data.(*[]ClaimPostResult); !ok {
|
||||
return nil, fmt.Errorf("Wrong data returned from API.")
|
||||
}
|
||||
|
||||
status := env.Code
|
||||
if status == http.StatusOK {
|
||||
return p, nil
|
||||
} else if c.isNotLoggedIn(status) {
|
||||
return nil, fmt.Errorf("Not authenticated.")
|
||||
} else if status == http.StatusBadRequest {
|
||||
return nil, fmt.Errorf("Bad request: %s", env.ErrorMessage)
|
||||
} else {
|
||||
return nil, fmt.Errorf("Problem getting post: %s. %v\n", status, err)
|
||||
}
|
||||
// TODO: does this also happen with moving posts?
|
||||
return p, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user