From 46bb816a233c091745cf40fb11167dc42feb8f8c Mon Sep 17 00:00:00 2001 From: miffy Date: Sun, 8 Sep 2019 01:49:56 +0200 Subject: [PATCH] Separating of Storage context --- config/config.exs | 4 +-- config/dev.exs | 6 ++-- config/prod.exs | 6 ++-- config/test.exs | 9 +++-- lib/mobilizon.ex | 9 +++-- lib/mobilizon/actors/actor.ex | 15 ++++---- lib/mobilizon/actors/actors.ex | 15 ++++---- lib/mobilizon/actors/member.ex | 11 +++--- lib/mobilizon/addresses/addresses.ex | 5 +-- lib/mobilizon/admin.ex | 7 ++-- lib/mobilizon/application.ex | 2 +- lib/mobilizon/ecto.ex | 44 ----------------------- lib/mobilizon/{ => email}/mailer.ex | 2 +- lib/mobilizon/events/events.ex | 31 +++++++++-------- lib/mobilizon/events/tag.ex | 46 +++++++------------------ lib/mobilizon/events/tag/title_slug.ex | 33 ++++++++++++++++++ lib/mobilizon/media/media.ex | 4 +-- lib/mobilizon/postgrex_types.ex | 5 --- lib/mobilizon/reports/reports.ex | 6 ++-- lib/mobilizon/storage/ecto.ex | 15 ++++++++ lib/mobilizon/{ => storage}/page.ex | 8 ++--- lib/mobilizon/storage/postgrex_types.ex | 5 +++ lib/mobilizon/{ => storage}/repo.ex | 8 ++--- lib/mobilizon/users/users.ex | 6 ++-- lib/service/activity_pub/utils.ex | 4 +-- lib/service/users/activation.ex | 5 +-- lib/service/users/reset_password.ex | 10 +++--- mix.exs | 6 ++-- priv/repo/seeds.exs | 2 +- test/support/channel_case.ex | 4 +-- test/support/conn_case.ex | 4 +-- test/support/data_case.ex | 6 ++-- test/support/factory.ex | 2 +- test/test_helper.exs | 2 +- 34 files changed, 167 insertions(+), 180 deletions(-) delete mode 100644 lib/mobilizon/ecto.ex rename lib/mobilizon/{ => email}/mailer.ex (70%) create mode 100644 lib/mobilizon/events/tag/title_slug.ex delete mode 100644 lib/mobilizon/postgrex_types.ex create mode 100644 lib/mobilizon/storage/ecto.ex rename lib/mobilizon/{ => storage}/page.ex (84%) create mode 100644 lib/mobilizon/storage/postgrex_types.ex rename lib/mobilizon/{ => storage}/repo.ex (61%) diff --git a/config/config.exs b/config/config.exs index 28978814f..9d19f5b02 100644 --- a/config/config.exs +++ b/config/config.exs @@ -7,7 +7,7 @@ use Mix.Config # General application configuration config :mobilizon, - ecto_repos: [Mobilizon.Repo] + ecto_repos: [Mobilizon.Storage.Repo] config :mobilizon, :instance, name: System.get_env("MOBILIZON_INSTANCE_NAME") || "Localhost", @@ -78,7 +78,7 @@ config :mobilizon, MobilizonWeb.Guardian, secret_key: "ty0WM7YBE3ojvxoUQxo8AERrNpfbXnIJ82ovkPdqbUFw31T5LcK8wGjaOiReVQjo" config :guardian, Guardian.DB, - repo: Mobilizon.Repo, + repo: Mobilizon.Storage.Repo, # default schema_name: "guardian_tokens", # store all token types if not set diff --git a/config/dev.exs b/config/dev.exs index 21c109e0b..c7345d110 100644 --- a/config/dev.exs +++ b/config/dev.exs @@ -61,11 +61,11 @@ config :phoenix, :stacktrace_depth, 20 # Initialize plugs at runtime for faster development compilation config :phoenix, :plug_init_mode, :runtime -config :mobilizon, Mobilizon.Mailer, adapter: Bamboo.LocalAdapter +config :mobilizon, Mobilizon.Email.Mailer, adapter: Bamboo.LocalAdapter # Configure your database -config :mobilizon, Mobilizon.Repo, - types: Mobilizon.PostgresTypes, +config :mobilizon, Mobilizon.Storage.Repo, + types: Mobilizon.Storage.PostgresTypes, username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon", password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon", database: System.get_env("MOBILIZON_DATABASE_DBNAME") || "mobilizon_dev", diff --git a/config/prod.exs b/config/prod.exs index 79835c89b..d580935ca 100644 --- a/config/prod.exs +++ b/config/prod.exs @@ -12,8 +12,8 @@ config :mobilizon, MobilizonWeb.Endpoint, cache_static_manifest: "priv/static/manifest.json" # Configure your database -config :mobilizon, Mobilizon.Repo, - types: Mobilizon.PostgresTypes, +config :mobilizon, Mobilizon.Storage.Repo, + types: Mobilizon.Storage.PostgresTypes, username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon", password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon", database: System.get_env("MOBILIZON_DATABASE_DBNAME") || "mobilizon_prod", @@ -21,7 +21,7 @@ config :mobilizon, Mobilizon.Repo, port: System.get_env("MOBILIZON_DATABASE_PORT") || "5432", pool_size: 15 -config :mobilizon, Mobilizon.Mailer, +config :mobilizon, Mobilizon.Email.Mailer, adapter: Bamboo.SMTPAdapter, server: "localhost", hostname: "localhost", diff --git a/config/test.exs b/config/test.exs index 21432393b..8d174e40b 100644 --- a/config/test.exs +++ b/config/test.exs @@ -22,16 +22,15 @@ config :logger, level: :info # Configure your database -config :mobilizon, Mobilizon.Repo, - types: Mobilizon.PostgresTypes, +config :mobilizon, Mobilizon.Storage.Repo, + types: Mobilizon.Storage.PostgresTypes, username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon", password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon", database: System.get_env("MOBILIZON_DATABASE_DBNAME") || "mobilizon_test", hostname: System.get_env("MOBILIZON_DATABASE_HOST") || "localhost", - pool: Ecto.Adapters.SQL.Sandbox, - types: Mobilizon.PostgresTypes + pool: Ecto.Adapters.SQL.Sandbox -config :mobilizon, Mobilizon.Mailer, adapter: Bamboo.TestAdapter +config :mobilizon, Mobilizon.Email.Mailer, adapter: Bamboo.TestAdapter config :mobilizon, MobilizonWeb.Upload, filters: [], link_name: false diff --git a/lib/mobilizon.ex b/lib/mobilizon.ex index 1953b2895..36e0d6c61 100644 --- a/lib/mobilizon.ex +++ b/lib/mobilizon.ex @@ -1,9 +1,12 @@ defmodule Mobilizon do @moduledoc """ - Mobilizon is a decentralized and federated Meetup-like using [ActivityPub](http://activitypub.rocks/). + Mobilizon is a decentralized and federated Meetup-like using + [ActivityPub](http://activitypub.rocks/). - It consists of an API server build with [Elixir](http://elixir-lang.github.io/) and the [Phoenix Framework](https://hexdocs.pm/phoenix). + It consists of an API server build with [Elixir](http://elixir-lang.github.io/) + and the [Phoenix Framework](https://hexdocs.pm/phoenix). - Mobilizon relies on `Guardian` for auth and `Geo`/Postgis for geographical informations. + Mobilizon relies on `Guardian` for auth and `Geo`/Postgis for geographical + information. """ end diff --git a/lib/mobilizon/actors/actor.ex b/lib/mobilizon/actors/actor.ex index 6fe728e2a..e83d9a308 100644 --- a/lib/mobilizon/actors/actor.ex +++ b/lib/mobilizon/actors/actor.ex @@ -26,25 +26,24 @@ defmodule Mobilizon.Actors.Actor do @moduledoc """ Represents an actor (local and remote actors) """ + use Ecto.Schema + import Ecto.Changeset + import Ecto.Query alias Mobilizon.Actors alias Mobilizon.Actors.{Actor, Follower, Member} alias Mobilizon.Config alias Mobilizon.Events.{Event, FeedToken} alias Mobilizon.Media.File - alias Mobilizon.Users.User - alias Mobilizon.Reports.{Report, Note} + alias Mobilizon.Storage.{Page, Repo} + alias Mobilizon.Users.User alias MobilizonWeb.Router.Helpers, as: Routes alias MobilizonWeb.Endpoint - import Ecto.Query - import Mobilizon.Ecto - alias Mobilizon.Repo - require Logger # @type t :: %Actor{description: String.t, id: integer(), inserted_at: DateTime.t, updated_at: DateTime.t, display_name: String.t, domain: String.t, keys: String.t, suspended: boolean(), url: String.t, username: String.t, organized_events: list(), groups: list(), group_request: list(), user: User.t, field: ActorTypeEnum.t} @@ -383,7 +382,7 @@ defmodule Mobilizon.Actors.Actor do ) total = Task.async(fn -> Repo.aggregate(query, :count, :id) end) - elements = Task.async(fn -> Repo.all(paginate(query, page, limit)) end) + elements = Task.async(fn -> Repo.all(Page.paginate(query, page, limit)) end) %{total: Task.await(total), elements: Task.await(elements)} end @@ -428,7 +427,7 @@ defmodule Mobilizon.Actors.Actor do ) total = Task.async(fn -> Repo.aggregate(query, :count, :id) end) - elements = Task.async(fn -> Repo.all(paginate(query, page, limit)) end) + elements = Task.async(fn -> Repo.all(Page.paginate(query, page, limit)) end) %{total: Task.await(total), elements: Task.await(elements)} end diff --git a/lib/mobilizon/actors/actors.ex b/lib/mobilizon/actors/actors.ex index 7ab1a9b0b..3a3617b9d 100644 --- a/lib/mobilizon/actors/actors.ex +++ b/lib/mobilizon/actors/actors.ex @@ -3,16 +3,15 @@ defmodule Mobilizon.Actors do The Actors context. """ - import Ecto.Query, warn: false - import Mobilizon.Ecto + import Ecto.Query - alias Mobilizon.Repo + alias Ecto.Multi alias Mobilizon.Actors.{Actor, Bot, Member, Follower} alias Mobilizon.Media.File - alias Ecto.Multi - alias Mobilizon.Service.ActivityPub + alias Mobilizon.Storage.{Page, Repo} + require Logger @doc false @@ -203,7 +202,7 @@ defmodule Mobilizon.Actors do where: a.type == ^:Group, where: a.visibility in [^:public, ^:unlisted] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) ) end @@ -586,7 +585,7 @@ defmodule Mobilizon.Actors do ^username ) ) - |> paginate(page, limit) + |> Page.paginate(page, limit) total = Task.async(fn -> Repo.aggregate(query, :count, :id) end) elements = Task.async(fn -> Repo.all(query) end) @@ -652,7 +651,7 @@ defmodule Mobilizon.Actors do }) try do - Mobilizon.Repo.insert!(actor) + Repo.insert!(actor) rescue e in Ecto.InvalidChangesetError -> {:error, e.changeset} diff --git a/lib/mobilizon/actors/member.ex b/lib/mobilizon/actors/member.ex index c3b4767be..02d70f1bf 100644 --- a/lib/mobilizon/actors/member.ex +++ b/lib/mobilizon/actors/member.ex @@ -12,15 +12,14 @@ defmodule Mobilizon.Actors.Member do @moduledoc """ Represents the membership of an actor to a group """ + use Ecto.Schema import Ecto.Changeset - import Ecto.Query, warn: false - import Mobilizon.Ecto + import Ecto.Query - alias Mobilizon.Actors.Member - alias Mobilizon.Actors.Actor - alias Mobilizon.Repo + alias Mobilizon.Actors.{Actor, Member} + alias Mobilizon.Storage.{Page, Repo} schema "members" do field(:role, Mobilizon.Actors.MemberRoleEnum, default: :member) @@ -64,7 +63,7 @@ defmodule Mobilizon.Actors.Member do where: m.parent_id == ^id and (m.role == ^:creator or m.role == ^:administrator), preload: [:actor] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) ) end diff --git a/lib/mobilizon/addresses/addresses.ex b/lib/mobilizon/addresses/addresses.ex index cc6c407c5..19f6325ad 100644 --- a/lib/mobilizon/addresses/addresses.ex +++ b/lib/mobilizon/addresses/addresses.ex @@ -4,10 +4,11 @@ defmodule Mobilizon.Addresses do """ import Ecto.Query, warn: false - alias Mobilizon.Repo - require Logger alias Mobilizon.Addresses.Address + alias Mobilizon.Storage.Repo + + require Logger @geom_types [:point] diff --git a/lib/mobilizon/admin.ex b/lib/mobilizon/admin.ex index fba5ee050..47eb3097b 100644 --- a/lib/mobilizon/admin.ex +++ b/lib/mobilizon/admin.ex @@ -3,11 +3,10 @@ defmodule Mobilizon.Admin do The Admin context. """ - import Ecto.Query, warn: false - alias Mobilizon.Repo - import Mobilizon.Ecto + import Ecto.Query alias Mobilizon.Admin.ActionLog + alias Mobilizon.Storage.{Page, Repo} @doc """ Returns the list of action_logs. @@ -24,7 +23,7 @@ defmodule Mobilizon.Admin do r in ActionLog, preload: [:actor] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) |> Repo.all() end diff --git a/lib/mobilizon/application.ex b/lib/mobilizon/application.ex index 3bbb4b0f3..5291896a8 100644 --- a/lib/mobilizon/application.ex +++ b/lib/mobilizon/application.ex @@ -21,7 +21,7 @@ defmodule Mobilizon.Application do # Define workers and child supervisors to be supervised children = [ # Start the Ecto repository - supervisor(Mobilizon.Repo, []), + supervisor(Mobilizon.Storage.Repo, []), # Start the endpoint when the application starts supervisor(MobilizonWeb.Endpoint, []), # Start your own worker by calling: Mobilizon.Worker.start_link(arg1, arg2, arg3) diff --git a/lib/mobilizon/ecto.ex b/lib/mobilizon/ecto.ex deleted file mode 100644 index 66fb2ec72..000000000 --- a/lib/mobilizon/ecto.ex +++ /dev/null @@ -1,44 +0,0 @@ -defmodule Mobilizon.Ecto do - @moduledoc """ - Mobilizon Ecto utils - """ - - import Ecto.Query, warn: false - - @doc """ - Add limit and offset to the query - """ - def paginate(query, page \\ 1, size \\ 10) - def paginate(query, page, _size) when is_nil(page), do: paginate(query) - def paginate(query, page, size) when is_nil(size), do: paginate(query, page) - - def paginate(query, page, size) do - from(query, - limit: ^size, - offset: ^((page - 1) * size) - ) - end - - @doc """ - Add sort to the query - """ - def sort(query, sort, direction) do - from( - query, - order_by: [{^direction, ^sort}] - ) - end - - def increment_slug(slug) do - case List.pop_at(String.split(slug, "-"), -1) do - {nil, _} -> - slug - - {suffix, slug_parts} -> - case Integer.parse(suffix) do - {id, _} -> Enum.join(slug_parts, "-") <> "-" <> Integer.to_string(id + 1) - :error -> slug <> "-1" - end - end - end -end diff --git a/lib/mobilizon/mailer.ex b/lib/mobilizon/email/mailer.ex similarity index 70% rename from lib/mobilizon/mailer.ex rename to lib/mobilizon/email/mailer.ex index 177399ebb..027647609 100644 --- a/lib/mobilizon/mailer.ex +++ b/lib/mobilizon/email/mailer.ex @@ -1,4 +1,4 @@ -defmodule Mobilizon.Mailer do +defmodule Mobilizon.Email.Mailer do @moduledoc """ Mobilizon Mailer. """ diff --git a/lib/mobilizon/events/events.ex b/lib/mobilizon/events/events.ex index cc6d71702..a6256b294 100644 --- a/lib/mobilizon/events/events.ex +++ b/lib/mobilizon/events/events.ex @@ -3,17 +3,18 @@ defmodule Mobilizon.Events do The Events context. """ - import Ecto.Query, warn: false - import Mobilizon.Ecto + import Ecto.Query + + import Mobilizon.Storage.Ecto - alias Mobilizon.Repo - alias Mobilizon.Events.{Event, Comment, Participant} alias Mobilizon.Actors.Actor - alias Mobilizon.Users.User alias Mobilizon.Addresses.Address + alias Mobilizon.Events.{Event, Comment, Participant} + alias Mobilizon.Storage.{Page, Repo} + alias Mobilizon.Users.User def data() do - Dataloader.Ecto.new(Mobilizon.Repo, query: &query/2) + Dataloader.Ecto.new(Repo, query: &query/2) end def query(queryable, _params) do @@ -36,7 +37,7 @@ defmodule Mobilizon.Events do :picture ] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) events = Repo.all(query) @@ -301,7 +302,7 @@ defmodule Mobilizon.Events do e in Event, preload: [:organizer_actor, :participants] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) |> sort(sort, direction) |> restrict_future_events(future) |> allow_unlisted(unlisted) @@ -348,7 +349,7 @@ defmodule Mobilizon.Events do ), preload: [:organizer_actor] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) total = Task.async(fn -> Repo.aggregate(query, :count, :id) end) elements = Task.async(fn -> Repo.all(query) end) @@ -493,7 +494,7 @@ defmodule Mobilizon.Events do def list_tags(page \\ nil, limit \\ nil) do Repo.all( Tag - |> paginate(page, limit) + |> Page.paginate(page, limit) ) end @@ -514,7 +515,7 @@ defmodule Mobilizon.Events do on: t.id == e.tag_id, where: e.event_id == ^id ) - |> paginate(page, limit) + |> Page.paginate(page, limit) ) end @@ -744,7 +745,7 @@ defmodule Mobilizon.Events do where: e.uuid == ^uuid, preload: [:actor] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) end @doc """ @@ -769,7 +770,7 @@ defmodule Mobilizon.Events do where: a.id == ^id and p.role != ^:not_approved, preload: [:picture, :tags] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) ) end @@ -789,7 +790,7 @@ defmodule Mobilizon.Events do where: p.event_id == ^id and p.role == ^:creator, preload: [:actor] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) ) end @@ -1159,7 +1160,7 @@ defmodule Mobilizon.Events do :event ] ) - |> paginate(page, limit) + |> Page.paginate(page, limit) comments = Repo.all(query) diff --git a/lib/mobilizon/events/tag.ex b/lib/mobilizon/events/tag.ex index fafcfe157..5d38dec3e 100644 --- a/lib/mobilizon/events/tag.ex +++ b/lib/mobilizon/events/tag.ex @@ -1,36 +1,3 @@ -defmodule Mobilizon.Events.Tag.TitleSlug do - @moduledoc """ - Generates slugs for tags - """ - alias Mobilizon.Events.Tag - import Ecto.Query - alias Mobilizon.Repo - use EctoAutoslugField.Slug, from: :title, to: :slug - - def build_slug(sources, changeset) do - slug = super(sources, changeset) - build_unique_slug(slug, changeset) - end - - defp build_unique_slug(slug, changeset) do - query = - from( - t in Tag, - where: t.slug == ^slug - ) - - case Repo.one(query) do - nil -> - slug - - _tag -> - slug - |> Mobilizon.Ecto.increment_slug() - |> build_unique_slug(changeset) - end - end -end - defmodule Mobilizon.Events.Tag do @moduledoc """ Represents a tag for events @@ -57,4 +24,17 @@ defmodule Mobilizon.Events.Tag do |> validate_required([:title, :slug]) |> TitleSlug.unique_constraint() end + + def increment_slug(slug) do + case List.pop_at(String.split(slug, "-"), -1) do + {nil, _} -> + slug + + {suffix, slug_parts} -> + case Integer.parse(suffix) do + {id, _} -> Enum.join(slug_parts, "-") <> "-" <> Integer.to_string(id + 1) + :error -> slug <> "-1" + end + end + end end diff --git a/lib/mobilizon/events/tag/title_slug.ex b/lib/mobilizon/events/tag/title_slug.ex new file mode 100644 index 000000000..9c64841d0 --- /dev/null +++ b/lib/mobilizon/events/tag/title_slug.ex @@ -0,0 +1,33 @@ +defmodule Mobilizon.Events.Tag.TitleSlug do + @moduledoc """ + Generates slugs for tags + """ + + alias Mobilizon.Events.Tag + import Ecto.Query + alias Mobilizon.Storage.Repo + use EctoAutoslugField.Slug, from: :title, to: :slug + + def build_slug(sources, changeset) do + slug = super(sources, changeset) + build_unique_slug(slug, changeset) + end + + defp build_unique_slug(slug, changeset) do + query = + from( + t in Tag, + where: t.slug == ^slug + ) + + case Repo.one(query) do + nil -> + slug + + _tag -> + slug + |> Tag.increment_slug() + |> build_unique_slug(changeset) + end + end +end diff --git a/lib/mobilizon/media/media.ex b/lib/mobilizon/media/media.ex index 70762091b..73533934d 100644 --- a/lib/mobilizon/media/media.ex +++ b/lib/mobilizon/media/media.ex @@ -8,11 +8,11 @@ defmodule Mobilizon.Media do alias Ecto.Multi alias Mobilizon.Media.{File, Picture} - alias Mobilizon.Repo + alias Mobilizon.Storage.Repo @doc false @spec data :: Dataloader.Ecto.t() - def data, do: Dataloader.Ecto.new(Mobilizon.Repo, query: &query/2) + def data, do: Dataloader.Ecto.new(Repo, query: &query/2) @doc false @spec query(Ecto.Query.t(), map) :: Ecto.Query.t() diff --git a/lib/mobilizon/postgrex_types.ex b/lib/mobilizon/postgrex_types.ex deleted file mode 100644 index d06805578..000000000 --- a/lib/mobilizon/postgrex_types.ex +++ /dev/null @@ -1,5 +0,0 @@ -Postgrex.Types.define( - Mobilizon.PostgresTypes, - [Geo.PostGIS.Extension] ++ Ecto.Adapters.Postgres.extensions(), - json: Jason -) diff --git a/lib/mobilizon/reports/reports.ex b/lib/mobilizon/reports/reports.ex index 173dc5fa7..055e2ee91 100644 --- a/lib/mobilizon/reports/reports.ex +++ b/lib/mobilizon/reports/reports.ex @@ -6,16 +6,16 @@ defmodule Mobilizon.Reports do import Ecto.Query import EctoEnum - import Mobilizon.Ecto + import Mobilizon.Storage.Ecto - alias Mobilizon.{Page, Repo} alias Mobilizon.Reports.{Note, Report} + alias Mobilizon.Storage.{Page, Repo} defenum(ReportStatus, :report_status, [:open, :closed, :resolved]) @doc false @spec data :: Dataloader.Ecto.t() - def data, do: Dataloader.Ecto.new(Mobilizon.Repo, query: &query/2) + def data, do: Dataloader.Ecto.new(Repo, query: &query/2) @doc false @spec query(Ecto.Query.t(), map) :: Ecto.Query.t() diff --git a/lib/mobilizon/storage/ecto.ex b/lib/mobilizon/storage/ecto.ex new file mode 100644 index 000000000..3a1dc5411 --- /dev/null +++ b/lib/mobilizon/storage/ecto.ex @@ -0,0 +1,15 @@ +defmodule Mobilizon.Storage.Ecto do + @moduledoc """ + Mobilizon Ecto utils + """ + + import Ecto.Query, warn: false + + @doc """ + Adds sort to the query. + """ + @spec sort(Ecto.Query.t(), atom, atom) :: Ecto.Query.t() + def sort(query, sort, direction) do + from(query, order_by: [{^direction, ^sort}]) + end +end diff --git a/lib/mobilizon/page.ex b/lib/mobilizon/storage/page.ex similarity index 84% rename from lib/mobilizon/page.ex rename to lib/mobilizon/storage/page.ex index ce2d440b5..af20d4a7b 100644 --- a/lib/mobilizon/page.ex +++ b/lib/mobilizon/storage/page.ex @@ -1,11 +1,11 @@ -defmodule Mobilizon.Page do +defmodule Mobilizon.Storage.Page do @moduledoc """ Module for pagination of queries. """ - import Ecto.Query, warn: false + import Ecto.Query - alias Mobilizon.Repo + alias Mobilizon.Storage.Repo defstruct [ :total, @@ -36,7 +36,7 @@ defmodule Mobilizon.Page do @doc """ Add limit and offset to the query. """ - @spec paginate(Ecto.Query.t(), integer | nil, integer | nil) :: Ecto.Query.t() + @spec paginate(Ecto.Query.t() | struct, integer | nil, integer | nil) :: Ecto.Query.t() def paginate(query, page \\ 1, size \\ 10) def paginate(query, page, _size) when is_nil(page), do: paginate(query) diff --git a/lib/mobilizon/storage/postgrex_types.ex b/lib/mobilizon/storage/postgrex_types.ex new file mode 100644 index 000000000..a260d062d --- /dev/null +++ b/lib/mobilizon/storage/postgrex_types.ex @@ -0,0 +1,5 @@ +Postgrex.Types.define( + Mobilizon.Storage.PostgresTypes, + [Geo.PostGIS.Extension | Ecto.Adapters.Postgres.extensions()], + json: Jason +) diff --git a/lib/mobilizon/repo.ex b/lib/mobilizon/storage/repo.ex similarity index 61% rename from lib/mobilizon/repo.ex rename to lib/mobilizon/storage/repo.ex index d25e5b21b..8cdde1a45 100644 --- a/lib/mobilizon/repo.ex +++ b/lib/mobilizon/storage/repo.ex @@ -1,14 +1,14 @@ -defmodule Mobilizon.Repo do +defmodule Mobilizon.Storage.Repo do @moduledoc """ - Mobilizon Repo + Mobilizon Repo. """ + use Ecto.Repo, otp_app: :mobilizon, adapter: Ecto.Adapters.Postgres @doc """ - Dynamically loads the repository url from the - DATABASE_URL environment variable. + Dynamically loads the repository url from the DATABASE_URL environment variable. """ def init(_, opts) do {:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))} diff --git a/lib/mobilizon/users/users.ex b/lib/mobilizon/users/users.ex index af515c2cf..c03fe3588 100644 --- a/lib/mobilizon/users/users.ex +++ b/lib/mobilizon/users/users.ex @@ -6,11 +6,11 @@ defmodule Mobilizon.Users do import Ecto.Query import EctoEnum - import Mobilizon.Ecto + import Mobilizon.Storage.Ecto alias Mobilizon.Actors.Actor alias Mobilizon.Events - alias Mobilizon.{Page, Repo} + alias Mobilizon.Storage.{Page, Repo} alias Mobilizon.Users.User @type tokens :: %{ @@ -22,7 +22,7 @@ defmodule Mobilizon.Users do @doc false @spec data :: Dataloader.Ecto.t() - def data, do: Dataloader.Ecto.new(Mobilizon.Repo, query: &query/2) + def data, do: Dataloader.Ecto.new(Repo, query: &query/2) @doc false @spec query(Ecto.Query.t(), map) :: Ecto.Query.t() diff --git a/lib/service/activity_pub/utils.ex b/lib/service/activity_pub/utils.ex index d68c62287..db8757bb6 100644 --- a/lib/service/activity_pub/utils.ex +++ b/lib/service/activity_pub/utils.ex @@ -10,7 +10,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do Various utils """ - alias Mobilizon.Repo + alias Mobilizon.Storage.Repo alias Mobilizon.Addresses alias Mobilizon.Addresses.Address alias Mobilizon.Actors @@ -165,7 +165,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do Enum.each(Users.list_moderators(), fn moderator -> moderator |> Mobilizon.Email.Admin.report(moderator, report) - |> Mobilizon.Mailer.deliver_later() + |> Mobilizon.Email.Mailer.deliver_later() end) {:ok, report} diff --git a/lib/service/users/activation.ex b/lib/service/users/activation.ex index c89d2ec95..a111c5698 100644 --- a/lib/service/users/activation.ex +++ b/lib/service/users/activation.ex @@ -1,9 +1,10 @@ defmodule Mobilizon.Service.Users.Activation do @moduledoc false - alias Mobilizon.{Mailer, Users} - alias Mobilizon.Users.User + alias Mobilizon.Email.Mailer alias Mobilizon.Email.User, as: UserEmail + alias Mobilizon.Users + alias Mobilizon.Users.User alias Mobilizon.Service.Users.Tools require Logger diff --git a/lib/service/users/reset_password.ex b/lib/service/users/reset_password.ex index e34565166..dd6341877 100644 --- a/lib/service/users/reset_password.ex +++ b/lib/service/users/reset_password.ex @@ -1,12 +1,14 @@ defmodule Mobilizon.Service.Users.ResetPassword do @moduledoc false - require Logger - - alias Mobilizon.Users.User - alias Mobilizon.{Mailer, Repo, Users} + alias Mobilizon.Email.Mailer alias Mobilizon.Email.User, as: UserEmail alias Mobilizon.Service.Users.Tools + alias Mobilizon.Storage.Repo + alias Mobilizon.Users + alias Mobilizon.Users.User + + require Logger @doc """ Check that the provided token is correct and update provided password diff --git a/mix.exs b/mix.exs index f54af6661..40eb97754 100644 --- a/mix.exs +++ b/mix.exs @@ -203,8 +203,8 @@ defmodule Mobilizon.Mixfile do Mobilizon.Users.UserRole, Mobilizon.Users.Guards, Mobilizon.Activity, - Mobilizon.Ecto, - Mobilizon.Repo + Mobilizon.Storage.Ecto, + Mobilizon.Storage.Repo ], APIs: [ MobilizonWeb.API.Comments, @@ -299,7 +299,7 @@ defmodule Mobilizon.Mixfile do Tools: [ Mobilizon.Application, Mobilizon.Factory, - Mobilizon.Mailer, + Mobilizon.Email.Mailer, Mobilizon.EmailView, Mobilizon.Email.User ] diff --git a/priv/repo/seeds.exs b/priv/repo/seeds.exs index f3367609b..9ef0b716a 100644 --- a/priv/repo/seeds.exs +++ b/priv/repo/seeds.exs @@ -5,7 +5,7 @@ # Inside the script, you can read and write to any of your # repositories directly: # -# Mobilizon.Repo.insert!(%Mobilizon.SomeSchema{}) +# Mobilizon.Storage.Repo.insert!(%Mobilizon.SomeSchema{}) # # We recommend using the bang functions (`insert!`, `update!` # and so on) as they will fail if something goes wrong. diff --git a/test/support/channel_case.ex b/test/support/channel_case.ex index cc77f22f4..c9b207b83 100644 --- a/test/support/channel_case.ex +++ b/test/support/channel_case.ex @@ -26,10 +26,10 @@ defmodule MobilizonWeb.ChannelCase do end setup tags do - :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Repo) + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo) unless tags[:async] do - Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Repo, {:shared, self()}) + Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()}) end :ok diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 170d6e9e6..eae09e0c7 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -35,10 +35,10 @@ defmodule MobilizonWeb.ConnCase do end setup tags do - :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Repo) + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo) unless tags[:async] do - Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Repo, {:shared, self()}) + Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()}) end {:ok, conn: Phoenix.ConnTest.build_conn()} diff --git a/test/support/data_case.ex b/test/support/data_case.ex index b4c47f9f5..76f4d26dc 100644 --- a/test/support/data_case.ex +++ b/test/support/data_case.ex @@ -18,7 +18,7 @@ defmodule Mobilizon.DataCase do using do quote do - alias Mobilizon.Repo + alias Mobilizon.Storage.Repo import Ecto import Ecto.Changeset @@ -28,10 +28,10 @@ defmodule Mobilizon.DataCase do end setup tags do - :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Repo) + :ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo) unless tags[:async] do - Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Repo, {:shared, self()}) + Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()}) end :ok diff --git a/test/support/factory.ex b/test/support/factory.ex index e91cd42a5..97974657d 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -3,7 +3,7 @@ defmodule Mobilizon.Factory do Factory for fixtures with ExMachina """ # with Ecto - use ExMachina.Ecto, repo: Mobilizon.Repo + use ExMachina.Ecto, repo: Mobilizon.Storage.Repo alias Mobilizon.Actors.Actor alias MobilizonWeb.Router.Helpers, as: Routes alias MobilizonWeb.Endpoint diff --git a/test/test_helper.exs b/test/test_helper.exs index 43b41df65..ee5409205 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -3,4 +3,4 @@ ExUnit.configure(formatters: [ExUnit.CLIFormatter, ExUnitNotifier]) ExUnit.start() -Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Repo, :manual) +Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, :manual)