Wrote function to sync with matrix room
This commit is contained in:
parent
7cb6ac4cbd
commit
d093f1a109
85
init.lua
85
init.lua
@ -1,3 +1,6 @@
|
||||
|
||||
print("Matrix bridge loaded.")
|
||||
|
||||
local modpath = minetest.get_modpath(minetest.get_current_modname())
|
||||
|
||||
dofile(modpath.."/config.lua")
|
||||
@ -9,7 +12,53 @@ if http == nil then
|
||||
end
|
||||
|
||||
local token = nil
|
||||
local txid = 0
|
||||
local txid = 0
|
||||
-- used for msync()
|
||||
local since = nil
|
||||
|
||||
local function msync()
|
||||
-- optimization note: request more recent instead of unfiltered req
|
||||
-- local param1 = '&filter={\"room\":{\"timeline\":{\"limit\":1}}}'
|
||||
local param1 = "?access_token=" .. token
|
||||
local param2 = "&full_state=false"
|
||||
local u = MATRIX_SERVER.."/_matrix/client/r0/sync" .. param1 .. param2
|
||||
if since == nil then -- first time sync
|
||||
-- do nothing for now
|
||||
else -- second time sync -> append since parameter
|
||||
u = u + "&since="..since
|
||||
end
|
||||
http.fetch({url=u},
|
||||
function (res)
|
||||
if res == nil then
|
||||
print("Matrix - sync response is nil")
|
||||
else
|
||||
local response = minetest.parse_json(res.data)
|
||||
since = response.next_batch
|
||||
minetest.log("action", "sync state:" .. since)
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
|
||||
local function mlogin()
|
||||
local u = MATRIX_SERVER.."/_matrix/client/r0/login"
|
||||
local d = '{"type":"m.login.password","password":"'..MATRIX_PASSWORD..'","identifier":{"type":"m.id.user","user":"'..MATRIX_USERNAME..'"}}'
|
||||
local h = {"Content-Type: application/json"}
|
||||
http.fetch({url=u, method="POST", extra_headers=h, data=d},
|
||||
function(res)
|
||||
if res.code == 200 then
|
||||
minetest.log("action", res.data)
|
||||
local data = minetest.parse_json(res.data)
|
||||
token = data.access_token
|
||||
minetest.log("action", "Matrix authenticated")
|
||||
msync()
|
||||
else
|
||||
minetest.log("error", to_string(res))
|
||||
end
|
||||
end
|
||||
)
|
||||
end
|
||||
mlogin()
|
||||
|
||||
function send_message(msg)
|
||||
txid = txid + 1
|
||||
@ -22,21 +71,23 @@ function send_message(msg)
|
||||
end)
|
||||
end
|
||||
|
||||
http.fetch({
|
||||
url = MATRIX_SERVER.."/_matrix/client/r0/login",
|
||||
method = "POST",
|
||||
extra_headers = {"Content-Type: application/json"},
|
||||
data = '{"type":"m.login.password","password":"'..MATRIX_PASSWORD..'","identifier":{"type":"m.id.user","user":"'..MATRIX_USERNAME..'"}}'
|
||||
}, function(res)
|
||||
if res.code == 200 then
|
||||
minetest.log("action", res.data)
|
||||
local data = minetest.parse_json(res.data)
|
||||
token = data.access_token
|
||||
minetest.log("action", "Matrix authenticated")
|
||||
else
|
||||
minetest.log("error", to_string(res))
|
||||
end
|
||||
end)
|
||||
-- http.fetch({
|
||||
-- url = MATRIX_SERVER.."/_matrix/client/r0/login",
|
||||
-- method = "POST",
|
||||
-- extra_headers = {"Content-Type: application/json"},
|
||||
-- data = '{"type":"m.login.password","password":"'..MATRIX_PASSWORD..'","identifier":{"type":"m.id.user","user":"'..MATRIX_USERNAME..'"}}'
|
||||
-- }, function(res)
|
||||
-- if res.code == 200 then
|
||||
-- minetest.log("action", res.data)
|
||||
-- local data = minetest.parse_json(res.data)
|
||||
-- token = data.access_token
|
||||
-- minetest.log("action", "Matrix authenticated")
|
||||
-- else
|
||||
-- minetest.log("error", to_string(res))
|
||||
-- end
|
||||
-- end)
|
||||
|
||||
|
||||
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
local name = player:get_player_name()
|
||||
@ -66,3 +117,5 @@ minetest.register_on_chat_message(function(name, message)
|
||||
end
|
||||
send_message("<"..name.."> "..message)
|
||||
end)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user