gte/cmd/android-app/component/tasks.go

90 lines
1.9 KiB
Go
Raw Normal View History

2022-10-18 16:58:12 +02:00
package component
import (
"fmt"
2022-10-18 16:58:12 +02:00
"sort"
2022-10-20 10:40:46 +02:00
"time"
2022-10-18 16:58:12 +02:00
"ewintr.nl/gte/internal/process"
"ewintr.nl/gte/internal/storage"
"ewintr.nl/gte/internal/task"
"ewintr.nl/gte/pkg/msend"
"ewintr.nl/gte/pkg/mstore"
)
type Tasks struct {
local storage.LocalRepository
remote *storage.RemoteRepository
disp *storage.Dispatcher
}
func NewTasks(conf *Configuration) (*Tasks, error) {
local := storage.NewMemory()
remote := storage.NewRemoteRepository(mstore.NewIMAP(conf.IMAP()))
disp := storage.NewDispatcher(msend.NewSSLSMTP(conf.SMTP()))
return &Tasks{
local: local,
remote: remote,
disp: disp,
}, nil
}
2022-10-20 10:40:46 +02:00
func (t *Tasks) Today() (map[string]string, error) {
2022-10-18 16:58:12 +02:00
reqs := process.ListReqs{
Due: task.Today(),
IncludeBefore: true,
ApplyUpdates: true,
}
res, err := process.NewList(t.local, reqs).Process()
if err != nil {
2022-10-20 10:40:46 +02:00
return map[string]string{}, err
2022-10-18 16:58:12 +02:00
}
sort.Sort(task.ByDefault(res.Tasks))
2022-10-20 10:40:46 +02:00
tasks := map[string]string{}
2022-10-18 16:58:12 +02:00
for _, t := range res.Tasks {
2022-10-20 10:40:46 +02:00
tasks[t.Id] = t.Action
2022-10-18 16:58:12 +02:00
}
return tasks, nil
}
func (t *Tasks) Sync(logger *Logger) (int, int, error) {
2022-10-20 10:40:46 +02:00
countDisp, err := process.NewSend(t.local, t.disp).Process()
if err != nil {
return 0, 0, err
}
logger.Log("finished dispatch")
2022-10-20 10:40:46 +02:00
latestFetch, err := t.local.LatestSync()
if err != nil {
return 0, 0, err
}
logger.Log(fmt.Sprintf("latest fetch time, was at %s", latestFetch.Format(time.Stamp)))
2022-10-20 10:40:46 +02:00
if time.Now().Before(latestFetch.Add(15 * time.Minute)) {
return countDisp, 0, nil
}
2022-10-18 16:58:12 +02:00
res, err := process.NewFetch(t.remote, t.local).Process()
if err != nil {
2022-10-20 10:40:46 +02:00
return countDisp, 0, err
2022-10-18 16:58:12 +02:00
}
logger.Log("fininshed actual fetch")
2022-10-20 10:40:46 +02:00
return countDisp, res.Count, nil
}
func (t *Tasks) MarkDone(id string) error {
localTask, err := t.local.FindById(id)
if err != nil {
return err
}
update := &task.LocalUpdate{
ForVersion: localTask.Version,
Fields: []string{task.FIELD_DONE},
Done: true,
}
return process.NewUpdate(t.local, id, update).Process()
2022-10-18 16:58:12 +02:00
}