Remove credo and use mix format, and lint everything

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2018-07-27 10:45:35 +02:00
parent df3f08c528
commit 979aad5acb
104 changed files with 2278 additions and 1487 deletions

View File

@@ -8,7 +8,7 @@ defmodule EventosWeb.ActorController do
alias Eventos.Actors.{Actor, User}
alias Eventos.Service.ActivityPub
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
actors = Actors.list_actors()
@@ -17,9 +17,9 @@ defmodule EventosWeb.ActorController do
def create(conn, %{"actor" => actor_params}) do
with %User{} = user <- Guardian.Plug.current_resource(conn),
actor_params <- Map.put(actor_params, "user_id", user.id),
actor_params <- Map.put(actor_params, "keys", keys_for_account()),
{:ok, %Actor{} = actor} <- Actors.create_actor(actor_params) do
actor_params <- Map.put(actor_params, "user_id", user.id),
actor_params <- Map.put(actor_params, "keys", keys_for_account()),
{:ok, %Actor{} = actor} <- Actors.create_actor(actor_params) do
conn
|> put_status(:created)
|> put_resp_header("location", actor_path(conn, :show, actor.preferred_username))
@@ -30,6 +30,7 @@ defmodule EventosWeb.ActorController do
defp keys_for_account() do
key = :public_key.generate_key({:rsa, 2048, 65_537})
entry = :public_key.pem_entry_encode(:RSAPrivateKey, key)
[entry]
|> :public_key.pem_encode()
|> String.trim_trailing()
@@ -42,10 +43,13 @@ defmodule EventosWeb.ActorController do
@email_regex ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
def search(conn, %{"name" => name}) do
case Actors.search(name) do # find already saved accounts
# find already saved accounts
case Actors.search(name) do
{:ok, actors} ->
render(conn, "index.json", actors: actors)
{:error, err} -> json(conn, err)
{:error, err} ->
json(conn, err)
end
end
@@ -57,15 +61,15 @@ defmodule EventosWeb.ActorController do
end
end
# def delete(conn, %{"id" => id_str}) do
# {id, _} = Integer.parse(id_str)
# if Guardian.Plug.current_resource(conn).actor.id == id do
# actor = Actors.get_actor!(id)
# with {:ok, %Actor{}} <- Actors.delete_actor(actor) do
# send_resp(conn, :no_content, "")
# end
# else
# send_resp(conn, 401, "")
# end
# end
# def delete(conn, %{"id" => id_str}) do
# {id, _} = Integer.parse(id_str)
# if Guardian.Plug.current_resource(conn).actor.id == id do
# actor = Actors.get_actor!(id)
# with {:ok, %Actor{}} <- Actors.delete_actor(actor) do
# send_resp(conn, :no_content, "")
# end
# else
# send_resp(conn, 401, "")
# end
# end
end

View File

@@ -8,7 +8,7 @@ defmodule EventosWeb.AddressController do
alias Eventos.Addresses
alias Eventos.Addresses.Address
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
addresses = Addresses.list_addresses()
@@ -18,6 +18,7 @@ defmodule EventosWeb.AddressController do
def create(conn, %{"address" => address_params}) do
with {:ok, geom} <- Addresses.process_geom(address_params["geom"]) do
address_params = %{address_params | "geom" => geom}
with {:ok, %Address{} = address} <- Addresses.create_address(address_params) do
conn
|> put_status(:created)
@@ -30,15 +31,18 @@ defmodule EventosWeb.AddressController do
def process_geom(%{"type" => type, "data" => data}) do
import Logger
Logger.debug("Process geom")
Logger.debug(inspect data)
Logger.debug(inspect type)
Logger.debug(inspect(data))
Logger.debug(inspect(type))
types = [:point]
unless is_atom(type) do
type = String.to_existing_atom(type)
end
case type do
:point ->
%Geo.Point{coordinates: {data["latitude"], data["longitude"]}, srid: 4326}
nil ->
nil
end
@@ -66,6 +70,7 @@ defmodule EventosWeb.AddressController do
def delete(conn, %{"id" => id}) do
address = Addresses.get_address!(id)
with {:ok, %Address{}} <- Addresses.delete_address(address) do
send_resp(conn, :no_content, "")
end

