can fetch and display Matrix messages in-game

This commit is contained in:
- 2022-01-22 19:41:51 -08:00
parent d093f1a109
commit e600d4a1e3
1 changed files with 46 additions and 6 deletions

View File

@ -16,6 +16,38 @@ local txid = 0
-- used for msync()
local since = nil
-- accepts list of events
local function mchat(data)
if data == nil then
return
end
-- lets just get this working
if data["rooms"] == nil then
return
end
if data["rooms"]["join"] == nil then
return
end
if data["rooms"]["join"][MATRIX_ROOM] == nil then
return
end
if data["rooms"]["join"][MATRIX_ROOM]["timeline"] == nil then
return
else
local events = data["rooms"]["join"][MATRIX_ROOM]["timeline"]["events"]
if events == nil then
return
end
for i, event in ipairs(events) do
if event.type == "m.room.message"
and event.sender ~= MATRIX_USERNAME_LONG then
local message = event.sender .. ": " .. event.content.body
minetest.chat_send_all(message)
end
end
end
end
local function msync()
-- optimization note: request more recent instead of unfiltered req
-- local param1 = '&filter={\"room\":{\"timeline\":{\"limit\":1}}}'
@ -25,16 +57,22 @@ local function msync()
if since == nil then -- first time sync
-- do nothing for now
else -- second time sync -> append since parameter
u = u + "&since="..since
u = u .. "&since="..since
end
http.fetch({url=u},
function (res)
if res == nil then
print("Matrix - sync response is nil")
if res == nil then -- received nothing from server
minetest.log("error", "matrix_bridge - sync response is nil")
elseif res.code == 0 then
minetest.log("info", "matrix_bridge - not found / timeout")
elseif res.code == 404 then
minetest.log("info", "matrix_bridge - 404")
else
local response = minetest.parse_json(res.data)
since = response.next_batch
minetest.log("action", "sync state:" .. since)
if response ~= nil then
since = response.next_batch
mchat(response)
end
end
end
)
@ -87,7 +125,9 @@ end
-- end
-- end)
minetest.register_globalstep(function()
msync()
end)
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()