2021-06-22 12:09:25 +00:00
|
|
|
defmodule MatrixServer do
|
2021-08-18 11:54:18 +00:00
|
|
|
alias MatrixServer.EncodableMap
|
2021-08-08 17:20:10 +00:00
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@random_string_alphabet Enum.into(?a..?z, []) ++ Enum.into(?A..?Z, [])
|
|
|
|
|
|
|
|
@spec get_mxid(String.t()) :: String.t()
|
2021-06-25 15:43:12 +00:00
|
|
|
def get_mxid(localpart) when is_binary(localpart) do
|
|
|
|
"@#{localpart}:#{server_name()}"
|
|
|
|
end
|
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec server_name() :: String.t()
|
2021-06-25 15:43:12 +00:00
|
|
|
def server_name do
|
|
|
|
Application.get_env(:matrix_server, :server_name)
|
|
|
|
end
|
2021-06-26 20:02:18 +00:00
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec localpart_regex() :: Regex.t()
|
2021-07-10 21:16:00 +00:00
|
|
|
def localpart_regex, do: ~r/^([a-z0-9\._=\/])+$/
|
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec random_string(pos_integer()) :: String.t()
|
|
|
|
def random_string(length), do: random_string(length, @random_string_alphabet)
|
2021-07-13 17:35:02 +00:00
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec random_string(pos_integer(), Enum.t()) :: String.t()
|
2021-07-13 17:35:02 +00:00
|
|
|
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
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec default_room_version() :: String.t()
|
2021-07-17 15:38:20 +00:00
|
|
|
def default_room_version, do: "7"
|
2021-07-26 17:47:38 +00:00
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec get_domain(String.t()) :: String.t() | nil
|
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
|
|
|
|
|
2021-08-10 16:02:53 +00:00
|
|
|
# TODO Eventually move to regex with named captures.
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec get_localpart(String.t()) :: String.t() | nil
|
2021-08-10 16:02:53 +00:00
|
|
|
def get_localpart(id) do
|
|
|
|
with [part, _] <- String.split(id, ":", parts: 2),
|
|
|
|
{_, localpart} <- String.split_at(part, 1) do
|
|
|
|
localpart
|
|
|
|
else
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-26 17:47:38 +00:00
|
|
|
# https://elixirforum.com/t/22709/9
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec has_duplicates?(list()) :: boolean()
|
2021-07-26 17:47:38 +00:00
|
|
|
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-18 21:22:04 +00:00
|
|
|
@spec encode_unpadded_base64(String.t()) :: String.t()
|
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.
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec decode_base64(String.t()) :: {:ok, String.t()} | :error
|
2021-08-08 17:20:10 +00:00
|
|
|
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
|
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec encode_canonical_json(map()) :: {:ok, String.t()} | {:error, Jason.EncodeError.t()}
|
2021-08-08 17:20:10 +00:00
|
|
|
def encode_canonical_json(object) do
|
|
|
|
object
|
2021-08-18 11:54:18 +00:00
|
|
|
|> EncodableMap.from_map()
|
2021-08-08 17:20:10 +00:00
|
|
|
|> Jason.encode()
|
|
|
|
end
|
|
|
|
|
|
|
|
# https://stackoverflow.com/questions/41523762/41671211
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec to_serializable_map(struct()) :: map()
|
2021-08-08 17:20:10 +00:00
|
|
|
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-08-12 22:45:07 +00:00
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec serialize_and_encode(struct()) :: {:ok, String.t()} | {:error, Jason.EncodeError.t()}
|
2021-08-13 11:45:10 +00:00
|
|
|
def serialize_and_encode(struct) do
|
|
|
|
# TODO: handle nil values in struct?
|
|
|
|
struct
|
|
|
|
|> to_serializable_map()
|
|
|
|
|> encode_canonical_json()
|
|
|
|
end
|
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec add_signature(map(), String.t(), String.t()) :: map()
|
2021-08-12 22:45:07 +00:00
|
|
|
def add_signature(object, key_id, sig) when not is_map_key(object, :signatures) do
|
|
|
|
Map.put(object, :signatures, %{MatrixServer.server_name() => %{key_id => sig}})
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_signature(%{signatures: sigs} = object, key_id, sig) do
|
|
|
|
new_sigs =
|
|
|
|
Map.update(sigs, MatrixServer.server_name(), %{key_id => sig}, &Map.put(&1, key_id, sig))
|
|
|
|
|
|
|
|
%{object | signatures: new_sigs}
|
|
|
|
end
|
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec validate_change_simple(Ecto.Changeset.t(), atom(), (term() -> boolean())) :: Ecto.Changeset.t()
|
2021-08-12 22:45:07 +00:00
|
|
|
def validate_change_simple(changeset, field, func) do
|
|
|
|
augmented_func = fn _, val ->
|
|
|
|
if func.(val), do: [], else: [{field, "invalid"}]
|
|
|
|
end
|
|
|
|
|
|
|
|
Ecto.Changeset.validate_change(changeset, field, augmented_func)
|
|
|
|
end
|
2021-08-13 15:36:34 +00:00
|
|
|
|
|
|
|
# Returns a Boolean whether the signature is valid.
|
|
|
|
# Also returns false on ArgumentError.
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec sign_verify(binary(), String.t(), binary()) :: boolean()
|
2021-08-13 15:36:34 +00:00
|
|
|
def sign_verify(sig, text, key) do
|
|
|
|
try do
|
|
|
|
:enacl.sign_verify_detached(sig, text, key)
|
|
|
|
rescue
|
|
|
|
ArgumentError -> false
|
|
|
|
end
|
|
|
|
end
|
2021-08-16 17:30:47 +00:00
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec min_datetime(DateTime.t(), DateTime.t()) :: DateTime.t()
|
2021-08-16 17:30:47 +00:00
|
|
|
def min_datetime(datetime1, datetime2) do
|
|
|
|
if DateTime.compare(datetime1, datetime2) == :gt do
|
|
|
|
datetime2
|
|
|
|
else
|
|
|
|
datetime1
|
|
|
|
end
|
|
|
|
end
|
2021-08-18 11:54:18 +00:00
|
|
|
|
2021-08-18 21:22:04 +00:00
|
|
|
@spec encode_url_safe_base64(String.t()) :: String.t()
|
2021-08-18 11:54:18 +00:00
|
|
|
def encode_url_safe_base64(data) do
|
|
|
|
data
|
|
|
|
|> encode_unpadded_base64()
|
|
|
|
|> String.replace("+", "-")
|
|
|
|
|> String.replace("/", "_")
|
|
|
|
end
|
2021-06-22 12:09:25 +00:00
|
|
|
end
|