Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

claim posts #34

Merged
merged 4 commits into from Jun 11, 2019
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Next

add claim posts to writeas CLI

this adds the ability to claim local posts under an authenticated
account.

removePost is now exported for use the claim command
  • Loading branch information...
robjloranger committed Jun 10, 2019
commit c371da1e5ee40562217445ce3cf5a143d968bcb4
@@ -219,7 +219,7 @@ func DoDelete(c *cli.Context, friendlyID, token string, tor bool) error {
} else {
log.Info(c, "Post deleted.")
}
removePost(c.App.ExtraInfo()["configDir"], friendlyID)
RemovePost(c.App.ExtraInfo()["configDir"], friendlyID)

return nil
}
@@ -57,6 +57,24 @@ func AddPost(c *cli.Context, id, token string) error {
return nil
}

// ClaimPost adds a local post to the authenticated user's account and deletes
// the local reference
func ClaimPosts(c *cli.Context, localPosts *[]Post) (*[]writeas.ClaimPostResult, error) {
cl, err := NewClient(c, true)
if err != nil {
return nil, err
}
postsToClaim := make([]writeas.OwnedPostParams, len(*localPosts))
for i, post := range *localPosts {
postsToClaim[i] = writeas.OwnedPostParams{
ID: post.ID,
Token: post.EditToken,
}
}

return cl.ClaimPosts(&postsToClaim)
}

func TokenFromID(c *cli.Context, id string) string {
post := fileutils.FindLine(filepath.Join(config.UserDataDir(c.App.ExtraInfo()["configDir"]), postsFile), id)
if post == "" {
@@ -71,7 +89,7 @@ func TokenFromID(c *cli.Context, id string) string {
return parts[1]
}

func removePost(path, id string) {
func RemovePost(path, id string) {
fileutils.RemoveLine(filepath.Join(config.UserDataDir(path), postsFile), id)
}

@@ -179,6 +179,11 @@ func main() {
Usage: "Show list with URLs",
},
},
}, {
Name: "claim",
Usage: "Claim local unsynced posts",
Action: commands.CmdClaim,
Description: "This will claim any unsynced posts local to this machine. To see which posts these are run: writeas posts.",
},
{
Name: "auth",
@@ -279,6 +279,41 @@ func CmdCollections(c *cli.Context) error {
return nil
}

func CmdClaim(c *cli.Context) error {
u, err := config.LoadUser(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
if err != nil {
return cli.NewExitError(fmt.Sprintf("couldn't load config: %v", err), 1)
}
if u == nil {
return cli.NewExitError("You must be authenticated to claim local posts.\nLog in first with: writeas auth <username>", 1)
}

localPosts := api.GetPosts(c)
if len(*localPosts) == 0 {
return nil
}

results, err := api.ClaimPosts(c, localPosts)
if err != nil {
return cli.NewExitError(fmt.Sprintf("Failed to claim posts: %v", err), 1)
}

for _, r := range *results {
fmt.Printf("Adding %s to user %s..", r.Post.ID, u.User.Username)
if r.ErrorMessage != "" {
fmt.Printf(" Failed\n")
if config.Debug() {
log.Errorln("Failed claiming post %s: %v", r.ID, r.ErrorMessage)
}
} else {
fmt.Printf(" OK\n")
// only delete local if successful
api.RemovePost(c.App.ExtraInfo()["configDir"], r.Post.ID)
}
}
return nil
}

func CmdAuth(c *cli.Context) error {
// Check configuration
u, err := config.LoadUser(config.UserDataDir(c.App.ExtraInfo()["configDir"]))
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.