planner/plan/command/sync.go

123 lines
3.1 KiB
Go
Raw Normal View History

2024-10-07 11:11:18 +02:00
package command
import (
"encoding/json"
"errors"
"fmt"
2025-01-05 14:37:46 +01:00
"time"
2024-10-07 11:11:18 +02:00
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/storage"
2025-01-13 09:13:48 +01:00
"go-mod.ewintr.nl/planner/sync/client"
2024-10-07 11:11:18 +02:00
)
2024-12-27 11:20:32 +01:00
type SyncArgs struct{}
2024-10-07 11:11:18 +02:00
2024-12-27 11:20:32 +01:00
func NewSyncArgs() SyncArgs {
return SyncArgs{}
2024-10-07 11:11:18 +02:00
}
2024-12-27 11:20:32 +01:00
func (sa SyncArgs) Parse(main []string, flags map[string]string) (Command, error) {
2024-10-29 07:22:04 +01:00
if len(main) == 0 || main[0] != "sync" {
2024-12-27 11:20:32 +01:00
return nil, ErrWrongCommand
2024-10-29 07:22:04 +01:00
}
2024-12-27 11:20:32 +01:00
return &Sync{}, nil
2024-10-29 07:22:04 +01:00
}
2024-12-27 11:20:32 +01:00
type Sync struct{}
2025-01-13 09:13:48 +01:00
func (s Sync) Do(repos Repositories, client client.Client) (CommandResult, error) {
tx, err := repos.Begin()
if err != nil {
return nil, fmt.Errorf("could not start transaction: %v", err)
}
defer tx.Rollback()
2024-10-07 11:11:18 +02:00
// local new and updated
2025-01-13 09:13:48 +01:00
sendItems, err := repos.Sync(tx).FindAll()
2024-10-07 11:11:18 +02:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not get updated items: %v", err)
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
if err := client.Update(sendItems); err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not send updated items: %v", err)
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
if err := repos.Sync(tx).DeleteAll(); err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not clear updated items: %v", err)
2024-10-07 11:11:18 +02:00
}
// get new/updated items
2025-01-13 09:13:48 +01:00
oldTS, err := repos.Sync(tx).LastUpdate()
2024-10-07 11:11:18 +02:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not find timestamp of last update: %v", err)
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
recItems, err := client.Updated([]item.Kind{item.KindTask}, oldTS)
2024-10-07 11:11:18 +02:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not receive updates: %v", err)
2024-10-07 11:11:18 +02:00
}
updated := make([]item.Item, 0)
2025-01-05 14:37:46 +01:00
var newTS time.Time
2024-10-07 11:11:18 +02:00
for _, ri := range recItems {
2025-01-05 14:37:46 +01:00
if ri.Updated.After(newTS) {
newTS = ri.Updated
}
2024-10-07 11:11:18 +02:00
if ri.Deleted {
2025-01-13 09:13:48 +01:00
if err := repos.LocalID(tx).Delete(ri.ID); err != nil && !errors.Is(err, storage.ErrNotFound) {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not delete local id: %v", err)
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
if err := repos.Task(tx).Delete(ri.ID); err != nil && !errors.Is(err, storage.ErrNotFound) {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not delete task: %v", err)
2024-10-07 11:11:18 +02:00
}
continue
}
updated = append(updated, ri)
}
2025-01-13 09:13:48 +01:00
lidMap, err := repos.LocalID(tx).FindAll()
2024-10-07 11:11:18 +02:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not get local ids: %v", err)
2024-10-07 11:11:18 +02:00
}
for _, u := range updated {
2024-12-24 08:00:23 +01:00
var tskBody item.TaskBody
if err := json.Unmarshal([]byte(u.Body), &tskBody); err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not unmarshal task body: %v", err)
2024-10-07 11:11:18 +02:00
}
2024-12-24 08:00:23 +01:00
tsk := item.Task{
2024-12-27 09:15:05 +01:00
ID: u.ID,
Date: u.Date,
Recurrer: u.Recurrer,
RecurNext: u.RecurNext,
TaskBody: tskBody,
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
if err := repos.Task(tx).Store(tsk); err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not store task: %v", err)
2024-10-07 11:11:18 +02:00
}
lid, ok := lidMap[u.ID]
if !ok {
2025-01-13 09:13:48 +01:00
lid, err = repos.LocalID(tx).Next()
2024-10-07 11:11:18 +02:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not get next local id: %v", err)
2024-10-07 11:11:18 +02:00
}
2025-01-13 09:13:48 +01:00
if err := repos.LocalID(tx).Store(u.ID, lid); err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not store local id: %v", err)
2024-10-07 11:11:18 +02:00
}
}
}
2025-01-13 09:13:48 +01:00
if err := repos.Sync(tx).SetLastUpdate(newTS); err != nil {
2025-01-05 14:37:46 +01:00
return nil, fmt.Errorf("could not store update timestamp: %v", err)
}
2025-01-13 09:13:48 +01:00
if err := tx.Commit(); err != nil {
return nil, fmt.Errorf("could not sync items: %v", err)
}
2024-12-29 09:32:49 +01:00
return SyncResult{}, nil
2024-10-07 11:11:18 +02:00
}
2024-12-29 09:32:49 +01:00
type SyncResult struct{}
2024-12-30 10:14:04 +01:00
func (sr SyncResult) Render() string { return "tasks synced" }