From 8bcfac93a8586c12661427187ba8147dacc28c5b Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Sun, 3 Feb 2019 18:44:18 +0100 Subject: [PATCH] Make credo happy --- .credo.exs | 2 +- lib/mix/tasks/pleroma/user.ex | 2 +- lib/pleroma/stats.ex | 2 +- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- lib/pleroma/web/common_api/utils.ex | 3 +- lib/pleroma/web/federator/federator.ex | 3 +- .../mastodon_api/mastodon_api_controller.ex | 124 +++++++++--------- .../web/mastodon_api/views/account_view.ex | 6 +- .../web/mastodon_api/views/filter_view.ex | 2 +- .../web/mastodon_api/views/status_view.ex | 8 +- .../web/mastodon_api/websocket_handler.ex | 2 +- lib/pleroma/web/media_proxy/controller.ex | 5 +- lib/pleroma/web/media_proxy/media_proxy.ex | 2 +- .../web/nodeinfo/nodeinfo_controller.ex | 11 +- lib/pleroma/web/uploader_controller.ex | 2 +- 15 files changed, 85 insertions(+), 91 deletions(-) diff --git a/.credo.exs b/.credo.exs index b1f2a6164..8a1cc1c9c 100644 --- a/.credo.exs +++ b/.credo.exs @@ -57,7 +57,7 @@ # For some checks, like AliasUsage, you can only customize the priority # Priority values are: `low, normal, high, higher` - {Credo.Check.Design.AliasUsage, priority: :low}, + {Credo.Check.Design.AliasUsage, priority: :low, if_called_more_often_than: 3}, # For others you can set parameters diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index ffc45fd03..5da3edfd2 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -211,7 +211,7 @@ defmodule Mix.Tasks.Pleroma.User do user = Repo.get(User, user.id) - if length(user.following) == 0 do + if Enum.empty?(user.following) do Mix.shell().info("Successfully unsubscribed all followers from #{user.nickname}") end else diff --git a/lib/pleroma/stats.ex b/lib/pleroma/stats.ex index b3566ceb6..16cc2856a 100644 --- a/lib/pleroma/stats.ex +++ b/lib/pleroma/stats.ex @@ -23,7 +23,7 @@ defmodule Pleroma.Stats do def schedule_update do spawn(fn -> # 1 hour - Process.sleep(1000 * 60 * 60 * 1) + Process.sleep(1000 * 60 * 60) schedule_update() end) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index b33912721..e1cd2d34c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -119,7 +119,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do activity.data["object"] |> Map.get("tag", []) |> Enum.filter(fn tag -> is_bitstring(tag) end) - |> Enum.map(fn tag -> Pleroma.Web.Streamer.stream("hashtag:" <> tag, activity) end) + |> Enum.each(fn tag -> Pleroma.Web.Streamer.stream("hashtag:" <> tag, activity) end) if activity.data["object"]["attachment"] != [] do Pleroma.Web.Streamer.stream("public:media", activity) diff --git a/lib/pleroma/web/common_api/utils.ex b/lib/pleroma/web/common_api/utils.ex index 208677bd7..84be1d4a3 100644 --- a/lib/pleroma/web/common_api/utils.ex +++ b/lib/pleroma/web/common_api/utils.ex @@ -6,8 +6,7 @@ defmodule Pleroma.Web.CommonAPI.Utils do alias Calendar.Strftime alias Comeonin.Pbkdf2 alias Pleroma.{Activity, Formatter, Object, Repo} - alias Pleroma.User - alias Pleroma.Web + alias Pleroma.{User, Web} alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.Endpoint alias Pleroma.Web.MediaProxy diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex index bb7676cf0..6fe3dd2a0 100644 --- a/lib/pleroma/web/federator/federator.ex +++ b/lib/pleroma/web/federator/federator.ex @@ -25,7 +25,7 @@ defmodule Pleroma.Web.Federator do def start_link do spawn(fn -> # 1 minute - Process.sleep(1000 * 60 * 1) + Process.sleep(1000 * 60) enqueue(:refresh_subscriptions, nil) end) @@ -197,7 +197,6 @@ defmodule Pleroma.Web.Federator do end def handle_cast(m, state) do - IO.inspect("Unknown: #{inspect(m)}, #{inspect(state)}") {:noreply, state} end diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index dbe7c2554..74f1bed4d 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -4,23 +4,22 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do use Pleroma.Web, :controller - alias Pleroma.{Repo, Object, Activity, User, Notification, Stats} + alias Pleroma.{Activity, Config, Filter, Notification, Object, Repo, Stats, User} alias Pleroma.Web + alias Pleroma.Web.{CommonAPI, MediaProxy, Push} + alias Push.Subscription alias Pleroma.Web.MastodonAPI.{ - StatusView, AccountView, - MastodonView, - ListView, FilterView, - PushSubscriptionView + ListView, + MastodonView, + PushSubscriptionView, + StatusView } - alias Pleroma.Web.ActivityPub.ActivityPub - alias Pleroma.Web.ActivityPub.Utils - alias Pleroma.Web.CommonAPI - alias Pleroma.Web.OAuth.{Authorization, Token, App} - alias Pleroma.Web.MediaProxy + alias Pleroma.Web.ActivityPub.{ActivityPub, Utils} + alias Pleroma.Web.OAuth.{App, Authorization, Token} import Ecto.Query require Logger @@ -131,7 +130,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do @mastodon_api_level "2.5.0" def masto_instance(conn, _params) do - instance = Pleroma.Config.get(:instance) + instance = Config.get(:instance) response = %{ uri: Web.base_url(), @@ -227,7 +226,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> Map.put("user", user) activities = - ActivityPub.fetch_activities([user.ap_id | user.following], params) + [user.ap_id | user.following] + |> ActivityPub.fetch_activities(params) |> ActivityPub.contain_timeline(user) |> Enum.reverse() @@ -240,14 +240,12 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do def public_timeline(%{assigns: %{user: user}} = conn, params) do local_only = params["local"] in [true, "True", "true", "1"] - params = + activities = params |> Map.put("type", ["Create", "Announce"]) |> Map.put("local_only", local_only) |> Map.put("blocking_user", user) - - activities = - ActivityPub.fetch_public_activities(params) + |> ActivityPub.fetch_public_activities() |> Enum.reverse() conn @@ -316,6 +314,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do as: :activity ) |> Enum.reverse(), + # credo:disable-for-previous-line Credo.Check.Refactor.PipeChainStart descendants: StatusView.render( "index.json", @@ -324,6 +323,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do as: :activity ) |> Enum.reverse() + # credo:disable-for-previous-line Credo.Check.Refactor.PipeChainStart } json(conn, result) @@ -451,9 +451,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do notifications = Notification.for_user(user, params) result = - Enum.map(notifications, fn x -> - render_notification(user, x) - end) + notifications + |> Enum.map(fn x -> render_notification(user, x) end) |> Enum.filter(& &1) conn @@ -582,7 +581,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do [] |> Enum.map(&String.downcase(&1)) - query_params = + activities = params |> Map.put("type", "Create") |> Map.put("local_only", local_only) @@ -590,9 +589,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> Map.put("tag", tags) |> Map.put("tag_all", tag_all) |> Map.put("tag_reject", tag_reject) - - activities = - ActivityPub.fetch_public_activities(query_params) + |> ActivityPub.fetch_public_activities() |> Enum.reverse() conn @@ -692,7 +689,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do {:ok, _activity} <- ActivityPub.follow(follower, followed), {:ok, follower, followed} <- User.wait_and_refresh( - Pleroma.Config.get([:activitypub, :follow_handshake_timeout]), + Config.get([:activitypub, :follow_handshake_timeout]), follower, followed ) do @@ -821,7 +818,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do tags_path = Web.base_url() <> "/tag/" tags = - String.split(query) + query + |> String.split() |> Enum.uniq() |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end) |> Enum.map(fn tag -> String.slice(tag, 1..-1) end) @@ -843,7 +841,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do statuses = status_search(user, query) tags = - String.split(query) + query + |> String.split() |> Enum.uniq() |> Enum.filter(fn tag -> String.starts_with?(tag, "#") end) |> Enum.map(fn tag -> String.slice(tag, 1..-1) end) @@ -867,14 +866,12 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def favourites(%{assigns: %{user: user}} = conn, params) do - params = + activities = params |> Map.put("type", "Create") |> Map.put("favorited_by", user.ap_id) |> Map.put("blocking_user", user) - - activities = - ActivityPub.fetch_public_activities(params) + |> ActivityPub.fetch_public_activities() |> Enum.reverse() conn @@ -990,12 +987,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do # we must filter the following list for the user to avoid leaking statuses the user # does not actually have permission to see (for more info, peruse security issue #270). - following_to = + activities = following |> Enum.filter(fn x -> x in user.following end) - - activities = - ActivityPub.fetch_activities_bounded(following_to, following, params) + |> ActivityPub.fetch_activities_bounded(following, params) |> Enum.reverse() conn @@ -1017,7 +1012,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do if user && token do mastodon_emoji = mastodonized_emoji() - limit = Pleroma.Config.get([:instance, :limit]) + limit = Config.get([:instance, :limit]) accounts = Map.put(%{}, user.id, AccountView.render("account.json", %{user: user, for: user})) @@ -1041,8 +1036,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do max_toot_chars: limit }, rights: %{ - delete_others_notice: !!user.info.is_moderator, - admin: !!user.info.is_admin + delete_others_notice: present?(user.info.is_moderator), + admin: present?(user.info.is_admin) }, compose: %{ me: "#{user.id}", @@ -1234,7 +1229,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def get_filters(%{assigns: %{user: user}} = conn, _) do - filters = Pleroma.Filter.get_filters(user) + filters = Filter.get_filters(user) res = FilterView.render("filters.json", filters: filters) json(conn, res) end @@ -1243,7 +1238,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do %{assigns: %{user: user}} = conn, %{"phrase" => phrase, "context" => context} = params ) do - query = %Pleroma.Filter{ + query = %Filter{ user_id: user.id, phrase: phrase, context: context, @@ -1252,13 +1247,13 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do # expires_at } - {:ok, response} = Pleroma.Filter.create(query) + {:ok, response} = Filter.create(query) res = FilterView.render("filter.json", filter: response) json(conn, res) end def get_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do - filter = Pleroma.Filter.get(filter_id, user) + filter = Filter.get(filter_id, user) res = FilterView.render("filter.json", filter: filter) json(conn, res) end @@ -1267,7 +1262,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do %{assigns: %{user: user}} = conn, %{"phrase" => phrase, "context" => context, "id" => filter_id} = params ) do - query = %Pleroma.Filter{ + query = %Filter{ user_id: user.id, filter_id: filter_id, phrase: phrase, @@ -1277,32 +1272,32 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do # expires_at } - {:ok, response} = Pleroma.Filter.update(query) + {:ok, response} = Filter.update(query) res = FilterView.render("filter.json", filter: response) json(conn, res) end def delete_filter(%{assigns: %{user: user}} = conn, %{"id" => filter_id}) do - query = %Pleroma.Filter{ + query = %Filter{ user_id: user.id, filter_id: filter_id } - {:ok, _} = Pleroma.Filter.delete(query) + {:ok, _} = Filter.delete(query) json(conn, %{}) end def create_push_subscription(%{assigns: %{user: user, token: token}} = conn, params) do - true = Pleroma.Web.Push.enabled() - Pleroma.Web.Push.Subscription.delete_if_exists(user, token) - {:ok, subscription} = Pleroma.Web.Push.Subscription.create(user, token, params) + true = Push.enabled() + Subscription.delete_if_exists(user, token) + {:ok, subscription} = Subscription.create(user, token, params) view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) json(conn, view) end def get_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do - true = Pleroma.Web.Push.enabled() - subscription = Pleroma.Web.Push.Subscription.get(user, token) + true = Push.enabled() + subscription = Subscription.get(user, token) view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) json(conn, view) end @@ -1311,15 +1306,15 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do %{assigns: %{user: user, token: token}} = conn, params ) do - true = Pleroma.Web.Push.enabled() - {:ok, subscription} = Pleroma.Web.Push.Subscription.update(user, token, params) + true = Push.enabled() + {:ok, subscription} = Subscription.update(user, token, params) view = PushSubscriptionView.render("push_subscription.json", subscription: subscription) json(conn, view) end def delete_push_subscription(%{assigns: %{user: user, token: token}} = conn, _params) do - true = Pleroma.Web.Push.enabled() - {:ok, _response} = Pleroma.Web.Push.Subscription.delete(user, token) + true = Push.enabled() + {:ok, _response} = Subscription.delete(user, token) json(conn, %{}) end @@ -1330,17 +1325,21 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end def suggestions(%{assigns: %{user: user}} = conn, _) do - suggestions = Pleroma.Config.get(:suggestions) + suggestions = Config.get(:suggestions) if Keyword.get(suggestions, :enabled, false) do api = Keyword.get(suggestions, :third_party_engine, "") timeout = Keyword.get(suggestions, :timeout, 5000) limit = Keyword.get(suggestions, :limit, 23) - host = Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host]) + host = Config.get([Pleroma.Web.Endpoint, :url, :host]) user = user.nickname - url = String.replace(api, "{{host}}", host) |> String.replace("{{user}}", user) + + url = + api + |> String.replace("{{host}}", host) + |> String.replace("{{user}}", user) with {:ok, %{status: 200, body: body}} <- @httpoison.get( @@ -1353,8 +1352,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do ] ), {:ok, data} <- Jason.decode(body) do - data2 = - Enum.slice(data, 0, limit) + data = + data + |> Enum.slice(0, limit) |> Enum.map(fn x -> Map.put( x, @@ -1373,7 +1373,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do end) conn - |> json(data2) + |> json(data) else e -> Logger.error("Could not retrieve suggestions at fetch #{url}, #{inspect(e)}") end @@ -1416,4 +1416,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do |> put_status(501) |> json(%{error: "Can't display this activity"}) end + + defp present?(nil), do: false + defp present?(false), do: false + defp present?(_), do: true end diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 0ba4289da..0235f5d5b 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -4,11 +4,11 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do use Pleroma.Web, :view - alias Pleroma.User - alias Pleroma.Web.MastodonAPI.AccountView + + alias Pleroma.{HTML, User} alias Pleroma.Web.CommonAPI.Utils + alias Pleroma.Web.MastodonAPI.AccountView alias Pleroma.Web.MediaProxy - alias Pleroma.HTML def render("accounts.json", %{users: users} = opts) do users diff --git a/lib/pleroma/web/mastodon_api/views/filter_view.ex b/lib/pleroma/web/mastodon_api/views/filter_view.ex index 1052a449d..a685bc7b6 100644 --- a/lib/pleroma/web/mastodon_api/views/filter_view.ex +++ b/lib/pleroma/web/mastodon_api/views/filter_view.ex @@ -4,8 +4,8 @@ defmodule Pleroma.Web.MastodonAPI.FilterView do use Pleroma.Web, :view - alias Pleroma.Web.MastodonAPI.FilterView alias Pleroma.Web.CommonAPI.Utils + alias Pleroma.Web.MastodonAPI.FilterView def render("filters.json", %{filters: filters} = opts) do render_many(filters, FilterView, "filter.json", opts) diff --git a/lib/pleroma/web/mastodon_api/views/status_view.ex b/lib/pleroma/web/mastodon_api/views/status_view.ex index a227d742d..cd030fe54 100644 --- a/lib/pleroma/web/mastodon_api/views/status_view.ex +++ b/lib/pleroma/web/mastodon_api/views/status_view.ex @@ -5,14 +5,10 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do use Pleroma.Web, :view - alias Pleroma.Activity - alias Pleroma.HTML - alias Pleroma.Repo - alias Pleroma.User + alias Pleroma.{Activity, HTML, Repo, User} alias Pleroma.Web.CommonAPI.Utils + alias Pleroma.Web.MastodonAPI.{AccountView, StatusView} alias Pleroma.Web.MediaProxy - alias Pleroma.Web.MastodonAPI.AccountView - alias Pleroma.Web.MastodonAPI.StatusView # TODO: Add cached version. defp get_replied_to_activities(activities) do diff --git a/lib/pleroma/web/mastodon_api/websocket_handler.ex b/lib/pleroma/web/mastodon_api/websocket_handler.ex index c0254c8e6..ce42338a7 100644 --- a/lib/pleroma/web/mastodon_api/websocket_handler.ex +++ b/lib/pleroma/web/mastodon_api/websocket_handler.ex @@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do require Logger alias Pleroma.Web.OAuth.Token - alias Pleroma.{User, Repo} + alias Pleroma.{Repo, User} @behaviour :cowboy_websocket_handler diff --git a/lib/pleroma/web/media_proxy/controller.ex b/lib/pleroma/web/media_proxy/controller.ex index de79cad73..c0552d89f 100644 --- a/lib/pleroma/web/media_proxy/controller.ex +++ b/lib/pleroma/web/media_proxy/controller.ex @@ -4,11 +4,12 @@ defmodule Pleroma.Web.MediaProxy.MediaProxyController do use Pleroma.Web, :controller - alias Pleroma.{Web.MediaProxy, ReverseProxy} + alias Pleroma.ReverseProxy + alias Pleroma.Web.MediaProxy @default_proxy_opts [max_body_length: 25 * 1_048_576, http: [follow_redirect: true]] - def remote(conn, params = %{"sig" => sig64, "url" => url64}) do + def remote(conn, %{"sig" => sig64, "url" => url64} = params) do with config <- Pleroma.Config.get([:media_proxy], []), true <- Keyword.get(config, :enabled, false), {:ok, url} <- MediaProxy.decode_url(sig64, url64), diff --git a/lib/pleroma/web/media_proxy/media_proxy.ex b/lib/pleroma/web/media_proxy/media_proxy.ex index e1eb1472d..1e9da7283 100644 --- a/lib/pleroma/web/media_proxy/media_proxy.ex +++ b/lib/pleroma/web/media_proxy/media_proxy.ex @@ -9,7 +9,7 @@ defmodule Pleroma.Web.MediaProxy do def url(""), do: nil - def url(url = "/" <> _), do: url + def url("/" <> _ = url), do: url def url(url) do config = Application.get_env(:pleroma, :media_proxy, []) diff --git a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex index 21694a5ee..e81de7bbd 100644 --- a/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex +++ b/lib/pleroma/web/nodeinfo/nodeinfo_controller.ex @@ -5,10 +5,7 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do use Pleroma.Web, :controller - alias Pleroma.Stats - alias Pleroma.Web - alias Pleroma.{User, Repo} - alias Pleroma.Config + alias Pleroma.{Config, Repo, Stats, User, Web} alias Pleroma.Web.ActivityPub.MRF plug(Pleroma.Web.FederatingPlug) @@ -32,7 +29,7 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do # returns a nodeinfo 2.0 map, since 2.1 just adds a repository field # under software. - def raw_nodeinfo() do + def raw_nodeinfo do instance = Application.get_env(:pleroma, :instance) media_proxy = Application.get_env(:pleroma, :media_proxy) suggestions = Application.get_env(:pleroma, :suggestions) @@ -66,10 +63,8 @@ defmodule Pleroma.Web.Nodeinfo.NodeinfoController do Config.get([:mrf_user_allowlist], []) |> Enum.into(%{}, fn {k, v} -> {k, length(v)} end) - mrf_transparency = Keyword.get(instance, :mrf_transparency) - federation_response = - if mrf_transparency do + if Keyword.get(instance, :mrf_transparency) do %{ mrf_policies: mrf_policies, mrf_simple: mrf_simple, diff --git a/lib/pleroma/web/uploader_controller.ex b/lib/pleroma/web/uploader_controller.ex index 6c28d1197..5d8a77346 100644 --- a/lib/pleroma/web/uploader_controller.ex +++ b/lib/pleroma/web/uploader_controller.ex @@ -3,7 +3,7 @@ defmodule Pleroma.Web.UploaderController do alias Pleroma.Uploaders.Uploader - def callback(conn, params = %{"upload_path" => upload_path}) do + def callback(conn, %{"upload_path" => upload_path} = params) do process_callback(conn, :global.whereis_name({Uploader, upload_path}), params) end