function sync(u::MatrixUser; timeout = 0, since::Union{String,Nothing} = nothing, full = false) req = Dict("timeout" => string(timeout), "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) : # updates sync(s.user, since = s.last, timeout = s.timeout, full = false) 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 = sync!(s) if :sync in keys(hooks) hooks[:sync](res) end end end function stop_sync!() global sync_enabled oldstatus = sync_enabled sync_enabled = false sync_enabled 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 rooms_last_batch = Dict() 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