View File

@@ -4,7 +4,7 @@ defmodule EventosWeb.BotController do
alias Eventos.Actors
alias Eventos.Actors.{Bot, Actor}
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
bots = Actors.list_bots()
@@ -14,7 +14,8 @@ defmodule EventosWeb.BotController do
def create(conn, %{"bot" => bot_params}) do
with user <- Guardian.Plug.current_resource(conn),
bot_params <- Map.put(bot_params, "user_id", user.id),
%Actor{} = actor <- Actors.register_bot_account(%{name: bot_params["name"], summary: bot_params["summary"]}),
%Actor{} = actor <-
Actors.register_bot_account(%{name: bot_params["name"], summary: bot_params["summary"]}),
bot_params <- Map.put(bot_params, "actor_id", actor.id),
{:ok, %Bot{} = bot} <- Actors.create_bot(bot_params) do
conn
@@ -39,6 +40,7 @@ defmodule EventosWeb.BotController do
def delete(conn, %{"id" => id}) do
bot = Actors.get_bot!(id)
with {:ok, %Bot{}} <- Actors.delete_bot(bot) do
send_resp(conn, :no_content, "")
end

View File

@@ -7,7 +7,7 @@ defmodule EventosWeb.CategoryController do
alias Eventos.Events
alias Eventos.Events.Category
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
categories = Events.list_categories()
@@ -38,6 +38,7 @@ defmodule EventosWeb.CategoryController do
def delete(conn, %{"id" => id}) do
category = Events.get_category!(id)
with {:ok, %Category{}} <- Events.delete_category(category) do
send_resp(conn, :no_content, "")
end

View File

@@ -4,7 +4,7 @@ defmodule EventosWeb.CommentController do
alias Eventos.Events
alias Eventos.Events.Comment
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
comments = Events.list_comments()
@@ -35,6 +35,7 @@ defmodule EventosWeb.CommentController do
def delete(conn, %{"uuid" => uuid}) do
comment = Events.get_comment_with_uuid!(uuid)
with {:ok, %Comment{}} <- Events.delete_comment(comment) do
send_resp(conn, :no_content, "")
end

View File

@@ -10,27 +10,48 @@ defmodule EventosWeb.EventController do
require Logger
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
ip = "88.161.154.97"
Logger.debug(inspect Geolix.lookup(ip), pretty: true)
with %{city: %Geolix.Result.City{city: city, country: country, location: %Geolix.Record.Location{latitude: latitude, longitude: longitude}}} <- Geolix.lookup(ip) do
Logger.debug(inspect city)
Logger.debug(inspect [latitude, longitude])
distance = case city do
nil -> 500_000
_ -> 50_000
end
Logger.debug(inspect(Geolix.lookup(ip), pretty: true))
with %{
city: %Geolix.Result.City{
city: city,
country: country,
location: %Geolix.Record.Location{latitude: latitude, longitude: longitude}
}
} <- Geolix.lookup(ip) do
distance =
case city do
nil -> 500_000
_ -> 50_000
end
events = Events.find_close_events(longitude, latitude, distance)
render(conn, "index.json", events: events, coord: %{longitude: longitude, latitude: latitude, distance: distance}, city: city, country: country)
render(
conn,
"index.json",
events: events,
coord: %{longitude: longitude, latitude: latitude, distance: distance},
city: city,
country: country
)
end
end
def index_all(conn, _params) do
events = Events.list_events()
render(conn, "index_all.json", events: events)
end
def create(conn, %{"event" => event_params}) do
event_params = process_event_address(event_params)
Logger.debug("creating event with")
Logger.debug(inspect event_params)
Logger.debug(inspect(event_params))
with {:ok, %Event{} = event} <- Events.create_event(event_params) do
conn
|> put_status(:created)
@@ -43,16 +64,22 @@ defmodule EventosWeb.EventController do
cond do
Map.has_key?(event, "address_type") && event["address_type"] !== :physical ->
event
Map.has_key?(event, "physical_address") ->
address = event["physical_address"]
geom = EventosWeb.AddressController.process_geom(address["geom"])
address = case geom do
nil ->
address
_ ->
%{address | "geom" => geom}
end
address =
case geom do
nil ->
address
_ ->
%{address | "geom" => geom}
end
%{event | "physical_address" => address}
true ->
event
end
@@ -67,6 +94,7 @@ defmodule EventosWeb.EventController do
case Events.get_event_full_by_uuid(uuid) do
nil ->
send_resp(conn, 404, "")
event ->
render(conn, "show.json", event: event)
end
@@ -87,7 +115,7 @@ defmodule EventosWeb.EventController do
def delete(conn, %{"uuid" => uuid}) do
with event <- Events.get_event_by_uuid(uuid),
{:ok, %Event{}} <- Events.delete_event(event) do
{:ok, %Event{}} <- Events.delete_event(event) do
send_resp(conn, :no_content, "")
end
end

