MatrixChat.jl/src/types.jl

83 lines
2.0 KiB
Julia

const API_PATH = "_matrix/client/r0"
struct MatrixServer
instance::String
secret::String
api::String
end
struct MatrixUser
server::MatrixServer
userid::String
token::String
deviceid::String
end
import Base.==
==(u::MatrixUser, s::String) = string(u) == s
==(s::String, u::MatrixUser) = u == s
import Base.show
function Base.show(io::IO, n::MatrixServer)
print(io, string("(matrix) ", n.instance))
nothing
end
function Base.show(io::IO, u::MatrixUser)
print(io, string("(", u.deviceid, ") ", u.userid))
nothing
end
MatrixServer(s::String, secret::String) = MatrixServer(s, secret, API_PATH)
MatrixServer(s::String) = MatrixServer(s, "")
mutable struct MatrixSync
user::MatrixUser
last::Union{Nothing,String}
timeout::Int
sync
presence::String
end
MatrixSync(u::MatrixUser; presence = "offline") = MatrixSync(u, nothing, 30000, Dict(), presence)
struct MatrixMsg
sender::String
timestamp::Int
mtype::String
body::String
end
using Dates
function Base.show(io::IO, m::MatrixMsg; muser::Union{Nothing,MatrixUser} = nothing)
global user
dirforward = false
if muser === nothing
if isdefined(MatrixChat, :user)
dirforward = m.sender == user
end
else
dirforward = m.sender == muser
end
# read = m.isread
read = false
direction = dirforward ? (read ? " ««« " : " <<< ") :
(read ? " »»» " : " >>> ")
d = Dates.epochms2datetime(m.timestamp +
Dates.datetime2epochms(Dates.unix2datetime(0)))
dtfmt = if Dates.Hour(now()) - Dates.Hour(d) < Dates.Hour(24)
string(string(hour(d),pad=2), ":", string(minute(d),pad=2))
else
string(day(d), " ", Dates.monthabbr(d))
end
print(io, string("[", dtfmt, "] ", m.sender, direction, m.body))
nothing
end
import Base.string
function string(u::MatrixUser)
string("@", u.userid, ":", join(split(URI(u.server.instance).host, ".")[end-1:end], "."))
end