MatrixChat.jl/src/rooms.jl

77 lines
2.4 KiB
Julia

function listjoined(u::MatrixUser)
JSON.parse(String(get(u, "joined_rooms").body))
end
function createroom(u::MatrixUser, room::String)
raw_response = post(u, "createRoom", Dict("room_alias_name" => room))
JSON.parse(String(raw_response.body))
end
function invite(u::MatrixUser, room::String, user::String)
post(u, string("rooms/",room,"/invite"), Dict("user_id" => user))
end
function send(u::MatrixUser, room_id::String, text::String)
res = post(u, string("rooms/", room_id, "/send/m.room.message"),
Dict("msgtype" => "m.text", "body" => text))
JSON.parse(String(res.body))
end
function getroomevent(u::MatrixUser, room_id::String, event::String)
get(u, string("rooms/", HTTP.escapeuri(room_id), "/state",
event == "" ? "" : string("/", event)))
end
getroomevent(u::MatrixUser, room_id::String) = getroomevent(u, room_id, "")
getroomname(u::MatrixUser, room_id::String) = getroomevent(u, room_id, "m.room.name")
function roomhistory(u::MatrixUser, room_id::String;
from::Union{Nothing,String} = nothing,
direction::String = "f", limit = 15)
global rooms_last_batch
query = Dict("dir" => direction,
"limit" => string(limit),
"from" => from === nothing ?
rooms_last_batch[room_id] : from)
res = get(u, join(["rooms", room_id, "messages",], "/"), extraquery = query)
res = res.body |> String |> JSON.parse
rooms_last_batch[room_id] = res["end"]
res
end
function checkifjoin(syncres)
requests = String[]
let joins = syncres["rooms"]["invite"]
# @show joins
for j in keys(joins)
# @show joins[j]["invite_state"]["events"]
push!(requests,j)
end
end
requests
end
function joinroom(u::MatrixUser, room_id::String)
# @show join(["rooms", HTTP.escapeuri(room_id), "join"], "/")
post(u, join(["rooms", HTTP.escapeuri(room_id), "join"], "/"), Dict{String,String}())
end
function history(u::MatrixUser, room_id::String; limit = 15)
res = roomhistory(u, room_id, limit = limit)
map(x -> try
println(MatrixMsg(x["sender"], x["origin_server_ts"],
x["content"]["msgtype"], x["content"]["body"],
))
catch e
if isa(e, KeyError)
@warn "Improve me! Key not found:"
@show x
else
throw(e)
end
end
, res["chunk"])
end