feat(sync): selective push, remote deletion detection, and gcal fix
Problem: `push_pass` updated all remote-linked tasks unconditionally, causing unnecessary API calls and potential clobbering of remote edits made between syncs. `pull`/`sync` never noticed when a task disappeared from remote. `update_event` omitted `transparency` that `create_event` set. Failure counts were absent from sync log summaries. Solution: Introduce `_gtasks_synced_at` in `_extra` — stamped after every successful push/pull create or update — so `push_pass` skips tasks unchanged since last sync. Add `detect_remote_deletions` to unlink local tasks whose remote entry disappeared from a successfully fetched list. Surface failures as `!N` in all sync logs and `unlinked: N` for pull/sync. Add `transparency = 'transparent'` to `update_event`. Cover new behaviour with 7 tests in `gtasks_spec.lua`.
This commit is contained in:
parent
e1923ff2e4
commit
3795999f3d
3 changed files with 280 additions and 17 deletions
|
|
@ -97,6 +97,7 @@ local function update_event(access_token, calendar_id, event_id, task)
|
|||
summary = task.description,
|
||||
start = { date = task.due },
|
||||
['end'] = { date = next_day(task.due or '') },
|
||||
transparency = 'transparent',
|
||||
}
|
||||
local _, err = oauth.curl_request(
|
||||
'PATCH',
|
||||
|
|
|
|||
|
|
@ -231,8 +231,9 @@ end
|
|||
---@return integer created
|
||||
---@return integer updated
|
||||
---@return integer deleted
|
||||
---@return integer failed
|
||||
local function push_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
local created, updated, deleted = 0, 0, 0
|
||||
local created, updated, deleted, failed = 0, 0, 0, 0
|
||||
for _, task in ipairs(s:tasks()) do
|
||||
local extra = task._extra or {}
|
||||
local gtid = extra['_gtasks_task_id'] --[[@as string?]]
|
||||
|
|
@ -242,12 +243,14 @@ local function push_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
|||
local err = delete_gtask(access_token, list_id, gtid)
|
||||
if err then
|
||||
log.warn('gtasks delete failed: ' .. err)
|
||||
failed = failed + 1
|
||||
else
|
||||
if not task._extra then
|
||||
task._extra = {}
|
||||
end
|
||||
task._extra['_gtasks_task_id'] = nil
|
||||
task._extra['_gtasks_list_id'] = nil
|
||||
task._extra['_gtasks_synced_at'] = nil
|
||||
if next(task._extra) == nil then
|
||||
task._extra = nil
|
||||
end
|
||||
|
|
@ -256,11 +259,17 @@ local function push_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
|||
end
|
||||
elseif task.status ~= 'deleted' then
|
||||
if gtid and list_id then
|
||||
local err = update_gtask(access_token, list_id, gtid, task_to_gtask(task))
|
||||
if err then
|
||||
log.warn('gtasks update failed: ' .. err)
|
||||
else
|
||||
updated = updated + 1
|
||||
local synced_at = extra['_gtasks_synced_at'] --[[@as string?]]
|
||||
if not synced_at or task.modified > synced_at then
|
||||
local err = update_gtask(access_token, list_id, gtid, task_to_gtask(task))
|
||||
if err then
|
||||
log.warn('gtasks update failed: ' .. err)
|
||||
failed = failed + 1
|
||||
else
|
||||
task._extra = task._extra or {}
|
||||
task._extra['_gtasks_synced_at'] = now_ts
|
||||
updated = updated + 1
|
||||
end
|
||||
end
|
||||
elseif task.status == 'pending' then
|
||||
local cat = task.category or config.get().default_category
|
||||
|
|
@ -269,12 +278,14 @@ local function push_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
|||
local new_id, create_err = create_gtask(access_token, lid, task_to_gtask(task))
|
||||
if create_err then
|
||||
log.warn('gtasks create failed: ' .. create_err)
|
||||
failed = failed + 1
|
||||
elseif new_id then
|
||||
if not task._extra then
|
||||
task._extra = {}
|
||||
end
|
||||
task._extra['_gtasks_task_id'] = new_id
|
||||
task._extra['_gtasks_list_id'] = lid
|
||||
task._extra['_gtasks_synced_at'] = now_ts
|
||||
task.modified = now_ts
|
||||
by_gtasks_id[new_id] = task
|
||||
created = created + 1
|
||||
|
|
@ -283,7 +294,7 @@ local function push_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
|||
end
|
||||
end
|
||||
end
|
||||
return created, updated, deleted
|
||||
return created, updated, deleted, failed
|
||||
end
|
||||
|
||||
---@param access_token string
|
||||
|
|
@ -293,14 +304,24 @@ end
|
|||
---@param by_gtasks_id table<string, pending.Task>
|
||||
---@return integer created
|
||||
---@return integer updated
|
||||
---@return integer failed
|
||||
---@return table<string, true> seen_remote_ids
|
||||
---@return table<string, true> fetched_list_ids
|
||||
local function pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
local created, updated = 0, 0
|
||||
local created, updated, failed = 0, 0, 0
|
||||
---@type table<string, true>
|
||||
local seen_remote_ids = {}
|
||||
---@type table<string, true>
|
||||
local fetched_list_ids = {}
|
||||
for list_name, list_id in pairs(tasklists) do
|
||||
local items, err = list_gtasks(access_token, list_id)
|
||||
if err then
|
||||
log.warn('error fetching list ' .. list_name .. ': ' .. err)
|
||||
failed = failed + 1
|
||||
else
|
||||
fetched_list_ids[list_id] = true
|
||||
for _, gtask in ipairs(items or {}) do
|
||||
seen_remote_ids[gtask.id] = true
|
||||
local local_task = by_gtasks_id[gtask.id]
|
||||
if local_task then
|
||||
local gtask_updated = gtask.updated or ''
|
||||
|
|
@ -310,6 +331,8 @@ local function pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
|||
for k, v in pairs(fields) do
|
||||
local_task[k] = v
|
||||
end
|
||||
local_task._extra = local_task._extra or {}
|
||||
local_task._extra['_gtasks_synced_at'] = now_ts
|
||||
local_task.modified = now_ts
|
||||
updated = updated + 1
|
||||
end
|
||||
|
|
@ -318,6 +341,7 @@ local function pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
|||
fields._extra = {
|
||||
_gtasks_task_id = gtask.id,
|
||||
_gtasks_list_id = list_id,
|
||||
_gtasks_synced_at = now_ts,
|
||||
}
|
||||
local new_task = s:add(fields)
|
||||
by_gtasks_id[gtask.id] = new_task
|
||||
|
|
@ -326,7 +350,34 @@ local function pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
|||
end
|
||||
end
|
||||
end
|
||||
return created, updated
|
||||
return created, updated, failed, seen_remote_ids, fetched_list_ids
|
||||
end
|
||||
|
||||
---@param s pending.Store
|
||||
---@param seen_remote_ids table<string, true>
|
||||
---@param fetched_list_ids table<string, true>
|
||||
---@param now_ts string
|
||||
---@return integer unlinked
|
||||
local function detect_remote_deletions(s, seen_remote_ids, fetched_list_ids, now_ts)
|
||||
local unlinked = 0
|
||||
for _, task in ipairs(s:tasks()) do
|
||||
local extra = task._extra or {}
|
||||
local gtid = extra['_gtasks_task_id']
|
||||
local list_id = extra['_gtasks_list_id']
|
||||
if task.status ~= 'deleted'
|
||||
and gtid and list_id
|
||||
and fetched_list_ids[list_id]
|
||||
and not seen_remote_ids[gtid]
|
||||
then
|
||||
task._extra['_gtasks_task_id'] = nil
|
||||
task._extra['_gtasks_list_id'] = nil
|
||||
task._extra['_gtasks_synced_at'] = nil
|
||||
if next(task._extra) == nil then task._extra = nil end
|
||||
task.modified = now_ts
|
||||
unlinked = unlinked + 1
|
||||
end
|
||||
end
|
||||
return unlinked
|
||||
end
|
||||
|
||||
---@param access_token string
|
||||
|
|
@ -374,14 +425,17 @@ function M.push()
|
|||
---@cast s pending.Store
|
||||
---@cast now_ts string
|
||||
local by_gtasks_id = build_id_index(s)
|
||||
local created, updated, deleted = push_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
local created, updated, deleted, failed =
|
||||
push_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
s:save()
|
||||
require('pending')._recompute_counts()
|
||||
local buffer = require('pending.buffer')
|
||||
if buffer.bufnr() and vim.api.nvim_buf_is_valid(buffer.bufnr()) then
|
||||
buffer.render(buffer.bufnr())
|
||||
end
|
||||
log.info(string.format('Google Tasks pushed — +%d ~%d -%d', created, updated, deleted))
|
||||
log.info(
|
||||
string.format('Google Tasks pushed — +%d ~%d -%d !%d', created, updated, deleted, failed)
|
||||
)
|
||||
end)
|
||||
end
|
||||
|
||||
|
|
@ -394,14 +448,24 @@ function M.pull()
|
|||
---@cast s pending.Store
|
||||
---@cast now_ts string
|
||||
local by_gtasks_id = build_id_index(s)
|
||||
local created, updated = pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
local created, updated, failed, seen_remote_ids, fetched_list_ids =
|
||||
pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
local unlinked = detect_remote_deletions(s, seen_remote_ids, fetched_list_ids, now_ts)
|
||||
s:save()
|
||||
require('pending')._recompute_counts()
|
||||
local buffer = require('pending.buffer')
|
||||
if buffer.bufnr() and vim.api.nvim_buf_is_valid(buffer.bufnr()) then
|
||||
buffer.render(buffer.bufnr())
|
||||
end
|
||||
log.info(string.format('Google Tasks pulled — +%d ~%d', created, updated))
|
||||
log.info(
|
||||
string.format(
|
||||
'Google Tasks pulled — +%d ~%d !%d, unlinked: %d',
|
||||
created,
|
||||
updated,
|
||||
failed,
|
||||
unlinked
|
||||
)
|
||||
)
|
||||
end)
|
||||
end
|
||||
|
||||
|
|
@ -414,9 +478,11 @@ function M.sync()
|
|||
---@cast s pending.Store
|
||||
---@cast now_ts string
|
||||
local by_gtasks_id = build_id_index(s)
|
||||
local pushed_create, pushed_update, pushed_delete =
|
||||
local pushed_create, pushed_update, pushed_delete, pushed_failed =
|
||||
push_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
local pulled_create, pulled_update = pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
local pulled_create, pulled_update, pulled_failed, seen_remote_ids, fetched_list_ids =
|
||||
pull_pass(access_token, tasklists, s, now_ts, by_gtasks_id)
|
||||
local unlinked = detect_remote_deletions(s, seen_remote_ids, fetched_list_ids, now_ts)
|
||||
s:save()
|
||||
require('pending')._recompute_counts()
|
||||
local buffer = require('pending.buffer')
|
||||
|
|
@ -425,12 +491,15 @@ function M.sync()
|
|||
end
|
||||
log.info(
|
||||
string.format(
|
||||
'Google Tasks synced — push: +%d ~%d -%d, pull: +%d ~%d',
|
||||
'Google Tasks synced — push: +%d ~%d -%d !%d, pull: +%d ~%d !%d, unlinked: %d',
|
||||
pushed_create,
|
||||
pushed_update,
|
||||
pushed_delete,
|
||||
pushed_failed,
|
||||
pulled_create,
|
||||
pulled_update
|
||||
pulled_update,
|
||||
pulled_failed,
|
||||
unlinked
|
||||
)
|
||||
)
|
||||
end)
|
||||
|
|
@ -442,6 +511,9 @@ M._build_notes = build_notes
|
|||
M._parse_notes = parse_notes
|
||||
M._task_to_gtask = task_to_gtask
|
||||
M._gtask_to_fields = gtask_to_fields
|
||||
M._push_pass = push_pass
|
||||
M._pull_pass = pull_pass
|
||||
M._detect_remote_deletions = detect_remote_deletions
|
||||
|
||||
---@return nil
|
||||
function M.health()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue