Client UUID nodes from server sync count

This commit is contained in:
Magnus Åhall 2025-01-12 20:57:25 +01:00
parent dc010df448
commit f33e5d54af
2 changed files with 61 additions and 3 deletions

30
main.go
View file

@ -124,6 +124,7 @@ func main() { // {{{
http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)
http.HandleFunc("/sync/from_server/count", authenticated(actionSyncFromServerCount))
http.HandleFunc("/sync/from_server/{sequence}/{offset}", authenticated(actionSyncFromServer))
http.HandleFunc("/sync/to_server", authenticated(actionSyncToServer))
@ -252,12 +253,12 @@ func actionSyncFromServer(w http.ResponseWriter, r *http.Request) { // {{{
nodes, maxSeq, moreRowsExist, err := Nodes(user.UserID, offset, uint64(changedFrom), user.ClientUUID)
if err != nil {
Log.Error("/node/tree", "error", err)
Log.Error("/sync/from_server", "error", err)
httpError(w, err)
return
}
Log.Debug("/node/tree", "num_nodes", len(nodes), "maxSeq", maxSeq)
Log.Debug("/sync/from_server", "num_nodes", len(nodes), "maxSeq", maxSeq)
foo, _ := json.Marshal(nodes)
os.WriteFile(fmt.Sprintf("/tmp/nodes-%d.json", offset), foo, 0644)
@ -269,6 +270,29 @@ func actionSyncFromServer(w http.ResponseWriter, r *http.Request) { // {{{
}{true, nodes, maxSeq, moreRowsExist})
w.Write(j)
} // }}}
func actionSyncFromServerCount(w http.ResponseWriter, r *http.Request) { // {{{
// The purpose of the Client UUID is to avoid
// sending nodes back once again to a client that
// just created or modified it.
user := getUser(r)
changedFrom, _ := strconv.Atoi(r.PathValue("sequence"))
count, err := NodesCount(user.UserID, uint64(changedFrom), user.ClientUUID)
if err != nil {
Log.Error("/sync/from_server/count", "error", err)
httpError(w, err)
return
}
j, _ := json.Marshal(struct {
OK bool
Count int
}{
true,
count,
})
w.Write(j)
} // }}}
func actionNodeRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
user := getUser(r)
var err error
@ -290,7 +314,7 @@ func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
body, _ := io.ReadAll(r.Body)
var request struct {
NodeData string
NodeData string
}
err := json.Unmarshal(body, &request)
if err != nil {