architex/lib/matrix_server.ex

150 lines
4.4 KiB
Elixir
Raw Normal View History

2021-06-22 12:09:25 +00:00
defmodule MatrixServer do
alias MatrixServer.EncodableMap
@random_string_alphabet Enum.into(?a..?z, []) ++ Enum.into(?A..?Z, [])
@spec get_mxid(String.t()) :: String.t()
def get_mxid(localpart) when is_binary(localpart) do
"@#{localpart}:#{server_name()}"
end
@spec server_name() :: String.t()
def server_name do
Application.get_env(:matrix_server, :server_name)
end
2021-06-26 20:02:18 +00:00
@spec localpart_regex() :: Regex.t()
2021-07-10 21:16:00 +00:00
def localpart_regex, do: ~r/^([a-z0-9\._=\/])+$/
@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
@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
@spec default_room_version() :: String.t()
2021-07-17 15:38:20 +00:00
def default_room_version, do: "7"
@spec get_domain(String.t()) :: String.t() | nil
def get_domain(id) do
case String.split(id, ":", parts: 2) do
[_, server_name] -> server_name
_ -> nil
end
end
2021-08-10 16:02:53 +00:00
# TODO Eventually move to regex with named captures.
@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
# https://elixirforum.com/t/22709/9
@spec has_duplicates?(list()) :: boolean()
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
# https://matrix.org/docs/spec/appendices#unpadded-base64
@spec encode_unpadded_base64(String.t()) :: String.t()
def encode_unpadded_base64(data) do
data
|> Base.encode64()
|> String.trim_trailing("=")
end
# Decode (possibly unpadded) base64.
@spec decode_base64(String.t()) :: {:ok, String.t()} | :error
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
@spec encode_canonical_json(map()) :: {:ok, String.t()} | {:error, Jason.EncodeError.t()}
def encode_canonical_json(object) do
object
|> EncodableMap.from_map()
|> Jason.encode()
end
# https://stackoverflow.com/questions/41523762/41671211
@spec to_serializable_map(struct()) :: map()
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
@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
@spec add_signature(map(), String.t(), String.t()) :: map()
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
@spec validate_change_simple(Ecto.Changeset.t(), atom(), (term() -> boolean())) :: Ecto.Changeset.t()
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
# Returns a Boolean whether the signature is valid.
# Also returns false on ArgumentError.
@spec sign_verify(binary(), String.t(), binary()) :: boolean()
def sign_verify(sig, text, key) do
try do
:enacl.sign_verify_detached(sig, text, key)
rescue
ArgumentError -> false
end
end
@spec min_datetime(DateTime.t(), DateTime.t()) :: DateTime.t()
def min_datetime(datetime1, datetime2) do
if DateTime.compare(datetime1, datetime2) == :gt do
datetime2
else
datetime1
end
end
@spec encode_url_safe_base64(String.t()) :: String.t()
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