Add Credo checks and refactor code

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Make Logger.debug calls lazy

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Add missing @moduledocs

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Refactor according to credo

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Final fixes and add credo to CI

Signed-off-by: Thomas Citharel <tcit@tcit.fr>

Closes #52
This commit is contained in:
Thomas Citharel
2019-01-03 14:59:59 +01:00
parent ea82d392e9
commit 2f2c538cc9
27 changed files with 163 additions and 121 deletions

View File

@@ -63,7 +63,7 @@ defmodule Mix.Tasks.Mobilizon.Instance do
will_overwrite = Enum.filter(paths, &File.exists?/1)
proceed? = Enum.empty?(will_overwrite) or Keyword.get(options, :force, false)
unless not proceed? do
if proceed? do
[domain, port | _] =
String.split(
Common.get_option(
@@ -163,7 +163,7 @@ defmodule Mix.Tasks.Mobilizon.Instance do
else
Mix.shell().error(
"The task would have overwritten the following files:\n" <>
(Enum.map(will_overwrite, &"- #{&1}\n") |> Enum.join("")) <>
(will_overwrite |> Enum.map(&"- #{&1}\n") |> Enum.join("")) <>
"Rerun with `-f/--force` to overwrite them."
)
end

View File

@@ -68,7 +68,7 @@ defmodule Mobilizon.Actors do
where: u.id == ^user.id
)
) do
nil -> get_actors_for_user(user) |> hd
nil -> user |> get_actors_for_user() |> hd
actor -> actor
end
end

View File

@@ -1,4 +1,7 @@
defmodule Mobilizon.Actors.Service.Tools do
@moduledoc """
Common functions for actors services
"""
alias Mobilizon.Actors.User
@spec we_can_send_email(User.t(), atom()) :: :ok | {:error, :email_too_soon}

View File