View File

@@ -1,4 +1,4 @@
#defmodule EventosWeb.EventRequestController do
# defmodule EventosWeb.EventRequestController do
# @moduledoc """
# Controller for Event requests
# """
@@ -49,4 +49,4 @@
# send_resp(conn, :no_content, "")
# end
# end
#end
# end

View File

@@ -7,7 +7,7 @@ defmodule EventosWeb.GroupController do
alias Eventos.Actors
alias Eventos.Actors.{Actor, Member}
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
groups = Actors.list_groups()
@@ -16,7 +16,7 @@ defmodule EventosWeb.GroupController do
def create(conn, %{"group" => group_params}) do
with actor_admin = Guardian.Plug.current_resource(conn).actor,
{:ok, %Actor{} = group} <- Actors.create_group(group_params) do
{:ok, %Actor{} = group} <- Actors.create_group(group_params) do
conn
|> put_status(:created)
|> put_resp_header("location", actor_path(conn, :show, group))
@@ -26,15 +26,16 @@ defmodule EventosWeb.GroupController do
def join(conn, %{"name" => group_name}) do
with actor = Guardian.Plug.current_resource(conn).actor,
group <- Actors.get_group_by_name(group_name),
%Member{} = member <- Actors.create_member(%{"parent_id" => group.id, "actor_id" => actor.id}) do
group <- Actors.get_group_by_name(group_name),
%Member{} = member <-
Actors.create_member(%{"parent_id" => group.id, "actor_id" => actor.id}) do
conn
|> put_status(:created)
|> render(EventosWeb.MemberView, "member.json", member: member)
else
err ->
import Logger
Logger.debug(inspect err)
Logger.debug(inspect(err))
end
end
end

View File

@@ -1,8 +1,6 @@
defmodule EventosWeb.InboxesController do
use EventosWeb, :controller
def create(conn) do
end
end

View File

