From 20dfce5c83998a8979bfef98da258f76dd904211 Mon Sep 17 00:00:00 2001 From: miffigriffi Date: Sun, 22 Sep 2019 13:41:24 +0200 Subject: [PATCH 1/4] Fix refactoring opportunities --- lib/mobilizon_web/api/reports.ex | 2 +- lib/mobilizon_web/mime.ex | 6 +- lib/mobilizon_web/plugs/uploaded_media.ex | 3 +- lib/mobilizon_web/resolvers/address.ex | 4 +- lib/mobilizon_web/resolvers/admin.ex | 29 ++++---- lib/mobilizon_web/resolvers/group.ex | 3 +- lib/mobilizon_web/reverse_proxy.ex | 73 ++++++++++--------- lib/mobilizon_web/upload/filter/dedupe.ex | 3 +- lib/service/activity_pub/transmogrifier.ex | 3 +- lib/service/activity_pub/utils.ex | 2 +- lib/service/export/feed.ex | 22 +++--- lib/service/formatter/formatter.ex | 3 +- lib/service/geospatial/addok.ex | 2 +- lib/service/geospatial/photon.ex | 2 +- .../activity_pub/activity_pub_test.exs | 2 +- .../activity_pub/transmogrifier_test.exs | 11 ++- .../controllers/feed_controller_test.exs | 39 ++++++---- .../resolvers/tag_resolver_test.exs | 3 +- 18 files changed, 122 insertions(+), 90 deletions(-) diff --git a/lib/mobilizon_web/api/reports.ex b/lib/mobilizon_web/api/reports.ex index ad5cfa0b..ac7e8ea4 100644 --- a/lib/mobilizon_web/api/reports.ex +++ b/lib/mobilizon_web/api/reports.ex @@ -73,7 +73,7 @@ defmodule MobilizonWeb.API.Reports do defp get_report_comments(%Actor{id: actor_id}, comment_ids) do {:get_report_comments, - Events.list_comments_by_actor_and_ids(actor_id, comment_ids) |> Enum.map(& &1.url)} + actor_id |> Events.list_comments_by_actor_and_ids(comment_ids) |> Enum.map(& &1.url)} end defp get_report_comments(_, _), do: {:get_report_comments, nil} diff --git a/lib/mobilizon_web/mime.ex b/lib/mobilizon_web/mime.ex index 49739915..ac7ff627 100644 --- a/lib/mobilizon_web/mime.ex +++ b/lib/mobilizon_web/mime.ex @@ -47,7 +47,7 @@ defmodule MobilizonWeb.MIME do new_filename = if length(parts) > 1 do - Enum.drop(parts, -1) |> Enum.join(".") + parts |> Enum.drop(-1) |> Enum.join(".") else Enum.join(parts) end @@ -60,7 +60,9 @@ defmodule MobilizonWeb.MIME do new_filename <> "." <> ext true -> - Enum.join([new_filename, String.split(content_type, "/") |> List.last()], ".") + extension = content_type |> String.split("/") |> List.last() + + Enum.join([new_filename, extension], ".") end end diff --git a/lib/mobilizon_web/plugs/uploaded_media.ex b/lib/mobilizon_web/plugs/uploaded_media.ex index 13164a8d..b8d648fb 100644 --- a/lib/mobilizon_web/plugs/uploaded_media.ex +++ b/lib/mobilizon_web/plugs/uploaded_media.ex @@ -60,7 +60,8 @@ defmodule MobilizonWeb.Plugs.UploadedMedia do defp get_media(conn, {:static_dir, directory}, _, opts) do static_opts = - Map.get(opts, :static_plug_opts) + opts + |> Map.get(:static_plug_opts) |> Map.put(:at, [@path]) |> Map.put(:from, directory) diff --git a/lib/mobilizon_web/resolvers/address.ex b/lib/mobilizon_web/resolvers/address.ex index cab23339..3754000a 100644 --- a/lib/mobilizon_web/resolvers/address.ex +++ b/lib/mobilizon_web/resolvers/address.ex @@ -12,7 +12,7 @@ defmodule MobilizonWeb.Resolvers.Address do """ @spec search(map(), map(), map()) :: {:ok, list(Address.t())} def search(_parent, %{query: query, page: _page, limit: _limit}, %{context: %{ip: ip}}) do - country = Geolix.lookup(ip) |> Map.get(:country, nil) + country = ip |> Geolix.lookup() |> Map.get(:country, nil) local_addresses = Task.async(fn -> Addresses.search_addresses(query, country: country) end) @@ -40,7 +40,7 @@ defmodule MobilizonWeb.Resolvers.Address do """ @spec reverse_geocode(map(), map(), map()) :: {:ok, list(Address.t())} def reverse_geocode(_parent, %{longitude: longitude, latitude: latitude}, %{context: %{ip: ip}}) do - country = Geolix.lookup(ip) |> Map.get(:country, nil) + country = ip |> Geolix.lookup() |> Map.get(:country, nil) local_addresses = Task.async(fn -> Addresses.reverse_geocode(longitude, latitude, country: country) end) diff --git a/lib/mobilizon_web/resolvers/admin.ex b/lib/mobilizon_web/resolvers/admin.ex index 944b50dd..f08b2318 100644 --- a/lib/mobilizon_web/resolvers/admin.ex +++ b/lib/mobilizon_web/resolvers/admin.ex @@ -10,26 +10,25 @@ defmodule MobilizonWeb.Resolvers.Admin do alias Mobilizon.Events.Event alias Mobilizon.Service.Statistics - def list_action_logs(_parent, %{page: page, limit: limit}, %{ - context: %{current_user: %User{role: role}} - }) + def list_action_logs( + _parent, + %{page: page, limit: limit}, + %{context: %{current_user: %User{role: role}}} + ) when is_moderator(role) do with action_logs <- Mobilizon.Admin.list_action_logs(page, limit) do action_logs = - Enum.map(action_logs, fn %ActionLog{ - target_type: target_type, - action: action, - actor: actor, - id: id, - inserted_at: inserted_at - } = action_log -> + action_logs + |> Enum.map(fn %ActionLog{ + target_type: target_type, + action: action, + actor: actor, + id: id, + inserted_at: inserted_at + } = action_log -> with data when is_map(data) <- transform_action_log(String.to_existing_atom(target_type), action, action_log) do - Map.merge(data, %{ - actor: actor, - id: id, - inserted_at: inserted_at - }) + Map.merge(data, %{actor: actor, id: id, inserted_at: inserted_at}) end end) |> Enum.filter(& &1) diff --git a/lib/mobilizon_web/resolvers/group.ex b/lib/mobilizon_web/resolvers/group.ex index a7330970..4d163891 100644 --- a/lib/mobilizon_web/resolvers/group.ex +++ b/lib/mobilizon_web/resolvers/group.ex @@ -31,7 +31,8 @@ defmodule MobilizonWeb.Resolvers.Group do def list_groups(_parent, %{page: page, limit: limit}, _resolution) do { :ok, - Actors.list_groups(page, limit) + page + |> Actors.list_groups(limit) |> Enum.map(fn actor -> Person.proxify_pictures(actor) end) } end diff --git a/lib/mobilizon_web/reverse_proxy.ex b/lib/mobilizon_web/reverse_proxy.ex index 30347097..0ca82c0b 100644 --- a/lib/mobilizon_web/reverse_proxy.ex +++ b/lib/mobilizon_web/reverse_proxy.ex @@ -4,12 +4,11 @@ # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/reverse_proxy.ex defmodule MobilizonWeb.ReverseProxy do - @keep_req_headers ~w(accept user-agent accept-encoding cache-control if-modified-since) ++ - ~w(if-unmodified-since if-none-match if-range range) + @keep_req_headers ~w(accept user-agent accept-encoding cache-control + if-modified-since if-unmodified-since if-none-match if-range range) @resp_cache_headers ~w(etag date last-modified cache-control) - @keep_resp_headers @resp_cache_headers ++ - ~w(content-type content-disposition content-encoding content-range) ++ - ~w(accept-ranges vary) + @keep_resp_headers @resp_cache_headers ++ ~w(content-type content-disposition + content-encoding content-range accept-ranges vary) @default_cache_control_header "public, max-age=1209600" @valid_resp_codes [200, 206, 304] @max_read_duration :timer.seconds(30) @@ -21,9 +20,11 @@ defmodule MobilizonWeb.ReverseProxy do MobilizonWeb.ReverseProxy.call(conn, url, options) - It is not meant to be added into a plug pipeline, but to be called from another plug or controller. + It is not meant to be added into a plug pipeline, but to be called from another + plug or controller. - Supports `#{inspect(@methods)}` HTTP methods, and only allows `#{inspect(@valid_resp_codes)}` status codes. + Supports `#{inspect(@methods)}` HTTP methods, and only allows + `#{inspect(@valid_resp_codes)}` status codes. Responses are chunked to the client while downloading from the upstream. @@ -32,34 +33,52 @@ defmodule MobilizonWeb.ReverseProxy do * request: `#{inspect(@keep_req_headers)}` * response: `#{inspect(@keep_resp_headers)}` - If no caching headers (`#{inspect(@resp_cache_headers)}`) are returned by upstream, `cache-control` will be - set to `#{inspect(@default_cache_control_header)}`. + If no caching headers (`#{inspect(@resp_cache_headers)}`) are returned by + upstream, `cache-control` will be set to `#{inspect(@default_cache_control_header)}`. Options: - * `redirect_on_failure` (default `false`). Redirects the client to the real remote URL if there's any HTTP - errors. Any error during body processing will not be redirected as the response is chunked. This may expose - remote URL, clients IPs, …. + * `redirect_on_failure` (default `false`). Redirects the client to the real + remote URL if there's any HTTP errors. Any error during body processing will + not be redirected as the response is chunked. This may expose remote URL, + clients IPs, …. - * `max_body_length` (default `#{inspect(@max_body_length)}`): limits the content length to be approximately the - specified length. It is validated with the `content-length` header and also verified when proxying. + * `max_body_length` (default `#{inspect(@max_body_length)}`): limits the + content length to be approximately the specified length. It is validated with + the `content-length` header and also verified when proxying. - * `max_read_duration` (default `#{inspect(@max_read_duration)}` ms): the total time the connection is allowed to - read from the remote upstream. + * `max_read_duration` (default `#{inspect(@max_read_duration)}` ms): the total + time the connection is allowed to read from the remote upstream. * `inline_content_types`: * `true` will not alter `content-disposition` (up to the upstream), * `false` will add `content-disposition: attachment` to any request, * a list of whitelisted content types - * `keep_user_agent` will forward the client's user-agent to the upstream. This may be useful if the upstream is - doing content transformation (encoding, …) depending on the request. + * `keep_user_agent` will forward the client's user-agent to the upstream. + This may be useful if the upstream is doing content transformation + (encoding, …) depending on the request. * `req_headers`, `resp_headers` additional headers. * `http`: options for [hackney](https://github.com/benoitc/hackney). """ + + import Plug.Conn + + require Logger + + @type option :: + {:keep_user_agent, boolean} + | {:max_read_duration, :timer.time() | :infinity} + | {:max_body_length, non_neg_integer | :infinity} + | {:http, []} + | {:req_headers, [{String.t(), String.t()}]} + | {:resp_headers, [{String.t(), String.t()}]} + | {:inline_content_types, boolean | [String.t()]} + | {:redirect_on_failure, boolean} + @hackney Application.get_env(:mobilizon, :hackney, :hackney) @httpoison Application.get_env(:mobilizon, :httpoison, HTTPoison) @@ -78,20 +97,7 @@ defmodule MobilizonWeb.ReverseProxy do "video/quicktime" ] - require Logger - import Plug.Conn - - @type option() :: - {:keep_user_agent, boolean} - | {:max_read_duration, :timer.time() | :infinity} - | {:max_body_length, non_neg_integer() | :infinity} - | {:http, []} - | {:req_headers, [{String.t(), String.t()}]} - | {:resp_headers, [{String.t(), String.t()}]} - | {:inline_content_types, boolean() | [String.t()]} - | {:redirect_on_failure, boolean()} - - @spec call(Plug.Conn.t(), url :: String.t(), [option()]) :: Plug.Conn.t() + @spec call(Plug.Conn.t(), url :: String.t(), [option]) :: Plug.Conn.t() def call(_conn, _url, _opts \\ []) def call(conn = %{method: method}, url, opts) when method in @methods do @@ -114,7 +120,8 @@ defmodule MobilizonWeb.ReverseProxy do response(conn, client, url, code, headers, opts) else {:ok, code, headers} -> - head_response(conn, url, code, headers, opts) + conn + |> head_response(url, code, headers, opts) |> halt() {:error, {:invalid_http_response, code}} -> diff --git a/lib/mobilizon_web/upload/filter/dedupe.ex b/lib/mobilizon_web/upload/filter/dedupe.ex index 2caa226e..42f1ef63 100644 --- a/lib/mobilizon_web/upload/filter/dedupe.ex +++ b/lib/mobilizon_web/upload/filter/dedupe.ex @@ -11,9 +11,10 @@ defmodule MobilizonWeb.Upload.Filter.Dedupe do alias MobilizonWeb.Upload def filter(%Upload{name: name} = upload) do - extension = String.split(name, ".") |> List.last() + extension = name |> String.split(".") |> List.last() shasum = :crypto.hash(:sha256, File.read!(upload.tempfile)) |> Base.encode16(case: :lower) filename = shasum <> "." <> extension + {:ok, %Upload{upload | id: shasum, path: filename}} end end diff --git a/lib/service/activity_pub/transmogrifier.ex b/lib/service/activity_pub/transmogrifier.ex index fedd980e..a41f538a 100644 --- a/lib/service/activity_pub/transmogrifier.ex +++ b/lib/service/activity_pub/transmogrifier.ex @@ -24,7 +24,8 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do if is_binary(Enum.at(actor, 0)) do Enum.at(actor, 0) else - Enum.find(actor, fn %{"type" => type} -> type in ["Person", "Service", "Application"] end) + actor + |> Enum.find(fn %{"type" => type} -> type in ["Person", "Service", "Application"] end) |> Map.get("id") end end diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index f1a2027b..f8efacbe 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -348,7 +348,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do if is_nil(metadata.options) do res else - options = struct(Mobilizon.Events.EventOptions, metadata.options) |> Map.from_struct() + options = Events.EventOptions |> struct(metadata.options) |> Map.from_struct() Enum.reduce(options, res, fn {key, value}, acc -> (value && Map.put(acc, camelize(key), value)) || diff --git a/lib/service/export/feed.ex b/lib/service/export/feed.ex index 6e5dd492..002ed83a 100644 --- a/lib/service/export/feed.ex +++ b/lib/service/export/feed.ex @@ -57,7 +57,11 @@ defmodule Mobilizon.Service.Export.Feed do @spec build_actor_feed(Actor.t(), list(), boolean()) :: String.t() defp build_actor_feed(%Actor{} = actor, events, public \\ true) do display_name = Actor.display_name(actor) - self_url = Routes.feed_url(Endpoint, :actor, actor.preferred_username, "atom") |> URI.decode() + + self_url = + Endpoint + |> Routes.feed_url(:actor, actor.preferred_username, "atom") + |> URI.decode() title = if public, @@ -66,8 +70,8 @@ defmodule Mobilizon.Service.Export.Feed do # Title uses default instance language feed = - Feed.new( - self_url, + self_url + |> Feed.new( DateTime.utc_now(), Gettext.gettext(MobilizonWeb.Gettext, title, actor: display_name) ) @@ -102,7 +106,8 @@ defmodule Mobilizon.Service.Export.Feed do description = event.description || "" entry = - Entry.new(event.url, event.publish_at || event.inserted_at, event.title) + event.url + |> Entry.new(event.publish_at || event.inserted_at, event.title) |> Entry.link(event.url, rel: "alternate", type: "text/html") |> Entry.content({:cdata, description}, type: "html") |> Entry.published(event.publish_at || event.inserted_at) @@ -156,14 +161,11 @@ defmodule Mobilizon.Service.Export.Feed do # Build an atom feed from actor and it's public events @spec build_user_feed(list(), User.t(), String.t()) :: String.t() defp build_user_feed(events, %User{email: email}, token) do - self_url = Routes.feed_url(Endpoint, :going, token, "atom") |> URI.decode() + self_url = Endpoint |> Routes.feed_url(:going, token, "atom") |> URI.decode() # Title uses default instance language - Feed.new( - self_url, - DateTime.utc_now(), - gettext("Feed for %{email} on Mobilizon", email: email) - ) + self_url + |> Feed.new(DateTime.utc_now(), gettext("Feed for %{email} on Mobilizon", email: email)) |> Feed.link(self_url, rel: "self") |> Feed.generator("Mobilizon", uri: "https://joinmobilizon.org", version: version()) |> Feed.entries(Enum.map(events, &get_entry/1)) diff --git a/lib/service/formatter/formatter.ex b/lib/service/formatter/formatter.ex index 7d57cedd..f6e02cc4 100644 --- a/lib/service/formatter/formatter.ex +++ b/lib/service/formatter/formatter.ex @@ -93,7 +93,8 @@ defmodule Mobilizon.Service.Formatter do end def html_escape(text, "text/plain") do - Regex.split(@link_regex, text, include_captures: true) + @link_regex + |> Regex.split(text, include_captures: true) |> Enum.map_every(2, fn chunk -> {:safe, part} = Phoenix.HTML.html_escape(chunk) part diff --git a/lib/service/geospatial/addok.ex b/lib/service/geospatial/addok.ex index 399ba284..a1b1ec0b 100644 --- a/lib/service/geospatial/addok.ex +++ b/lib/service/geospatial/addok.ex @@ -68,7 +68,7 @@ defmodule Mobilizon.Service.Geospatial.Addok do region: Map.get(properties, "state"), description: Map.get(properties, "name") || street_address(properties), floor: Map.get(properties, "floor"), - geom: Map.get(geometry, "coordinates") |> Provider.coordinates(), + geom: geometry |> Map.get("coordinates") |> Provider.coordinates(), postal_code: Map.get(properties, "postcode"), street: properties |> street_address() } diff --git a/lib/service/geospatial/photon.ex b/lib/service/geospatial/photon.ex index 5e4fcac4..3320b5cc 100644 --- a/lib/service/geospatial/photon.ex +++ b/lib/service/geospatial/photon.ex @@ -70,7 +70,7 @@ defmodule Mobilizon.Service.Geospatial.Photon do region: Map.get(properties, "state"), description: Map.get(properties, "name") || street_address(properties), floor: Map.get(properties, "floor"), - geom: Map.get(geometry, "coordinates") |> Provider.coordinates(), + geom: geometry |> Map.get("coordinates") |> Provider.coordinates(), postal_code: Map.get(properties, "postcode"), street: properties |> street_address() } diff --git a/test/mobilizon/service/activity_pub/activity_pub_test.exs b/test/mobilizon/service/activity_pub/activity_pub_test.exs index 8fa3f1d0..f7ae2c81 100644 --- a/test/mobilizon/service/activity_pub/activity_pub_test.exs +++ b/test/mobilizon/service/activity_pub/activity_pub_test.exs @@ -28,7 +28,7 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do Signature.sign(actor, %{ host: "example.com", "content-length": 15, - digest: Jason.encode!(%{id: "my_id"}) |> Signature.build_digest(), + digest: %{id: "my_id"} |> Jason.encode!() |> Signature.build_digest(), "(request-target)": Signature.generate_request_target("POST", "/inbox"), date: Signature.generate_date_header() }) diff --git a/test/mobilizon/service/activity_pub/transmogrifier_test.exs b/test/mobilizon/service/activity_pub/transmogrifier_test.exs index d3749954..b7ed9d3b 100644 --- a/test/mobilizon/service/activity_pub/transmogrifier_test.exs +++ b/test/mobilizon/service/activity_pub/transmogrifier_test.exs @@ -784,7 +784,9 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do assert :error == Transmogrifier.handle_incoming(reject_data) # Organiser is not present since we use factories directly - assert Events.list_participants_for_event(event.id) |> Enum.map(& &1.id) == + assert event.id + |> Events.list_participants_for_event() + |> Enum.map(& &1.id) == [] end @@ -812,9 +814,10 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do assert activity.data["actor"] == participant_url # The only participant left is the organizer - assert Events.list_participants_for_event(event.id) |> Enum.map(& &1.id) == [ - organizer_participation.id - ] + assert event.id + |> Events.list_participants_for_event() + |> Enum.map(& &1.id) == + [organizer_participation.id] end test "it refuses Leave activities when actor is the only organizer" do diff --git a/test/mobilizon_web/controllers/feed_controller_test.exs b/test/mobilizon_web/controllers/feed_controller_test.exs index 5ea66416..0851b40c 100644 --- a/test/mobilizon_web/controllers/feed_controller_test.exs +++ b/test/mobilizon_web/controllers/feed_controller_test.exs @@ -16,7 +16,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn = conn |> get( - Routes.feed_url(Endpoint, :actor, actor.preferred_username, "atom") + Endpoint + |> Routes.feed_url(:actor, actor.preferred_username, "atom") |> URI.decode() ) @@ -48,7 +49,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn = conn |> get( - Routes.feed_url(Endpoint, :actor, actor.preferred_username, "atom") + Endpoint + |> Routes.feed_url(:actor, actor.preferred_username, "atom") |> URI.decode() ) @@ -63,7 +65,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn |> put_req_header("accept", "application/atom+xml") |> get( - Routes.feed_url(Endpoint, :actor, actor.preferred_username, "atom") + Endpoint + |> Routes.feed_url(:actor, actor.preferred_username, "atom") |> URI.decode() ) @@ -93,7 +96,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn = conn |> get( - Routes.feed_url(Endpoint, :actor, actor.preferred_username, "ics") + Endpoint + |> Routes.feed_url(:actor, actor.preferred_username, "ics") |> URI.decode() ) @@ -121,7 +125,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn = conn |> get( - Routes.feed_url(Endpoint, :actor, actor.preferred_username, "ics") + Endpoint + |> Routes.feed_url(:actor, actor.preferred_username, "ics") |> URI.decode() ) @@ -136,7 +141,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn |> put_req_header("accept", "text/calendar") |> get( - Routes.feed_url(Endpoint, :actor, actor.preferred_username, "ics") + Endpoint + |> Routes.feed_url(:actor, actor.preferred_username, "ics") |> URI.decode() ) @@ -163,7 +169,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn = conn |> get( - Routes.feed_url(Endpoint, :event, event1.uuid, "ics") + Endpoint + |> Routes.feed_url(:event, event1.uuid, "ics") |> URI.decode() ) @@ -194,7 +201,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn = conn |> get( - Routes.feed_url(Endpoint, :going, feed_token.token, "atom") + Endpoint + |> Routes.feed_url(:going, feed_token.token, "atom") |> URI.decode() ) @@ -228,7 +236,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn |> put_req_header("accept", "application/atom+xml") |> get( - Routes.feed_url(Endpoint, :going, feed_token.token, "atom") + Endpoint + |> Routes.feed_url(:going, feed_token.token, "atom") |> URI.decode() ) @@ -247,7 +256,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn = conn |> get( - Routes.feed_url(Endpoint, :going, "not existing", "atom") + Endpoint + |> Routes.feed_url(:going, "not existing", "atom") |> URI.decode() ) @@ -272,7 +282,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn |> put_req_header("accept", "text/calendar") |> get( - Routes.feed_url(Endpoint, :going, feed_token.token, "ics") + Endpoint + |> Routes.feed_url(:going, feed_token.token, "ics") |> URI.decode() ) @@ -302,7 +313,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn |> put_req_header("accept", "text/calendar") |> get( - Routes.feed_url(Endpoint, :going, feed_token.token, "ics") + Endpoint + |> Routes.feed_url(:going, feed_token.token, "ics") |> URI.decode() ) @@ -318,7 +330,8 @@ defmodule MobilizonWeb.FeedControllerTest do conn = conn |> get( - Routes.feed_url(Endpoint, :going, "not existing", "ics") + Endpoint + |> Routes.feed_url(:going, "not existing", "ics") |> URI.decode() ) diff --git a/test/mobilizon_web/resolvers/tag_resolver_test.exs b/test/mobilizon_web/resolvers/tag_resolver_test.exs index ee84f0ee..ac288027 100644 --- a/test/mobilizon_web/resolvers/tag_resolver_test.exs +++ b/test/mobilizon_web/resolvers/tag_resolver_test.exs @@ -33,7 +33,8 @@ defmodule MobilizonWeb.Resolvers.TagResolverTest do tags = json_response(res, 200)["data"]["tags"] assert tags |> length == 3 - assert Enum.filter(tags, fn tag -> tag["slug"] == tag1.slug end) + assert tags + |> Enum.filter(fn tag -> tag["slug"] == tag1.slug end) |> hd |> Map.get("related") |> Enum.map(fn tag -> tag["slug"] end) From aed824f1aa1ad4acb132131726e8e26ca9ff00a3 Mon Sep 17 00:00:00 2001 From: miffigriffi Date: Sun, 22 Sep 2019 16:26:23 +0200 Subject: [PATCH 2/4] Fix code readability issues --- lib/mix/tasks/mobilizon/common.ex | 14 +++++-- lib/mix/tasks/mobilizon/create_bot.ex | 10 +++-- lib/mix/tasks/mobilizon/instance.ex | 12 +++--- lib/mix/tasks/mobilizon/relay.ex | 12 +++--- lib/mix/tasks/mobilizon/toot.ex | 3 +- lib/mobilizon/actors/actor.ex | 4 +- lib/mobilizon/events/comment.ex | 2 +- lib/mobilizon/events/event.ex | 2 +- lib/mobilizon/events/event_options.ex | 4 +- lib/mobilizon/events/tag.ex | 2 +- lib/mobilizon_web/api/comments.ex | 3 +- lib/mobilizon_web/api/events.ex | 6 ++- lib/mobilizon_web/api/follows.ex | 1 + lib/mobilizon_web/api/groups.ex | 6 ++- lib/mobilizon_web/api/participations.ex | 2 +- lib/mobilizon_web/api/reports.ex | 9 +++-- lib/mobilizon_web/api/utils.ex | 2 +- .../controllers/media_proxy_controller.ex | 2 +- lib/mobilizon_web/resolvers/admin.ex | 10 +++-- lib/mobilizon_web/resolvers/comment.ex | 5 ++- lib/mobilizon_web/resolvers/event.ex | 14 ++++--- lib/mobilizon_web/resolvers/feed_token.ex | 6 ++- lib/mobilizon_web/resolvers/group.ex | 5 ++- lib/mobilizon_web/resolvers/report.ex | 17 +++++---- lib/mobilizon_web/resolvers/user.ex | 8 ++-- lib/mobilizon_web/schema.ex | 7 ++-- lib/mobilizon_web/schema/actors/group.ex | 11 ++++-- lib/mobilizon_web/schema/admin.ex | 9 +++-- lib/mobilizon_web/schema/event.ex | 11 ++++-- lib/mobilizon_web/schema/events/feed_token.ex | 9 +++-- .../schema/events/participant.ex | 9 +++-- .../views/activity_pub/actor_view.ex | 3 +- .../views/json_ld/object_view.ex | 3 +- lib/mobilizon_web/views/page_view.ex | 2 +- lib/service/activity_pub/activity_pub.ex | 24 ++++-------- lib/service/activity_pub/converters/actor.ex | 4 +- .../activity_pub/converters/address.ex | 6 ++- .../activity_pub/converters/comment.ex | 9 +++-- lib/service/activity_pub/converters/event.ex | 28 +++++++------- lib/service/activity_pub/converters/flag.ex | 6 ++- .../activity_pub/converters/participant.ex | 6 ++- lib/service/activity_pub/relay.ex | 4 +- lib/service/activity_pub/transmogrifier.ex | 5 ++- lib/service/activity_pub/utils.ex | 12 ++---- lib/service/admin/action_log_service.ex | 6 +-- lib/service/email_checker.ex | 11 +++--- lib/service/export/feed.ex | 23 +++++------ lib/service/export/icalendar.ex | 8 ++-- lib/service/formatter/formatter.ex | 5 ++- lib/service/geospatial/addok.ex | 6 ++- lib/service/geospatial/geospatial.ex | 14 ++++--- lib/service/geospatial/google_maps.ex | 8 ++-- lib/service/geospatial/map_quest.ex | 4 +- lib/service/geospatial/nominatim.ex | 37 +++++++++--------- lib/service/geospatial/photon.ex | 6 ++- lib/service/geospatial/provider.ex | 38 ++++++++++--------- lib/service/users/activation.ex | 2 +- test/mobilizon/actors/actors_test.exs | 15 +------- test/mobilizon/events/events_test.exs | 18 +-------- .../activity_pub/activity_pub_test.exs | 10 +++-- .../activity_pub/converters/actor_test.exs | 3 +- .../activity_pub/transmogrifier_test.exs | 5 ++- .../service/activity_pub/utils_test.exs | 12 ++++-- .../service/admin/action_log_service_test.exs | 11 ++++-- .../service/geospatial/addok_test.exs | 15 ++++---- .../service/geospatial/google_maps_test.exs | 8 ++-- .../service/geospatial/map_quest_test.exs | 8 ++-- .../service/geospatial/nominatim_test.exs | 8 ++-- .../service/geospatial/photon_test.exs | 8 ++-- test/mobilizon_web/api/report_test.exs | 4 +- test/mobilizon_web/api/search_test.exs | 8 ++-- .../activity_pub_controller_test.exs | 12 ++++-- .../controllers/feed_controller_test.exs | 4 +- .../controllers/page_controller_test.exs | 5 ++- .../resolvers/admin_resolver_test.exs | 8 ++-- .../resolvers/report_resolver_test.exs | 8 ++-- .../resolvers/user_resolver_test.exs | 2 +- test/mobilizon_web/upload_test.exs | 2 +- test/support/factory.ex | 2 +- test/support/mocks/geospatial_mock.ex | 5 ++- test/tasks/relay_test.exs | 10 +++-- 81 files changed, 379 insertions(+), 309 deletions(-) diff --git a/lib/mix/tasks/mobilizon/common.ex b/lib/mix/tasks/mobilizon/common.ex index a511ddc7..074b8d54 100644 --- a/lib/mix/tasks/mobilizon/common.ex +++ b/lib/mix/tasks/mobilizon/common.ex @@ -4,7 +4,9 @@ # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/mix/tasks/pleroma/common.ex defmodule Mix.Tasks.Mobilizon.Common do - @moduledoc "Common functions to be reused in mix tasks" + @moduledoc """ + Common functions to be reused in mix tasks + """ def get_option(options, opt, prompt, defval \\ nil, defname \\ nil) do display = if defname || defval, do: "#{prompt} [#{defname || defval}]", else: "#{prompt}" @@ -13,17 +15,21 @@ defmodule Mix.Tasks.Mobilizon.Common do case Mix.shell().prompt(display) do "\n" -> case defval do - nil -> get_option(options, opt, prompt, defval) - defval -> defval + nil -> + get_option(options, opt, prompt, defval) + + defval -> + defval end opt -> - opt |> String.trim() + String.trim(opt) end end def start_mobilizon do Application.put_env(:phoenix, :serve_endpoints, false, persistent: true) + {:ok, _} = Application.ensure_all_started(:mobilizon) end diff --git a/lib/mix/tasks/mobilizon/create_bot.ex b/lib/mix/tasks/mobilizon/create_bot.ex index 13c32131..0f0f2035 100644 --- a/lib/mix/tasks/mobilizon/create_bot.ex +++ b/lib/mix/tasks/mobilizon/create_bot.ex @@ -1,13 +1,14 @@ defmodule Mix.Tasks.Mobilizon.CreateBot do @moduledoc """ - Creates a bot from a source + Creates a bot from a source. """ use Mix.Task - alias Mobilizon.Actors - alias Mobilizon.Users + + alias Mobilizon.{Actors, Users} alias Mobilizon.Actors.Bot alias Mobilizon.Users.User + require Logger @shortdoc "Register user" @@ -25,7 +26,8 @@ defmodule Mix.Tasks.Mobilizon.CreateBot do }) do bot else - e -> Logger.error(inspect(e)) + error -> + Logger.error(inspect(error)) end end end diff --git a/lib/mix/tasks/mobilizon/instance.ex b/lib/mix/tasks/mobilizon/instance.ex index 99c18388..451997f8 100644 --- a/lib/mix/tasks/mobilizon/instance.ex +++ b/lib/mix/tasks/mobilizon/instance.ex @@ -4,11 +4,6 @@ # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/mix/tasks/pleroma/instance.ex defmodule Mix.Tasks.Mobilizon.Instance do - use Mix.Task - alias Mix.Tasks.Mobilizon.Common - - @shortdoc "Generates a new config" - @preferred_cli_env "prod" @moduledoc """ Generates a new config @@ -31,6 +26,13 @@ defmodule Mix.Tasks.Mobilizon.Instance do - `--dbpass DBPASS` - the password to use for the database connection """ + use Mix.Task + + alias Mix.Tasks.Mobilizon.Common + + @preferred_cli_env "prod" + + @shortdoc "Generates a new config" def run(["gen" | options]) do {options, [], []} = OptionParser.parse( diff --git a/lib/mix/tasks/mobilizon/relay.ex b/lib/mix/tasks/mobilizon/relay.ex index e4db70f1..989c7ea9 100644 --- a/lib/mix/tasks/mobilizon/relay.ex +++ b/lib/mix/tasks/mobilizon/relay.ex @@ -4,11 +4,6 @@ # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/mix/tasks/pleroma/relay.ex defmodule Mix.Tasks.Mobilizon.Relay do - use Mix.Task - alias Mobilizon.Service.ActivityPub.Relay - alias Mix.Tasks.Mobilizon.Common - - @shortdoc "Manages remote relays" @moduledoc """ Manages remote relays @@ -24,6 +19,13 @@ defmodule Mix.Tasks.Mobilizon.Relay do Example: ``mix mobilizon.relay unfollow https://example.org/relay`` """ + + use Mix.Task + + alias Mix.Tasks.Mobilizon.Common + alias Mobilizon.Service.ActivityPub.Relay + + @shortdoc "Manages remote relays" def run(["follow", target]) do Common.start_mobilizon() diff --git a/lib/mix/tasks/mobilizon/toot.ex b/lib/mix/tasks/mobilizon/toot.ex index 3f9371eb..f93519d9 100644 --- a/lib/mix/tasks/mobilizon/toot.ex +++ b/lib/mix/tasks/mobilizon/toot.ex @@ -1,9 +1,10 @@ defmodule Mix.Tasks.Mobilizon.Toot do @moduledoc """ - Creates a bot from a source + Creates a bot from a source. """ use Mix.Task + require Logger @shortdoc "Toot to an user" diff --git a/lib/mobilizon/actors/actor.ex b/lib/mobilizon/actors/actor.ex index 610c68a7..93928776 100644 --- a/lib/mobilizon/actors/actor.ex +++ b/lib/mobilizon/actors/actor.ex @@ -11,11 +11,11 @@ defmodule Mobilizon.Actors.Actor do alias Mobilizon.Actors.{ActorOpenness, ActorType, ActorVisibility, Follower, Member} alias Mobilizon.Events.{Event, FeedToken} alias Mobilizon.Media.File - alias Mobilizon.Reports.{Report, Note} + alias Mobilizon.Reports.{Note, Report} alias Mobilizon.Users.User - alias MobilizonWeb.Router.Helpers, as: Routes alias MobilizonWeb.Endpoint + alias MobilizonWeb.Router.Helpers, as: Routes require Logger diff --git a/lib/mobilizon/events/comment.ex b/lib/mobilizon/events/comment.ex index a9c9b786..a168ffb4 100644 --- a/lib/mobilizon/events/comment.ex +++ b/lib/mobilizon/events/comment.ex @@ -10,8 +10,8 @@ defmodule Mobilizon.Events.Comment do alias Mobilizon.Actors.Actor alias Mobilizon.Events.{Comment, CommentVisibility, Event} - alias MobilizonWeb.Router.Helpers, as: Routes alias MobilizonWeb.Endpoint + alias MobilizonWeb.Router.Helpers, as: Routes @type t :: %__MODULE__{ text: String.t(), diff --git a/lib/mobilizon/events/event.ex b/lib/mobilizon/events/event.ex index 75233a7d..8a33cf09 100644 --- a/lib/mobilizon/events/event.ex +++ b/lib/mobilizon/events/event.ex @@ -16,8 +16,8 @@ defmodule Mobilizon.Events.Event do EventVisibility, JoinOptions, Participant, - Tag, Session, + Tag, Track } diff --git a/lib/mobilizon/events/event_options.ex b/lib/mobilizon/events/event_options.ex index e4d5f03c..e68a026f 100644 --- a/lib/mobilizon/events/event_options.ex +++ b/lib/mobilizon/events/event_options.ex @@ -8,9 +8,9 @@ defmodule Mobilizon.Events.EventOptions do import Ecto.Changeset alias Mobilizon.Events.{ + CommentModeration, EventOffer, - EventParticipationCondition, - CommentModeration + EventParticipationCondition } @type t :: %__MODULE__{ diff --git a/lib/mobilizon/events/tag.ex b/lib/mobilizon/events/tag.ex index b1d0ec8d..8ea6981f 100644 --- a/lib/mobilizon/events/tag.ex +++ b/lib/mobilizon/events/tag.ex @@ -7,8 +7,8 @@ defmodule Mobilizon.Events.Tag do import Ecto.Changeset - alias Mobilizon.Events.TagRelation alias Mobilizon.Events.Tag.TitleSlug + alias Mobilizon.Events.TagRelation @type t :: %__MODULE__{ title: String.t(), diff --git a/lib/mobilizon_web/api/comments.ex b/lib/mobilizon_web/api/comments.ex index fbef7141..92f186b7 100644 --- a/lib/mobilizon_web/api/comments.ex +++ b/lib/mobilizon_web/api/comments.ex @@ -1,6 +1,6 @@ defmodule MobilizonWeb.API.Comments do @moduledoc """ - API for Comments + API for Comments. """ alias Mobilizon.Actors @@ -8,6 +8,7 @@ defmodule MobilizonWeb.API.Comments do alias Mobilizon.Events.Comment alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils + alias MobilizonWeb.API.Utils @doc """ diff --git a/lib/mobilizon_web/api/events.ex b/lib/mobilizon_web/api/events.ex index 6a0c726c..6e1c93b8 100644 --- a/lib/mobilizon_web/api/events.ex +++ b/lib/mobilizon_web/api/events.ex @@ -1,12 +1,14 @@ defmodule MobilizonWeb.API.Events do @moduledoc """ - API for Events + API for Events. """ + alias Mobilizon.Events.Event alias Mobilizon.Actors.Actor alias Mobilizon.Service.ActivityPub - alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils alias Mobilizon.Service.ActivityPub.Activity + alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils + alias MobilizonWeb.API.Utils @doc """ diff --git a/lib/mobilizon_web/api/follows.ex b/lib/mobilizon_web/api/follows.ex index 4cba7a5e..7979a497 100644 --- a/lib/mobilizon_web/api/follows.ex +++ b/lib/mobilizon_web/api/follows.ex @@ -6,6 +6,7 @@ defmodule MobilizonWeb.API.Follows do alias Mobilizon.Actors alias Mobilizon.Actors.{Actor, Follower} alias Mobilizon.Service.ActivityPub + require Logger def follow(%Actor{} = follower, %Actor{} = followed) do diff --git a/lib/mobilizon_web/api/groups.ex b/lib/mobilizon_web/api/groups.ex index aaac7a6b..9dc78941 100644 --- a/lib/mobilizon_web/api/groups.ex +++ b/lib/mobilizon_web/api/groups.ex @@ -1,12 +1,14 @@ defmodule MobilizonWeb.API.Groups do @moduledoc """ - API for Events + API for Groups. """ + alias Mobilizon.Actors alias Mobilizon.Actors.Actor - alias Mobilizon.Users.User alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub.Utils, as: ActivityPubUtils + alias Mobilizon.Users.User + alias MobilizonWeb.API.Utils @doc """ diff --git a/lib/mobilizon_web/api/participations.ex b/lib/mobilizon_web/api/participations.ex index 859f3b7f..97b6ffaf 100644 --- a/lib/mobilizon_web/api/participations.ex +++ b/lib/mobilizon_web/api/participations.ex @@ -1,6 +1,6 @@ defmodule MobilizonWeb.API.Participations do @moduledoc """ - Common API to join events and groups + Common API to join events and groups. """ alias Mobilizon.Actors.Actor diff --git a/lib/mobilizon_web/api/reports.ex b/lib/mobilizon_web/api/reports.ex index ac7e8ea4..5f74ee1e 100644 --- a/lib/mobilizon_web/api/reports.ex +++ b/lib/mobilizon_web/api/reports.ex @@ -1,18 +1,19 @@ defmodule MobilizonWeb.API.Reports do @moduledoc """ - API for Reports + API for Reports. """ - import MobilizonWeb.API.Utils import Mobilizon.Service.Admin.ActionLogService + import MobilizonWeb.API.Utils + alias Mobilizon.Actors alias Mobilizon.Actors.Actor alias Mobilizon.Events - alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Reports, as: ReportsAction - alias Mobilizon.Reports.{Report, Note} + alias Mobilizon.Reports.{Note, Report} alias Mobilizon.Service.ActivityPub + alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Users alias Mobilizon.Users.User diff --git a/lib/mobilizon_web/api/utils.ex b/lib/mobilizon_web/api/utils.ex index 6e98453f..a6165b8d 100644 --- a/lib/mobilizon_web/api/utils.ex +++ b/lib/mobilizon_web/api/utils.ex @@ -1,6 +1,6 @@ defmodule MobilizonWeb.API.Utils do @moduledoc """ - Utils for API + Utils for API. """ alias Mobilizon.Actors.Actor diff --git a/lib/mobilizon_web/controllers/media_proxy_controller.ex b/lib/mobilizon_web/controllers/media_proxy_controller.ex index 630d8b25..175a9497 100644 --- a/lib/mobilizon_web/controllers/media_proxy_controller.ex +++ b/lib/mobilizon_web/controllers/media_proxy_controller.ex @@ -8,8 +8,8 @@ defmodule MobilizonWeb.MediaProxyController do alias Mobilizon.Config - alias MobilizonWeb.ReverseProxy alias MobilizonWeb.MediaProxy + alias MobilizonWeb.ReverseProxy @default_proxy_opts [max_body_length: 25 * 1_048_576, http: [follow_redirect: true]] diff --git a/lib/mobilizon_web/resolvers/admin.ex b/lib/mobilizon_web/resolvers/admin.ex index f08b2318..b0688d79 100644 --- a/lib/mobilizon_web/resolvers/admin.ex +++ b/lib/mobilizon_web/resolvers/admin.ex @@ -1,14 +1,16 @@ defmodule MobilizonWeb.Resolvers.Admin do @moduledoc """ - Handles the report-related GraphQL calls + Handles the report-related GraphQL calls. """ - alias Mobilizon.Events - alias Mobilizon.Users.User + import Mobilizon.Users.Guards + alias Mobilizon.Admin.ActionLog - alias Mobilizon.Reports.{Report, Note} + alias Mobilizon.Events alias Mobilizon.Events.Event + alias Mobilizon.Reports.{Note, Report} alias Mobilizon.Service.Statistics + alias Mobilizon.Users.User def list_action_logs( _parent, diff --git a/lib/mobilizon_web/resolvers/comment.ex b/lib/mobilizon_web/resolvers/comment.ex index d00b610c..da0eb84f 100644 --- a/lib/mobilizon_web/resolvers/comment.ex +++ b/lib/mobilizon_web/resolvers/comment.ex @@ -1,11 +1,12 @@ defmodule MobilizonWeb.Resolvers.Comment do @moduledoc """ - Handles the comment-related GraphQL calls + Handles the comment-related GraphQL calls. """ alias Mobilizon.Events.Comment - alias Mobilizon.Users.User alias Mobilizon.Service.ActivityPub.Activity + alias Mobilizon.Users.User + alias MobilizonWeb.API.Comments require Logger diff --git a/lib/mobilizon_web/resolvers/event.ex b/lib/mobilizon_web/resolvers/event.ex index 29f058ac..e20e5279 100644 --- a/lib/mobilizon_web/resolvers/event.ex +++ b/lib/mobilizon_web/resolvers/event.ex @@ -1,19 +1,21 @@ defmodule MobilizonWeb.Resolvers.Event do @moduledoc """ - Handles the event-related GraphQL calls + Handles the event-related GraphQL calls. """ + + import Mobilizon.Service.Admin.ActionLogService + + alias Mobilizon.Actors alias Mobilizon.Actors.Actor alias Mobilizon.Addresses alias Mobilizon.Addresses.Address alias Mobilizon.Events alias Mobilizon.Events.{Event, Participant} alias Mobilizon.Media.Picture - alias Mobilizon.Users.User - alias Mobilizon.Actors - alias Mobilizon.Actors.Actor - alias MobilizonWeb.Resolvers.Person alias Mobilizon.Service.ActivityPub.Activity - import Mobilizon.Service.Admin.ActionLogService + alias Mobilizon.Users.User + + alias MobilizonWeb.Resolvers.Person # We limit the max number of events that can be retrieved @event_max_limit 100 diff --git a/lib/mobilizon_web/resolvers/feed_token.ex b/lib/mobilizon_web/resolvers/feed_token.ex index 3d8242a8..53004443 100644 --- a/lib/mobilizon_web/resolvers/feed_token.ex +++ b/lib/mobilizon_web/resolvers/feed_token.ex @@ -1,11 +1,13 @@ defmodule MobilizonWeb.Resolvers.FeedToken do @moduledoc """ - Handles the feed tokens-related GraphQL calls + Handles the feed tokens-related GraphQL calls. """ + alias Mobilizon.Actors.Actor - alias Mobilizon.Users.User alias Mobilizon.Events alias Mobilizon.Events.FeedToken + alias Mobilizon.Users.User + require Logger @doc """ diff --git a/lib/mobilizon_web/resolvers/group.ex b/lib/mobilizon_web/resolvers/group.ex index 4d163891..c2242576 100644 --- a/lib/mobilizon_web/resolvers/group.ex +++ b/lib/mobilizon_web/resolvers/group.ex @@ -1,12 +1,13 @@ defmodule MobilizonWeb.Resolvers.Group do @moduledoc """ - Handles the group-related GraphQL calls + Handles the group-related GraphQL calls. """ + alias Mobilizon.Actors alias Mobilizon.Actors.{Actor, Member} + alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Users.User - alias Mobilizon.Service.ActivityPub alias MobilizonWeb.Resolvers.Person diff --git a/lib/mobilizon_web/resolvers/report.ex b/lib/mobilizon_web/resolvers/report.ex index ff9ce039..0b06622e 100644 --- a/lib/mobilizon_web/resolvers/report.ex +++ b/lib/mobilizon_web/resolvers/report.ex @@ -1,15 +1,18 @@ defmodule MobilizonWeb.Resolvers.Report do @moduledoc """ - Handles the report-related GraphQL calls + Handles the report-related GraphQL calls. """ - alias Mobilizon.Reports - alias Mobilizon.Reports.{Report, Note} - alias Mobilizon.Actors.Actor - alias Mobilizon.Actors - alias Mobilizon.Users.User - alias MobilizonWeb.API.Reports, as: ReportsAPI + import Mobilizon.Users.Guards + alias Mobilizon.Actors + alias Mobilizon.Actors.Actor + alias Mobilizon.Reports + alias Mobilizon.Reports.{Note, Report} + alias Mobilizon.Users.User + + alias MobilizonWeb.API.Reports, as: ReportsAPI + def list_reports( _parent, %{page: page, limit: limit, status: status}, diff --git a/lib/mobilizon_web/resolvers/user.ex b/lib/mobilizon_web/resolvers/user.ex index fbefd49c..6d765e5e 100644 --- a/lib/mobilizon_web/resolvers/user.ex +++ b/lib/mobilizon_web/resolvers/user.ex @@ -1,16 +1,16 @@ defmodule MobilizonWeb.Resolvers.User do @moduledoc """ - Handles the user-related GraphQL calls + Handles the user-related GraphQL calls. """ + import Mobilizon.Users.Guards + alias Mobilizon.{Actors, Config, Users, Events} alias Mobilizon.Actors.Actor - alias Mobilizon.Service.Users.{ResetPassword, Activation} + alias Mobilizon.Service.Users.{Activation, ResetPassword} alias Mobilizon.Users.User alias Mobilizon.Storage.Repo - import Mobilizon.Users.Guards - require Logger @doc """ diff --git a/lib/mobilizon_web/schema.ex b/lib/mobilizon_web/schema.ex index 9eaa2641..93dc840b 100644 --- a/lib/mobilizon_web/schema.ex +++ b/lib/mobilizon_web/schema.ex @@ -1,12 +1,13 @@ defmodule MobilizonWeb.Schema do @moduledoc """ - GraphQL schema representation + GraphQL schema representation. """ + use Absinthe.Schema - alias Mobilizon.{Actors, Events, Users, Addresses, Media, Reports} + alias Mobilizon.{Actors, Addresses, Events, Media, Reports, Users} alias Mobilizon.Actors.{Actor, Follower, Member} - alias Mobilizon.Events.{Event, Comment, Participant} + alias Mobilizon.Events.{Comment, Event, Participant} alias Mobilizon.Storage.Repo import_types(MobilizonWeb.Schema.Custom.UUID) diff --git a/lib/mobilizon_web/schema/actors/group.ex b/lib/mobilizon_web/schema/actors/group.ex index dcf5929d..f88192dd 100644 --- a/lib/mobilizon_web/schema/actors/group.ex +++ b/lib/mobilizon_web/schema/actors/group.ex @@ -1,13 +1,18 @@ defmodule MobilizonWeb.Schema.Actors.GroupType do @moduledoc """ - Schema representation for Group + Schema representation for Group. """ + use Absinthe.Schema.Notation + import Absinthe.Resolution.Helpers, only: [dataloader: 1] - import_types(MobilizonWeb.Schema.Actors.MemberType) - alias MobilizonWeb.Resolvers.{Member, Group} + alias Mobilizon.Events + alias MobilizonWeb.Resolvers.{Group, Member} + + import_types(MobilizonWeb.Schema.Actors.MemberType) + @desc """ Represents a group of actors """ diff --git a/lib/mobilizon_web/schema/admin.ex b/lib/mobilizon_web/schema/admin.ex index 715d7fdd..e73678f9 100644 --- a/lib/mobilizon_web/schema/admin.ex +++ b/lib/mobilizon_web/schema/admin.ex @@ -1,11 +1,14 @@ defmodule MobilizonWeb.Schema.AdminType do @moduledoc """ - Schema representation for ActionLog + Schema representation for ActionLog. """ + use Absinthe.Schema.Notation - alias MobilizonWeb.Resolvers.Admin - alias Mobilizon.Reports.{Report, Note} + alias Mobilizon.Events.Event + alias Mobilizon.Reports.{Note, Report} + + alias MobilizonWeb.Resolvers.Admin @desc "An action log" object :action_log do diff --git a/lib/mobilizon_web/schema/event.ex b/lib/mobilizon_web/schema/event.ex index 913023e5..63084834 100644 --- a/lib/mobilizon_web/schema/event.ex +++ b/lib/mobilizon_web/schema/event.ex @@ -1,14 +1,19 @@ defmodule MobilizonWeb.Schema.EventType do @moduledoc """ - Schema representation for Event + Schema representation for Event. """ + use Absinthe.Schema.Notation - alias Mobilizon.{Actors, Addresses} + import Absinthe.Resolution.Helpers, only: [dataloader: 1] + + alias Mobilizon.{Actors, Addresses} + + alias MobilizonWeb.Resolvers.{Event, Picture, Tag} + import_types(MobilizonWeb.Schema.AddressType) import_types(MobilizonWeb.Schema.Events.ParticipantType) import_types(MobilizonWeb.Schema.TagType) - alias MobilizonWeb.Resolvers.{Picture, Event, Tag} @desc "An event" object :event do diff --git a/lib/mobilizon_web/schema/events/feed_token.ex b/lib/mobilizon_web/schema/events/feed_token.ex index 15cf80e6..ac8ba8b5 100644 --- a/lib/mobilizon_web/schema/events/feed_token.ex +++ b/lib/mobilizon_web/schema/events/feed_token.ex @@ -1,12 +1,15 @@ defmodule MobilizonWeb.Schema.Events.FeedTokenType do @moduledoc """ - Schema representation for Participant + Schema representation for Participant. """ + use Absinthe.Schema.Notation + import Absinthe.Resolution.Helpers, only: [dataloader: 1] + + alias Mobilizon.{Actors, Users} + alias MobilizonWeb.Resolvers - alias Mobilizon.Users - alias Mobilizon.Actors @desc "Represents a participant to an event" object :feed_token do diff --git a/lib/mobilizon_web/schema/events/participant.ex b/lib/mobilizon_web/schema/events/participant.ex index 3dab2406..c70a3ee6 100644 --- a/lib/mobilizon_web/schema/events/participant.ex +++ b/lib/mobilizon_web/schema/events/participant.ex @@ -1,12 +1,15 @@ defmodule MobilizonWeb.Schema.Events.ParticipantType do @moduledoc """ - Schema representation for Participant + Schema representation for Participant. """ + use Absinthe.Schema.Notation + import Absinthe.Resolution.Helpers, only: [dataloader: 1] + + alias Mobilizon.{Actors, Events} + alias MobilizonWeb.Resolvers - alias Mobilizon.Events - alias Mobilizon.Actors @desc "Represents a participant to an event" object :participant do diff --git a/lib/mobilizon_web/views/activity_pub/actor_view.ex b/lib/mobilizon_web/views/activity_pub/actor_view.ex index b37c5848..57160956 100644 --- a/lib/mobilizon_web/views/activity_pub/actor_view.ex +++ b/lib/mobilizon_web/views/activity_pub/actor_view.ex @@ -3,9 +3,8 @@ defmodule MobilizonWeb.ActivityPub.ActorView do alias Mobilizon.Actors alias Mobilizon.Actors.Actor - alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Service.ActivityPub - alias Mobilizon.Service.ActivityPub.Utils + alias Mobilizon.Service.ActivityPub.{Activity, Utils} @private_visibility_empty_collection %{elements: [], total: 0} diff --git a/lib/mobilizon_web/views/json_ld/object_view.ex b/lib/mobilizon_web/views/json_ld/object_view.ex index 9bfb028f..96021a23 100644 --- a/lib/mobilizon_web/views/json_ld/object_view.ex +++ b/lib/mobilizon_web/views/json_ld/object_view.ex @@ -1,9 +1,10 @@ defmodule MobilizonWeb.JsonLD.ObjectView do use MobilizonWeb, :view - alias Mobilizon.Events.Event alias Mobilizon.Actors.Actor alias Mobilizon.Addresses.Address + alias Mobilizon.Events.Event + alias MobilizonWeb.JsonLD.ObjectView alias MobilizonWeb.MediaProxy diff --git a/lib/mobilizon_web/views/page_view.ex b/lib/mobilizon_web/views/page_view.ex index 9064aa4b..27a943ad 100644 --- a/lib/mobilizon_web/views/page_view.ex +++ b/lib/mobilizon_web/views/page_view.ex @@ -84,7 +84,7 @@ defmodule MobilizonWeb.PageView do end end - defp index_file_path() do + defp index_file_path do Path.join(Application.app_dir(:mobilizon, "priv/static"), "index.html") end end diff --git a/lib/service/activity_pub/activity_pub.ex b/lib/service/activity_pub/activity_pub.ex index 604a6016..961cfd91 100644 --- a/lib/service/activity_pub/activity_pub.ex +++ b/lib/service/activity_pub/activity_pub.ex @@ -5,28 +5,19 @@ defmodule Mobilizon.Service.ActivityPub do @moduledoc """ - # ActivityPub - - Every ActivityPub method + # ActivityPub context. """ - alias Mobilizon.Config - alias Mobilizon.Events - alias Mobilizon.Events.{Event, Comment, Participant} - alias Mobilizon.Service.ActivityPub.Transmogrifier - alias Mobilizon.Service.WebFinger + import Mobilizon.Service.ActivityPub.{Utils, Visibility} - alias Mobilizon.Actors + alias Mobilizon.{Actors, Config, Events} alias Mobilizon.Actors.{Actor, Follower} - - alias Mobilizon.Service.Federator + alias Mobilizon.Events.{Comment, Event, Participant} + alias Mobilizon.Service.ActivityPub.{Activity, Convertible, Transmogrifier} + alias Mobilizon.Service.{Federator, WebFinger} alias Mobilizon.Service.HTTPSignatures.Signature - alias Mobilizon.Service.ActivityPub.{Activity, Convertible} - require Logger - import Mobilizon.Service.ActivityPub.Utils - import Mobilizon.Service.ActivityPub.Visibility @doc """ Get recipients for an activity or object @@ -452,7 +443,8 @@ defmodule Mobilizon.Service.ActivityPub do def leave(object, actor, local \\ true) - # TODO: If we want to use this for exclusion we need to have an extra field for the actor that excluded the participant + # TODO: If we want to use this for exclusion we need to have an extra field + # for the actor that excluded the participant def leave( %Event{id: event_id, url: event_url} = event, %Actor{id: actor_id, url: actor_url} = _actor, diff --git a/lib/service/activity_pub/converters/actor.ex b/lib/service/activity_pub/converters/actor.ex index 002e7c3e..69acba8d 100644 --- a/lib/service/activity_pub/converters/actor.ex +++ b/lib/service/activity_pub/converters/actor.ex @@ -2,8 +2,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Actor do @moduledoc """ Actor converter - This module allows to convert events from ActivityStream format to our own internal one, and back + This module allows to convert events from ActivityStream format to our own + internal one, and back. """ + alias Mobilizon.Actors.Actor, as: ActorModel alias Mobilizon.Service.ActivityPub.Converter diff --git a/lib/service/activity_pub/converters/address.ex b/lib/service/activity_pub/converters/address.ex index 1d5ae083..20ce1d73 100644 --- a/lib/service/activity_pub/converters/address.ex +++ b/lib/service/activity_pub/converters/address.ex @@ -1,11 +1,13 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Address do @moduledoc """ - Flag converter + Address converter. - This module allows to convert reports from ActivityStream format to our own internal one, and back. + This module allows to convert reports from ActivityStream format to our own + internal one, and back. Note: Reports are named Flag in AS. """ + alias Mobilizon.Addresses.Address, as: AddressModel alias Mobilizon.Service.ActivityPub.Converter diff --git a/lib/service/activity_pub/converters/comment.ex b/lib/service/activity_pub/converters/comment.ex index 920d699e..0fe596ba 100644 --- a/lib/service/activity_pub/converters/comment.ex +++ b/lib/service/activity_pub/converters/comment.ex @@ -1,14 +1,17 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do @moduledoc """ - Comment converter + Comment converter. - This module allows to convert events from ActivityStream format to our own internal one, and back + This module allows to convert events from ActivityStream format to our own + internal one, and back. """ + alias Mobilizon.Actors.Actor alias Mobilizon.Events.Comment, as: CommentModel alias Mobilizon.Events.Event - alias Mobilizon.Service.ActivityPub.Converter alias Mobilizon.Service.ActivityPub + alias Mobilizon.Service.ActivityPub.Converter + require Logger @behaviour Converter diff --git a/lib/service/activity_pub/converters/event.ex b/lib/service/activity_pub/converters/event.ex index b0586829..ec6c56c9 100644 --- a/lib/service/activity_pub/converters/event.ex +++ b/lib/service/activity_pub/converters/event.ex @@ -1,26 +1,24 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do @moduledoc """ - Event converter + Event converter. - This module allows to convert events from ActivityStream format to our own internal one, and back + This module allows to convert events from ActivityStream format to our own + internal one, and back. """ - alias Mobilizon.Actors - alias Mobilizon.Media - alias Mobilizon.Media.Picture - alias Mobilizon.Actors.Actor - alias Mobilizon.Events.Event, as: EventModel - alias Mobilizon.Service.ActivityPub.Converter - alias Mobilizon.Service.ActivityPub.Converters.Address, as: AddressConverter - alias Mobilizon.Service.ActivityPub.Utils - alias Mobilizon.Events - alias Mobilizon.Events.Tag - alias Mobilizon.Addresses - alias Mobilizon.Addresses.Address - @behaviour Converter + alias Mobilizon.{Actors, Addresses, Events, Media} + alias Mobilizon.Actors.Actor + alias Mobilizon.Addresses.Address + alias Mobilizon.Events.Event, as: EventModel + alias Mobilizon.Events.Tag + alias Mobilizon.Media.Picture + alias Mobilizon.Service.ActivityPub.{Converter, Utils} + alias Mobilizon.Service.ActivityPub.Converters.Address, as: AddressConverter require Logger + @behaviour Converter + @doc """ Converts an AP object data to our internal data structure """ diff --git a/lib/service/activity_pub/converters/flag.ex b/lib/service/activity_pub/converters/flag.ex index fd548270..e81a49a1 100644 --- a/lib/service/activity_pub/converters/flag.ex +++ b/lib/service/activity_pub/converters/flag.ex @@ -1,11 +1,13 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Flag do @moduledoc """ - Flag converter + Flag converter. - This module allows to convert reports from ActivityStream format to our own internal one, and back. + This module allows to convert reports from ActivityStream format to our own + internal one, and back. Note: Reports are named Flag in AS. """ + alias Mobilizon.Actors alias Mobilizon.Actors.Actor alias Mobilizon.Events diff --git a/lib/service/activity_pub/converters/participant.ex b/lib/service/activity_pub/converters/participant.ex index 35de9d85..28fb4fe2 100644 --- a/lib/service/activity_pub/converters/participant.ex +++ b/lib/service/activity_pub/converters/participant.ex @@ -1,11 +1,13 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Participant do @moduledoc """ - Flag converter + Participant converter. - This module allows to convert reports from ActivityStream format to our own internal one, and back. + This module allows to convert reports from ActivityStream format to our own + internal one, and back. Note: Reports are named Flag in AS. """ + alias Mobilizon.Events.Participant, as: ParticipantModel @doc """ diff --git a/lib/service/activity_pub/relay.ex b/lib/service/activity_pub/relay.ex index 7b4396a1..e6686bae 100644 --- a/lib/service/activity_pub/relay.ex +++ b/lib/service/activity_pub/relay.ex @@ -5,13 +5,13 @@ defmodule Mobilizon.Service.ActivityPub.Relay do @moduledoc """ - Handles following and unfollowing relays and instances + Handles following and unfollowing relays and instances. """ alias Mobilizon.Actors alias Mobilizon.Actors.Actor - alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Service.ActivityPub + alias Mobilizon.Service.ActivityPub.Activity alias MobilizonWeb.API.Follows diff --git a/lib/service/activity_pub/transmogrifier.ex b/lib/service/activity_pub/transmogrifier.ex index a41f538a..446dab9c 100644 --- a/lib/service/activity_pub/transmogrifier.ex +++ b/lib/service/activity_pub/transmogrifier.ex @@ -7,12 +7,13 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do @moduledoc """ A module to handle coding from internal to wire ActivityPub and back. """ + alias Mobilizon.Actors alias Mobilizon.Actors.{Actor, Follower} alias Mobilizon.Events - alias Mobilizon.Events.{Event, Comment, Participant} + alias Mobilizon.Events.{Comment, Event, Participant} alias Mobilizon.Service.ActivityPub - alias Mobilizon.Service.ActivityPub.{Visibility, Utils} + alias Mobilizon.Service.ActivityPub.{Utils, Visibility} require Logger diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index f8efacbe..77b59f39 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -5,25 +5,19 @@ defmodule Mobilizon.Service.ActivityPub.Utils do @moduledoc """ - # Utils - - Various utils + # Various ActivityPub related utils. """ alias Ecto.Changeset - alias Mobilizon.Addresses - alias Mobilizon.Addresses.Address - alias Mobilizon.Actors + alias Mobilizon.{Actors, Addresses, Events, Reports, Users} alias Mobilizon.Actors.Actor - alias Mobilizon.Events + alias Mobilizon.Addresses.Address alias Mobilizon.Events.{Comment, Event} alias Mobilizon.Media.Picture - alias Mobilizon.Reports alias Mobilizon.Reports.Report alias Mobilizon.Service.ActivityPub.{Activity, Converters} alias Mobilizon.Storage.Repo - alias Mobilizon.Users alias MobilizonWeb.{Email, Endpoint} alias MobilizonWeb.Router.Helpers, as: Routes diff --git a/lib/service/admin/action_log_service.ex b/lib/service/admin/action_log_service.ex index 6f82ead2..7acbdb3a 100644 --- a/lib/service/admin/action_log_service.ex +++ b/lib/service/admin/action_log_service.ex @@ -1,13 +1,13 @@ defmodule Mobilizon.Service.Admin.ActionLogService do @moduledoc """ - Module to handle action log creations + Module to handle action log creations. """ - alias Mobilizon.Users - alias Mobilizon.Users.User alias Mobilizon.Actors.Actor alias Mobilizon.Admin alias Mobilizon.Admin.ActionLog + alias Mobilizon.Users + alias Mobilizon.Users.User @doc """ Log an admin action diff --git a/lib/service/email_checker.ex b/lib/service/email_checker.ex index 62cbe7ef..f92ec33c 100644 --- a/lib/service/email_checker.ex +++ b/lib/service/email_checker.ex @@ -1,15 +1,14 @@ defmodule Mobilizon.Service.EmailChecker do @moduledoc """ - Provides a function to test emails against a "not so bad" regex + Provides a function to test emails against a "not so bad" regex. """ + # TODO: simplify me! @email_regex ~r/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ @doc """ - Returns whether the email is valid + Returns whether the email is valid. """ - @spec valid?(String.t()) :: boolean() - def valid?(email) do - email =~ @email_regex - end + @spec valid?(String.t()) :: boolean + def valid?(email), do: email =~ @email_regex end diff --git a/lib/service/export/feed.ex b/lib/service/export/feed.ex index 002ed83a..77ff7142 100644 --- a/lib/service/export/feed.ex +++ b/lib/service/export/feed.ex @@ -1,23 +1,24 @@ defmodule Mobilizon.Service.Export.Feed do @moduledoc """ - Serve Atom Syndication Feeds + Serve Atom Syndication Feeds. """ - alias Mobilizon.Users.User - alias Mobilizon.Users - alias Mobilizon.Actors - alias Mobilizon.Actors.Actor - alias Mobilizon.Events - alias Mobilizon.Events.{Event, FeedToken} - alias Atomex.{Feed, Entry} import MobilizonWeb.Gettext + + alias Atomex.{Entry, Feed} + + alias Mobilizon.{Actors, Events, Users} + alias Mobilizon.Actors.Actor + alias Mobilizon.Events.{Event, FeedToken} + alias Mobilizon.Users.User + + alias MobilizonWeb.{Endpoint, MediaProxy} alias MobilizonWeb.Router.Helpers, as: Routes - alias MobilizonWeb.Endpoint - alias MobilizonWeb.MediaProxy + require Logger @version Mix.Project.config()[:version] - def version(), do: @version + def version, do: @version @spec create_cache(String.t()) :: {:commit, String.t()} | {:ignore, any()} def create_cache("actor_" <> name) do diff --git a/lib/service/export/icalendar.ex b/lib/service/export/icalendar.ex index fe053d45..94a465d5 100644 --- a/lib/service/export/icalendar.ex +++ b/lib/service/export/icalendar.ex @@ -1,14 +1,12 @@ defmodule Mobilizon.Service.Export.ICalendar do @moduledoc """ - Export an event to iCalendar format + Export an event to iCalendar format. """ - alias Mobilizon.Events.{Event, FeedToken} - alias Mobilizon.Events + alias Mobilizon.{Actors, Events, Users} alias Mobilizon.Actors.Actor - alias Mobilizon.Actors + alias Mobilizon.Events.{Event, FeedToken} alias Mobilizon.Users.User - alias Mobilizon.Users @doc """ Export a public event to iCalendar format. diff --git a/lib/service/formatter/formatter.ex b/lib/service/formatter/formatter.ex index f6e02cc4..bbff1ef5 100644 --- a/lib/service/formatter/formatter.ex +++ b/lib/service/formatter/formatter.ex @@ -5,10 +5,11 @@ defmodule Mobilizon.Service.Formatter do @moduledoc """ - Formats input text to structured data, extracts mentions and hashtags + Formats input text to structured data, extracts mentions and hashtags. """ - alias Mobilizon.Actors.Actor + alias Mobilizon.Actors + alias Mobilizon.Actors.Actor alias Mobilizon.Service.HTML @link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui diff --git a/lib/service/geospatial/addok.ex b/lib/service/geospatial/addok.ex index a1b1ec0b..b209ec0d 100644 --- a/lib/service/geospatial/addok.ex +++ b/lib/service/geospatial/addok.ex @@ -2,9 +2,11 @@ defmodule Mobilizon.Service.Geospatial.Addok do @moduledoc """ [Addok](https://github.com/addok/addok) backend. """ - alias Mobilizon.Service.Geospatial.Provider - require Logger + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Provider + + require Logger @behaviour Provider diff --git a/lib/service/geospatial/geospatial.ex b/lib/service/geospatial/geospatial.ex index 0321997a..69386a1e 100644 --- a/lib/service/geospatial/geospatial.ex +++ b/lib/service/geospatial/geospatial.ex @@ -1,15 +1,17 @@ defmodule Mobilizon.Service.Geospatial do @moduledoc """ - Module to load the service adapter defined inside the configuration + Module to load the service adapter defined inside the configuration. - See `Mobilizon.Service.Geospatial.Provider` + See `Mobilizon.Service.Geospatial.Provider`. """ @doc """ - Returns the appropriate service adapter + Returns the appropriate service adapter. - According to the config behind `config :mobilizon, Mobilizon.Service.Geospatial, service: Mobilizon.Service.Geospatial.Module` + According to the config behind + `config :mobilizon, Mobilizon.Service.Geospatial, + service: Mobilizon.Service.Geospatial.Module` """ - @spec service() :: module() - def service(), do: Application.get_env(:mobilizon, __MODULE__) |> get_in([:service]) + @spec service :: module + def service, do: get_in(Application.get_env(:mobilizon, __MODULE__), [:service]) end diff --git a/lib/service/geospatial/google_maps.ex b/lib/service/geospatial/google_maps.ex index 7b73475f..72054c09 100644 --- a/lib/service/geospatial/google_maps.ex +++ b/lib/service/geospatial/google_maps.ex @@ -1,11 +1,13 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do @moduledoc """ - Google Maps [Geocoding service](https://developers.google.com/maps/documentation/geocoding/intro) + Google Maps [Geocoding service](https://developers.google.com/maps/documentation/geocoding/intro). - Note: Endpoint is hardcoded to Google Maps API + Note: Endpoint is hardcoded to Google Maps API. """ - alias Mobilizon.Service.Geospatial.Provider + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Provider + require Logger @behaviour Provider diff --git a/lib/service/geospatial/map_quest.ex b/lib/service/geospatial/map_quest.ex index 340d173a..1c45a38b 100644 --- a/lib/service/geospatial/map_quest.ex +++ b/lib/service/geospatial/map_quest.ex @@ -8,8 +8,10 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do * `:open_data` Whether to use [Open Data or Licenced Data](https://developer.mapquest.com/documentation/open/). Defaults to `true` """ - alias Mobilizon.Service.Geospatial.Provider + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Provider + require Logger @behaviour Provider diff --git a/lib/service/geospatial/nominatim.ex b/lib/service/geospatial/nominatim.ex index 029bed62..47dda84f 100644 --- a/lib/service/geospatial/nominatim.ex +++ b/lib/service/geospatial/nominatim.ex @@ -2,8 +2,10 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do @moduledoc """ [Nominatim](https://wiki.openstreetmap.org/wiki/Nominatim) backend. """ - alias Mobilizon.Service.Geospatial.Provider + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Provider + require Logger @behaviour Provider @@ -66,23 +68,22 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do @spec process_data(map()) :: Address.t() defp process_data(%{"address" => address} = body) do - try do - %Address{ - country: Map.get(address, "country"), - locality: Map.get(address, "city"), - region: Map.get(address, "state"), - description: description(body), - floor: Map.get(address, "floor"), - geom: [Map.get(body, "lon"), Map.get(body, "lat")] |> Provider.coordinates(), - postal_code: Map.get(address, "postcode"), - street: street_address(address), - origin_id: "osm:" <> to_string(Map.get(body, "osm_id")) - } - rescue - e in ArgumentError -> - Logger.warn(inspect(e)) - nil - end + %Address{ + country: Map.get(address, "country"), + locality: Map.get(address, "city"), + region: Map.get(address, "state"), + description: description(body), + floor: Map.get(address, "floor"), + geom: [Map.get(body, "lon"), Map.get(body, "lat")] |> Provider.coordinates(), + postal_code: Map.get(address, "postcode"), + street: street_address(address), + origin_id: "osm:" <> to_string(Map.get(body, "osm_id")) + } + rescue + error in ArgumentError -> + Logger.warn(inspect(error)) + + nil end @spec street_address(map()) :: String.t() diff --git a/lib/service/geospatial/photon.ex b/lib/service/geospatial/photon.ex index 3320b5cc..7bf11131 100644 --- a/lib/service/geospatial/photon.ex +++ b/lib/service/geospatial/photon.ex @@ -2,9 +2,11 @@ defmodule Mobilizon.Service.Geospatial.Photon do @moduledoc """ [Photon](https://photon.komoot.de) backend. """ - alias Mobilizon.Service.Geospatial.Provider - require Logger + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Provider + + require Logger @behaviour Provider diff --git a/lib/service/geospatial/provider.ex b/lib/service/geospatial/provider.ex index e79ef2df..e421df7f 100644 --- a/lib/service/geospatial/provider.ex +++ b/lib/service/geospatial/provider.ex @@ -14,11 +14,13 @@ defmodule Mobilizon.Service.Geospatial.Provider do ## Shared options * `:user_agent` User-Agent string to send to the backend. Defaults to `"Mobilizon"` - * `:lang` Lang in which to prefer results. Used as a request parameter or through an `Accept-Language` HTTP header. - Defaults to `"en"`. - * `:limit` Maximum limit for the number of results returned by the backend. Defaults to `10` - * `:api_key` Allows to override the API key (if the backend requires one) set inside the configuration. - * `:endpoint` Allows to override the endpoint set inside the configuration + * `:lang` Lang in which to prefer results. Used as a request parameter or + through an `Accept-Language` HTTP header. Defaults to `"en"`. + * `:limit` Maximum limit for the number of results returned by the backend. + Defaults to `10` + * `:api_key` Allows to override the API key (if the backend requires one) set + inside the configuration. + * `:endpoint` Allows to override the endpoint set inside the configuration. """ alias Mobilizon.Addresses.Address @@ -35,38 +37,40 @@ defmodule Mobilizon.Service.Geospatial.Provider do iex> geocode(48.11, -1.77) %Address{} """ - @callback geocode(longitude :: number(), latitude :: number(), options :: keyword()) :: - list(Address.t()) + @callback geocode(longitude :: number, latitude :: number, options :: keyword) :: [Address.t()] @doc """ Search for an address ## Options - In addition to [the shared options](#module-shared-options), `c:search/2` also accepts the following options: + In addition to [the shared options](#module-shared-options), `c:search/2` also + accepts the following options: - * `coords` Map of coordinates (ex: `%{lon: 48.11, lat: -1.77}`) allowing to give a geographic priority to the search. - Defaults to `nil` + * `coords` Map of coordinates (ex: `%{lon: 48.11, lat: -1.77}`) allowing to + give a geographic priority to the search. Defaults to `nil`. ## Examples iex> search("10 rue Jangot") %Address{} """ - @callback search(address :: String.t(), options :: keyword()) :: list(Address.t()) + @callback search(address :: String.t(), options :: keyword) :: [Address.t()] @doc """ Returns a `Geo.Point` for given coordinates """ - @spec coordinates(list(number()), number()) :: Geo.Point.t() + @spec coordinates([number], number) :: Geo.Point.t() def coordinates(coords, srid \\ 4326) - def coordinates([x, y], srid) when is_number(x) and is_number(y), - do: %Geo.Point{coordinates: {x, y}, srid: srid} + def coordinates([x, y], srid) when is_number(x) and is_number(y) do + %Geo.Point{coordinates: {x, y}, srid: srid} + end - def coordinates([x, y], srid) when is_bitstring(x) and is_bitstring(y), - do: %Geo.Point{coordinates: {String.to_float(x), String.to_float(y)}, srid: srid} + def coordinates([x, y], srid) when is_bitstring(x) and is_bitstring(y) do + %Geo.Point{coordinates: {String.to_float(x), String.to_float(y)}, srid: srid} + end - @spec coordinates(any()) :: nil + @spec coordinates(any) :: nil def coordinates(_, _), do: nil end diff --git a/lib/service/users/activation.ex b/lib/service/users/activation.ex index 1d03da1a..016650ce 100644 --- a/lib/service/users/activation.ex +++ b/lib/service/users/activation.ex @@ -1,9 +1,9 @@ defmodule Mobilizon.Service.Users.Activation do @moduledoc false + alias Mobilizon.Service.Users.Tools alias Mobilizon.Users alias Mobilizon.Users.User - alias Mobilizon.Service.Users.Tools alias MobilizonWeb.Email diff --git a/test/mobilizon/actors/actors_test.exs b/test/mobilizon/actors/actors_test.exs index df5a5755..2727f4d1 100644 --- a/test/mobilizon/actors/actors_test.exs +++ b/test/mobilizon/actors/actors_test.exs @@ -6,7 +6,7 @@ defmodule Mobilizon.ActorsTest do import Mobilizon.Factory alias Mobilizon.{Actors, Config, Users} - alias Mobilizon.Actors.{Actor, Member, Follower, Bot} + alias Mobilizon.Actors.{Actor, Bot, Follower, Member} alias Mobilizon.Media.File, as: FileModel alias Mobilizon.Service.ActivityPub alias Mobilizon.Storage.Page @@ -311,9 +311,6 @@ defmodule Mobilizon.ActorsTest do end describe "groups" do - alias Mobilizon.Actors - alias Mobilizon.Actors.Actor - @valid_attrs %{ summary: "some description", suspended: true, @@ -356,11 +353,7 @@ defmodule Mobilizon.ActorsTest do end end - alias Mobilizon.Actors - describe "bots" do - alias Mobilizon.Actors.Bot - @valid_attrs %{source: "some source", type: "some type"} @update_attrs %{source: "some updated source", type: "some updated type"} @invalid_attrs %{source: nil, type: nil} @@ -419,9 +412,6 @@ defmodule Mobilizon.ActorsTest do end describe "followers" do - alias Mobilizon.Actors.Follower - alias Mobilizon.Actors.Actor - @valid_attrs %{approved: true} @update_attrs %{approved: false} @invalid_attrs %{approved: nil} @@ -529,9 +519,6 @@ defmodule Mobilizon.ActorsTest do end describe "members" do - alias Mobilizon.Actors.Member - alias Mobilizon.Actors.Actor - @valid_attrs %{role: :member} @update_attrs %{role: :not_approved} @invalid_attrs %{role: nil} diff --git a/test/mobilizon/events/events_test.exs b/test/mobilizon/events/events_test.exs index 5564c4d0..5aabd690 100644 --- a/test/mobilizon/events/events_test.exs +++ b/test/mobilizon/events/events_test.exs @@ -3,7 +3,9 @@ defmodule Mobilizon.EventsTest do import Mobilizon.Factory + alias Mobilizon.Actors.Actor alias Mobilizon.Events + alias Mobilizon.Events.{Comment, Event, Participant, Session, Tag, TagRelation, Track} alias Mobilizon.Storage.Page @event_valid_attrs %{ @@ -17,8 +19,6 @@ defmodule Mobilizon.EventsTest do } describe "events" do - alias Mobilizon.Events.Event - setup do actor = insert(:actor) event = insert(:event, organizer_actor: actor, visibility: :public) @@ -176,8 +176,6 @@ defmodule Mobilizon.EventsTest do end describe "tags" do - alias Mobilizon.Events.Tag - @valid_attrs %{title: "some title"} @update_attrs %{title: "some updated title"} @invalid_attrs %{title: nil} @@ -227,9 +225,6 @@ defmodule Mobilizon.EventsTest do end describe "tags_relations" do - alias Mobilizon.Events.TagRelation - alias Mobilizon.Events.Tag - setup do tag1 = insert(:tag) tag2 = insert(:tag) @@ -302,9 +297,6 @@ defmodule Mobilizon.EventsTest do end describe "participants" do - alias Mobilizon.Events.{Participant, Event} - alias Mobilizon.Actors.Actor - @valid_attrs %{role: :creator} @update_attrs %{role: :moderator} @invalid_attrs %{role: :no_such_role} @@ -369,8 +361,6 @@ defmodule Mobilizon.EventsTest do end describe "sessions" do - alias Mobilizon.Events.Session - @valid_attrs %{ audios_urls: "some audios_urls", language: "some language", @@ -463,8 +453,6 @@ defmodule Mobilizon.EventsTest do end describe "tracks" do - alias Mobilizon.Events.Track - @valid_attrs %{color: "some color", description: "some description", name: "some name"} @update_attrs %{ color: "some updated color", @@ -525,8 +513,6 @@ defmodule Mobilizon.EventsTest do end describe "comments" do - alias Mobilizon.Events.Comment - @valid_attrs %{text: "some text"} @update_attrs %{text: "some updated text"} @invalid_attrs %{text: nil, url: nil} diff --git a/test/mobilizon/service/activity_pub/activity_pub_test.exs b/test/mobilizon/service/activity_pub/activity_pub_test.exs index f7ae2c81..1b7d1048 100644 --- a/test/mobilizon/service/activity_pub/activity_pub_test.exs +++ b/test/mobilizon/service/activity_pub/activity_pub_test.exs @@ -4,17 +4,19 @@ # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/web/activity_pub/activity_pub_test.exs defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do + use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + use Mobilizon.DataCase + import Mock + import Mobilizon.Factory + alias Mobilizon.Actors.Actor alias Mobilizon.Events alias Mobilizon.Events.Event - alias Mobilizon.Actors.Actor - alias Mobilizon.Service.HTTPSignatures.Signature alias Mobilizon.Service.ActivityPub - use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney - import Mock + alias Mobilizon.Service.HTTPSignatures.Signature setup_all do HTTPoison.start() diff --git a/test/mobilizon/service/activity_pub/converters/actor_test.exs b/test/mobilizon/service/activity_pub/converters/actor_test.exs index 242fc938..b9f31b44 100644 --- a/test/mobilizon/service/activity_pub/converters/actor_test.exs +++ b/test/mobilizon/service/activity_pub/converters/actor_test.exs @@ -1,7 +1,8 @@ defmodule Mobilizon.Service.ActivityPub.Converters.ActorTest do use Mobilizon.DataCase - alias Mobilizon.Service.ActivityPub.Converters.Actor, as: ActorConverter + alias Mobilizon.Actors.Actor + alias Mobilizon.Service.ActivityPub.Converters.Actor, as: ActorConverter describe "actor to AS" do test "valid actor to as" do diff --git a/test/mobilizon/service/activity_pub/transmogrifier_test.exs b/test/mobilizon/service/activity_pub/transmogrifier_test.exs index b7ed9d3b..2f2f6e01 100644 --- a/test/mobilizon/service/activity_pub/transmogrifier_test.exs +++ b/test/mobilizon/service/activity_pub/transmogrifier_test.exs @@ -4,6 +4,8 @@ # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/web/activity_pub/transmogrifier_test.exs defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do + use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + use Mobilizon.DataCase import Mobilizon.Factory @@ -13,9 +15,8 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do alias Mobilizon.Events alias Mobilizon.Events.{Comment, Event, Participant} alias Mobilizon.Service.ActivityPub - alias Mobilizon.Service.ActivityPub.{Utils, Activity} + alias Mobilizon.Service.ActivityPub.{Activity, Utils} alias Mobilizon.Service.ActivityPub.Transmogrifier - use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney setup_all do HTTPoison.start() diff --git a/test/mobilizon/service/activity_pub/utils_test.exs b/test/mobilizon/service/activity_pub/utils_test.exs index dacb69bf..7d74df3c 100644 --- a/test/mobilizon/service/activity_pub/utils_test.exs +++ b/test/mobilizon/service/activity_pub/utils_test.exs @@ -1,10 +1,14 @@ defmodule Mobilizon.Service.ActivityPub.UtilsTest do - use Mobilizon.DataCase - import Mobilizon.Factory - alias Mobilizon.Service.ActivityPub.Utils use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney - alias MobilizonWeb.Router.Helpers, as: Routes + + use Mobilizon.DataCase + + import Mobilizon.Factory + + alias Mobilizon.Service.ActivityPub.Utils + alias MobilizonWeb.Endpoint + alias MobilizonWeb.Router.Helpers, as: Routes setup_all do HTTPoison.start() diff --git a/test/mobilizon/service/admin/action_log_service_test.exs b/test/mobilizon/service/admin/action_log_service_test.exs index b96a3eef..8ec336d3 100644 --- a/test/mobilizon/service/admin/action_log_service_test.exs +++ b/test/mobilizon/service/admin/action_log_service_test.exs @@ -1,12 +1,15 @@ defmodule Mobilizon.Service.Admin.ActionLogServiceTest do @moduledoc """ - Test the ActionLogService module + Test the ActionLogService module. """ + use Mobilizon.DataCase - import Mobilizon.Service.Admin.ActionLogService - alias Mobilizon.Reports.{Report, Note} - alias Mobilizon.Admin.ActionLog + import Mobilizon.Factory + import Mobilizon.Service.Admin.ActionLogService + + alias Mobilizon.Admin.ActionLog + alias Mobilizon.Reports.{Note, Report} setup do moderator_user = insert(:user, role: :moderator) diff --git a/test/mobilizon/service/geospatial/addok_test.exs b/test/mobilizon/service/geospatial/addok_test.exs index e28cf7be..d81cfaab 100644 --- a/test/mobilizon/service/geospatial/addok_test.exs +++ b/test/mobilizon/service/geospatial/addok_test.exs @@ -1,13 +1,14 @@ defmodule Mobilizon.Service.Geospatial.AddokTest do - use Mobilizon.DataCase, async: false - alias Mobilizon.Service.Geospatial.Addok - alias Mobilizon.Addresses.Address - - import Mock use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney - @endpoint Application.get_env(:mobilizon, Mobilizon.Service.Geospatial.Addok) - |> get_in([:endpoint]) + use Mobilizon.DataCase, async: false + + import Mock + + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Addok + + @endpoint get_in(Application.get_env(:mobilizon, Addok), [:endpoint]) @fake_endpoint "https://domain.tld" describe "search address" do diff --git a/test/mobilizon/service/geospatial/google_maps_test.exs b/test/mobilizon/service/geospatial/google_maps_test.exs index 8cab2460..c29a3f10 100644 --- a/test/mobilizon/service/geospatial/google_maps_test.exs +++ b/test/mobilizon/service/geospatial/google_maps_test.exs @@ -1,10 +1,12 @@ defmodule Mobilizon.Service.Geospatial.GoogleMapsTest do + use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + use Mobilizon.DataCase, async: false - alias Mobilizon.Service.Geospatial.GoogleMaps - alias Mobilizon.Addresses.Address import Mock - use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.GoogleMaps describe "search address" do test "without API Key triggers an error" do diff --git a/test/mobilizon/service/geospatial/map_quest_test.exs b/test/mobilizon/service/geospatial/map_quest_test.exs index 51d5646a..1176ef31 100644 --- a/test/mobilizon/service/geospatial/map_quest_test.exs +++ b/test/mobilizon/service/geospatial/map_quest_test.exs @@ -1,10 +1,12 @@ defmodule Mobilizon.Service.Geospatial.MapQuestTest do + use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + use Mobilizon.DataCase, async: false - alias Mobilizon.Service.Geospatial.MapQuest - alias Mobilizon.Addresses.Address import Mock - use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.MapQuest describe "search address" do test "without API Key triggers an error" do diff --git a/test/mobilizon/service/geospatial/nominatim_test.exs b/test/mobilizon/service/geospatial/nominatim_test.exs index 11116072..e436afbe 100644 --- a/test/mobilizon/service/geospatial/nominatim_test.exs +++ b/test/mobilizon/service/geospatial/nominatim_test.exs @@ -1,10 +1,12 @@ defmodule Mobilizon.Service.Geospatial.NominatimTest do + use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + use Mobilizon.DataCase, async: false - alias Mobilizon.Service.Geospatial.Nominatim - alias Mobilizon.Addresses.Address import Mock - use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Nominatim describe "search address" do test "produces a valid search address with options" do diff --git a/test/mobilizon/service/geospatial/photon_test.exs b/test/mobilizon/service/geospatial/photon_test.exs index 6d9c3872..630035c6 100644 --- a/test/mobilizon/service/geospatial/photon_test.exs +++ b/test/mobilizon/service/geospatial/photon_test.exs @@ -1,10 +1,12 @@ defmodule Mobilizon.Service.Geospatial.PhotonTest do + use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + use Mobilizon.DataCase, async: false - alias Mobilizon.Service.Geospatial.Photon - alias Mobilizon.Addresses.Address import Mock - use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Photon describe "search address" do test "produces a valid search address with options" do diff --git a/test/mobilizon_web/api/report_test.exs b/test/mobilizon_web/api/report_test.exs index 9ff39b98..26ba77f2 100644 --- a/test/mobilizon_web/api/report_test.exs +++ b/test/mobilizon_web/api/report_test.exs @@ -5,10 +5,10 @@ defmodule MobilizonWeb.API.ReportTest do alias Mobilizon.Actors.Actor alias Mobilizon.Events.{Comment, Event} - alias Mobilizon.Reports.{Report, Note} + alias Mobilizon.Reports.{Note, Report} + alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Users alias Mobilizon.Users.User - alias Mobilizon.Service.ActivityPub.Activity alias MobilizonWeb.API.Reports diff --git a/test/mobilizon_web/api/search_test.exs b/test/mobilizon_web/api/search_test.exs index af26b401..c9acc25a 100644 --- a/test/mobilizon_web/api/search_test.exs +++ b/test/mobilizon_web/api/search_test.exs @@ -1,17 +1,17 @@ defmodule MobilizonWeb.API.SearchTest do use ExUnit.Case, async: false - alias Mobilizon.Events - alias Mobilizon.Events.Event + import Mock + alias Mobilizon.Actors alias Mobilizon.Actors.Actor + alias Mobilizon.Events + alias Mobilizon.Events.Event alias Mobilizon.Service.ActivityPub alias Mobilizon.Storage.Page alias MobilizonWeb.API.Search - import Mock - test "search an user by username" do with_mock ActivityPub, find_or_make_actor_from_nickname: fn "toto@domain.tld" -> {:ok, %Actor{id: 42}} end do diff --git a/test/mobilizon_web/controllers/activity_pub_controller_test.exs b/test/mobilizon_web/controllers/activity_pub_controller_test.exs index c53c420e..30e1f67a 100644 --- a/test/mobilizon_web/controllers/activity_pub_controller_test.exs +++ b/test/mobilizon_web/controllers/activity_pub_controller_test.exs @@ -4,16 +4,20 @@ # Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/test/web/web_finger/web_finger_controller_test.exs defmodule MobilizonWeb.ActivityPubControllerTest do + use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + use MobilizonWeb.ConnCase + import Mobilizon.Factory - alias MobilizonWeb.ActivityPub.ActorView - alias MobilizonWeb.PageView + alias Mobilizon.{Actors, Config} alias Mobilizon.Actors.Actor alias Mobilizon.Service.ActivityPub - use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney - alias MobilizonWeb.Router.Helpers, as: Routes + + alias MobilizonWeb.ActivityPub.ActorView alias MobilizonWeb.Endpoint + alias MobilizonWeb.PageView + alias MobilizonWeb.Router.Helpers, as: Routes setup do conn = build_conn() |> put_req_header("accept", "application/activity+json") diff --git a/test/mobilizon_web/controllers/feed_controller_test.exs b/test/mobilizon_web/controllers/feed_controller_test.exs index 0851b40c..d793586b 100644 --- a/test/mobilizon_web/controllers/feed_controller_test.exs +++ b/test/mobilizon_web/controllers/feed_controller_test.exs @@ -1,8 +1,10 @@ defmodule MobilizonWeb.FeedControllerTest do use MobilizonWeb.ConnCase + import Mobilizon.Factory - alias MobilizonWeb.Router.Helpers, as: Routes + alias MobilizonWeb.Endpoint + alias MobilizonWeb.Router.Helpers, as: Routes describe "/@:preferred_username/feed/atom" do test "it returns an RSS representation of the actor's public events if the actor is publicly visible", diff --git a/test/mobilizon_web/controllers/page_controller_test.exs b/test/mobilizon_web/controllers/page_controller_test.exs index 477a11d2..9077a7af 100644 --- a/test/mobilizon_web/controllers/page_controller_test.exs +++ b/test/mobilizon_web/controllers/page_controller_test.exs @@ -1,9 +1,12 @@ defmodule MobilizonWeb.PageControllerTest do use MobilizonWeb.ConnCase + import Mobilizon.Factory + alias Mobilizon.Actors.Actor - alias MobilizonWeb.Router.Helpers, as: Routes + alias MobilizonWeb.Endpoint + alias MobilizonWeb.Router.Helpers, as: Routes setup do conn = build_conn() |> put_req_header("accept", "text/html") diff --git a/test/mobilizon_web/resolvers/admin_resolver_test.exs b/test/mobilizon_web/resolvers/admin_resolver_test.exs index a1452d48..5078f28b 100644 --- a/test/mobilizon_web/resolvers/admin_resolver_test.exs +++ b/test/mobilizon_web/resolvers/admin_resolver_test.exs @@ -1,12 +1,14 @@ defmodule MobilizonWeb.Resolvers.AdminResolverTest do - alias MobilizonWeb.AbsintheHelpers use MobilizonWeb.ConnCase + import Mobilizon.Factory - alias Mobilizon.Events.Event alias Mobilizon.Actors.Actor + alias Mobilizon.Events.Event + alias Mobilizon.Reports.{Note, Report} alias Mobilizon.Users.User - alias Mobilizon.Reports.{Report, Note} + + alias MobilizonWeb.AbsintheHelpers describe "Resolver: List the action logs" do @note_content "This a note on a report" diff --git a/test/mobilizon_web/resolvers/report_resolver_test.exs b/test/mobilizon_web/resolvers/report_resolver_test.exs index 5a4c24a7..b66f111e 100644 --- a/test/mobilizon_web/resolvers/report_resolver_test.exs +++ b/test/mobilizon_web/resolvers/report_resolver_test.exs @@ -1,12 +1,14 @@ defmodule MobilizonWeb.Resolvers.ReportResolverTest do - alias MobilizonWeb.AbsintheHelpers use MobilizonWeb.ConnCase + import Mobilizon.Factory alias Mobilizon.Actors.Actor - alias Mobilizon.Users.User alias Mobilizon.Events.Event - alias Mobilizon.Reports.{Report, Note} + alias Mobilizon.Reports.{Note, Report} + alias Mobilizon.Users.User + + alias MobilizonWeb.AbsintheHelpers describe "Resolver: Report a content" do test "create_report/3 creates a report", %{conn: conn} do diff --git a/test/mobilizon_web/resolvers/user_resolver_test.exs b/test/mobilizon_web/resolvers/user_resolver_test.exs index fa8a662e..338de03c 100644 --- a/test/mobilizon_web/resolvers/user_resolver_test.exs +++ b/test/mobilizon_web/resolvers/user_resolver_test.exs @@ -9,8 +9,8 @@ defmodule MobilizonWeb.Resolvers.UserResolverTest do alias Mobilizon.{Actors, Config, Users} alias Mobilizon.Actors.Actor alias Mobilizon.Service.Users.ResetPassword - alias Mobilizon.Users.User alias Mobilizon.Users + alias Mobilizon.Users.User alias MobilizonWeb.{AbsintheHelpers, Email} diff --git a/test/mobilizon_web/upload_test.exs b/test/mobilizon_web/upload_test.exs index de41d172..907d6800 100644 --- a/test/mobilizon_web/upload_test.exs +++ b/test/mobilizon_web/upload_test.exs @@ -198,7 +198,7 @@ defmodule Mobilizon.UploadTest do end end - defp upload() do + defp upload do File.cp!("test/fixtures/image.jpg", "test/fixtures/image_tmp.jpg") file = %Plug.Upload{ diff --git a/test/support/factory.ex b/test/support/factory.ex index a11634d3..c866ca6c 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -8,8 +8,8 @@ defmodule Mobilizon.Factory do alias Mobilizon.Actors.Actor alias Mobilizon.Crypto - alias MobilizonWeb.Router.Helpers, as: Routes alias MobilizonWeb.Endpoint + alias MobilizonWeb.Router.Helpers, as: Routes alias MobilizonWeb.Upload def user_factory do diff --git a/test/support/mocks/geospatial_mock.ex b/test/support/mocks/geospatial_mock.ex index f5a3de8b..87004746 100644 --- a/test/support/mocks/geospatial_mock.ex +++ b/test/support/mocks/geospatial_mock.ex @@ -1,9 +1,10 @@ defmodule Mobilizon.Service.Geospatial.Mock do @moduledoc """ - Mock for Geospatial Provider implementations + Mock for Geospatial Provider implementations. """ - alias Mobilizon.Service.Geospatial.Provider + alias Mobilizon.Addresses.Address + alias Mobilizon.Service.Geospatial.Provider @behaviour Provider diff --git a/test/tasks/relay_test.exs b/test/tasks/relay_test.exs index c93e8ad4..26ee7cb2 100644 --- a/test/tasks/relay_test.exs +++ b/test/tasks/relay_test.exs @@ -3,12 +3,14 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Mix.Tasks.Mobilizon.RelayTest do - alias Mobilizon.Actors.{Actor, Follower} - alias Mobilizon.Actors - alias Mobilizon.Service.ActivityPub.Relay - use Mobilizon.DataCase use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney + use Mobilizon.DataCase + + alias Mobilizon.Actors + alias Mobilizon.Actors.{Actor, Follower} + alias Mobilizon.Service.ActivityPub.Relay + describe "running follow" do test "relay is followed" do use_cassette "relay/fetch_relay_follow" do From 0a0d07cf38124afc0e343049dffa719ea1b43d21 Mon Sep 17 00:00:00 2001 From: miffigriffi Date: Sun, 22 Sep 2019 18:29:13 +0200 Subject: [PATCH 3/4] Fix software design suggestions --- lib/mix/tasks/mobilizon/toot.ex | 4 +- lib/mobilizon.ex | 2 +- lib/mobilizon_web/api/reports.ex | 4 +- lib/mobilizon_web/cache/activity_pub.ex | 3 +- .../controllers/media_proxy_controller.ex | 6 +- .../controllers/node_info_controller.ex | 6 +- lib/mobilizon_web/resolvers/event.ex | 5 +- lib/mobilizon_web/resolvers/group.ex | 3 +- lib/mobilizon_web/reverse_proxy.ex | 6 +- lib/mobilizon_web/upload.ex | 8 +- .../views/activity_pub/actor_view.ex | 2 +- lib/mobilizon_web/views/page_view.ex | 8 +- lib/service/activity_pub/activity_pub.ex | 9 +- lib/service/activity_pub/converter.ex | 15 ++-- .../{converters => converter}/actor.ex | 26 +++--- .../{converters => converter}/address.ex | 12 ++- .../{converters => converter}/comment.ex | 24 +++--- .../{converters => converter}/event.ex | 82 +++++++++++++++---- .../{converters => converter}/flag.ex | 35 ++++---- .../activity_pub/converter/participant.ex | 31 +++++++ lib/service/activity_pub/converter/picture.ex | 28 +++++++ .../activity_pub/converters/participant.ex | 31 ------- lib/service/activity_pub/convertible.ex | 10 +++ lib/service/activity_pub/relay.ex | 6 +- lib/service/activity_pub/transmogrifier.ex | 6 +- lib/service/activity_pub/utils.ex | 37 ++++----- .../activity_pub/activity_pub_test.exs | 7 +- .../{converters => converter}/actor_test.exs | 4 +- .../activity_pub/transmogrifier_test.exs | 16 ++-- .../service/activity_pub/utils_test.exs | 4 +- test/mobilizon_web/api/report_test.exs | 3 +- .../controllers/nodeinfo_controller_test.exs | 17 ++-- .../resolvers/admin_resolver_test.exs | 8 +- test/support/channel_case.ex | 10 ++- test/support/conn_case.ex | 13 +-- test/support/data_case.ex | 9 +- 36 files changed, 292 insertions(+), 208 deletions(-) rename lib/service/activity_pub/{converters => converter}/actor.ex (76%) rename lib/service/activity_pub/{converters => converter}/address.ex (85%) rename lib/service/activity_pub/{converters => converter}/comment.ex (84%) rename lib/service/activity_pub/{converters => converter}/event.ex (69%) rename lib/service/activity_pub/{converters => converter}/flag.ex (91%) create mode 100644 lib/service/activity_pub/converter/participant.ex create mode 100644 lib/service/activity_pub/converter/picture.ex delete mode 100644 lib/service/activity_pub/converters/participant.ex create mode 100644 lib/service/activity_pub/convertible.ex rename test/mobilizon/service/activity_pub/{converters => converter}/actor_test.exs (83%) diff --git a/lib/mix/tasks/mobilizon/toot.ex b/lib/mix/tasks/mobilizon/toot.ex index f93519d9..f76ed4cf 100644 --- a/lib/mix/tasks/mobilizon/toot.ex +++ b/lib/mix/tasks/mobilizon/toot.ex @@ -5,13 +5,15 @@ defmodule Mix.Tasks.Mobilizon.Toot do use Mix.Task + alias MobilizonWeb.API + require Logger @shortdoc "Toot to an user" def run([from, content]) do Mix.Task.run("app.start") - case MobilizonWeb.API.Comments.create_comment(from, content) do + case API.Comments.create_comment(from, content) do {:ok, _, _} -> Mix.shell().info("Tooted") diff --git a/lib/mobilizon.ex b/lib/mobilizon.ex index 7b37e7a1..ca6a7df2 100644 --- a/lib/mobilizon.ex +++ b/lib/mobilizon.ex @@ -23,7 +23,7 @@ defmodule Mobilizon do @spec named_version :: String.t() def named_version, do: "#{@name} #{@version}" - @spec user_agent :: String.t(:w) + @spec user_agent :: String.t() def user_agent do info = "#{MobilizonWeb.Endpoint.url()} <#{Config.get([:instance, :email], "")}>" diff --git a/lib/mobilizon_web/api/reports.ex b/lib/mobilizon_web/api/reports.ex index 5f74ee1e..965b33b3 100644 --- a/lib/mobilizon_web/api/reports.ex +++ b/lib/mobilizon_web/api/reports.ex @@ -11,7 +11,7 @@ defmodule MobilizonWeb.API.Reports do alias Mobilizon.Actors.Actor alias Mobilizon.Events alias Mobilizon.Reports, as: ReportsAction - alias Mobilizon.Reports.{Note, Report} + alias Mobilizon.Reports.{Note, Report, ReportStatus} alias Mobilizon.Service.ActivityPub alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Users @@ -63,7 +63,7 @@ defmodule MobilizonWeb.API.Reports do """ def update_report_status(%Actor{} = actor, %Report{} = report, state) do with {:valid_state, true} <- - {:valid_state, Mobilizon.Reports.ReportStatus.valid_value?(state)}, + {:valid_state, ReportStatus.valid_value?(state)}, {:ok, report} <- ReportsAction.update_report(report, %{"status" => state}), {:ok, _} <- log_action(actor, "update", report) do {:ok, report} diff --git a/lib/mobilizon_web/cache/activity_pub.ex b/lib/mobilizon_web/cache/activity_pub.ex index 06e9e431..d7d5c87d 100644 --- a/lib/mobilizon_web/cache/activity_pub.ex +++ b/lib/mobilizon_web/cache/activity_pub.ex @@ -6,6 +6,7 @@ defmodule MobilizonWeb.Cache.ActivityPub do alias Mobilizon.{Actors, Events, Service} alias Mobilizon.Actors.Actor alias Mobilizon.Events.{Comment, Event} + alias Service.ActivityPub @cache :activity_pub @@ -65,6 +66,6 @@ defmodule MobilizonWeb.Cache.ActivityPub do """ @spec get_relay :: {:commit, Actor.t()} | {:ignore, nil} def get_relay do - Cachex.fetch(@cache, "relay_actor", &Service.ActivityPub.Relay.get_actor/0) + Cachex.fetch(@cache, "relay_actor", &ActivityPub.Relay.get_actor/0) end end diff --git a/lib/mobilizon_web/controllers/media_proxy_controller.ex b/lib/mobilizon_web/controllers/media_proxy_controller.ex index 175a9497..6980cac3 100644 --- a/lib/mobilizon_web/controllers/media_proxy_controller.ex +++ b/lib/mobilizon_web/controllers/media_proxy_controller.ex @@ -6,6 +6,8 @@ defmodule MobilizonWeb.MediaProxyController do use MobilizonWeb, :controller + alias Plug.Conn + alias Mobilizon.Config alias MobilizonWeb.MediaProxy @@ -21,10 +23,10 @@ defmodule MobilizonWeb.MediaProxyController do ReverseProxy.call(conn, url, Keyword.get(config, :proxy_opts, @default_proxy_opts)) else false -> - send_resp(conn, 404, Plug.Conn.Status.reason_phrase(404)) + send_resp(conn, 404, Conn.Status.reason_phrase(404)) {:error, :invalid_signature} -> - send_resp(conn, 403, Plug.Conn.Status.reason_phrase(403)) + send_resp(conn, 403, Conn.Status.reason_phrase(403)) {:wrong_filename, filename} -> redirect(conn, external: MediaProxy.build_url(sig64, url64, filename)) diff --git a/lib/mobilizon_web/controllers/node_info_controller.ex b/lib/mobilizon_web/controllers/node_info_controller.ex index f5df307a..d96f84e6 100644 --- a/lib/mobilizon_web/controllers/node_info_controller.ex +++ b/lib/mobilizon_web/controllers/node_info_controller.ex @@ -9,6 +9,9 @@ defmodule MobilizonWeb.NodeInfoController do alias Mobilizon.Config alias Mobilizon.Service.Statistics + alias MobilizonWeb.Endpoint + alias MobilizonWeb.Router.Helpers, as: Routes + @node_info_supported_versions ["2.0", "2.1"] @node_info_schema_uri "http://nodeinfo.diaspora.software/ns/schema/" @@ -18,8 +21,7 @@ defmodule MobilizonWeb.NodeInfoController do |> Enum.map(fn version -> %{ rel: @node_info_schema_uri <> version, - href: - MobilizonWeb.Router.Helpers.node_info_url(MobilizonWeb.Endpoint, :nodeinfo, version) + href: Routes.node_info_url(Endpoint, :nodeinfo, version) } end) diff --git a/lib/mobilizon_web/resolvers/event.ex b/lib/mobilizon_web/resolvers/event.ex index e20e5279..300fdd26 100644 --- a/lib/mobilizon_web/resolvers/event.ex +++ b/lib/mobilizon_web/resolvers/event.ex @@ -15,6 +15,7 @@ defmodule MobilizonWeb.Resolvers.Event do alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Users.User + alias MobilizonWeb.API alias MobilizonWeb.Resolvers.Person # We limit the max number of events that can be retrieved @@ -144,7 +145,7 @@ defmodule MobilizonWeb.Resolvers.Event do {:has_event, {:ok, %Event{} = event}} <- {:has_event, Mobilizon.Events.get_event_with_preload(event_id)}, {:error, :participant_not_found} <- Mobilizon.Events.get_participant(event_id, actor_id), - {:ok, _activity, participant} <- MobilizonWeb.API.Participations.join(event, actor), + {:ok, _activity, participant} <- API.Participations.join(event, actor), participant <- participant |> Map.put(:event, event) @@ -180,7 +181,7 @@ defmodule MobilizonWeb.Resolvers.Event do with {:is_owned, %Actor{} = actor} <- User.owns_actor(user, actor_id), {:has_event, {:ok, %Event{} = event}} <- {:has_event, Mobilizon.Events.get_event_with_preload(event_id)}, - {:ok, _activity, _participant} <- MobilizonWeb.API.Participations.leave(event, actor) do + {:ok, _activity, _participant} <- API.Participations.leave(event, actor) do {:ok, %{event: %{id: event_id}, actor: %{id: actor_id}}} else {:has_event, _} -> diff --git a/lib/mobilizon_web/resolvers/group.ex b/lib/mobilizon_web/resolvers/group.ex index c2242576..a457c786 100644 --- a/lib/mobilizon_web/resolvers/group.ex +++ b/lib/mobilizon_web/resolvers/group.ex @@ -9,6 +9,7 @@ defmodule MobilizonWeb.Resolvers.Group do alias Mobilizon.Service.ActivityPub.Activity alias Mobilizon.Users.User + alias MobilizonWeb.API alias MobilizonWeb.Resolvers.Person require Logger @@ -51,7 +52,7 @@ defmodule MobilizonWeb.Resolvers.Group do %Activity{data: %{"object" => %{"type" => "Group"} = _object}}, %Actor{} = group } <- - MobilizonWeb.API.Groups.create_group( + API.Groups.create_group( user, %{ preferred_username: args.preferred_username, diff --git a/lib/mobilizon_web/reverse_proxy.ex b/lib/mobilizon_web/reverse_proxy.ex index 0ca82c0b..a56e36b4 100644 --- a/lib/mobilizon_web/reverse_proxy.ex +++ b/lib/mobilizon_web/reverse_proxy.ex @@ -67,6 +67,8 @@ defmodule MobilizonWeb.ReverseProxy do import Plug.Conn + alias Plug.Conn + require Logger @type option :: @@ -131,7 +133,7 @@ defmodule MobilizonWeb.ReverseProxy do |> error_or_redirect( url, code, - "Request failed: " <> Plug.Conn.Status.reason_phrase(code), + "Request failed: " <> Conn.Status.reason_phrase(code), opts ) |> halt() @@ -147,7 +149,7 @@ defmodule MobilizonWeb.ReverseProxy do def call(conn, _, _) do conn - |> send_resp(400, Plug.Conn.Status.reason_phrase(400)) + |> send_resp(400, Conn.Status.reason_phrase(400)) |> halt() end diff --git a/lib/mobilizon_web/upload.ex b/lib/mobilizon_web/upload.ex index 71f16d6e..6c32f289 100644 --- a/lib/mobilizon_web/upload.ex +++ b/lib/mobilizon_web/upload.ex @@ -36,7 +36,7 @@ defmodule MobilizonWeb.Upload do alias Mobilizon.Config - alias MobilizonWeb.MIME + alias MobilizonWeb.{MIME, Upload, Uploaders} require Logger @@ -69,8 +69,8 @@ defmodule MobilizonWeb.Upload do with {:ok, upload} <- prepare_upload(upload, opts), upload = %__MODULE__{upload | path: upload.path || "#{upload.id}/#{upload.name}"}, - {:ok, upload} <- MobilizonWeb.Upload.Filter.filter(opts.filters, upload), - {:ok, url_spec} <- MobilizonWeb.Uploaders.Uploader.put_file(opts.uploader, upload) do + {:ok, upload} <- Upload.Filter.filter(opts.filters, upload), + {:ok, url_spec} <- Uploaders.Uploader.put_file(opts.uploader, upload) do {:ok, %{ "type" => opts.activity_type || get_type(upload.content_type), @@ -98,7 +98,7 @@ defmodule MobilizonWeb.Upload do with opts <- get_opts(opts), %URI{path: "/media/" <> path, host: host} <- URI.parse(url), {:same_host, true} <- {:same_host, host == MobilizonWeb.Endpoint.host()} do - MobilizonWeb.Uploaders.Uploader.remove_file(opts.uploader, path) + Uploaders.Uploader.remove_file(opts.uploader, path) else %URI{} = _uri -> {:error, "URL doesn't match pattern"} diff --git a/lib/mobilizon_web/views/activity_pub/actor_view.ex b/lib/mobilizon_web/views/activity_pub/actor_view.ex index 57160956..e384b95d 100644 --- a/lib/mobilizon_web/views/activity_pub/actor_view.ex +++ b/lib/mobilizon_web/views/activity_pub/actor_view.ex @@ -9,7 +9,7 @@ defmodule MobilizonWeb.ActivityPub.ActorView do @private_visibility_empty_collection %{elements: [], total: 0} def render("actor.json", %{actor: actor}) do - public_key = Mobilizon.Service.ActivityPub.Utils.pem_to_public_key_pem(actor.keys) + public_key = Utils.pem_to_public_key_pem(actor.keys) %{ "id" => actor.url, diff --git a/lib/mobilizon_web/views/page_view.ex b/lib/mobilizon_web/views/page_view.ex index 27a943ad..3037a985 100644 --- a/lib/mobilizon_web/views/page_view.ex +++ b/lib/mobilizon_web/views/page_view.ex @@ -4,12 +4,12 @@ defmodule MobilizonWeb.PageView do """ use MobilizonWeb, :view alias Mobilizon.Actors.Actor - alias Mobilizon.Service.ActivityPub.Utils + alias Mobilizon.Service.ActivityPub.{Converter, Utils} alias Mobilizon.Service.Metadata alias Mobilizon.Service.MetadataUtils def render("actor.activity-json", %{conn: %{assigns: %{object: actor}}}) do - public_key = Mobilizon.Service.ActivityPub.Utils.pem_to_public_key_pem(actor.keys) + public_key = Utils.pem_to_public_key_pem(actor.keys) %{ "id" => Actor.build_url(actor.preferred_username, :page), @@ -47,12 +47,12 @@ defmodule MobilizonWeb.PageView do def render("event.activity-json", %{conn: %{assigns: %{object: event}}}) do event - |> Mobilizon.Service.ActivityPub.Converters.Event.model_to_as() + |> Converter.Event.model_to_as() |> Map.merge(Utils.make_json_ld_header()) end def render("comment.activity-json", %{conn: %{assigns: %{object: comment}}}) do - comment = Mobilizon.Service.ActivityPub.Converters.Comment.model_to_as(comment) + comment = Converter.Comment.model_to_as(comment) %{ "actor" => comment["actor"], diff --git a/lib/service/activity_pub/activity_pub.ex b/lib/service/activity_pub/activity_pub.ex index 961cfd91..79aac27a 100644 --- a/lib/service/activity_pub/activity_pub.ex +++ b/lib/service/activity_pub/activity_pub.ex @@ -13,7 +13,7 @@ defmodule Mobilizon.Service.ActivityPub do alias Mobilizon.{Actors, Config, Events} alias Mobilizon.Actors.{Actor, Follower} alias Mobilizon.Events.{Comment, Event, Participant} - alias Mobilizon.Service.ActivityPub.{Activity, Convertible, Transmogrifier} + alias Mobilizon.Service.ActivityPub.{Activity, Converter, Convertible, Relay, Transmogrifier} alias Mobilizon.Service.{Federator, WebFinger} alias Mobilizon.Service.HTTPSignatures.Signature @@ -535,7 +535,8 @@ defmodule Mobilizon.Service.ActivityPub do if public && !is_delete_activity?(activity) && Config.get([:instance, :allow_relay]) do Logger.info(fn -> "Relaying #{activity.data["id"]} out" end) - Mobilizon.Service.ActivityPub.Relay.publish(activity) + + Relay.publish(activity) end followers = @@ -694,7 +695,7 @@ defmodule Mobilizon.Service.ActivityPub do %Activity{ recipients: ["https://www.w3.org/ns/activitystreams#Public"], actor: event.organizer_actor.url, - data: event |> Mobilizon.Service.ActivityPub.Converters.Event.model_to_as(), + data: Converter.Event.model_to_as(event), local: local } end @@ -705,7 +706,7 @@ defmodule Mobilizon.Service.ActivityPub do %Activity{ recipients: ["https://www.w3.org/ns/activitystreams#Public"], actor: comment.actor.url, - data: comment |> Mobilizon.Service.ActivityPub.Converters.Comment.model_to_as(), + data: Converter.Comment.model_to_as(comment), local: local } end diff --git a/lib/service/activity_pub/converter.ex b/lib/service/activity_pub/converter.ex index 858859db..920efada 100644 --- a/lib/service/activity_pub/converter.ex +++ b/lib/service/activity_pub/converter.ex @@ -1,16 +1,11 @@ defmodule Mobilizon.Service.ActivityPub.Converter do @moduledoc """ - Converter behaviour + Converter behaviour. - This module allows to convert from ActivityStream format to our own internal one, and back + This module allows to convert from ActivityStream format to our own internal + one, and back. """ - @callback as_to_model_data(map()) :: map() - @callback model_to_as(struct()) :: map() -end -defprotocol Mobilizon.Service.ActivityPub.Convertible do - @type activitystreams :: map() - - @spec model_to_as(t) :: activitystreams - def model_to_as(convertible) + @callback as_to_model_data(map) :: map + @callback model_to_as(struct) :: map end diff --git a/lib/service/activity_pub/converters/actor.ex b/lib/service/activity_pub/converter/actor.ex similarity index 76% rename from lib/service/activity_pub/converters/actor.ex rename to lib/service/activity_pub/converter/actor.ex index 69acba8d..d8611c7c 100644 --- a/lib/service/activity_pub/converters/actor.ex +++ b/lib/service/activity_pub/converter/actor.ex @@ -1,21 +1,27 @@ -defmodule Mobilizon.Service.ActivityPub.Converters.Actor do +defmodule Mobilizon.Service.ActivityPub.Converter.Actor do @moduledoc """ - Actor converter + Actor converter. This module allows to convert events from ActivityStream format to our own internal one, and back. """ alias Mobilizon.Actors.Actor, as: ActorModel - alias Mobilizon.Service.ActivityPub.Converter + alias Mobilizon.Service.ActivityPub.{Converter, Convertible} @behaviour Converter + defimpl Convertible, for: ActorModel do + alias Mobilizon.Service.ActivityPub.Converter.Actor, as: ActorConverter + + defdelegate model_to_as(actor), to: ActorConverter + end + @doc """ - Converts an AP object data to our internal data structure + Converts an AP object data to our internal data structure. """ @impl Converter - @spec as_to_model_data(map()) :: map() + @spec as_to_model_data(map) :: map def as_to_model_data(object) do avatar = object["icon"]["url"] && @@ -45,10 +51,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Actor do end @doc """ - Convert an actor struct to an ActivityStream representation + Convert an actor struct to an ActivityStream representation. """ @impl Converter - @spec model_to_as(ActorModel.t()) :: map() + @spec model_to_as(ActorModel.t()) :: map def model_to_as(%ActorModel{} = actor) do %{ "type" => Atom.to_string(actor.type), @@ -65,9 +71,3 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Actor do } end end - -defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Actors.Actor do - alias Mobilizon.Service.ActivityPub.Converters.Actor, as: ActorConverter - - defdelegate model_to_as(actor), to: ActorConverter -end diff --git a/lib/service/activity_pub/converters/address.ex b/lib/service/activity_pub/converter/address.ex similarity index 85% rename from lib/service/activity_pub/converters/address.ex rename to lib/service/activity_pub/converter/address.ex index 20ce1d73..e5ff7030 100644 --- a/lib/service/activity_pub/converters/address.ex +++ b/lib/service/activity_pub/converter/address.ex @@ -1,11 +1,9 @@ -defmodule Mobilizon.Service.ActivityPub.Converters.Address do +defmodule Mobilizon.Service.ActivityPub.Converter.Address do @moduledoc """ Address converter. This module allows to convert reports from ActivityStream format to our own internal one, and back. - - Note: Reports are named Flag in AS. """ alias Mobilizon.Addresses.Address, as: AddressModel @@ -14,10 +12,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Address do @behaviour Converter @doc """ - Converts an AP object data to our internal data structure + Converts an AP object data to our internal data structure. """ @impl Converter - @spec as_to_model_data(map()) :: map() + @spec as_to_model_data(map) :: map def as_to_model_data(object) do res = %{ "description" => object["name"], @@ -50,10 +48,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Address do end @doc """ - Convert an event struct to an ActivityStream representation + Convert an event struct to an ActivityStream representation. """ @impl Converter - @spec model_to_as(AddressModel.t()) :: map() + @spec model_to_as(AddressModel.t()) :: map def model_to_as(%AddressModel{} = address) do res = %{ "type" => "Place", diff --git a/lib/service/activity_pub/converters/comment.ex b/lib/service/activity_pub/converter/comment.ex similarity index 84% rename from lib/service/activity_pub/converters/comment.ex rename to lib/service/activity_pub/converter/comment.ex index 0fe596ba..3c2fcd22 100644 --- a/lib/service/activity_pub/converters/comment.ex +++ b/lib/service/activity_pub/converter/comment.ex @@ -1,4 +1,4 @@ -defmodule Mobilizon.Service.ActivityPub.Converters.Comment do +defmodule Mobilizon.Service.ActivityPub.Converter.Comment do @moduledoc """ Comment converter. @@ -10,19 +10,26 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do alias Mobilizon.Events.Comment, as: CommentModel alias Mobilizon.Events.Event alias Mobilizon.Service.ActivityPub - alias Mobilizon.Service.ActivityPub.Converter + alias Mobilizon.Service.ActivityPub.{Converter, Convertible} require Logger @behaviour Converter + defimpl Convertible, for: CommentModel do + alias Mobilizon.Service.ActivityPub.Converter.Comment, as: CommentConverter + + defdelegate model_to_as(comment), to: CommentConverter + end + @doc """ - Converts an AP object data to our internal data structure + Converts an AP object data to our internal data structure. """ @impl Converter - @spec as_to_model_data(map()) :: map() + @spec as_to_model_data(map) :: map def as_to_model_data(object) do {:ok, %Actor{id: actor_id}} = ActivityPub.get_or_fetch_by_url(object["actor"]) + Logger.debug("Inserting full comment") Logger.debug(inspect(object)) @@ -65,6 +72,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do end else Logger.debug("No parent object for this comment") + data end @@ -75,7 +83,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do Make an AS comment object from an existing `Comment` structure. """ @impl Converter - @spec model_to_as(CommentModel.t()) :: map() + @spec model_to_as(CommentModel.t()) :: map def model_to_as(%CommentModel{} = comment) do object = %{ "type" => "Note", @@ -94,9 +102,3 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Comment do end end end - -defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Events.Comment do - alias Mobilizon.Service.ActivityPub.Converters.Comment, as: CommentConverter - - defdelegate model_to_as(comment), to: CommentConverter -end diff --git a/lib/service/activity_pub/converters/event.ex b/lib/service/activity_pub/converter/event.ex similarity index 69% rename from lib/service/activity_pub/converters/event.ex rename to lib/service/activity_pub/converter/event.ex index ec6c56c9..fb63c959 100644 --- a/lib/service/activity_pub/converters/event.ex +++ b/lib/service/activity_pub/converter/event.ex @@ -1,4 +1,4 @@ -defmodule Mobilizon.Service.ActivityPub.Converters.Event do +defmodule Mobilizon.Service.ActivityPub.Converter.Event do @moduledoc """ Event converter. @@ -10,20 +10,27 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do alias Mobilizon.Actors.Actor alias Mobilizon.Addresses.Address alias Mobilizon.Events.Event, as: EventModel - alias Mobilizon.Events.Tag + alias Mobilizon.Events.{EventOptions, Tag} alias Mobilizon.Media.Picture - alias Mobilizon.Service.ActivityPub.{Converter, Utils} - alias Mobilizon.Service.ActivityPub.Converters.Address, as: AddressConverter + alias Mobilizon.Service.ActivityPub.{Converter, Convertible, Utils} + alias Mobilizon.Service.ActivityPub.Converter.Address, as: AddressConverter + alias Mobilizon.Service.ActivityPub.Converter.Picture, as: PictureConverter require Logger @behaviour Converter + defimpl Convertible, for: EventModel do + alias Mobilizon.Service.ActivityPub.Converter.Event, as: EventConverter + + defdelegate model_to_as(event), to: EventConverter + end + @doc """ - Converts an AP object data to our internal data structure + Converts an AP object data to our internal data structure. """ @impl Converter - @spec as_to_model_data(map()) :: map() + @spec as_to_model_data(map) :: map def as_to_model_data(object) do Logger.debug("event as_to_model_data") Logger.debug(inspect(object)) @@ -71,15 +78,57 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do {:ok, Map.put(entity, "options", options)} else - err -> - {:error, err} + error -> + {:error, error} end end + @doc """ + Convert an event struct to an ActivityStream representation. + """ + @impl Converter + @spec model_to_as(EventModel.t()) :: map + def model_to_as(%EventModel{} = event) do + to = + if event.visibility == :public, + do: ["https://www.w3.org/ns/activitystreams#Public"], + else: [event.organizer_actor.followers_url] + + res = %{ + "type" => "Event", + "to" => to, + "cc" => [], + "attributedTo" => event.organizer_actor.url, + "name" => event.title, + "actor" => event.organizer_actor.url, + "uuid" => event.uuid, + "category" => event.category, + "content" => event.description, + "publish_at" => (event.publish_at || event.inserted_at) |> date_to_string(), + "updated_at" => event.updated_at |> date_to_string(), + "mediaType" => "text/html", + "startTime" => event.begins_on |> date_to_string(), + "endTime" => event.ends_on |> date_to_string(), + "tag" => event.tags |> build_tags(), + "id" => event.url, + "url" => event.url + } + + res = + if is_nil(event.physical_address), + do: res, + else: Map.put(res, "location", AddressConverter.model_to_as(event.physical_address)) + + if is_nil(event.picture), + do: res, + else: Map.put(res, "attachment", [PictureConverter.model_to_as(event.picture)]) + end + # Get only elements that we have in EventOptions + @spec get_options(map) :: map defp get_options(object) do keys = - Mobilizon.Events.EventOptions + EventOptions |> struct |> Map.keys() |> List.delete(:__struct__) @@ -91,6 +140,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do end) end + @spec get_address(map | binary | nil) :: integer | nil defp get_address(address_url) when is_bitstring(address_url) do get_address(%{"id" => address_url}) end @@ -115,8 +165,9 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do defp get_address(nil), do: nil + @spec do_get_address(map) :: integer | nil defp do_get_address(map) do - map = Mobilizon.Service.ActivityPub.Converters.Address.as_to_model_data(map) + map = Mobilizon.Service.ActivityPub.Converter.Address.as_to_model_data(map) case Addresses.create_address(map) do {:ok, %Address{id: address_id}} -> @@ -127,6 +178,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do end end + @spec fetch_tags([String.t()]) :: [String.t()] defp fetch_tags(tags) do Logger.debug("fetching tags") @@ -141,6 +193,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do end) end + @spec build_tags([String.t()]) :: String.t() defp build_tags(tags) do Enum.map(tags, fn %Tag{} = tag -> %{ @@ -203,12 +256,7 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Event do else: Map.put(res, "attachment", [Utils.make_picture_data(event.picture)]) end + @spec date_to_string(DateTime.t() | nil) :: String.t() defp date_to_string(nil), do: nil - defp date_to_string(date), do: DateTime.to_iso8601(date) -end - -defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Events.Event do - alias Mobilizon.Service.ActivityPub.Converters.Event, as: EventConverter - - defdelegate model_to_as(event), to: EventConverter + defp date_to_string(%DateTime{} = date), do: DateTime.to_iso8601(date) end diff --git a/lib/service/activity_pub/converters/flag.ex b/lib/service/activity_pub/converter/flag.ex similarity index 91% rename from lib/service/activity_pub/converters/flag.ex rename to lib/service/activity_pub/converter/flag.ex index e81a49a1..93108613 100644 --- a/lib/service/activity_pub/converters/flag.ex +++ b/lib/service/activity_pub/converter/flag.ex @@ -1,4 +1,4 @@ -defmodule Mobilizon.Service.ActivityPub.Converters.Flag do +defmodule Mobilizon.Service.ActivityPub.Converter.Flag do @moduledoc """ Flag converter. @@ -18,10 +18,10 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Flag do @behaviour Converter @doc """ - Converts an AP object data to our internal data structure + Converts an AP object data to our internal data structure. """ @impl Converter - @spec as_to_model_data(map()) :: map() + @spec as_to_model_data(map) :: map def as_to_model_data(object) do with params <- as_to_model(object) do %{ @@ -35,6 +35,21 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Flag do end end + @doc """ + Convert an event struct to an ActivityStream representation + """ + @impl Converter + @spec model_to_as(EventModel.t()) :: map + def model_to_as(%Report{} = report) do + %{ + "type" => "Flag", + "to" => ["https://www.w3.org/ns/activitystreams#Public"], + "actor" => report.reporter.url, + "id" => report.url + } + end + + @spec as_to_model(map) :: map def as_to_model(%{"object" => objects} = object) do with {:ok, %Actor{} = reporter} <- Actors.get_actor_by_url(object["actor"]), %Actor{} = reported <- @@ -74,18 +89,4 @@ defmodule Mobilizon.Service.ActivityPub.Converters.Flag do } end end - - @doc """ - Convert an event struct to an ActivityStream representation - """ - @impl Converter - @spec model_to_as(EventModel.t()) :: map() - def model_to_as(%Report{} = report) do - %{ - "type" => "Flag", - "to" => ["https://www.w3.org/ns/activitystreams#Public"], - "actor" => report.reporter.url, - "id" => report.url - } - end end diff --git a/lib/service/activity_pub/converter/participant.ex b/lib/service/activity_pub/converter/participant.ex new file mode 100644 index 00000000..d50e892e --- /dev/null +++ b/lib/service/activity_pub/converter/participant.ex @@ -0,0 +1,31 @@ +defmodule Mobilizon.Service.ActivityPub.Converter.Participant do + @moduledoc """ + Participant converter. + + This module allows to convert reports from ActivityStream format to our own + internal one, and back. + """ + + alias Mobilizon.Events.Participant, as: ParticipantModel + + alias Mobilizon.Service.ActivityPub.Convertible + + defimpl Convertible, for: ParticipantModel do + alias Mobilizon.Service.ActivityPub.Converter.Participant, as: ParticipantConverter + + defdelegate model_to_as(participant), to: ParticipantConverter + end + + @doc """ + Convert an event struct to an ActivityStream representation. + """ + @spec model_to_as(ParticipantModel.t()) :: map + def model_to_as(%ParticipantModel{} = participant) do + %{ + "type" => "Join", + "id" => participant.url, + "actor" => participant.actor.url, + "object" => participant.event.url + } + end +end diff --git a/lib/service/activity_pub/converter/picture.ex b/lib/service/activity_pub/converter/picture.ex new file mode 100644 index 00000000..6c6e93ab --- /dev/null +++ b/lib/service/activity_pub/converter/picture.ex @@ -0,0 +1,28 @@ +defmodule Mobilizon.Service.ActivityPub.Converter.Picture do + @moduledoc """ + Picture converter. + + This module allows to convert events from ActivityStream format to our own + internal one, and back. + """ + + alias Mobilizon.Media.Picture, as: PictureModel + + @doc """ + Convert a picture struct to an ActivityStream representation. + """ + @spec model_to_as(PictureModel.t()) :: map + def model_to_as(%PictureModel{file: file}) do + %{ + "type" => "Document", + "url" => [ + %{ + "type" => "Link", + "mediaType" => file.content_type, + "href" => file.url + } + ], + "name" => file.name + } + end +end diff --git a/lib/service/activity_pub/converters/participant.ex b/lib/service/activity_pub/converters/participant.ex deleted file mode 100644 index 28fb4fe2..00000000 --- a/lib/service/activity_pub/converters/participant.ex +++ /dev/null @@ -1,31 +0,0 @@ -defmodule Mobilizon.Service.ActivityPub.Converters.Participant do - @moduledoc """ - Participant converter. - - This module allows to convert reports from ActivityStream format to our own - internal one, and back. - - Note: Reports are named Flag in AS. - """ - - alias Mobilizon.Events.Participant, as: ParticipantModel - - @doc """ - Convert an event struct to an ActivityStream representation - """ - @spec model_to_as(ParticipantModel.t()) :: map() - def model_to_as(%ParticipantModel{} = participant) do - %{ - "type" => "Join", - "id" => participant.url, - "actor" => participant.actor.url, - "object" => participant.event.url - } - end - - defimpl Mobilizon.Service.ActivityPub.Convertible, for: Mobilizon.Events.Participant do - alias Mobilizon.Service.ActivityPub.Converters.Participant, as: ParticipantConverter - - defdelegate model_to_as(event), to: ParticipantConverter - end -end diff --git a/lib/service/activity_pub/convertible.ex b/lib/service/activity_pub/convertible.ex new file mode 100644 index 00000000..a757cc41 --- /dev/null +++ b/lib/service/activity_pub/convertible.ex @@ -0,0 +1,10 @@ +defprotocol Mobilizon.Service.ActivityPub.Convertible do + @moduledoc """ + Convertible protocol. + """ + + @type activity_streams :: map + + @spec model_to_as(t) :: activity_streams + def model_to_as(convertible) +end diff --git a/lib/service/activity_pub/relay.ex b/lib/service/activity_pub/relay.ex index e6686bae..0597e34d 100644 --- a/lib/service/activity_pub/relay.ex +++ b/lib/service/activity_pub/relay.ex @@ -11,7 +11,7 @@ defmodule Mobilizon.Service.ActivityPub.Relay do alias Mobilizon.Actors alias Mobilizon.Actors.Actor alias Mobilizon.Service.ActivityPub - alias Mobilizon.Service.ActivityPub.Activity + alias Mobilizon.Service.ActivityPub.{Activity, Transmogrifier} alias MobilizonWeb.API.Follows @@ -72,9 +72,7 @@ defmodule Mobilizon.Service.ActivityPub.Relay do def publish(%Activity{data: %{"object" => object}} = _activity) do with %Actor{id: actor_id} = actor <- get_actor(), {:ok, object} <- - Mobilizon.Service.ActivityPub.Transmogrifier.fetch_obj_helper_as_activity_streams( - object - ) do + Transmogrifier.fetch_obj_helper_as_activity_streams(object) do ActivityPub.announce(actor, object, "#{object["id"]}/announces/#{actor_id}", true, false) else e -> diff --git a/lib/service/activity_pub/transmogrifier.ex b/lib/service/activity_pub/transmogrifier.ex index 446dab9c..71330ab2 100644 --- a/lib/service/activity_pub/transmogrifier.ex +++ b/lib/service/activity_pub/transmogrifier.ex @@ -13,7 +13,7 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do alias Mobilizon.Events alias Mobilizon.Events.{Comment, Event, Participant} alias Mobilizon.Service.ActivityPub - alias Mobilizon.Service.ActivityPub.{Utils, Visibility} + alias Mobilizon.Service.ActivityPub.{Converter, Convertible, Utils, Visibility} require Logger @@ -122,7 +122,7 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do def handle_incoming(%{"id" => ""}), do: :error def handle_incoming(%{"type" => "Flag"} = data) do - with params <- Mobilizon.Service.ActivityPub.Converters.Flag.as_to_model(data) do + with params <- Converter.Flag.as_to_model(data) do params = %{ reporter_url: params["reporter"].url, reported_actor_url: params["reported"].url, @@ -868,7 +868,7 @@ defmodule Mobilizon.Service.ActivityPub.Transmogrifier do def fetch_obj_helper_as_activity_streams(object) do with {:ok, object} <- fetch_obj_helper(object) do - {:ok, Mobilizon.Service.ActivityPub.Convertible.model_to_as(object)} + {:ok, Convertible.model_to_as(object)} end end end diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index 77b59f39..565aafe9 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -16,7 +16,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do alias Mobilizon.Events.{Comment, Event} alias Mobilizon.Media.Picture alias Mobilizon.Reports.Report - alias Mobilizon.Service.ActivityPub.{Activity, Converters} + alias Mobilizon.Service.ActivityPub.{Activity, Converter} + alias Mobilizon.Service.Federator alias Mobilizon.Storage.Repo alias MobilizonWeb.{Email, Endpoint} @@ -64,7 +65,8 @@ defmodule Mobilizon.Service.ActivityPub.Utils do _ -> 5 end - Mobilizon.Service.Federator.enqueue(:publish, activity, priority) + Federator.enqueue(:publish, activity, priority) + :ok end @@ -119,7 +121,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do def insert_full_object(%{"object" => %{"type" => "Event"} = object_data, "type" => "Create"}) when is_map(object_data) do with {:ok, object_data} <- - Converters.Event.as_to_model_data(object_data), + Converter.Event.as_to_model_data(object_data), {:ok, %Event{} = event} <- Events.create_event(object_data) do {:ok, event} end @@ -139,7 +141,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do """ def insert_full_object(%{"object" => %{"type" => "Note"} = object_data, "type" => "Create"}) when is_map(object_data) do - with data <- Converters.Comment.as_to_model_data(object_data), + with data <- Converter.Comment.as_to_model_data(object_data), {:ok, %Comment{} = comment} <- Events.create_comment(data) do {:ok, comment} else @@ -155,7 +157,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do """ def insert_full_object(%{"type" => "Flag"} = object_data) when is_map(object_data) do - with data <- Converters.Flag.as_to_model_data(object_data), + with data <- Converter.Flag.as_to_model_data(object_data), {:ok, %Report{} = report} <- Reports.create_report(data) do Enum.each(Users.list_moderators(), fn moderator -> moderator @@ -187,7 +189,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do when is_map(object_data) do with {:event_not_found, %Event{} = event} <- {:event_not_found, Events.get_event_by_url(event_url)}, - {:ok, object_data} <- Converters.Event.as_to_model_data(object_data), + {:ok, object_data} <- Converter.Event.as_to_model_data(object_data), {:ok, %Event{} = event} <- Events.update_event(event, object_data) do {:ok, event} end @@ -199,7 +201,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do }) when is_map(object_data) and type_actor in @actor_types do with {:ok, %Actor{} = actor} <- Actors.get_actor_by_url(actor_url), - object_data <- Converters.Actor.as_to_model_data(object_data), + object_data <- Converter.Actor.as_to_model_data(object_data), {:ok, %Actor{} = actor} <- Actors.update_actor(actor, object_data) do {:ok, actor} end @@ -245,21 +247,10 @@ defmodule Mobilizon.Service.ActivityPub.Utils do end @doc """ - Convert a picture model into an AS Link representation + Convert a picture model into an AS Link representation. """ - # TODO: Move me to Mobilizon.Service.ActivityPub.Converters - def make_picture_data(%Picture{file: file} = _picture) do - %{ - "type" => "Document", - "url" => [ - %{ - "type" => "Link", - "mediaType" => file.content_type, - "href" => file.url - } - ], - "name" => file.name - } + def make_picture_data(%Picture{} = picture) do + Converter.Picture.model_to_as(picture) end @doc """ @@ -268,7 +259,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do def make_picture_data(picture) when is_map(picture) do with {:ok, %{"url" => [%{"href" => url, "mediaType" => content_type}], "size" => size}} <- MobilizonWeb.Upload.store(picture.file), - {:ok, %Picture{file: _file} = pic} <- + {:ok, %Picture{file: _file} = picture} <- Mobilizon.Media.create_picture(%{ "file" => %{ "url" => url, @@ -278,7 +269,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do }, "actor_id" => picture.actor_id }) do - make_picture_data(pic) + Converter.Picture.model_to_as(picture) end end diff --git a/test/mobilizon/service/activity_pub/activity_pub_test.exs b/test/mobilizon/service/activity_pub/activity_pub_test.exs index 1b7d1048..59583ecf 100644 --- a/test/mobilizon/service/activity_pub/activity_pub_test.exs +++ b/test/mobilizon/service/activity_pub/activity_pub_test.exs @@ -16,8 +16,11 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do alias Mobilizon.Events alias Mobilizon.Events.Event alias Mobilizon.Service.ActivityPub + alias Mobilizon.Service.ActivityPub.Converter alias Mobilizon.Service.HTTPSignatures.Signature + alias MobilizonWeb.ActivityPub.ActorView + setup_all do HTTPoison.start() end @@ -162,7 +165,7 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do test "it creates an update activity with the new actor data" do actor = insert(:actor) - actor_data = MobilizonWeb.ActivityPub.ActorView.render("actor.json", %{actor: actor}) + actor_data = ActorView.render("actor.json", %{actor: actor}) actor_data = Map.put(actor_data, "summary", @updated_actor_summary) {:ok, update, updated_actor} = @@ -191,7 +194,7 @@ defmodule Mobilizon.Service.ActivityPub.ActivityPubTest do test "it creates an update activity with the new event data" do actor = insert(:actor) event = insert(:event, organizer_actor: actor) - event_data = Mobilizon.Service.ActivityPub.Converters.Event.model_to_as(event) + event_data = Converter.Event.model_to_as(event) event_data = Map.put(event_data, "startTime", @updated_start_time) {:ok, update, updated_event} = diff --git a/test/mobilizon/service/activity_pub/converters/actor_test.exs b/test/mobilizon/service/activity_pub/converter/actor_test.exs similarity index 83% rename from test/mobilizon/service/activity_pub/converters/actor_test.exs rename to test/mobilizon/service/activity_pub/converter/actor_test.exs index b9f31b44..2c9fdf5d 100644 --- a/test/mobilizon/service/activity_pub/converters/actor_test.exs +++ b/test/mobilizon/service/activity_pub/converter/actor_test.exs @@ -1,8 +1,8 @@ -defmodule Mobilizon.Service.ActivityPub.Converters.ActorTest do +defmodule Mobilizon.Service.ActivityPub.Converter.ActorTest do use Mobilizon.DataCase alias Mobilizon.Actors.Actor - alias Mobilizon.Service.ActivityPub.Converters.Actor, as: ActorConverter + alias Mobilizon.Service.ActivityPub.Converter.Actor, as: ActorConverter describe "actor to AS" do test "valid actor to as" do diff --git a/test/mobilizon/service/activity_pub/transmogrifier_test.exs b/test/mobilizon/service/activity_pub/transmogrifier_test.exs index 2f2f6e01..399a140d 100644 --- a/test/mobilizon/service/activity_pub/transmogrifier_test.exs +++ b/test/mobilizon/service/activity_pub/transmogrifier_test.exs @@ -18,6 +18,8 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do alias Mobilizon.Service.ActivityPub.{Activity, Utils} alias Mobilizon.Service.ActivityPub.Transmogrifier + alias MobilizonWeb.API + setup_all do HTTPoison.start() end @@ -845,7 +847,7 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do other_actor = insert(:actor) {:ok, activity, _} = - MobilizonWeb.API.Comments.create_comment( + API.Comments.create_comment( actor.preferred_username, "hey, @#{other_actor.preferred_username}, how are ya? #2hu" ) @@ -881,8 +883,7 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do test "it adds the json-ld context and the conversation property" do actor = insert(:actor) - {:ok, activity, _} = - MobilizonWeb.API.Comments.create_comment(actor.preferred_username, "hey") + {:ok, activity, _} = API.Comments.create_comment(actor.preferred_username, "hey") {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) @@ -892,8 +893,7 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do actor = insert(:actor) - {:ok, activity, _} = - MobilizonWeb.API.Comments.create_comment(actor.preferred_username, "hey") + {:ok, activity, _} = API.Comments.create_comment(actor.preferred_username, "hey") {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) @@ -903,8 +903,7 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do test "it strips internal hashtag data" do actor = insert(:actor) - {:ok, activity, _} = - MobilizonWeb.API.Comments.create_comment(actor.preferred_username, "#2hu") + {:ok, activity, _} = API.Comments.create_comment(actor.preferred_username, "#2hu") expected_tag = %{ "href" => MobilizonWeb.Endpoint.url() <> "/tags/2hu", @@ -920,8 +919,7 @@ defmodule Mobilizon.Service.ActivityPub.TransmogrifierTest do test "it strips internal fields" do actor = insert(:actor) - {:ok, activity, _} = - MobilizonWeb.API.Comments.create_comment(actor.preferred_username, "#2hu") + {:ok, activity, _} = API.Comments.create_comment(actor.preferred_username, "#2hu") {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data) diff --git a/test/mobilizon/service/activity_pub/utils_test.exs b/test/mobilizon/service/activity_pub/utils_test.exs index 7d74df3c..dfb0e737 100644 --- a/test/mobilizon/service/activity_pub/utils_test.exs +++ b/test/mobilizon/service/activity_pub/utils_test.exs @@ -5,7 +5,7 @@ defmodule Mobilizon.Service.ActivityPub.UtilsTest do import Mobilizon.Factory - alias Mobilizon.Service.ActivityPub.Utils + alias Mobilizon.Service.ActivityPub.{Converter, Utils} alias MobilizonWeb.Endpoint alias MobilizonWeb.Router.Helpers, as: Routes @@ -28,7 +28,7 @@ defmodule Mobilizon.Service.ActivityPub.UtilsTest do "id" => Routes.page_url(Endpoint, :comment, reply.uuid), "inReplyTo" => comment.url, "attributedTo" => reply.actor.url - } == Mobilizon.Service.ActivityPub.Converters.Comment.model_to_as(reply) + } == Converter.Comment.model_to_as(reply) end test "comment data from map" do diff --git a/test/mobilizon_web/api/report_test.exs b/test/mobilizon_web/api/report_test.exs index 26ba77f2..12e02246 100644 --- a/test/mobilizon_web/api/report_test.exs +++ b/test/mobilizon_web/api/report_test.exs @@ -7,6 +7,7 @@ defmodule MobilizonWeb.API.ReportTest do alias Mobilizon.Events.{Comment, Event} alias Mobilizon.Reports.{Note, Report} alias Mobilizon.Service.ActivityPub.Activity + alias Mobilizon.Service.Formatter alias Mobilizon.Users alias Mobilizon.Users.User @@ -86,7 +87,7 @@ defmodule MobilizonWeb.API.ReportTest do _comment_2 = insert(:comment, actor: reported) comment = "This is really not acceptable, remote admin I don't know" - encoded_comment = Mobilizon.Service.Formatter.html_escape(comment, "text/plain") + encoded_comment = Formatter.html_escape(comment, "text/plain") assert {:ok, %Activity{} = flag_activity, _} = Reports.report(%{ diff --git a/test/mobilizon_web/controllers/nodeinfo_controller_test.exs b/test/mobilizon_web/controllers/nodeinfo_controller_test.exs index 6715c545..28c0fa10 100644 --- a/test/mobilizon_web/controllers/nodeinfo_controller_test.exs +++ b/test/mobilizon_web/controllers/nodeinfo_controller_test.exs @@ -3,27 +3,20 @@ defmodule MobilizonWeb.NodeInfoControllerTest do alias Mobilizon.Config + alias MobilizonWeb.Endpoint + alias MobilizonWeb.Router.Helpers, as: Routes + test "Get node info schemas", %{conn: conn} do conn = get(conn, node_info_path(conn, :schemas)) assert json_response(conn, 200) == %{ "links" => [ %{ - "href" => - MobilizonWeb.Router.Helpers.node_info_url( - MobilizonWeb.Endpoint, - :nodeinfo, - "2.0" - ), + "href" => Routes.node_info_url(Endpoint, :nodeinfo, "2.0"), "rel" => "http://nodeinfo.diaspora.software/ns/schema/2.0" }, %{ - "href" => - MobilizonWeb.Router.Helpers.node_info_url( - MobilizonWeb.Endpoint, - :nodeinfo, - "2.1" - ), + "href" => Routes.node_info_url(Endpoint, :nodeinfo, "2.1"), "rel" => "http://nodeinfo.diaspora.software/ns/schema/2.1" } ] diff --git a/test/mobilizon_web/resolvers/admin_resolver_test.exs b/test/mobilizon_web/resolvers/admin_resolver_test.exs index 5078f28b..a6a079b3 100644 --- a/test/mobilizon_web/resolvers/admin_resolver_test.exs +++ b/test/mobilizon_web/resolvers/admin_resolver_test.exs @@ -9,6 +9,7 @@ defmodule MobilizonWeb.Resolvers.AdminResolverTest do alias Mobilizon.Users.User alias MobilizonWeb.AbsintheHelpers + alias MobilizonWeb.API describe "Resolver: List the action logs" do @note_content "This a note on a report" @@ -20,12 +21,11 @@ defmodule MobilizonWeb.Resolvers.AdminResolverTest do %Actor{} = moderator_2 = insert(:actor, user: user_moderator_2) %Report{} = report = insert(:report) - MobilizonWeb.API.Reports.update_report_status(moderator, report, "resolved") + API.Reports.update_report_status(moderator, report, "resolved") - {:ok, %Note{} = note} = - MobilizonWeb.API.Reports.create_report_note(report, moderator_2, @note_content) + {:ok, %Note{} = note} = API.Reports.create_report_note(report, moderator_2, @note_content) - MobilizonWeb.API.Reports.delete_report_note(note, moderator_2) + API.Reports.delete_report_note(note, moderator_2) query = """ { diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex index c9b207b8..5bfb388e 100644 --- a/test/support/channel_case.ex +++ b/test/support/channel_case.ex @@ -15,6 +15,10 @@ defmodule MobilizonWeb.ChannelCase do use ExUnit.CaseTemplate + alias Ecto.Adapters.SQL.Sandbox, as: Adapter + + alias Mobilizon.Storage.Repo + using do quote do # Import conveniences for testing with channels @@ -26,11 +30,9 @@ defmodule MobilizonWeb.ChannelCase do end setup tags do - :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo) + :ok = Adapter.checkout(Repo) - unless tags[:async] do - Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()}) - end + unless tags[:async], do: Adapter.mode(Repo, {:shared, self()}) :ok end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index eae09e0c..4c10b2d5 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -15,6 +15,11 @@ defmodule MobilizonWeb.ConnCase do use ExUnit.CaseTemplate + alias Ecto.Adapters.SQL.Sandbox, as: Adapter + + alias Mobilizon.Storage.Repo + alias Mobilizon.Users.User + using do quote do # Import conveniences for testing with connections @@ -24,7 +29,7 @@ defmodule MobilizonWeb.ConnCase do # The default endpoint for testing @endpoint MobilizonWeb.Endpoint - def auth_conn(%Plug.Conn{} = conn, %Mobilizon.Users.User{} = user) do + def auth_conn(%Plug.Conn{} = conn, %User{} = user) do {:ok, token, _claims} = MobilizonWeb.Guardian.encode_and_sign(user) conn @@ -35,11 +40,9 @@ defmodule MobilizonWeb.ConnCase do end setup tags do - :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo) + :ok = Adapter.checkout(Repo) - unless tags[:async] do - Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()}) - end + unless tags[:async], do: Adapter.mode(Repo, {:shared, self()}) {:ok, conn: Phoenix.ConnTest.build_conn()} end diff --git a/test/support/data_case.ex b/test/support/data_case.ex index 76f4d26d..30d0d260 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -14,7 +14,10 @@ defmodule Mobilizon.DataCase do use ExUnit.CaseTemplate + alias Ecto.Adapters.SQL.Sandbox, as: Adapter + alias Mobilizon.Config + alias Mobilizon.Storage.Repo using do quote do @@ -28,11 +31,9 @@ defmodule Mobilizon.DataCase do end setup tags do - :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo) + :ok = Adapter.checkout(Repo) - unless tags[:async] do - Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()}) - end + unless tags[:async], do: Adapter.mode(Repo, {:shared, self()}) :ok end From 8dc2fbcf984efe04e84cee17d21ab32872c0448b Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 26 Sep 2019 18:10:11 +0200 Subject: [PATCH 4/4] Fix rebase issue Signed-off-by: Thomas Citharel --- lib/service/activity_pub/converter/event.ex | 43 +-------------------- 1 file changed, 1 insertion(+), 42 deletions(-) diff --git a/lib/service/activity_pub/converter/event.ex b/lib/service/activity_pub/converter/event.ex index fb63c959..c849c481 100644 --- a/lib/service/activity_pub/converter/event.ex +++ b/lib/service/activity_pub/converter/event.ex @@ -108,6 +108,7 @@ defmodule Mobilizon.Service.ActivityPub.Converter.Event do "updated_at" => event.updated_at |> date_to_string(), "mediaType" => "text/html", "startTime" => event.begins_on |> date_to_string(), + "joinOptions" => to_string(event.join_options), "endTime" => event.ends_on |> date_to_string(), "tag" => event.tags |> build_tags(), "id" => event.url, @@ -214,48 +215,6 @@ defmodule Mobilizon.Service.ActivityPub.Converter.Event do end end - @doc """ - Convert an event struct to an ActivityStream representation - """ - @impl Converter - @spec model_to_as(EventModel.t()) :: map() - def model_to_as(%EventModel{} = event) do - to = - if event.visibility == :public, - do: ["https://www.w3.org/ns/activitystreams#Public"], - else: [event.organizer_actor.followers_url] - - res = %{ - "type" => "Event", - "to" => to, - "cc" => [], - "attributedTo" => event.organizer_actor.url, - "name" => event.title, - "actor" => event.organizer_actor.url, - "uuid" => event.uuid, - "category" => event.category, - "content" => event.description, - "publish_at" => (event.publish_at || event.inserted_at) |> date_to_string(), - "updated_at" => event.updated_at |> date_to_string(), - "mediaType" => "text/html", - "startTime" => event.begins_on |> date_to_string(), - "endTime" => event.ends_on |> date_to_string(), - "joinOptions" => to_string(event.join_options), - "tag" => event.tags |> build_tags(), - "id" => event.url, - "url" => event.url - } - - res = - if is_nil(event.physical_address), - do: res, - else: Map.put(res, "location", AddressConverter.model_to_as(event.physical_address)) - - if is_nil(event.picture), - do: res, - else: Map.put(res, "attachment", [Utils.make_picture_data(event.picture)]) - end - @spec date_to_string(DateTime.t() | nil) :: String.t() defp date_to_string(nil), do: nil defp date_to_string(%DateTime{} = date), do: DateTime.to_iso8601(date)