77 lines
1.9 KiB
Julia
77 lines
1.9 KiB
Julia
function sync(u::MatrixUser;
|
|
timeout = 0, since::Union{String,Nothing} = nothing, full = false,
|
|
presence = "offline")
|
|
req = Dict("timeout" => string(timeout),
|
|
"set_presence" => presence,
|
|
"full" => string(full))
|
|
since === nothing ? nothing : (req["since"] = string(since))
|
|
get(u, "sync"; extraquery = req)
|
|
end
|
|
|
|
function sync!(s::MatrixSync)
|
|
# initial sync
|
|
res = s.last in [nothing, ""] ?
|
|
sync(s.user, timeout = s.timeout, full = true, presence = s.presence) :
|
|
# updates
|
|
sync(s.user, since = s.last, timeout = s.timeout, full = false, presence = s.presence)
|
|
res = JSON.parse(String(res.body))
|
|
s.last = res["next_batch"]
|
|
s.sync = res
|
|
res
|
|
end
|
|
|
|
sync_enabled = false
|
|
hooks = Dict{Symbol,Function}()
|
|
function background_sync(s::MatrixSync)
|
|
global sync_enabled, hooks
|
|
sync_enabled = true
|
|
@async while sync_enabled
|
|
res = try
|
|
sync!(s)
|
|
catch e
|
|
@warn "Error $e during sync!"
|
|
continue
|
|
end
|
|
if :sync in keys(hooks)
|
|
try
|
|
hooks[:sync](res)
|
|
catch
|
|
@warn "Error running hook"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function stop_sync!()
|
|
global sync_enabled
|
|
oldstatus = sync_enabled
|
|
sync_enabled = false
|
|
oldstatus
|
|
end
|
|
|
|
function set_hook!(event::Symbol, f::Function)
|
|
global hooks
|
|
hooks[event] = f
|
|
end
|
|
|
|
function remove_hook!(event::Symbol)
|
|
global hooks
|
|
if event in keys(hooks)
|
|
pop!(hooks, event)
|
|
return true
|
|
end
|
|
false
|
|
end
|
|
|
|
function updateroomlastbatch(fullsync::Dict{String,Any})
|
|
global rooms_last_batch
|
|
map(x ->
|
|
rooms_last_batch[x] = fullsync["rooms"]["join"][x]["timeline"]["prev_batch"],
|
|
collect(keys(fullsync["rooms"]["join"])))
|
|
rooms_last_batch
|
|
end
|
|
|
|
function updateroomlastbatch(user::MatrixUser)
|
|
updateroomlastbatch(sync(user, full = true).body |> String |> JSON.parse)
|
|
end
|