@@ -22,8 +22,8 @@ defmodule EventosWeb.NodeinfoController do
# Schema definition: https://github.com/jhass/nodeinfo/blob/master/schemas/2.0/schema.json
def nodeinfo(conn, %{"version" => "2.0.json"}) do
import Logger
Logger.debug(inspect @instance)
#stats = Stats.get_stats()
Logger.debug(inspect(@instance))
# stats = Stats.get_stats()
response = %{
version: "2.0",
@@ -39,12 +39,12 @@ defmodule EventosWeb.NodeinfoController do
openRegistrations: Keyword.get(@instance, :registrations_open),
usage: %{
users: %{
#total: stats.user_count || 0
# total: stats.user_count || 0
total: Actors.count_users()
},
localPosts: Events.count_local_events(),
localComments: Events.count_local_comments(),
#localPosts: stats.status_count || 0
localComments: Events.count_local_comments()
# localPosts: stats.status_count || 0
},
metadata: %{
nodeName: Keyword.get(@instance, :name)

View File

@@ -1,5 +1,4 @@
defmodule EventosWeb.OutboxesController do
use EventosWeb, :controller
def show(conn) do

View File

@@ -4,9 +4,9 @@ defmodule EventosWeb.PageController do
"""
use EventosWeb, :controller
plug :put_layout, false
plug(:put_layout, false)
def index(conn, _params) do
render conn, "index.html"
render(conn, "index.html")
end
end

View File

@@ -8,9 +8,11 @@ defmodule EventosWeb.ParticipantController do
def join(conn, %{"uuid" => uuid}) do
with event <- Events.get_event_by_uuid(uuid),
%{actor: actor} <- Guardian.Plug.current_resource(conn) do
participant = Events.create_participant(%{"event_id" => event.id, "actor_id" => actor.id, "role" => 1})
render(conn, "participant.json", %{participant: participant})
%{actor: actor} <- Guardian.Plug.current_resource(conn) do
participant =
Events.create_participant(%{"event_id" => event.id, "actor_id" => actor.id, "role" => 1})
render(conn, "participant.json", %{participant: participant})
end
end
end

View File

@@ -7,14 +7,17 @@ defmodule EventosWeb.SearchController do
alias Eventos.Events
alias Eventos.Actors
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def search(conn, %{"name" => name}) do
events = Events.find_events_by_name(name)
case Actors.search(name) do # find already saved accounts
# find already saved accounts
case Actors.search(name) do
{:ok, actors} ->
render(conn, "search.json", events: events, actors: actors)
{:error, err} -> json(conn, err)
{:error, err} ->
json(conn, err)
end
end
end

View File

@@ -7,7 +7,7 @@ defmodule EventosWeb.SessionController do
alias Eventos.Events
alias Eventos.Events.Session
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
sessions = Events.list_sessions()
@@ -48,6 +48,7 @@ defmodule EventosWeb.SessionController do
def delete(conn, %{"id" => id}) do
session = Events.get_session!(id)
with {:ok, %Session{}} <- Events.delete_session(session) do
send_resp(conn, :no_content, "")
end

View File

@@ -7,7 +7,7 @@ defmodule EventosWeb.TagController do
alias Eventos.Events
alias Eventos.Events.Tag
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
tags = Events.list_tags()
@@ -38,6 +38,7 @@ defmodule EventosWeb.TagController do
def delete(conn, %{"id" => id}) do
tag = Events.get_tag!(id)
with {:ok, %Tag{}} <- Events.delete_tag(tag) do
send_resp(conn, :no_content, "")
end

View File

@@ -7,7 +7,7 @@ defmodule EventosWeb.TrackController do
alias Eventos.Events
alias Eventos.Events.Track
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
tracks = Events.list_tracks()
@@ -38,6 +38,7 @@ defmodule EventosWeb.TrackController do
def delete(conn, %{"id" => id}) do
track = Events.get_track!(id)
with {:ok, %Track{}} <- Events.delete_track(track) do
send_resp(conn, :no_content, "")
end

View File

@@ -9,7 +9,7 @@ defmodule EventosWeb.UserController do
alias Eventos.Repo
alias Eventos.Actors.Service.{Activation, ResetPassword}
action_fallback EventosWeb.FallbackController
action_fallback(EventosWeb.FallbackController)
def index(conn, _params) do
users = Actors.list_users_with_actors()
@@ -17,17 +17,20 @@ defmodule EventosWeb.UserController do
end
def register(conn, %{"username" => username, "email" => email, "password" => password}) do
with {:ok, %User{} = user} <- Actors.register(%{email: email, password: password, username: username}) do
with {:ok, %User{} = user} <-
Actors.register(%{email: email, password: password, username: username}) do
Activation.send_confirmation_email(user, "locale")
conn
|> put_status(:created)
|> render("confirmation.json", %{user: user})
|> put_status(:created)
|> render("confirmation.json", %{user: user})
end
end
def validate(conn, %{"token" => token}) do
with {:ok, %User{} = user} <- Activation.check_confirmation_token(token) do
{:ok, token, _claims} = EventosWeb.Guardian.encode_and_sign(user)
conn
|> put_resp_header("location", user_path(conn, :show_current_actor))
|> render("show_with_token.json", %{user: user, token: token})
@@ -42,7 +45,8 @@ defmodule EventosWeb.UserController do
def resend_confirmation(conn, %{"email" => email}) do
with {:ok, %User{} = user} <- Actors.find_by_email(email),
false <- is_nil(user.confirmation_token),
true <- Timex.before?(Timex.shift(user.confirmation_sent_at, hours: 1), DateTime.utc_now()) do
true <-
Timex.before?(Timex.shift(user.confirmation_sent_at, hours: 1), DateTime.utc_now()) do
Activation.resend_confirmation_email(user)
render(conn, "confirmation.json", %{user: user})
else
@@ -50,6 +54,7 @@ defmodule EventosWeb.UserController do
conn
|> put_status(:not_found)
|> json(%{"error" => "Unable to find an user with this email"})
_ ->
conn
|> put_status(:not_found)
@@ -66,6 +71,7 @@ defmodule EventosWeb.UserController do
conn
|> put_status(:not_found)
|> json(%{"errors" => "Unable to find an user with this email"})
{:error, :email_too_soon} ->
conn
|> put_status(:not_found)
@@ -82,6 +88,7 @@ defmodule EventosWeb.UserController do
conn
|> put_status(:not_found)
|> json(%{"errors" => %{"token" => ["Wrong token for password reset"]}})
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_status(:unprocessable_entity)
@@ -90,9 +97,11 @@ defmodule EventosWeb.UserController do
end
def show_current_actor(conn, _params) do
user = conn
|> Guardian.Plug.current_resource()
|> Repo.preload(:actors)
user =
conn
|> Guardian.Plug.current_resource()
|> Repo.preload(:actors)
render(conn, "show_simple.json", user: user)
end
@@ -101,14 +110,13 @@ defmodule EventosWeb.UserController do
|> Enum.map(fn {field, detail} ->
"#{field} " <> render_detail(detail)
end)
|> Enum.join
|> Enum.join()
end
defp render_detail({message, values}) do
Enum.reduce values, message, fn {k, v}, acc ->
Enum.reduce(values, message, fn {k, v}, acc ->
String.replace(acc, "%{#{k}}", to_string(v))
end
end)
end
defp render_detail(message) do
@@ -125,6 +133,7 @@ defmodule EventosWeb.UserController do
def delete(conn, %{"id" => id}) do
user = Actors.get_user!(id)
with {:ok, %User{}} <- Actors.delete_user(user) do
send_resp(conn, :no_content, "")
end

View File

@@ -10,17 +10,22 @@ defmodule EventosWeb.UserSessionController do
with {:ok, %User{} = user} <- Actors.find_by_email(email),
{:ok, %User{} = _user} <- User.is_confirmed(user),
{:ok, token, _claims} <- Actors.authenticate(%{user: user, password: password}) do
# Render the token
render conn, "token.json", %{token: token, user: user}
# Render the token
render(conn, "token.json", %{token: token, user: user})
else
{:error, :not_found} ->
conn
|> put_status(401)
|> json(%{"error_msg" => "No such user", "display_error" => "session.error.bad_login"})
{:error, :unconfirmed} ->
conn
|> put_status(401)
|> json(%{"error_msg" => "User is not activated", "display_error" => "session.error.not_activated"})
|> json(%{
"error_msg" => "User is not activated",
"display_error" => "session.error.not_activated"
})
{:error, :unauthorized} ->
conn
|> put_status(401)