Separating of Storage context
This commit is contained in:
parent
86a0630a7d
commit
fa037fd683
@ -7,7 +7,7 @@ use Mix.Config
|
|||||||
|
|
||||||
# General application configuration
|
# General application configuration
|
||||||
config :mobilizon,
|
config :mobilizon,
|
||||||
ecto_repos: [Mobilizon.Repo]
|
ecto_repos: [Mobilizon.Storage.Repo]
|
||||||
|
|
||||||
config :mobilizon, :instance,
|
config :mobilizon, :instance,
|
||||||
name: System.get_env("MOBILIZON_INSTANCE_NAME") || "Localhost",
|
name: System.get_env("MOBILIZON_INSTANCE_NAME") || "Localhost",
|
||||||
@ -78,7 +78,7 @@ config :mobilizon, MobilizonWeb.Guardian,
|
|||||||
secret_key: "ty0WM7YBE3ojvxoUQxo8AERrNpfbXnIJ82ovkPdqbUFw31T5LcK8wGjaOiReVQjo"
|
secret_key: "ty0WM7YBE3ojvxoUQxo8AERrNpfbXnIJ82ovkPdqbUFw31T5LcK8wGjaOiReVQjo"
|
||||||
|
|
||||||
config :guardian, Guardian.DB,
|
config :guardian, Guardian.DB,
|
||||||
repo: Mobilizon.Repo,
|
repo: Mobilizon.Storage.Repo,
|
||||||
# default
|
# default
|
||||||
schema_name: "guardian_tokens",
|
schema_name: "guardian_tokens",
|
||||||
# store all token types if not set
|
# store all token types if not set
|
||||||
|
@ -61,11 +61,11 @@ config :phoenix, :stacktrace_depth, 20
|
|||||||
# Initialize plugs at runtime for faster development compilation
|
# Initialize plugs at runtime for faster development compilation
|
||||||
config :phoenix, :plug_init_mode, :runtime
|
config :phoenix, :plug_init_mode, :runtime
|
||||||
|
|
||||||
config :mobilizon, Mobilizon.Mailer, adapter: Bamboo.LocalAdapter
|
config :mobilizon, Mobilizon.Email.Mailer, adapter: Bamboo.LocalAdapter
|
||||||
|
|
||||||
# Configure your database
|
# Configure your database
|
||||||
config :mobilizon, Mobilizon.Repo,
|
config :mobilizon, Mobilizon.Storage.Repo,
|
||||||
types: Mobilizon.PostgresTypes,
|
types: Mobilizon.Storage.PostgresTypes,
|
||||||
username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon",
|
username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon",
|
||||||
password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon",
|
password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon",
|
||||||
database: System.get_env("MOBILIZON_DATABASE_DBNAME") || "mobilizon_dev",
|
database: System.get_env("MOBILIZON_DATABASE_DBNAME") || "mobilizon_dev",
|
||||||
|
@ -12,8 +12,8 @@ config :mobilizon, MobilizonWeb.Endpoint,
|
|||||||
cache_static_manifest: "priv/static/manifest.json"
|
cache_static_manifest: "priv/static/manifest.json"
|
||||||
|
|
||||||
# Configure your database
|
# Configure your database
|
||||||
config :mobilizon, Mobilizon.Repo,
|
config :mobilizon, Mobilizon.Storage.Repo,
|
||||||
types: Mobilizon.PostgresTypes,
|
types: Mobilizon.Storage.PostgresTypes,
|
||||||
username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon",
|
username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon",
|
||||||
password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon",
|
password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon",
|
||||||
database: System.get_env("MOBILIZON_DATABASE_DBNAME") || "mobilizon_prod",
|
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",
|
port: System.get_env("MOBILIZON_DATABASE_PORT") || "5432",
|
||||||
pool_size: 15
|
pool_size: 15
|
||||||
|
|
||||||
config :mobilizon, Mobilizon.Mailer,
|
config :mobilizon, Mobilizon.Email.Mailer,
|
||||||
adapter: Bamboo.SMTPAdapter,
|
adapter: Bamboo.SMTPAdapter,
|
||||||
server: "localhost",
|
server: "localhost",
|
||||||
hostname: "localhost",
|
hostname: "localhost",
|
||||||
|
@ -22,16 +22,15 @@ config :logger,
|
|||||||
level: :info
|
level: :info
|
||||||
|
|
||||||
# Configure your database
|
# Configure your database
|
||||||
config :mobilizon, Mobilizon.Repo,
|
config :mobilizon, Mobilizon.Storage.Repo,
|
||||||
types: Mobilizon.PostgresTypes,
|
types: Mobilizon.Storage.PostgresTypes,
|
||||||
username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon",
|
username: System.get_env("MOBILIZON_DATABASE_USERNAME") || "mobilizon",
|
||||||
password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon",
|
password: System.get_env("MOBILIZON_DATABASE_PASSWORD") || "mobilizon",
|
||||||
database: System.get_env("MOBILIZON_DATABASE_DBNAME") || "mobilizon_test",
|
database: System.get_env("MOBILIZON_DATABASE_DBNAME") || "mobilizon_test",
|
||||||
hostname: System.get_env("MOBILIZON_DATABASE_HOST") || "localhost",
|
hostname: System.get_env("MOBILIZON_DATABASE_HOST") || "localhost",
|
||||||
pool: Ecto.Adapters.SQL.Sandbox,
|
pool: Ecto.Adapters.SQL.Sandbox
|
||||||
types: Mobilizon.PostgresTypes
|
|
||||||
|
|
||||||
config :mobilizon, Mobilizon.Mailer, adapter: Bamboo.TestAdapter
|
config :mobilizon, Mobilizon.Email.Mailer, adapter: Bamboo.TestAdapter
|
||||||
|
|
||||||
config :mobilizon, MobilizonWeb.Upload, filters: [], link_name: false
|
config :mobilizon, MobilizonWeb.Upload, filters: [], link_name: false
|
||||||
|
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
defmodule Mobilizon do
|
defmodule Mobilizon do
|
||||||
@moduledoc """
|
@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
|
end
|
||||||
|
@ -26,25 +26,24 @@ defmodule Mobilizon.Actors.Actor do
|
|||||||
@moduledoc """
|
@moduledoc """
|
||||||
Represents an actor (local and remote actors)
|
Represents an actor (local and remote actors)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
alias Mobilizon.Actors
|
alias Mobilizon.Actors
|
||||||
alias Mobilizon.Actors.{Actor, Follower, Member}
|
alias Mobilizon.Actors.{Actor, Follower, Member}
|
||||||
alias Mobilizon.Config
|
alias Mobilizon.Config
|
||||||
alias Mobilizon.Events.{Event, FeedToken}
|
alias Mobilizon.Events.{Event, FeedToken}
|
||||||
alias Mobilizon.Media.File
|
alias Mobilizon.Media.File
|
||||||
alias Mobilizon.Users.User
|
|
||||||
|
|
||||||
alias Mobilizon.Reports.{Report, Note}
|
alias Mobilizon.Reports.{Report, Note}
|
||||||
|
alias Mobilizon.Storage.{Page, Repo}
|
||||||
|
alias Mobilizon.Users.User
|
||||||
|
|
||||||
alias MobilizonWeb.Router.Helpers, as: Routes
|
alias MobilizonWeb.Router.Helpers, as: Routes
|
||||||
alias MobilizonWeb.Endpoint
|
alias MobilizonWeb.Endpoint
|
||||||
|
|
||||||
import Ecto.Query
|
|
||||||
import Mobilizon.Ecto
|
|
||||||
alias Mobilizon.Repo
|
|
||||||
|
|
||||||
require Logger
|
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}
|
# @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)
|
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)}
|
%{total: Task.await(total), elements: Task.await(elements)}
|
||||||
end
|
end
|
||||||
@ -428,7 +427,7 @@ defmodule Mobilizon.Actors.Actor do
|
|||||||
)
|
)
|
||||||
|
|
||||||
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
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)}
|
%{total: Task.await(total), elements: Task.await(elements)}
|
||||||
end
|
end
|
||||||
|
@ -3,16 +3,15 @@ defmodule Mobilizon.Actors do
|
|||||||
The Actors context.
|
The Actors context.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query
|
||||||
import Mobilizon.Ecto
|
|
||||||
|
|
||||||
alias Mobilizon.Repo
|
alias Ecto.Multi
|
||||||
|
|
||||||
alias Mobilizon.Actors.{Actor, Bot, Member, Follower}
|
alias Mobilizon.Actors.{Actor, Bot, Member, Follower}
|
||||||
alias Mobilizon.Media.File
|
alias Mobilizon.Media.File
|
||||||
alias Ecto.Multi
|
|
||||||
|
|
||||||
alias Mobilizon.Service.ActivityPub
|
alias Mobilizon.Service.ActivityPub
|
||||||
|
alias Mobilizon.Storage.{Page, Repo}
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
@ -203,7 +202,7 @@ defmodule Mobilizon.Actors do
|
|||||||
where: a.type == ^:Group,
|
where: a.type == ^:Group,
|
||||||
where: a.visibility in [^:public, ^:unlisted]
|
where: a.visibility in [^:public, ^:unlisted]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -586,7 +585,7 @@ defmodule Mobilizon.Actors do
|
|||||||
^username
|
^username
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
|
|
||||||
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
||||||
elements = Task.async(fn -> Repo.all(query) end)
|
elements = Task.async(fn -> Repo.all(query) end)
|
||||||
@ -652,7 +651,7 @@ defmodule Mobilizon.Actors do
|
|||||||
})
|
})
|
||||||
|
|
||||||
try do
|
try do
|
||||||
Mobilizon.Repo.insert!(actor)
|
Repo.insert!(actor)
|
||||||
rescue
|
rescue
|
||||||
e in Ecto.InvalidChangesetError ->
|
e in Ecto.InvalidChangesetError ->
|
||||||
{:error, e.changeset}
|
{:error, e.changeset}
|
||||||
|
@ -12,15 +12,14 @@ defmodule Mobilizon.Actors.Member do
|
|||||||
@moduledoc """
|
@moduledoc """
|
||||||
Represents the membership of an actor to a group
|
Represents the membership of an actor to a group
|
||||||
"""
|
"""
|
||||||
|
|
||||||
use Ecto.Schema
|
use Ecto.Schema
|
||||||
|
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query
|
||||||
import Mobilizon.Ecto
|
|
||||||
|
|
||||||
alias Mobilizon.Actors.Member
|
alias Mobilizon.Actors.{Actor, Member}
|
||||||
alias Mobilizon.Actors.Actor
|
alias Mobilizon.Storage.{Page, Repo}
|
||||||
alias Mobilizon.Repo
|
|
||||||
|
|
||||||
schema "members" do
|
schema "members" do
|
||||||
field(:role, Mobilizon.Actors.MemberRoleEnum, default: :member)
|
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),
|
where: m.parent_id == ^id and (m.role == ^:creator or m.role == ^:administrator),
|
||||||
preload: [:actor]
|
preload: [:actor]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -4,10 +4,11 @@ defmodule Mobilizon.Addresses do
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias Mobilizon.Repo
|
|
||||||
require Logger
|
|
||||||
|
|
||||||
alias Mobilizon.Addresses.Address
|
alias Mobilizon.Addresses.Address
|
||||||
|
alias Mobilizon.Storage.Repo
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
@geom_types [:point]
|
@geom_types [:point]
|
||||||
|
|
||||||
|
@ -3,11 +3,10 @@ defmodule Mobilizon.Admin do
|
|||||||
The Admin context.
|
The Admin context.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query
|
||||||
alias Mobilizon.Repo
|
|
||||||
import Mobilizon.Ecto
|
|
||||||
|
|
||||||
alias Mobilizon.Admin.ActionLog
|
alias Mobilizon.Admin.ActionLog
|
||||||
|
alias Mobilizon.Storage.{Page, Repo}
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns the list of action_logs.
|
Returns the list of action_logs.
|
||||||
@ -24,7 +23,7 @@ defmodule Mobilizon.Admin do
|
|||||||
r in ActionLog,
|
r in ActionLog,
|
||||||
preload: [:actor]
|
preload: [:actor]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ defmodule Mobilizon.Application do
|
|||||||
# Define workers and child supervisors to be supervised
|
# Define workers and child supervisors to be supervised
|
||||||
children = [
|
children = [
|
||||||
# Start the Ecto repository
|
# Start the Ecto repository
|
||||||
supervisor(Mobilizon.Repo, []),
|
supervisor(Mobilizon.Storage.Repo, []),
|
||||||
# Start the endpoint when the application starts
|
# Start the endpoint when the application starts
|
||||||
supervisor(MobilizonWeb.Endpoint, []),
|
supervisor(MobilizonWeb.Endpoint, []),
|
||||||
# Start your own worker by calling: Mobilizon.Worker.start_link(arg1, arg2, arg3)
|
# Start your own worker by calling: Mobilizon.Worker.start_link(arg1, arg2, arg3)
|
||||||
|
@ -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
|
|
@ -1,4 +1,4 @@
|
|||||||
defmodule Mobilizon.Mailer do
|
defmodule Mobilizon.Email.Mailer do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Mobilizon Mailer.
|
Mobilizon Mailer.
|
||||||
"""
|
"""
|
@ -3,17 +3,18 @@ defmodule Mobilizon.Events do
|
|||||||
The Events context.
|
The Events context.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query
|
||||||
import Mobilizon.Ecto
|
|
||||||
|
import Mobilizon.Storage.Ecto
|
||||||
|
|
||||||
alias Mobilizon.Repo
|
|
||||||
alias Mobilizon.Events.{Event, Comment, Participant}
|
|
||||||
alias Mobilizon.Actors.Actor
|
alias Mobilizon.Actors.Actor
|
||||||
alias Mobilizon.Users.User
|
|
||||||
alias Mobilizon.Addresses.Address
|
alias Mobilizon.Addresses.Address
|
||||||
|
alias Mobilizon.Events.{Event, Comment, Participant}
|
||||||
|
alias Mobilizon.Storage.{Page, Repo}
|
||||||
|
alias Mobilizon.Users.User
|
||||||
|
|
||||||
def data() do
|
def data() do
|
||||||
Dataloader.Ecto.new(Mobilizon.Repo, query: &query/2)
|
Dataloader.Ecto.new(Repo, query: &query/2)
|
||||||
end
|
end
|
||||||
|
|
||||||
def query(queryable, _params) do
|
def query(queryable, _params) do
|
||||||
@ -36,7 +37,7 @@ defmodule Mobilizon.Events do
|
|||||||
:picture
|
:picture
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
|
|
||||||
events = Repo.all(query)
|
events = Repo.all(query)
|
||||||
|
|
||||||
@ -301,7 +302,7 @@ defmodule Mobilizon.Events do
|
|||||||
e in Event,
|
e in Event,
|
||||||
preload: [:organizer_actor, :participants]
|
preload: [:organizer_actor, :participants]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
|> sort(sort, direction)
|
|> sort(sort, direction)
|
||||||
|> restrict_future_events(future)
|
|> restrict_future_events(future)
|
||||||
|> allow_unlisted(unlisted)
|
|> allow_unlisted(unlisted)
|
||||||
@ -348,7 +349,7 @@ defmodule Mobilizon.Events do
|
|||||||
),
|
),
|
||||||
preload: [:organizer_actor]
|
preload: [:organizer_actor]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
|
|
||||||
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
total = Task.async(fn -> Repo.aggregate(query, :count, :id) end)
|
||||||
elements = Task.async(fn -> Repo.all(query) 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
|
def list_tags(page \\ nil, limit \\ nil) do
|
||||||
Repo.all(
|
Repo.all(
|
||||||
Tag
|
Tag
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -514,7 +515,7 @@ defmodule Mobilizon.Events do
|
|||||||
on: t.id == e.tag_id,
|
on: t.id == e.tag_id,
|
||||||
where: e.event_id == ^id
|
where: e.event_id == ^id
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -744,7 +745,7 @@ defmodule Mobilizon.Events do
|
|||||||
where: e.uuid == ^uuid,
|
where: e.uuid == ^uuid,
|
||||||
preload: [:actor]
|
preload: [:actor]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
@ -769,7 +770,7 @@ defmodule Mobilizon.Events do
|
|||||||
where: a.id == ^id and p.role != ^:not_approved,
|
where: a.id == ^id and p.role != ^:not_approved,
|
||||||
preload: [:picture, :tags]
|
preload: [:picture, :tags]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -789,7 +790,7 @@ defmodule Mobilizon.Events do
|
|||||||
where: p.event_id == ^id and p.role == ^:creator,
|
where: p.event_id == ^id and p.role == ^:creator,
|
||||||
preload: [:actor]
|
preload: [:actor]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -1159,7 +1160,7 @@ defmodule Mobilizon.Events do
|
|||||||
:event
|
:event
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|> paginate(page, limit)
|
|> Page.paginate(page, limit)
|
||||||
|
|
||||||
comments = Repo.all(query)
|
comments = Repo.all(query)
|
||||||
|
|
||||||
|
@ -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
|
defmodule Mobilizon.Events.Tag do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Represents a tag for events
|
Represents a tag for events
|
||||||
@ -57,4 +24,17 @@ defmodule Mobilizon.Events.Tag do
|
|||||||
|> validate_required([:title, :slug])
|
|> validate_required([:title, :slug])
|
||||||
|> TitleSlug.unique_constraint()
|
|> TitleSlug.unique_constraint()
|
||||||
end
|
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
|
end
|
||||||
|
33
lib/mobilizon/events/tag/title_slug.ex
Normal file
33
lib/mobilizon/events/tag/title_slug.ex
Normal file
@ -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
|
@ -8,11 +8,11 @@ defmodule Mobilizon.Media do
|
|||||||
alias Ecto.Multi
|
alias Ecto.Multi
|
||||||
|
|
||||||
alias Mobilizon.Media.{File, Picture}
|
alias Mobilizon.Media.{File, Picture}
|
||||||
alias Mobilizon.Repo
|
alias Mobilizon.Storage.Repo
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
@spec data :: Dataloader.Ecto.t()
|
@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
|
@doc false
|
||||||
@spec query(Ecto.Query.t(), map) :: Ecto.Query.t()
|
@spec query(Ecto.Query.t(), map) :: Ecto.Query.t()
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
Postgrex.Types.define(
|
|
||||||
Mobilizon.PostgresTypes,
|
|
||||||
[Geo.PostGIS.Extension] ++ Ecto.Adapters.Postgres.extensions(),
|
|
||||||
json: Jason
|
|
||||||
)
|
|
@ -6,16 +6,16 @@ defmodule Mobilizon.Reports do
|
|||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
import EctoEnum
|
import EctoEnum
|
||||||
|
|
||||||
import Mobilizon.Ecto
|
import Mobilizon.Storage.Ecto
|
||||||
|
|
||||||
alias Mobilizon.{Page, Repo}
|
|
||||||
alias Mobilizon.Reports.{Note, Report}
|
alias Mobilizon.Reports.{Note, Report}
|
||||||
|
alias Mobilizon.Storage.{Page, Repo}
|
||||||
|
|
||||||
defenum(ReportStatus, :report_status, [:open, :closed, :resolved])
|
defenum(ReportStatus, :report_status, [:open, :closed, :resolved])
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
@spec data :: Dataloader.Ecto.t()
|
@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
|
@doc false
|
||||||
@spec query(Ecto.Query.t(), map) :: Ecto.Query.t()
|
@spec query(Ecto.Query.t(), map) :: Ecto.Query.t()
|
||||||
|
15
lib/mobilizon/storage/ecto.ex
Normal file
15
lib/mobilizon/storage/ecto.ex
Normal file
@ -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
|
@ -1,11 +1,11 @@
|
|||||||
defmodule Mobilizon.Page do
|
defmodule Mobilizon.Storage.Page do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Module for pagination of queries.
|
Module for pagination of queries.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query
|
||||||
|
|
||||||
alias Mobilizon.Repo
|
alias Mobilizon.Storage.Repo
|
||||||
|
|
||||||
defstruct [
|
defstruct [
|
||||||
:total,
|
:total,
|
||||||
@ -36,7 +36,7 @@ defmodule Mobilizon.Page do
|
|||||||
@doc """
|
@doc """
|
||||||
Add limit and offset to the query.
|
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 \\ 1, size \\ 10)
|
||||||
|
|
||||||
def paginate(query, page, _size) when is_nil(page), do: paginate(query)
|
def paginate(query, page, _size) when is_nil(page), do: paginate(query)
|
5
lib/mobilizon/storage/postgrex_types.ex
Normal file
5
lib/mobilizon/storage/postgrex_types.ex
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
Postgrex.Types.define(
|
||||||
|
Mobilizon.Storage.PostgresTypes,
|
||||||
|
[Geo.PostGIS.Extension | Ecto.Adapters.Postgres.extensions()],
|
||||||
|
json: Jason
|
||||||
|
)
|
@ -1,14 +1,14 @@
|
|||||||
defmodule Mobilizon.Repo do
|
defmodule Mobilizon.Storage.Repo do
|
||||||
@moduledoc """
|
@moduledoc """
|
||||||
Mobilizon Repo
|
Mobilizon Repo.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
use Ecto.Repo,
|
use Ecto.Repo,
|
||||||
otp_app: :mobilizon,
|
otp_app: :mobilizon,
|
||||||
adapter: Ecto.Adapters.Postgres
|
adapter: Ecto.Adapters.Postgres
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Dynamically loads the repository url from the
|
Dynamically loads the repository url from the DATABASE_URL environment variable.
|
||||||
DATABASE_URL environment variable.
|
|
||||||
"""
|
"""
|
||||||
def init(_, opts) do
|
def init(_, opts) do
|
||||||
{:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
|
{:ok, Keyword.put(opts, :url, System.get_env("DATABASE_URL"))}
|
@ -6,11 +6,11 @@ defmodule Mobilizon.Users do
|
|||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
import EctoEnum
|
import EctoEnum
|
||||||
|
|
||||||
import Mobilizon.Ecto
|
import Mobilizon.Storage.Ecto
|
||||||
|
|
||||||
alias Mobilizon.Actors.Actor
|
alias Mobilizon.Actors.Actor
|
||||||
alias Mobilizon.Events
|
alias Mobilizon.Events
|
||||||
alias Mobilizon.{Page, Repo}
|
alias Mobilizon.Storage.{Page, Repo}
|
||||||
alias Mobilizon.Users.User
|
alias Mobilizon.Users.User
|
||||||
|
|
||||||
@type tokens :: %{
|
@type tokens :: %{
|
||||||
@ -22,7 +22,7 @@ defmodule Mobilizon.Users do
|
|||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
@spec data :: Dataloader.Ecto.t()
|
@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
|
@doc false
|
||||||
@spec query(Ecto.Query.t(), map) :: Ecto.Query.t()
|
@spec query(Ecto.Query.t(), map) :: Ecto.Query.t()
|
||||||
|
@ -10,7 +10,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
|||||||
Various utils
|
Various utils
|
||||||
"""
|
"""
|
||||||
|
|
||||||
alias Mobilizon.Repo
|
alias Mobilizon.Storage.Repo
|
||||||
alias Mobilizon.Addresses
|
alias Mobilizon.Addresses
|
||||||
alias Mobilizon.Addresses.Address
|
alias Mobilizon.Addresses.Address
|
||||||
alias Mobilizon.Actors
|
alias Mobilizon.Actors
|
||||||
@ -165,7 +165,7 @@ defmodule Mobilizon.Service.ActivityPub.Utils do
|
|||||||
Enum.each(Users.list_moderators(), fn moderator ->
|
Enum.each(Users.list_moderators(), fn moderator ->
|
||||||
moderator
|
moderator
|
||||||
|> Mobilizon.Email.Admin.report(moderator, report)
|
|> Mobilizon.Email.Admin.report(moderator, report)
|
||||||
|> Mobilizon.Mailer.deliver_later()
|
|> Mobilizon.Email.Mailer.deliver_later()
|
||||||
end)
|
end)
|
||||||
|
|
||||||
{:ok, report}
|
{:ok, report}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
defmodule Mobilizon.Service.Users.Activation do
|
defmodule Mobilizon.Service.Users.Activation do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
alias Mobilizon.{Mailer, Users}
|
alias Mobilizon.Email.Mailer
|
||||||
alias Mobilizon.Users.User
|
|
||||||
alias Mobilizon.Email.User, as: UserEmail
|
alias Mobilizon.Email.User, as: UserEmail
|
||||||
|
alias Mobilizon.Users
|
||||||
|
alias Mobilizon.Users.User
|
||||||
alias Mobilizon.Service.Users.Tools
|
alias Mobilizon.Service.Users.Tools
|
||||||
|
|
||||||
require Logger
|
require Logger
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
defmodule Mobilizon.Service.Users.ResetPassword do
|
defmodule Mobilizon.Service.Users.ResetPassword do
|
||||||
@moduledoc false
|
@moduledoc false
|
||||||
|
|
||||||
require Logger
|
alias Mobilizon.Email.Mailer
|
||||||
|
|
||||||
alias Mobilizon.Users.User
|
|
||||||
alias Mobilizon.{Mailer, Repo, Users}
|
|
||||||
alias Mobilizon.Email.User, as: UserEmail
|
alias Mobilizon.Email.User, as: UserEmail
|
||||||
alias Mobilizon.Service.Users.Tools
|
alias Mobilizon.Service.Users.Tools
|
||||||
|
alias Mobilizon.Storage.Repo
|
||||||
|
alias Mobilizon.Users
|
||||||
|
alias Mobilizon.Users.User
|
||||||
|
|
||||||
|
require Logger
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Check that the provided token is correct and update provided password
|
Check that the provided token is correct and update provided password
|
||||||
|
6
mix.exs
6
mix.exs
@ -203,8 +203,8 @@ defmodule Mobilizon.Mixfile do
|
|||||||
Mobilizon.Users.UserRole,
|
Mobilizon.Users.UserRole,
|
||||||
Mobilizon.Users.Guards,
|
Mobilizon.Users.Guards,
|
||||||
Mobilizon.Activity,
|
Mobilizon.Activity,
|
||||||
Mobilizon.Ecto,
|
Mobilizon.Storage.Ecto,
|
||||||
Mobilizon.Repo
|
Mobilizon.Storage.Repo
|
||||||
],
|
],
|
||||||
APIs: [
|
APIs: [
|
||||||
MobilizonWeb.API.Comments,
|
MobilizonWeb.API.Comments,
|
||||||
@ -299,7 +299,7 @@ defmodule Mobilizon.Mixfile do
|
|||||||
Tools: [
|
Tools: [
|
||||||
Mobilizon.Application,
|
Mobilizon.Application,
|
||||||
Mobilizon.Factory,
|
Mobilizon.Factory,
|
||||||
Mobilizon.Mailer,
|
Mobilizon.Email.Mailer,
|
||||||
Mobilizon.EmailView,
|
Mobilizon.EmailView,
|
||||||
Mobilizon.Email.User
|
Mobilizon.Email.User
|
||||||
]
|
]
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
# Inside the script, you can read and write to any of your
|
# Inside the script, you can read and write to any of your
|
||||||
# repositories directly:
|
# repositories directly:
|
||||||
#
|
#
|
||||||
# Mobilizon.Repo.insert!(%Mobilizon.SomeSchema{})
|
# Mobilizon.Storage.Repo.insert!(%Mobilizon.SomeSchema{})
|
||||||
#
|
#
|
||||||
# We recommend using the bang functions (`insert!`, `update!`
|
# We recommend using the bang functions (`insert!`, `update!`
|
||||||
# and so on) as they will fail if something goes wrong.
|
# and so on) as they will fail if something goes wrong.
|
||||||
|
@ -26,10 +26,10 @@ defmodule MobilizonWeb.ChannelCase do
|
|||||||
end
|
end
|
||||||
|
|
||||||
setup tags do
|
setup tags do
|
||||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Repo)
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo)
|
||||||
|
|
||||||
unless tags[:async] do
|
unless tags[:async] do
|
||||||
Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Repo, {:shared, self()})
|
Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()})
|
||||||
end
|
end
|
||||||
|
|
||||||
:ok
|
:ok
|
||||||
|
@ -35,10 +35,10 @@ defmodule MobilizonWeb.ConnCase do
|
|||||||
end
|
end
|
||||||
|
|
||||||
setup tags do
|
setup tags do
|
||||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Repo)
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo)
|
||||||
|
|
||||||
unless tags[:async] do
|
unless tags[:async] do
|
||||||
Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Repo, {:shared, self()})
|
Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()})
|
||||||
end
|
end
|
||||||
|
|
||||||
{:ok, conn: Phoenix.ConnTest.build_conn()}
|
{:ok, conn: Phoenix.ConnTest.build_conn()}
|
||||||
|
@ -18,7 +18,7 @@ defmodule Mobilizon.DataCase do
|
|||||||
|
|
||||||
using do
|
using do
|
||||||
quote do
|
quote do
|
||||||
alias Mobilizon.Repo
|
alias Mobilizon.Storage.Repo
|
||||||
|
|
||||||
import Ecto
|
import Ecto
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
@ -28,10 +28,10 @@ defmodule Mobilizon.DataCase do
|
|||||||
end
|
end
|
||||||
|
|
||||||
setup tags do
|
setup tags do
|
||||||
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Repo)
|
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Mobilizon.Storage.Repo)
|
||||||
|
|
||||||
unless tags[:async] do
|
unless tags[:async] do
|
||||||
Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Repo, {:shared, self()})
|
Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, {:shared, self()})
|
||||||
end
|
end
|
||||||
|
|
||||||
:ok
|
:ok
|
||||||
|
@ -3,7 +3,7 @@ defmodule Mobilizon.Factory do
|
|||||||
Factory for fixtures with ExMachina
|
Factory for fixtures with ExMachina
|
||||||
"""
|
"""
|
||||||
# with Ecto
|
# with Ecto
|
||||||
use ExMachina.Ecto, repo: Mobilizon.Repo
|
use ExMachina.Ecto, repo: Mobilizon.Storage.Repo
|
||||||
alias Mobilizon.Actors.Actor
|
alias Mobilizon.Actors.Actor
|
||||||
alias MobilizonWeb.Router.Helpers, as: Routes
|
alias MobilizonWeb.Router.Helpers, as: Routes
|
||||||
alias MobilizonWeb.Endpoint
|
alias MobilizonWeb.Endpoint
|
||||||
|
@ -3,4 +3,4 @@ ExUnit.configure(formatters: [ExUnit.CLIFormatter, ExUnitNotifier])
|
|||||||
|
|
||||||
ExUnit.start()
|
ExUnit.start()
|
||||||
|
|
||||||
Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Repo, :manual)
|
Ecto.Adapters.SQL.Sandbox.mode(Mobilizon.Storage.Repo, :manual)
|
||||||
|
Loading…
Reference in New Issue
Block a user