Add dialyzer dependency
Add typespecs to several functions
This commit is contained in:
parent
7b011123f8
commit
58d3e17259
12 changed files with 79 additions and 44 deletions
3
.dialyzer_ignore.exs
Normal file
3
.dialyzer_ignore.exs
Normal file
|
@ -0,0 +1,3 @@
|
|||
[
|
||||
{"lib/phoenix/router.ex", :pattern_match, 402}
|
||||
]
|
|
@ -1,39 +1,33 @@
|
|||
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
|
||||
|
||||
def maybe_update_map(map, old_key, new_key) do
|
||||
maybe_update_map(map, old_key, new_key, &Function.identity/1)
|
||||
end
|
||||
|
||||
def maybe_update_map(map, old_key, new_key, fun) when is_map_key(map, old_key) do
|
||||
value = Map.fetch!(map, old_key)
|
||||
|
||||
map
|
||||
|> Map.put(new_key, fun.(value))
|
||||
|> Map.delete(old_key)
|
||||
end
|
||||
|
||||
def maybe_update_map(map, _, _, _), do: map
|
||||
|
||||
@spec localpart_regex() :: Regex.t()
|
||||
def localpart_regex, do: ~r/^([a-z0-9\._=\/])+$/
|
||||
|
||||
@alphabet Enum.into(?a..?z, []) ++ Enum.into(?A..?Z, [])
|
||||
def random_string(length), do: random_string(length, @alphabet)
|
||||
@spec random_string(pos_integer()) :: String.t()
|
||||
def random_string(length), do: random_string(length, @random_string_alphabet)
|
||||
|
||||
@spec random_string(pos_integer(), Enum.t()) :: String.t()
|
||||
def random_string(length, alphabet) when length >= 1 do
|
||||
for _ <- 1..length, into: "", do: <<Enum.random(alphabet)>>
|
||||
end
|
||||
|
||||
@spec default_room_version() :: String.t()
|
||||
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
|
||||
|
@ -42,6 +36,7 @@ defmodule MatrixServer do
|
|||
end
|
||||
|
||||
# TODO Eventually move to regex with named captures.
|
||||
@spec get_localpart(String.t()) :: String.t() | nil
|
||||
def get_localpart(id) do
|
||||
with [part, _] <- String.split(id, ":", parts: 2),
|
||||
{_, localpart} <- String.split_at(part, 1) do
|
||||
|
@ -52,6 +47,7 @@ defmodule MatrixServer do
|
|||
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 ->
|
||||
|
@ -61,6 +57,7 @@ defmodule MatrixServer do
|
|||
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()
|
||||
|
@ -68,12 +65,14 @@ defmodule MatrixServer do
|
|||
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()
|
||||
|
@ -81,6 +80,7 @@ defmodule MatrixServer do
|
|||
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__]
|
||||
|
@ -90,6 +90,7 @@ defmodule MatrixServer do
|
|||
|> Map.drop(waste_fields)
|
||||
end
|
||||
|
||||
@spec serialize_and_encode(struct()) :: {:ok, String.t()} | {:error, Jason.EncodeError.t()}
|
||||
def serialize_and_encode(struct) do
|
||||
# TODO: handle nil values in struct?
|
||||
struct
|
||||
|
@ -97,6 +98,7 @@ defmodule MatrixServer do
|
|||
|> 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
|
||||
|
@ -108,6 +110,7 @@ defmodule MatrixServer do
|
|||
%{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"}]
|
||||
|
@ -118,6 +121,7 @@ defmodule MatrixServer do
|
|||
|
||||
# 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)
|
||||
|
@ -126,6 +130,7 @@ defmodule MatrixServer do
|
|||
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
|
||||
|
@ -134,6 +139,7 @@ defmodule MatrixServer do
|
|||
end
|
||||
end
|
||||
|
||||
@spec encode_url_safe_base64(String.t()) :: String.t()
|
||||
def encode_url_safe_base64(data) do
|
||||
data
|
||||
|> encode_unpadded_base64()
|
||||
|
|
|
@ -10,10 +10,12 @@ defmodule MatrixServer.KeyServer do
|
|||
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
|
||||
end
|
||||
|
||||
@spec sign_object(map()) :: {:ok, String.t(), String.t()} | :error
|
||||
def sign_object(object) do
|
||||
GenServer.call(__MODULE__, {:sign_object, object})
|
||||
end
|
||||
|
||||
@spec get_own_signing_keys() :: list({String.t(), binary()})
|
||||
def get_own_signing_keys() do
|
||||
GenServer.call(__MODULE__, :get_own_signing_keys)
|
||||
end
|
||||
|
@ -41,6 +43,7 @@ defmodule MatrixServer.KeyServer do
|
|||
end
|
||||
|
||||
# https://blog.swwomm.com/2020/09/elixir-ed25519-signatures-with-enacl.html
|
||||
@spec sign_object(map(), binary()) :: {:ok, String.t()} | {:error, Jason.EncodeError.t()}
|
||||
defp sign_object(object, private_key) do
|
||||
object = Map.drop(object, [:signatures, :unsigned])
|
||||
|
||||
|
@ -55,7 +58,8 @@ defmodule MatrixServer.KeyServer do
|
|||
end
|
||||
|
||||
# TODO: not sure if there is a better way to do this...
|
||||
def read_keys do
|
||||
@spec read_keys() :: {binary(), binary()}
|
||||
defp read_keys do
|
||||
raw_priv_key =
|
||||
Application.get_env(:matrix_server, :private_key_file)
|
||||
|> File.read!()
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
defmodule MatrixServer.QuickCheck do
|
||||
import Ecto.Query
|
||||
|
||||
alias MatrixServer.{Repo, Room, Account, RoomServer}
|
||||
alias MatrixServerWeb.Client.Request.CreateRoom
|
||||
|
||||
def create_room(name \\ nil, topic \\ nil) do
|
||||
account = Repo.one!(from a in Account, limit: 1)
|
||||
input = %CreateRoom{name: name, topic: topic}
|
||||
%Room{id: room_id} = Repo.insert!(Room.create_changeset(input))
|
||||
{:ok, pid} = RoomServer.get_room_server(room_id)
|
||||
RoomServer.create_room(pid, account, input)
|
||||
end
|
||||
end
|
|
@ -20,6 +20,7 @@ defmodule MatrixServer.RoomServer do
|
|||
|
||||
# Get room server pid, or spin one up for the room.
|
||||
# If the room does not exist, return an error.
|
||||
@spec get_room_server(String.t()) :: {:error, :not_found} | DynamicSupervisor.on_start_child()
|
||||
def get_room_server(room_id) do
|
||||
case Repo.one(from r in Room, where: r.id == ^room_id) do
|
||||
nil ->
|
||||
|
@ -42,10 +43,12 @@ defmodule MatrixServer.RoomServer do
|
|||
end
|
||||
end
|
||||
|
||||
@spec create_room(pid(), MatrixServer.Account.t(), MatrixServerWeb.Client.Request.CreateRoom.t()) :: {:ok, String.t()} | {:error, atom()}
|
||||
def create_room(pid, account, input) do
|
||||
GenServer.call(pid, {:create_room, account, input})
|
||||
end
|
||||
|
||||
@spec server_in_room(pid(), String.t()) :: boolean()
|
||||
def server_in_room(pid, domain) do
|
||||
GenServer.call(pid, {:server_in_room, domain})
|
||||
end
|
||||
|
|
|
@ -7,6 +7,10 @@ defmodule MatrixServer.Account do
|
|||
alias MatrixServerWeb.Client.Request.{Register, Login}
|
||||
alias Ecto.Multi
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
password_hash: String.t()
|
||||
}
|
||||
|
||||
@max_mxid_length 255
|
||||
|
||||
@primary_key {:localpart, :string, []}
|
||||
|
|
|
@ -7,6 +7,12 @@ defmodule MatrixServer.Room do
|
|||
alias MatrixServer.{Repo, Room, Event, Alias, RoomServer}
|
||||
alias MatrixServerWeb.Client.Request.CreateRoom
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
visibility: :public | :private,
|
||||
state: list(list(String.t())),
|
||||
forward_extremities: list(String.t())
|
||||
}
|
||||
|
||||
@primary_key {:id, :string, []}
|
||||
schema "rooms" do
|
||||
field :visibility, Ecto.Enum, values: [:public, :private]
|
||||
|
|
|
@ -129,8 +129,10 @@ defmodule MatrixServer.StateResolution.Authorization do
|
|||
end
|
||||
|
||||
defp _authorized?(%Event{type: "m.room.third_party_invite", sender: sender}, state_set) do
|
||||
power_levels = get_power_levels(state_set)
|
||||
# Check rule: 7.1
|
||||
has_power_level(sender, state_set, :invite)
|
||||
|
||||
has_power_level(sender, power_levels, :invite)
|
||||
end
|
||||
|
||||
defp _authorized?(%Event{state_key: state_key, sender: sender} = event, state_set) do
|
||||
|
@ -202,8 +204,10 @@ defmodule MatrixServer.StateResolution.Authorization do
|
|||
defp get_action_power_level(:invite, _), do: 50
|
||||
defp get_action_power_level(:ban, %{"ban" => pl}), do: pl
|
||||
defp get_action_power_level(:ban, _), do: 50
|
||||
defp get_action_power_level(:redact, %{"redact" => pl}), do: pl
|
||||
defp get_action_power_level(:redact, _), do: 50
|
||||
# defp get_action_power_level(:redact, %{"redact" => pl}), do: pl
|
||||
# defp get_action_power_level(:redact, _), do: 50
|
||||
# defp get_action_power_level(:kick, %{"kick" => pl}), do: pl
|
||||
# defp get_action_power_level(:kick, _), do: 50
|
||||
|
||||
defp get_action_power_level({:event, %Event{type: type}}, %{"events" => events})
|
||||
when is_map_key(events, type),
|
||||
|
|
|
@ -5,6 +5,16 @@ defmodule MatrixServerWeb.Client.Request.CreateRoom do
|
|||
|
||||
alias Ecto.Changeset
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
visibility: String.t(),
|
||||
room_alias_name: String.t(),
|
||||
name: String.t(),
|
||||
topic: String.t(),
|
||||
invite: list(String.t()),
|
||||
room_version: String.t(),
|
||||
preset: String.t()
|
||||
}
|
||||
|
||||
@primary_key false
|
||||
embedded_schema do
|
||||
field :visibility, :string
|
||||
|
|
6
mix.exs
6
mix.exs
|
@ -10,7 +10,8 @@ defmodule MatrixServer.MixProject do
|
|||
compilers: [:phoenix] ++ Mix.compilers(),
|
||||
start_permanent: Mix.env() == :prod,
|
||||
aliases: aliases(),
|
||||
deps: deps()
|
||||
deps: deps(),
|
||||
dialyzer: [plt_add_deps: :app_tree]
|
||||
]
|
||||
end
|
||||
|
||||
|
@ -46,7 +47,8 @@ defmodule MatrixServer.MixProject do
|
|||
{:ex_machina, "~> 2.7", only: :test},
|
||||
{:enacl, "~> 1.2"},
|
||||
{:tesla, "~> 1.4"},
|
||||
{:finch, "~> 0.8.1"}
|
||||
{:finch, "~> 0.8.1"},
|
||||
{:dialyxir, "~> 1.1", only: [:dev], runtime: false}
|
||||
]
|
||||
end
|
||||
|
||||
|
|
2
mix.lock
2
mix.lock
|
@ -9,10 +9,12 @@
|
|||
"cowlib": {:hex, :cowlib, "2.11.0", "0b9ff9c346629256c42ebe1eeb769a83c6cb771a6ee5960bd110ab0b9b872063", [:make, :rebar3], [], "hexpm", "2b3e9da0b21c4565751a6d4901c20d1b4cc25cbb7fd50d91d2ab6dd287bc86a9"},
|
||||
"db_connection": {:hex, :db_connection, "2.4.0", "d04b1b73795dae60cead94189f1b8a51cc9e1f911c234cc23074017c43c031e5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad416c21ad9f61b3103d254a71b63696ecadb6a917b36f563921e0de00d7d7c8"},
|
||||
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"},
|
||||
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
|
||||
"ecto": {:hex, :ecto, "3.6.2", "efdf52acfc4ce29249bab5417415bd50abd62db7b0603b8bab0d7b996548c2bc", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "efad6dfb04e6f986b8a3047822b0f826d9affe8e4ebdd2aeedbfcb14fd48884e"},
|
||||
"ecto_sql": {:hex, :ecto_sql, "3.6.2", "9526b5f691701a5181427634c30655ac33d11e17e4069eff3ae1176c764e0ba3", [:mix], [{:db_connection, "~> 2.2", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.6.2", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.4.0 or ~> 0.5.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5ec9d7e6f742ea39b63aceaea9ac1d1773d574ea40df5a53ef8afbd9242fdb6b"},
|
||||
"elixir_make": {:hex, :elixir_make, "0.6.2", "7dffacd77dec4c37b39af867cedaabb0b59f6a871f89722c25b28fcd4bd70530", [:mix], [], "hexpm", "03e49eadda22526a7e5279d53321d1cced6552f344ba4e03e619063de75348d9"},
|
||||
"enacl": {:hex, :enacl, "1.2.1", "7776480b9b3d42a51d66dbbcbf17fa3d79285b3d2adcb4d5b5bd0b70f0ef1949", [:rebar3], [], "hexpm", "67bbbeddd2564dc899a3dcbc3765cd6ad71629134f1e500a50ec071f0f75e552"},
|
||||
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
|
||||
"ex_machina": {:hex, :ex_machina, "2.7.0", "b792cc3127fd0680fecdb6299235b4727a4944a09ff0fa904cc639272cd92dc7", [:mix], [{:ecto, "~> 2.2 or ~> 3.0", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}], "hexpm", "419aa7a39bde11894c87a615c4ecaa52d8f107bbdd81d810465186f783245bf8"},
|
||||
"finch": {:hex, :finch, "0.8.1", "761e39640c98d12c9ac7aa15b4d0732205669b20055e26bcc8da7ce826070fdc", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: false]}, {:mint, "~> 1.3", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.3.5", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "bc80f759f701dd56793f4edfaa6dc6480707c23046fde3a98c9887b164924cad"},
|
||||
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
|
||||
|
|
|
@ -40,14 +40,16 @@ room =
|
|||
visibility: :public
|
||||
})
|
||||
|
||||
Repo.insert!(
|
||||
Event.create_room(room, alice, "v1")
|
||||
|> Map.put(:origin_server_ts, timestamp.(0))
|
||||
|> Map.put(:event_id, "create")
|
||||
)
|
||||
create_room =
|
||||
Repo.insert!(
|
||||
Event.create_room(room, alice, "v1", false)
|
||||
|> Map.put(:origin_server_ts, timestamp.(0))
|
||||
|> Event.post_process()
|
||||
|> elem(1)
|
||||
)
|
||||
|
||||
Repo.insert!(
|
||||
Event.join(room, alice)
|
||||
Event.join(room, alice, false)
|
||||
|> Map.put(:prev_events, ["create"])
|
||||
|> Map.put(:auth_events, ["create"])
|
||||
|> Map.put(:origin_server_ts, timestamp.(1))
|
||||
|
@ -56,6 +58,7 @@ Repo.insert!(
|
|||
|
||||
Repo.insert!(
|
||||
Event.join(room, bob)
|
||||
|> elem(1)
|
||||
|> Map.put(:prev_events, ["join_alice"])
|
||||
|> Map.put(:auth_events, ["create"])
|
||||
|> Map.put(:origin_server_ts, timestamp.(2))
|
||||
|
@ -64,13 +67,14 @@ Repo.insert!(
|
|||
|
||||
Repo.insert!(
|
||||
Event.join(room, charlie)
|
||||
|> elem(1)
|
||||
|> Map.put(:prev_events, ["join_bob"])
|
||||
|> Map.put(:auth_events, ["create"])
|
||||
|> Map.put(:origin_server_ts, timestamp.(3))
|
||||
|> Map.put(:event_id, "join_charlie")
|
||||
)
|
||||
|
||||
%Event{content: content} = event = Event.power_levels(room, alice)
|
||||
%Event{content: content} = event = Event.power_levels(room, alice) |> elem(1)
|
||||
event = %Event{event | content: %{content | "users" => %{"alice" => 100, "bob" => 100}}}
|
||||
|
||||
Repo.insert!(
|
||||
|
@ -81,7 +85,7 @@ Repo.insert!(
|
|||
|> Map.put(:event_id, "a")
|
||||
)
|
||||
|
||||
%Event{content: content} = event = Event.power_levels(room, bob)
|
||||
%Event{content: content} = event = Event.power_levels(room, bob) |> elem(1)
|
||||
|
||||
event = %Event{
|
||||
event
|
||||
|
@ -98,6 +102,7 @@ Repo.insert!(
|
|||
|
||||
Repo.insert!(
|
||||
Event.topic(room, alice, "sneed")
|
||||
|> elem(1)
|
||||
|> Map.put(:prev_events, ["a"])
|
||||
|> Map.put(:auth_events, ["create", "join_alice", "a"])
|
||||
|> Map.put(:origin_server_ts, timestamp.(5))
|
||||
|
|
Loading…
Reference in a new issue