2021-06-22 12:09:25 +00:00
|
|
|
defmodule MatrixServer do
|
2021-06-25 15:43:12 +00:00
|
|
|
def get_mxid(localpart) when is_binary(localpart) do
|
|
|
|
"@#{localpart}:#{server_name()}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def server_name do
|
|
|
|
Application.get_env(:matrix_server, :server_name)
|
|
|
|
end
|
2021-06-26 20:02:18 +00:00
|
|
|
|
2021-07-10 21:16:00 +00:00
|
|
|
def maybe_update_map(map, old_key, new_key) do
|
|
|
|
maybe_update_map(map, old_key, new_key, &Function.identity/1)
|
2021-06-26 20:02:18 +00:00
|
|
|
end
|
|
|
|
|
2021-07-10 21:16:00 +00:00
|
|
|
def maybe_update_map(map, old_key, new_key, fun) when is_map_key(map, old_key) do
|
2021-06-26 20:02:18 +00:00
|
|
|
value = Map.fetch!(map, old_key)
|
|
|
|
|
|
|
|
map
|
|
|
|
|> Map.put(new_key, fun.(value))
|
|
|
|
|> Map.delete(old_key)
|
|
|
|
end
|
|
|
|
|
2021-07-10 21:16:00 +00:00
|
|
|
def maybe_update_map(map, _, _, _), do: map
|
|
|
|
|
|
|
|
def localpart_regex, do: ~r/^([a-z0-9\._=\/])+$/
|
|
|
|
|
|
|
|
@alphabet Enum.into(?a..?z, []) ++ Enum.into(?A..?Z, [])
|
2021-07-13 17:35:02 +00:00
|
|
|
def random_string(length), do: random_string(length, @alphabet)
|
|
|
|
|
|
|
|
def random_string(length, alphabet) when length >= 1 do
|
|
|
|
for _ <- 1..length, into: "", do: <<Enum.random(alphabet)>>
|
2021-07-10 21:16:00 +00:00
|
|
|
end
|
2021-07-17 15:38:20 +00:00
|
|
|
|
|
|
|
def default_room_version, do: "7"
|
2021-07-26 17:47:38 +00:00
|
|
|
|
|
|
|
def get_domain(id) do
|
|
|
|
case String.split(id, ":") do
|
|
|
|
[_, server_name] -> server_name
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# https://elixirforum.com/t/22709/9
|
|
|
|
def has_duplicates?(list) do
|
|
|
|
list
|
|
|
|
|> Enum.reduce_while(%MapSet{}, fn x, acc ->
|
|
|
|
if MapSet.member?(acc, x), do: {:halt, false}, else: {:cont, MapSet.put(acc, x)}
|
|
|
|
end)
|
|
|
|
|> is_boolean()
|
|
|
|
end
|
2021-08-04 13:28:27 +00:00
|
|
|
|
|
|
|
# https://matrix.org/docs/spec/appendices#unpadded-base64
|
2021-08-05 11:19:38 +00:00
|
|
|
def encode_unpadded_base64(data) do
|
2021-08-04 13:28:27 +00:00
|
|
|
data
|
|
|
|
|> Base.encode64()
|
|
|
|
|> String.trim_trailing("=")
|
|
|
|
end
|
2021-06-22 12:09:25 +00:00
|
|
|
end
|