index.community/backend/lib/backend_web/controllers/admin_controller.ex

42 lines
1.2 KiB
Elixir
Raw Normal View History

2019-07-26 14:34:23 +00:00
defmodule BackendWeb.AdminController do
2019-08-21 12:30:47 +00:00
alias Backend.{Api, Auth, Instance}
2019-07-26 14:34:23 +00:00
use BackendWeb, :controller
action_fallback BackendWeb.FallbackController
def show(conn, _params) do
[token] = get_req_header(conn, "token")
with {:ok, domain} <- Auth.verify_token(token) do
2019-08-23 13:08:05 +00:00
instance = Api.get_instance(domain)
2019-07-26 14:34:23 +00:00
render(conn, "show.json", instance: instance)
end
end
def update(conn, params) do
[token] = get_req_header(conn, "token")
with {:ok, domain} <- Auth.verify_token(token) do
%{"optIn" => opt_in, "optOut" => opt_out} = params
# Make sure to update ElasticSearch so that the instance is no longer returned in search results
es_instance =
Api.get_instance_with_peers(domain)
|> Map.put(:opt_in, opt_in)
|> Map.put(:opt_out, opt_out)
Elasticsearch.put_document!(Backend.Elasticsearch.Cluster, es_instance, "instances")
ecto_instance = %Instance{
2019-07-26 14:34:23 +00:00
domain: domain,
opt_in: opt_in,
opt_out: opt_out
}
with {:ok, updated_instance} <- Api.update_instance(ecto_instance) do
2019-07-26 14:34:23 +00:00
render(conn, "show.json", instance: updated_instance)
end
end
end
end