diff options
author | Hubert Chathi <hubert@uhoreg.ca> | 2019-10-15 19:05:26 +0200 |
---|---|---|
committer | Hubert Chathi <hubert@uhoreg.ca> | 2019-10-15 19:05:26 +0200 |
commit | 8608bbcde3aba1c5e775db1e9290d1d582aa0e71 (patch) | |
tree | ba54aa63e9dc252ad9055925a8423d282c6c86ce /lib/polyjuice | |
parent | add namespacing and deleting to key-value storage (diff) |
make update and put in Polyjuice.Client.Filter public
Diffstat (limited to 'lib/polyjuice')
-rw-r--r-- | lib/polyjuice/client/filter.ex | 136 |
1 files changed, 128 insertions, 8 deletions
diff --git a/lib/polyjuice/client/filter.ex b/lib/polyjuice/client/filter.ex index 1466dfe..99ce4dd 100644 --- a/lib/polyjuice/client/filter.ex +++ b/lib/polyjuice/client/filter.ex @@ -40,20 +40,90 @@ defmodule Polyjuice.Client.Filter do """ - defp update(map, [key], initial, func) when is_map(map) do - Map.update(map, key, initial, func) + @doc """ + Update the value in a filter. + + The `key_path` is a list of keys to traverse to find the element to update. + The `initial` and `func` arguments are like the corresponding arguments to + `Map.update`. + + Examples: + + iex> Polyjuice.Client.Filter.update( + ...> %{ + ...> "presence" => %{ + ...> "not_senders" => ["@alice:example.com"] + ...> } + ...> }, + ...> ["presence", "not_senders"], + ...> ["@bob:example.com"], + ...> &Enum.concat(&1, ["@bob:example.com"]) + ...> ) + %{ + "presence" => %{ + "not_senders" => ["@alice:example.com", "@bob:example.com"] + } + } + + iex> Polyjuice.Client.Filter.update( + ...> %{ + ...> "presence" => %{ + ...> "not_senders" => ["@alice:example.com"] + ...> } + ...> }, + ...> ["presence", "senders"], + ...> ["@bob:example.com"], + ...> &Enum.concat(&1, ["@bob:example.com"]) + ...> ) + %{ + "presence" => %{ + "not_senders" => ["@alice:example.com"], + "senders" => ["@bob:example.com"] + } + } + + """ + @spec update(map, [String.t()], Any, (Any -> Any)) :: map + def update(filter, key_path, initial, func) + def update(filter, [key], initial, func) when is_map(filter) do + Map.update(filter, key, initial, func) end - defp update(map, [key | rest], initial, func) when is_map(map) do - Map.put(map, key, update(Map.get(map, key, %{}), rest, initial, func)) + def update(filter, [key | rest], initial, func) when is_map(filter) do + Map.put(filter, key, update(Map.get(filter, key, %{}), rest, initial, func)) end - defp put(map, [key], val) when is_map(map) do - Map.put(map, key, val) + @doc """ + Set the value in a filter. + + The `key_path` is a list of keys to traverse to find the element to update. + The `initial` and `func` arguments are like the corresponding arguments to + `Map.update`. + + iex> Polyjuice.Client.Filter.put( + ...> %{ + ...> "presence" => %{ + ...> "not_senders" => ["@alice:example.com"] + ...> } + ...> }, + ...> ["presence", "not_senders"], + ...> ["@bob:example.com"] + ...> ) + %{ + "presence" => %{ + "not_senders" => ["@bob:example.com"] + } + } + + """ + @spec put(map, [String.t()], Any) :: map + def put(filter, key_path, val) + def put(filter, [key], val) when is_map(filter) do + Map.put(filter, key, val) end - defp put(map, [key | rest], val) when is_map(map) do - Map.put(map, key, put(Map.get(map, key, %{}), rest, val)) + def put(filter, [key | rest], val) when is_map(filter) do + Map.put(filter, key, put(Map.get(filter, key, %{}), rest, val)) end @doc """ @@ -203,6 +273,56 @@ defmodule Polyjuice.Client.Filter do end @doc """ + Allow certain types of timeline events to be included. + """ + @spec include_timeline_types(filter :: map, types :: list) :: map + def include_timeline_types(filter \\ %{}, types) + + def include_timeline_types(filter, types) when filter == %{} and is_list(types) do + %{ + "room" => %{ + "timeline" => %{ + "types" => types + } + } + } + end + + def include_timeline_types(filter, types) when is_map(filter) and is_list(types) do + update( + filter, + ["room", "timeline", "types"], + types, + &Enum.concat(&1, types) + ) + end + + @doc """ + Don't allow certain types of timeline events. + """ + @spec exclude_timeline_types(filter :: map, types :: list) :: map + def exclude_timeline_types(filter \\ %{}, types) + + def exclude_timeline_types(filter, types) when filter == %{} and is_list(types) do + %{ + "room" => %{ + "timeline" => %{ + "not_types" => types + } + } + } + end + + def exclude_timeline_types(filter, types) when is_map(filter) and is_list(types) do + update( + filter, + ["room", "timeline", "not_types"], + types, + &Enum.concat(&1, types) + ) + end + + @doc """ Set the maximum number of timeline events. """ @spec limit_timeline_events(filter :: map, limit :: integer) :: map |