2021-06-22 12:09:25 +00:00
|
|
|
defmodule MatrixServer do
|
2021-08-08 17:20:10 +00:00
|
|
|
alias MatrixServer.OrderedMap
|
|
|
|
|
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
|
2021-08-06 20:03:34 +00:00
|
|
|
case String.split(id, ":", parts: 2) do
|
2021-07-26 17:47:38 +00:00
|
|
|
[_, 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-08-08 17:20:10 +00:00
|
|
|
|
|
|
|
# Decode (possibly unpadded) base64.
|
|
|
|
def decode_base64(data) when is_binary(data) do
|
|
|
|
rem = rem(String.length(data), 4)
|
|
|
|
padded_data = if rem > 0, do: data <> String.duplicate("=", 4 - rem), else: data
|
|
|
|
Base.decode64(padded_data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def encode_canonical_json(object) do
|
|
|
|
object
|
|
|
|
|> Map.drop([:signatures, :unsigned])
|
|
|
|
|> OrderedMap.from_map()
|
|
|
|
|> Jason.encode()
|
|
|
|
end
|
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/41523762/41671211
|
|
|
|
def to_serializable_map(struct) do
|
|
|
|
association_fields = struct.__struct__.__schema__(:associations)
|
|
|
|
waste_fields = association_fields ++ [:__meta__]
|
|
|
|
|
|
|
|
struct
|
|
|
|
|> Map.from_struct()
|
|
|
|
|> Map.drop(waste_fields)
|
|
|
|
end
|
2021-06-22 12:09:25 +00:00
|
|
|
end
|