Using auth. header
This commit is contained in:
parent
1aabbeaee3
commit
704fe630cc
49
init.lua
49
init.lua
|
@ -14,8 +14,8 @@ end
|
||||||
local token = nil
|
local token = nil
|
||||||
local txid = 0
|
local txid = 0
|
||||||
-- used for msync()
|
-- used for msync()
|
||||||
local since = nil
|
local since = nil
|
||||||
local last_batch = nil
|
local eventid = nil
|
||||||
|
|
||||||
local function mchat(data)
|
local function mchat(data)
|
||||||
if data == nil then
|
if data == nil then
|
||||||
|
@ -57,16 +57,17 @@ end
|
||||||
local function msync(s)
|
local function msync(s)
|
||||||
-- optimization note: request more recent instead of unfiltered req
|
-- optimization note: request more recent instead of unfiltered req
|
||||||
-- local param1 = '&filter={\"room\":{\"timeline\":{\"limit\":1}}}'
|
-- local param1 = '&filter={\"room\":{\"timeline\":{\"limit\":1}}}'
|
||||||
local param1 = "?access_token=" .. token
|
-- local param1 = "?access_token=" .. token
|
||||||
local param2 = "&full_state=false"
|
local param2 = "?full_state=false"
|
||||||
local param3 = "&timeout=5000"
|
local param3 = "&timeout=5000"
|
||||||
local u = MATRIX_SERVER.."/_matrix/client/r0/sync" .. param1 .. param2 -- .. param3
|
local u = MATRIX_SERVER.."/_matrix/client/r0/sync" .. param2 .. param3
|
||||||
if s == nil then -- first time sync
|
if s == nil then -- first time sync
|
||||||
-- do nothing for now
|
-- do nothing for now
|
||||||
else -- second time sync -> append since parameter
|
else -- second time sync -> append since parameter
|
||||||
u = u .. "&since="..s
|
u = u .. "&since="..s
|
||||||
end
|
end
|
||||||
http.fetch({url=u, method="GET"},
|
local h = {"Authorization: Bearer " .. token}
|
||||||
|
http.fetch({url=u, method="GET", extra_headers=h},
|
||||||
function (res)
|
function (res)
|
||||||
if res == nil then -- received nothing from server
|
if res == nil then -- received nothing from server
|
||||||
minetest.log("error", "matrix_bridge - sync response is nil")
|
minetest.log("error", "matrix_bridge - sync response is nil")
|
||||||
|
@ -77,8 +78,7 @@ local function msync(s)
|
||||||
local response = minetest.parse_json(res.data)
|
local response = minetest.parse_json(res.data)
|
||||||
if response ~= nil then
|
if response ~= nil then
|
||||||
mchat(response)
|
mchat(response)
|
||||||
since = response.next_batch
|
since = response.next_batch
|
||||||
last_batch = response.prev_batch
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -105,15 +105,23 @@ local function mlogin()
|
||||||
end
|
end
|
||||||
mlogin()
|
mlogin()
|
||||||
|
|
||||||
function send_message(msg)
|
local function send_message(msg)
|
||||||
txid = txid + 1
|
txid = txid + 1
|
||||||
http.fetch({
|
local u = MATRIX_SERVER.."/_matrix/client/r0/rooms/"..MATRIX_ROOM.."/send/m.room.message/" .. txid -- ?access_token=..token
|
||||||
url = MATRIX_SERVER.."/_matrix/client/r0/rooms/"..MATRIX_ROOM.."/send/m.room.message/"..txid.."?access_token="..token,
|
local h = {"Content-Type: application/json", "Authorization: Bearer " .. token}
|
||||||
method = "PUT",
|
local d = minetest.write_json({msgtype="m.text", body=msg})
|
||||||
extra_headers = {"Content-Type: application/json"},
|
http.fetch({url=u, method="PUT", extra_headers=h, data=d},
|
||||||
data = minetest.write_json({msgtype="m.text", body=msg})
|
function (res)
|
||||||
}, function(res)
|
if res.code == 200 then
|
||||||
end)
|
local data = minetest.parse_json(res.data)
|
||||||
|
minetest.log("action", "got " .. data["event_id"])
|
||||||
|
eventid = data["event_id"]
|
||||||
|
elseif res.code == 401 then
|
||||||
|
minetest.log("error", "matrix_bridge - not authorized to send messages")
|
||||||
|
elseif res.code == 404 then
|
||||||
|
minetest.log("error", "matrix_bridge - could not find endpoint for send")
|
||||||
|
end
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- http.fetch({
|
-- http.fetch({
|
||||||
|
@ -135,10 +143,11 @@ end
|
||||||
local GLOBAL_STEPS = 0
|
local GLOBAL_STEPS = 0
|
||||||
minetest.register_globalstep(function(dtine)
|
minetest.register_globalstep(function(dtine)
|
||||||
-- print(GLOBAL_STEPS)
|
-- print(GLOBAL_STEPS)
|
||||||
-- if GLOBAL_STEPS == 4 then
|
local GLOBAL_THRESHHOLD = 50
|
||||||
|
if GLOBAL_STEPS == GLOBAL_THRESHHOLD then
|
||||||
msync(since)
|
msync(since)
|
||||||
-- end
|
end
|
||||||
-- GLOBAL_STEPS = (GLOBAL_STEPS + 1) % 5
|
GLOBAL_STEPS = (GLOBAL_STEPS + 1) % (GLOBAL_THRESHHOLD+1)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_chatcommand("msync", {
|
minetest.register_chatcommand("msync", {
|
||||||
|
|
Loading…
Reference in New Issue