Skip to content

Commit

Permalink
feat: improve push impl of local service
Browse files Browse the repository at this point in the history
  • Loading branch information
theiskaa committed Dec 12, 2022
1 parent 95814b3 commit 032ed65
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions lib/services/local_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ func (l *LocalService) Fetch(remote ServiceRepo) ([]models.Node, []error) {
return nil, []error{err}
}

// Sort nodes via title-len decreasing order.
// Sort nodes via title-len ascending order.
sort.Slice(
nodes,
func(i, j int) bool { return len(nodes[i].Title) < len(nodes[j].Title) },
Expand Down Expand Up @@ -565,13 +565,12 @@ func (l *LocalService) Push(remote ServiceRepo) ([]models.Node, []error) {
return nil, []error{err}
}

// Sort nodes via title-len decreasing order.
sort.Slice(
nodes,
func(i, j int) bool { return len(nodes[i].Title) > len(nodes[j].Title) },
func(i, j int) bool { return len(nodes[i].Title) < len(nodes[j].Title) },
)

fetched := []models.Node{}
pushed := []models.Node{}
errors := []error{}

for _, node := range nodes {
Expand All @@ -580,25 +579,24 @@ func (l *LocalService) Push(remote ServiceRepo) ([]models.Node, []error) {
continue
}

if pkg.IsDir(path) {
exists, _ := remote.IsNodeExists(node)

if pkg.IsDir(path) && !exists {
if _, err := remote.Mkdir(node.ToFolder()); err != nil {
errors = append(errors, assets.CannotDoSth("push", node.Title, err))
} else {
fetched = append(fetched, node)
pushed = append(pushed, node)
}

continue
}

r, err := remote.View(node.ToNote())
if err != nil && err.Error() != assets.NotExists("", node.Title).Error() {
errors = append(errors, assets.CannotDoSth("push", node.Title, err))
continue
} else if err != nil {
r, _ := remote.View(node.ToNote())
if !exists {
if _, err := remote.Create(node.ToNote()); err != nil {
errors = append(errors, assets.CannotDoSth("push", node.Title, err))
} else {
fetched = append(fetched, node)
pushed = append(pushed, node)
}

continue
Expand All @@ -607,14 +605,13 @@ func (l *LocalService) Push(remote ServiceRepo) ([]models.Node, []error) {
if r.Body != node.Body {
if _, err := remote.Edit(node.ToNote()); err != nil {
errors = append(errors, assets.CannotDoSth("push", node.Title, err))
continue
} else {
pushed = append(pushed, node)
}

fetched = append(fetched, node)
}
}

return fetched, errors
return pushed, errors
}

// Migrate overwrites all notes of given [remote] service with [l](current-service).
Expand Down

0 comments on commit 032ed65

Please sign in to comment.