@@ -17,13 +17,18 @@ defmodule MobilizonWeb.API.Comments do
Creates a comment from an actor and a status
"""
@spec create_comment(String.t(), String.t(), String.t()) :: {:ok, Activity.t()} | any()
def create_comment(from_username, status, visibility \\ "public", inReplyToCommentURL \\ nil) do
def create_comment(
from_username,
status,
visibility \\ "public",
in_reply_to_comment_URL \\ nil
) do
with {:local_actor, %Actor{url: url} = actor} <-
{:local_actor, Actors.get_local_actor_by_name(from_username)},
status <- String.trim(status),
mentions <- Formatter.parse_mentions(status),
inReplyToComment <- get_in_reply_to_comment(inReplyToCommentURL),
{to, cc} <- to_for_actor_and_mentions(actor, mentions, inReplyToComment, visibility),
in_reply_to_comment <- get_in_reply_to_comment(in_reply_to_comment_URL),
{to, cc} <- to_for_actor_and_mentions(actor, mentions, in_reply_to_comment, visibility),
tags <- Formatter.parse_tags(status),
content_html <-
make_content_html(
@@ -37,7 +42,7 @@ defmodule MobilizonWeb.API.Comments do
url,
to,
content_html,
inReplyToComment,
in_reply_to_comment,
tags,
cc
) do

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Context do
@moduledoc """
Guardian context for MobilizonWeb
"""
@behaviour Plug
import Plug.Conn

View File

@@ -23,14 +23,12 @@ defmodule MobilizonWeb.ActivityPubController do
def actor(conn, %{"name" => name}) do
with %Actor{} = actor <- Actors.get_local_actor_by_name(name) do
cond do
conn |> get_req_header("accept") |> is_ap_header() ->
conn |> render_ap_actor(actor)
true ->
conn
|> put_resp_content_type("text/html")
|> send_file(200, "priv/static/index.html")
if conn |> get_req_header("accept") |> is_ap_header() do
conn |> render_ap_actor(actor)
else
conn
|> put_resp_content_type("text/html")
|> send_file(200, "priv/static/index.html")
end
else
nil -> {:error, :not_found}

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Resolvers.Category do
@moduledoc """
Handles the category-related GraphQL calls
"""
require Logger
alias Mobilizon.Actors.User

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Resolvers.Comment do
@moduledoc """
Handles the comment-related GraphQL calls
"""
require Logger
alias Mobilizon.Events.Comment
alias Mobilizon.Activity

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Resolvers.Event do
@moduledoc """
Handles the event-related GraphQL calls
"""
alias Mobilizon.Service.ActivityPub
alias Mobilizon.Activity
alias Mobilizon.Actors

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Resolvers.Group do
@moduledoc """
Handles the group-related GraphQL calls
"""
alias Mobilizon.Actors
alias Mobilizon.Actors.{Actor}
alias Mobilizon.Service.ActivityPub

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Resolvers.Person do
@moduledoc """
Handles the person-related GraphQL calls
"""
alias Mobilizon.Actors
alias Mobilizon.Service.ActivityPub

View File

@@ -1,2 +0,0 @@
defmodule MobilizonWeb.Resolvers.Upload do
end

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Resolvers.User do
@moduledoc """
Handles the user-related GraphQL calls
"""
alias Mobilizon.Actors.{User, Actor}
alias Mobilizon.Actors
require Logger

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Schema do
@moduledoc """
GraphQL schema representation
"""
use Absinthe.Schema
import Absinthe.Resolution.Helpers, only: [dataloader: 1]

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.UploadPlug do
@moduledoc """
Plug to intercept uploads
"""
use Plug.Builder
plug(Plug.Static,

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Uploaders.Avatar do
@moduledoc """
Handles avatar uploads
"""
use Arc.Definition
# Include ecto support (requires package arc_ecto installed):

View File

@@ -1,4 +1,7 @@
defmodule MobilizonWeb.Uploaders.Category do
@moduledoc """
Handles file uploads for categories
"""
use Arc.Definition
use Arc.Ecto.Definition

View File

@@ -43,14 +43,7 @@ defmodule Mobilizon.Service.ActivityPub do
def insert(map, local \\ true) when is_map(map) do
with map <- lazy_put_activity_defaults(map),
:ok <- insert_full_object(map) do
object_id =
cond do
is_map(map["object"]) ->
map["object"]["id"]
is_binary(map["object"]) ->
map["id"]
end
object_id = if is_map(map["object"]), do: map["object"]["id"], else: map["id"]
map = if local, do: Map.put(map, "id", "#{object_id}/activity"), else: map
@@ -384,7 +377,7 @@ defmodule Mobilizon.Service.ActivityPub do
{:ok, data} = Transmogrifier.prepare_outgoing(activity.data)
json = Jason.encode!(data)
Logger.debug("Remote inboxes are : #{inspect(remote_inboxes)}")
Logger.debug(fn -> "Remote inboxes are : #{inspect(remote_inboxes)}" end)
Enum.each(remote_inboxes, fn inbox ->
Federator.enqueue(:publish_single_ap, %{

View File

@@ -48,28 +48,35 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
|> fix_tag
end
def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object)
when not is_nil(in_reply_to) and is_bitstring(in_reply_to) do
in_reply_to |> do_fix_in_reply_to(object)
end
def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object)
when not is_nil(in_reply_to) and is_map(in_reply_to) do
if is_bitstring(in_reply_to["id"]) do
in_reply_to["id"] |> do_fix_in_reply_to(object)
end
end
def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object)
when not is_nil(in_reply_to) and is_list(in_reply_to) do
if is_bitstring(Enum.at(in_reply_to, 0)) do
in_reply_to |> Enum.at(0) |> do_fix_in_reply_to(object)
end
end
def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object)
when not is_nil(in_reply_to) do
in_reply_to_id =
cond do
# If the inReplyTo is just an AP ID
is_bitstring(in_reply_to) ->
in_reply_to
Logger.error("inReplyTo ID seem incorrect")
Logger.error(inspect(in_reply_to))
do_fix_in_reply_to("", object)
end
# If the inReplyTo is a object itself
is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
in_reply_to["id"]
# If the inReplyTo is an array
is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
Enum.at(in_reply_to, 0)
true ->
Logger.error("inReplyTo ID seem incorrect")
Logger.error(inspect(in_reply_to))
""
end
def fix_in_reply_to(object), do: object
def do_fix_in_reply_to(in_reply_to_id, object) do
case fetch_obj_helper(in_reply_to_id) do
{:ok, replied_object} ->
object
@@ -85,8 +92,6 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
end
end
def fix_in_reply_to(object), do: object
def fix_attachments(object) do
attachments =
(object["attachment"] || [])
@@ -500,7 +505,7 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do
@spec normalize(String.t()) :: struct() | nil
def get_anything_by_url(url) do
Logger.debug("Getting anything from url #{url}")
Logger.debug(fn -> "Getting anything from url #{url}" end)
get_actor_url(url) || get_event_url(url) || get_comment_url(url)
end

View File

@@ -171,7 +171,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
data =
if Map.has_key?(object_data, "inReplyTo") && object_data["inReplyTo"] != nil &&
object_data["inReplyTo"] != "" do
Logger.debug("Object has inReplyTo #{object_data["inReplyTo"]}")
Logger.debug(fn -> "Object has inReplyTo #{object_data["inReplyTo"]}" end)
case ActivityPub.fetch_object_from_url(object_data["inReplyTo"]) do
# Reply to an event (Comment)

View File

@@ -4,6 +4,9 @@
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/formatter.ex
defmodule Mobilizon.Service.Formatter do
@moduledoc """
Formats input text to structured data, extracts mentions and hashtags
"""
alias Mobilizon.Actors.Actor
alias Mobilizon.Actors