97 lines
4.5 KiB
Julia
97 lines
4.5 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
|
|
|
|
"""Send message TEXT from user U to room ROOM_ID.
|
|
If HTML is not nothing, send also an html formatted text."""
|
|
function send(u::MatrixUser, room_id::String, text::String;
|
|
html::Union{Nothing,String} = nothing)
|
|
msg = Dict("msgtype" => "m.text", "body" => text)
|
|
if !isnothing(html)
|
|
msg["format"] = "org.matrix.custom.html"
|
|
msg["formatted_body"] = html
|
|
end
|
|
res = post(u, string("rooms/", room_id, "/send/m.room.message"), msg)
|
|
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::Int = 15)
|
|
global rooms_last_batch
|
|
query = Dict("dir" => direction,
|
|
"limit" => 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
|
|
|
|
#= Events to add:
|
|
|
|
x = Dict{String,Any}("membership"=>"join","user_id"=>"@nixo:nixo.xyz","origin_server_ts"=>1537029572015,"age"=>224344448,"prev_content"=>Dict{String,Any}("membership"=>"join","avatar_url"=>nothing,"displayname"=>"nixo"),"event_id"=>"\$1537029572100njqiB:nixo.xyz","content"=>Dict{String,Any}("membership"=>"join","avatar_url"=>"mxc://nixo.xyz/WRPiswpDebphCvsRQicTSyiD","displayname"=>"nixo"),"sender"=>"@nixo:nixo.xyz","replaces_state"=>"\$1536679158150Cmcgz:nixo.xyz","state_key"=>"@nixo:nixo.xyz","unsigned"=>Dict{String,Any}("prev_sender"=>"@nixo:nixo.xyz","replaces_state"=>"\$1536679158150Cmcgz:nixo.xyz","age"=>224344448,"prev_content"=>Dict{String,Any}("membership"=>"join","avatar_url"=>nothing,"displayname"=>"nixo")),"room_id"=>"!txmboRHuIhMLNpOAyq:nixo.xyz","type"=>"m.room.member")
|
|
|
|
x = Dict{String,Any}("event_id"=>"\$153691926341cbnvi:nixo.xyz","content"=>Dict{String,Any}("session_id"=>"4qEwc+2D5UdIUz/OeWp3V7+p2utCx3tCsfM7lrm3Wis","algorithm"=>"m.megolm.v1.aes-sha2","device_id"=>"EJJLBSNNXU","ciphertext"=>"AwgBEoAB7Jz1CXI9oDT6HJs0NtzMGLXnsZC4TIwJVtwLqP2DaLJRkiIdOgxW9lSCqrvWnmLkiUhqrdvAAC01C9zhH1HN7PgO31ZKVVdRlRobWxHb3/x6PCFJDRl8kcfy2xE3zHmI5s/i9GfzUbEhvgpoYHejJhbKri92BAInu5rmumnlA5Jhdy4wKXJQcN8wPBaYCoe9QpmGZzpyK9VbRm2Jhz5ZUPx2XZuTv8KEmtQjFneJCbjb95QELYE6ArrmAg4lgJW5A9T1v4NnjA4","sender_key"=>"q4um1YU6MvVfCnILkQiQU15L/yEMAFBm2G+x0/u/jwA"),"room_id"=>"!txmboRHuIhMLNpOAyq:nixo.xyz","unsigned"=>Dict{String,Any}("age"=>334652905),"sender"=>"@nixo:nixo.xyz","user_id"=>"@nixo:nixo.xyz","origin_server_ts"=>1536919263558,"age"=>334652905,"type"=>"m.room.encrypted")
|
|
|
|
|
|
=#
|
|
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
|
|
|
|
function sendtyping(u::MatrixUser, room_id::String; typing = true, timeout = 30000)
|
|
path = string("rooms/", HTTP.escapeuri(room_id), "/typing/", u.userid)
|
|
data = Dict{String,Any}("typing" => typing, "timeout" => timeout)
|
|
put(u, path, data)
|
|
end
|