Implement Credo software design suggestions

This commit is contained in:
rustra 2020-01-28 19:18:33 +01:00
parent a781c2d3e2
commit 97651e88e9
37 changed files with 146 additions and 97 deletions

View file

@ -32,6 +32,7 @@ defmodule Mobilizon.Federation.ActivityPub do
alias Mobilizon.GraphQL.API.Utils, as: APIUtils
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Email.{Admin, Mailer}
require Logger
@ -318,7 +319,7 @@ defmodule Mobilizon.Federation.ActivityPub do
Convertible.model_to_as(%{follow | actor: follower, target_actor: followed}),
{:ok, follow_activity} <- create_activity(follow_as_data, local),
activity_unfollow_id <-
activity_id || "#{Mobilizon.Web.Endpoint.url()}/unfollow/#{follow_id}/activity",
activity_id || "#{Endpoint.url()}/unfollow/#{follow_id}/activity",
unfollow_data <-
make_unfollow_data(follower, followed, follow_activity, activity_unfollow_id),
{:ok, activity} <- create_activity(unfollow_data, local),
@ -483,7 +484,7 @@ defmodule Mobilizon.Federation.ActivityPub do
# If it's an exclusion it should be something else
"actor" => actor_url,
"object" => event_url,
"id" => "#{Mobilizon.Web.Endpoint.url()}/leave/event/#{participant.id}"
"id" => "#{Endpoint.url()}/leave/event/#{participant.id}"
},
audience <-
Audience.calculate_to_and_cc_from_mentions(participant),
@ -778,7 +779,7 @@ defmodule Mobilizon.Federation.ActivityPub do
make_accept_join_data(
follower_as_data,
Map.merge(additional, %{
"id" => "#{Mobilizon.Web.Endpoint.url()}/accept/follow/#{follower.id}",
"id" => "#{Endpoint.url()}/accept/follow/#{follower.id}",
"to" => [follower.actor.url],
"cc" => [],
"actor" => follower.target_actor.url
@ -797,7 +798,7 @@ defmodule Mobilizon.Federation.ActivityPub do
defp accept_join(%Participant{} = participant, additional) do
with {:ok, %Participant{} = participant} <-
Events.update_participant(participant, %{role: :participant}),
Absinthe.Subscription.publish(Mobilizon.Web.Endpoint, participant.actor,
Absinthe.Subscription.publish(Endpoint, participant.actor,
event_person_participation_changed: participant.actor.id
),
participant_as_data <- Convertible.model_to_as(participant),
@ -807,7 +808,7 @@ defmodule Mobilizon.Federation.ActivityPub do
make_accept_join_data(
participant_as_data,
Map.merge(Map.merge(audience, additional), %{
"id" => "#{Mobilizon.Web.Endpoint.url()}/accept/join/#{participant.id}"
"id" => "#{Endpoint.url()}/accept/join/#{participant.id}"
})
) do
{:ok, participant, update_data}
@ -823,7 +824,7 @@ defmodule Mobilizon.Federation.ActivityPub do
defp reject_join(%Participant{} = participant, additional) do
with {:ok, %Participant{} = participant} <-
Events.update_participant(participant, %{approved: false, role: :rejected}),
Absinthe.Subscription.publish(Mobilizon.Web.Endpoint, participant.actor,
Absinthe.Subscription.publish(Endpoint, participant.actor,
event_person_participation_changed: participant.actor.id
),
participant_as_data <- Convertible.model_to_as(participant),
@ -839,7 +840,7 @@ defmodule Mobilizon.Federation.ActivityPub do
reject_data
|> Map.merge(audience)
|> Map.merge(%{
"id" => "#{Mobilizon.Web.Endpoint.url()}/reject/join/#{participant.id}"
"id" => "#{Endpoint.url()}/reject/join/#{participant.id}"
}) do
{:ok, participant, update_data}
else
@ -866,7 +867,7 @@ defmodule Mobilizon.Federation.ActivityPub do
reject_data
|> Map.merge(audience)
|> Map.merge(%{
"id" => "#{Mobilizon.Web.Endpoint.url()}/reject/follow/#{follower.id}"
"id" => "#{Endpoint.url()}/reject/follow/#{follower.id}"
}) do
{:ok, follower, update_data}
else

View file

@ -7,6 +7,7 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.{Comment, Event, Participant}
alias Mobilizon.Share
alias Mobilizon.Storage.Repo
require Logger
@ -108,7 +109,7 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do
end
def calculate_to_and_cc_from_mentions(%Participant{} = participant) do
participant = Mobilizon.Storage.Repo.preload(participant, [:actor, :event])
participant = Repo.preload(participant, [:actor, :event])
actor_participants_urls =
participant.event.id
@ -132,13 +133,13 @@ defmodule Mobilizon.Federation.ActivityPub.Audience do
defp add_event_author(nil), do: []
defp add_event_author(%Event{} = event) do
[Mobilizon.Storage.Repo.preload(event, [:organizer_actor]).organizer_actor.url]
[Repo.preload(event, [:organizer_actor]).organizer_actor.url]
end
defp add_comment_author(nil), do: nil
defp add_comment_author(%Comment{} = comment) do
case Mobilizon.Storage.Repo.preload(comment, [:actor]) do
case Repo.preload(comment, [:actor]) do
%Comment{actor: %Actor{url: url}} ->
url

View file

@ -17,6 +17,8 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do
alias Mobilizon.GraphQL.API.Follows
alias Mobilizon.Web.Endpoint
require Logger
def init() do
@ -28,7 +30,7 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do
@spec get_actor() :: Actor.t() | {:error, Ecto.Changeset.t()}
def get_actor do
with {:ok, %Actor{} = actor} <-
Actors.get_or_create_instance_actor_by_url("#{Mobilizon.Web.Endpoint.url()}/relay") do
Actors.get_or_create_instance_actor_by_url("#{Endpoint.url()}/relay") do
actor
end
end

View file

@ -11,6 +11,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
alias Mobilizon.Federation.ActivityPub.Utils
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
alias Mobilizon.Web.MediaProxy
@behaviour Converter
defimpl Convertible, for: ActorModel do
@ -29,14 +31,14 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
data["icon"]["url"] &&
%{
"name" => data["icon"]["name"] || "avatar",
"url" => Mobilizon.Web.MediaProxy.url(data["icon"]["url"])
"url" => MediaProxy.url(data["icon"]["url"])
}
banner =
data["image"]["url"] &&
%{
"name" => data["image"]["name"] || "banner",
"url" => Mobilizon.Web.MediaProxy.url(data["image"]["url"])
"url" => MediaProxy.url(data["image"]["url"])
}
actor_data = %{

View file

@ -6,8 +6,11 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Picture do
internal one, and back.
"""
alias Mobilizon.Media
alias Mobilizon.Media.Picture, as: PictureModel
alias Mobilizon.Web.Upload
@doc """
Convert a picture struct to an ActivityStream representation.
"""
@ -34,9 +37,9 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Picture do
when is_bitstring(picture_url) do
with {:ok, %HTTPoison.Response{body: body}} <- HTTPoison.get(picture_url),
{:ok, %{name: name, url: url, content_type: content_type, size: size}} <-
Mobilizon.Web.Upload.store(%{body: body, name: name}),
{:picture_exists, nil} <- {:picture_exists, Mobilizon.Media.get_picture_by_url(url)} do
Mobilizon.Media.create_picture(%{
Upload.store(%{body: body, name: name}),
{:picture_exists, nil} <- {:picture_exists, Media.get_picture_by_url(url)} do
Media.create_picture(%{
"file" => %{
"url" => url,
"name" => name,

View file

@ -11,6 +11,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Utils do
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Web.Endpoint
require Logger
@spec fetch_tags([String.t()]) :: [Tag.t()]
@ -40,7 +42,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Utils do
def build_tags(tags) do
Enum.map(tags, fn %Tag{} = tag ->
%{
"href" => Mobilizon.Web.Endpoint.url() <> "/tags/#{tag.slug}",
"href" => Endpoint.url() <> "/tags/#{tag.slug}",
"name" => "##{tag.title}",
"type" => "Hashtag"
}
@ -60,7 +62,7 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Utils do
defp build_mention(%Actor{} = actor) do
%{
"href" => actor.url,
"name" => "@#{Mobilizon.Actors.Actor.preferred_username_and_domain(actor)}",
"name" => "@#{Actor.preferred_username_and_domain(actor)}",
"type" => "Mention"
}
end

View file

@ -13,11 +13,13 @@ defmodule Mobilizon.Federation.WebFinger do
alias Mobilizon.Federation.WebFinger.XmlBuilder
alias Mobilizon.Web.Endpoint
require Jason
require Logger
def host_meta do
base_url = Mobilizon.Web.Endpoint.url()
base_url = Endpoint.url()
{
:XRD,
@ -35,7 +37,7 @@ defmodule Mobilizon.Federation.WebFinger do
end
def webfinger(resource, "JSON") do
host = Mobilizon.Web.Endpoint.host()
host = Endpoint.host()
regex = ~r/(acct:)?(?<name>\w+)@#{host}/
with %{"name" => name} <- Regex.named_captures(regex, resource),
@ -59,7 +61,7 @@ defmodule Mobilizon.Federation.WebFinger do
@spec represent_actor(Actor.t(), String.t()) :: struct()
def represent_actor(actor, "JSON") do
%{
"subject" => "acct:#{actor.preferred_username}@#{Mobilizon.Web.Endpoint.host()}",
"subject" => "acct:#{actor.preferred_username}@#{Endpoint.host()}",
"aliases" => [actor.url],
"links" => [
%{"rel" => "self", "type" => "application/activity+json", "href" => actor.url},

View file

@ -12,6 +12,8 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Web.{MediaProxy, Upload}
@doc """
Get a person
"""
@ -157,7 +159,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
pic = args[key][:picture]
with {:ok, %{name: name, url: url, content_type: content_type, size: _size}} <-
Mobilizon.Web.Upload.store(pic.file, type: key, description: pic.alt) do
Upload.store(pic.file, type: key, description: pic.alt) do
Map.put(args, key, %{"name" => name, "url" => url, "mediaType" => content_type})
end
else
@ -236,7 +238,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
@spec proxify_avatar(Actor.t()) :: Actor.t()
defp proxify_avatar(%Actor{avatar: %{url: avatar_url} = avatar} = actor) do
actor |> Map.put(:avatar, avatar |> Map.put(:url, Mobilizon.Web.MediaProxy.url(avatar_url)))
actor |> Map.put(:avatar, avatar |> Map.put(:url, MediaProxy.url(avatar_url)))
end
@spec proxify_avatar(Actor.t()) :: Actor.t()
@ -244,7 +246,7 @@ defmodule Mobilizon.GraphQL.Resolvers.Person do
@spec proxify_banner(Actor.t()) :: Actor.t()
defp proxify_banner(%Actor{banner: %{url: banner_url} = banner} = actor) do
actor |> Map.put(:banner, banner |> Map.put(:url, Mobilizon.Web.MediaProxy.url(banner_url)))
actor |> Map.put(:banner, banner |> Map.put(:url, MediaProxy.url(banner_url)))
end
@spec proxify_banner(Actor.t()) :: Actor.t()

View file

@ -2,13 +2,16 @@ defmodule Mix.Tasks.Mobilizon.Actors do
@moduledoc """
Tasks to manage actors
"""
use Mix.Task
alias Mix.Tasks
@shortdoc "Manages Mobilizon actors"
@impl Mix.Task
def run(_) do
Mix.shell().info("\nAvailable tasks:")
Mix.Tasks.Help.run(["--search", "mobilizon.actors."])
Tasks.Help.run(["--search", "mobilizon.actors."])
end
end

View file

@ -2,13 +2,16 @@ defmodule Mix.Tasks.Mobilizon.Users do
@moduledoc """
Tasks to manage users
"""
use Mix.Task
alias Mix.Tasks
@shortdoc "Manages Mobilizon users"
@impl Mix.Task
def run(_) do
Mix.shell().info("\nAvailable tasks:")
Mix.Tasks.Help.run(["--search", "mobilizon.users."])
Tasks.Help.run(["--search", "mobilizon.users."])
end
end

View file

@ -14,7 +14,7 @@ defmodule Mobilizon do
import Cachex.Spec
alias Mobilizon.Config
alias Mobilizon.{Config, Storage, Web}
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Service.Export.{Feed, ICalendar}
@ -26,7 +26,7 @@ defmodule Mobilizon do
@spec user_agent :: String.t()
def user_agent do
info = "#{Mobilizon.Web.Endpoint.url()} <#{Config.get([:instance, :email], "")}>"
info = "#{Web.Endpoint.url()} <#{Config.get([:instance, :email], "")}>"
"#{named_version()}; #{info}"
end
@ -36,9 +36,9 @@ defmodule Mobilizon do
def start(_type, _args) do
children = [
# supervisors
Mobilizon.Storage.Repo,
Mobilizon.Web.Endpoint,
{Absinthe.Subscription, [Mobilizon.Web.Endpoint]},
Storage.Repo,
Web.Endpoint,
{Absinthe.Subscription, [Web.Endpoint]},
{Oban, Application.get_env(:mobilizon, Oban)},
# workers
Guardian.DB.Token.SweeperServer,
@ -55,7 +55,7 @@ defmodule Mobilizon do
@spec config_change(keyword, keyword, [atom]) :: :ok
def config_change(changed, _new, removed) do
Mobilizon.Web.Endpoint.config_change(changed, removed)
Web.Endpoint.config_change(changed, removed)
:ok
end

View file

@ -336,7 +336,7 @@ defmodule Mobilizon.Actors.Actor do
|> put_change(:followers_url, build_url(username, :followers))
|> put_change(:following_url, build_url(username, :following))
|> put_change(:inbox_url, build_url(username, :inbox))
|> put_change(:shared_inbox_url, "#{Mobilizon.Web.Endpoint.url()}/inbox")
|> put_change(:shared_inbox_url, "#{Endpoint.url()}/inbox")
|> put_change(:url, build_url(username, :page))
end
@ -376,10 +376,10 @@ defmodule Mobilizon.Actors.Actor do
"keys" => Crypto.generate_rsa_2048_private_key(),
"preferred_username" => preferred_username,
"domain" => nil,
"inbox_url" => "#{Mobilizon.Web.Endpoint.url()}/inbox",
"inbox_url" => "#{Endpoint.url()}/inbox",
"followers_url" => "#{url}/followers",
"following_url" => "#{url}/following",
"shared_inbox_url" => "#{Mobilizon.Web.Endpoint.url()}/inbox",
"shared_inbox_url" => "#{Endpoint.url()}/inbox",
"type" => :Application
}
end

View file

@ -16,6 +16,8 @@ defmodule Mobilizon.Actors do
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Web.Upload
require Logger
defenum(ActorType, :actor_type, [
@ -755,7 +757,7 @@ defmodule Mobilizon.Actors do
@spec safe_remove_file(String.t(), Actor.t()) :: {:ok, Actor.t()}
defp safe_remove_file(url, %Actor{} = actor) do
case Mobilizon.Web.Upload.remove(url) do
case Upload.remove(url) do
{:ok, _value} ->
{:ok, actor}
@ -774,7 +776,7 @@ defmodule Mobilizon.Actors do
with %Ecto.Changeset{changes: %{url: new_url}} <- changes[key],
%{url: old_url} <- data |> Map.from_struct() |> Map.get(key),
false <- new_url == old_url do
Mobilizon.Web.Upload.remove(old_url)
Upload.remove(old_url)
end
end
end)

View file

@ -9,6 +9,8 @@ defmodule Mobilizon.Actors.Follower do
alias Mobilizon.Actors.Actor
alias Mobilizon.Web.Endpoint
@type t :: %__MODULE__{
approved: boolean,
url: String.t(),
@ -65,6 +67,6 @@ defmodule Mobilizon.Actors.Follower do
changeset
|> put_change(:id, uuid)
|> put_change(:url, "#{Mobilizon.Web.Endpoint.url()}/follow/#{uuid}")
|> put_change(:url, "#{Endpoint.url()}/follow/#{uuid}")
end
end

View file

@ -9,6 +9,8 @@ defmodule Mobilizon.Addresses.Address do
alias Mobilizon.Events.Event
alias Mobilizon.Web.Endpoint
@type t :: %__MODULE__{
country: String.t(),
locality: String.t(),
@ -65,8 +67,7 @@ defmodule Mobilizon.Addresses.Address do
@spec set_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp set_url(%Ecto.Changeset{changes: changes} = changeset) do
url =
Map.get(changes, :url, "#{Mobilizon.Web.Endpoint.url()}/address/#{Ecto.UUID.generate()}")
url = Map.get(changes, :url, "#{Endpoint.url()}/address/#{Ecto.UUID.generate()}")
put_change(changeset, :url, url)
end

View file

@ -10,6 +10,8 @@ defmodule Mobilizon.Media do
alias Mobilizon.Media.{File, Picture}
alias Mobilizon.Storage.Repo
alias Mobilizon.Web.Upload
@doc """
Gets a single picture.
"""
@ -62,7 +64,7 @@ defmodule Mobilizon.Media do
Multi.new()
|> Multi.delete(:picture, picture)
|> Multi.run(:remove, fn _repo, %{picture: %Picture{file: %File{url: url}}} ->
Mobilizon.Web.Upload.remove(url)
Upload.remove(url)
end)
|> Repo.transaction()

View file

@ -11,6 +11,8 @@ defmodule Mobilizon.Reports.Report do
alias Mobilizon.Events.{Comment, Event}
alias Mobilizon.Reports.{Note, ReportStatus}
alias Mobilizon.Web.Endpoint
@type t :: %__MODULE__{
content: String.t(),
status: ReportStatus.t(),
@ -71,7 +73,7 @@ defmodule Mobilizon.Reports.Report do
@spec maybe_generate_url(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp maybe_generate_url(%Ecto.Changeset{} = changeset) do
with res when res in [:error, {:data, nil}] <- fetch_field(changeset, :url),
url <- "#{Mobilizon.Web.Endpoint.url()}/report/#{Ecto.UUID.generate()}" do
url <- "#{Endpoint.url()}/report/#{Ecto.UUID.generate()}" do
put_change(changeset, :url, url)
else
_ -> changeset

View file

@ -12,6 +12,8 @@ defmodule Mobilizon.Service.Formatter do
alias Mobilizon.Actors.Actor
alias Mobilizon.Service.Formatter.HTML
alias Mobilizon.Web.Endpoint
@link_regex ~r"((?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+)|[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+"ui
@markdown_characters_regex ~r/(`|\*|_|{|}|[|]|\(|\)|#|\+|-|\.|!)/
@ -52,7 +54,7 @@ defmodule Mobilizon.Service.Formatter do
def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do
tag = String.downcase(tag)
url = "#{Mobilizon.Web.Endpoint.url()}/tag/#{tag}"
url = "#{Endpoint.url()}/tag/#{tag}"
link = "<a class='hashtag' data-tag='#{tag}' href='#{url}' rel='tag'>#{tag_text}</a>"
{link, %{acc | tags: MapSet.put(acc.tags, {tag_text, tag})}}

View file

@ -13,11 +13,11 @@ defmodule Mobilizon.Web.Email.Admin do
alias Mobilizon.Reports.Report
alias Mobilizon.Users.User
alias Mobilizon.Web.Email
alias Mobilizon.Web.{Email, Gettext}
@spec report(User.t(), Report.t(), String.t()) :: Bamboo.Email.t()
def report(%User{email: email}, %Report{} = report, locale \\ "en") do
Mobilizon.Web.Gettext.put_locale(locale)
Gettext.put_locale(locale)
subject =
gettext(

View file

@ -7,6 +7,8 @@ defmodule Mobilizon.Web.Email do
alias Mobilizon.Config
alias Mobilizon.Web.EmailView
@spec base_email(keyword()) :: Bamboo.Email.t()
def base_email(args) do
instance = Config.instance_config()
@ -16,7 +18,7 @@ defmodule Mobilizon.Web.Email do
|> from({Config.instance_name(), Config.instance_email_from()})
|> put_header("Reply-To", Config.instance_email_reply_to())
|> assign(:instance, instance)
|> put_html_layout({Mobilizon.Web.EmailView, "email.html"})
|> put_text_layout({Mobilizon.Web.EmailView, "email.text"})
|> put_html_layout({EmailView, "email.html"})
|> put_text_layout({EmailView, "email.text"})
end
end

View file

@ -15,7 +15,7 @@ defmodule Mobilizon.Web.Email.Event do
alias Mobilizon.Storage.Repo
alias Mobilizon.Users.User
alias Mobilizon.Web.Email
alias Mobilizon.Web.{Gettext, Email}
@important_changes [:title, :begins_on, :ends_on, :status]
@ -29,7 +29,7 @@ defmodule Mobilizon.Web.Email.Event do
changes,
locale \\ "en"
) do
Mobilizon.Web.Gettext.put_locale(locale)
Gettext.put_locale(locale)
subject =
gettext(

View file

@ -13,7 +13,7 @@ defmodule Mobilizon.Web.Email.Participation do
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.Participant
alias Mobilizon.Web.Email
alias Mobilizon.Web.{Email, Gettext}
@doc """
Send emails to local user
@ -46,7 +46,7 @@ defmodule Mobilizon.Web.Email.Participation do
%Participant{event: event, role: :rejected},
locale
) do
Mobilizon.Web.Gettext.put_locale(locale)
Gettext.put_locale(locale)
subject =
gettext(
@ -67,7 +67,7 @@ defmodule Mobilizon.Web.Email.Participation do
%Participant{event: event, role: :participant},
locale
) do
Mobilizon.Web.Gettext.put_locale(locale)
Gettext.put_locale(locale)
subject =
gettext(

View file

@ -13,7 +13,7 @@ defmodule Mobilizon.Web.Email.User do
alias Mobilizon.Storage.Repo
alias Mobilizon.Users.User
alias Mobilizon.Web.Email
alias Mobilizon.Web.{Email, Gettext}
require Logger
@ -22,7 +22,7 @@ defmodule Mobilizon.Web.Email.User do
%User{email: email, confirmation_token: confirmation_token},
locale \\ "en"
) do
Mobilizon.Web.Gettext.put_locale(locale)
Gettext.put_locale(locale)
subject =
gettext(
@ -42,7 +42,7 @@ defmodule Mobilizon.Web.Email.User do
%User{email: email, reset_password_token: reset_password_token},
locale \\ "en"
) do
Mobilizon.Web.Gettext.put_locale(locale)
Gettext.put_locale(locale)
subject =
gettext(

View file

@ -14,6 +14,8 @@ defmodule Mobilizon.Web.Plugs.UploadedMedia do
alias Mobilizon.Config
alias Mobilizon.Web.{ReverseProxy, Upload}
require Logger
# no slashes
@ -41,7 +43,7 @@ defmodule Mobilizon.Web.Plugs.UploadedMedia do
conn
end
config = Config.get([Mobilizon.Web.Upload])
config = Config.get([Upload])
with uploader <- Keyword.fetch!(config, :uploader),
proxy_remote = Keyword.get(config, :proxy_remote, false),
@ -76,7 +78,7 @@ defmodule Mobilizon.Web.Plugs.UploadedMedia do
end
defp get_media(conn, {:url, url}, true, _) do
Mobilizon.Web.ReverseProxy.call(conn, url, Config.get([Mobilizon.Upload, :proxy_opts], []))
ReverseProxy.call(conn, url, Config.get([Mobilizon.Upload, :proxy_opts], []))
end
defp get_media(conn, {:url, url}, _, _) do

View file

@ -10,6 +10,8 @@ defmodule Mobilizon.Web.MediaProxy do
alias Mobilizon.Config
alias Mobilizon.Web.Endpoint
@base64_opts [padding: false]
def url(nil), do: nil
@ -22,7 +24,7 @@ defmodule Mobilizon.Web.MediaProxy do
config = Application.get_env(:mobilizon, :media_proxy, [])
if !Keyword.get(config, :enabled, false) or
String.starts_with?(url, Mobilizon.Web.Endpoint.url()) do
String.starts_with?(url, Endpoint.url()) do
url
else
encode_url(url)
@ -30,7 +32,7 @@ defmodule Mobilizon.Web.MediaProxy do
end
def encode_url(url) do
secret = Application.get_env(:mobilizon, Mobilizon.Web.Endpoint)[:secret_key_base]
secret = Application.get_env(:mobilizon, Endpoint)[:secret_key_base]
# Must preserve `%2F` for compatibility with S3
# https://git.pleroma.social/pleroma/pleroma/issues/580
@ -52,7 +54,7 @@ defmodule Mobilizon.Web.MediaProxy do
end
def decode_url(sig, url) do
secret = Application.get_env(:mobilizon, Mobilizon.Web.Endpoint)[:secret_key_base]
secret = Application.get_env(:mobilizon, Endpoint)[:secret_key_base]
sig = Base.url_decode64!(sig, @base64_opts)
local_sig = :crypto.hmac(:sha, secret, url)
@ -69,7 +71,7 @@ defmodule Mobilizon.Web.MediaProxy do
def build_url(sig_base64, url_base64, filename \\ nil) do
[
Config.get([:media_proxy, :base_url], Mobilizon.Web.Endpoint.url()),
Config.get([:media_proxy, :base_url], Endpoint.url()),
"proxy",
sig_base64,
url_base64,

View file

@ -69,6 +69,8 @@ defmodule Mobilizon.Web.ReverseProxy do
alias Plug.Conn
alias Mobilizon.Web.MediaProxy
require Logger
@type option ::
@ -111,7 +113,7 @@ defmodule Mobilizon.Web.ReverseProxy do
req_headers = build_req_headers(conn.req_headers, opts)
opts =
if filename = Mobilizon.Web.MediaProxy.filename(url) do
if filename = MediaProxy.filename(url) do
Keyword.put_new(opts, :attachment_name, filename)
else
opts

View file

@ -36,6 +36,7 @@ defmodule Mobilizon.Web.Upload do
alias Mobilizon.Config
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Upload.{Filter, MIME, Uploader}
require Logger
@ -91,7 +92,7 @@ defmodule Mobilizon.Web.Upload do
def remove(url, opts \\ []) do
with opts <- get_opts(opts),
%URI{path: "/media/" <> path, host: host} <- URI.parse(url),
{:same_host, true} <- {:same_host, host == Mobilizon.Web.Endpoint.host()} do
{:same_host, true} <- {:same_host, host == Endpoint.host()} do
Uploader.remove_file(opts.uploader, path)
else
%URI{} = _uri ->
@ -129,7 +130,7 @@ defmodule Mobilizon.Web.Upload do
Keyword.get(
opts,
:base_url,
Config.get([__MODULE__, :base_url], Mobilizon.Web.Endpoint.url())
Config.get([__MODULE__, :base_url], Endpoint.url())
)
}
end

View file

@ -22,6 +22,8 @@ defmodule Mobilizon.Federation.ActivityPub.TransmogrifierTest do
alias Mobilizon.GraphQL.API
alias Mobilizon.Web.Endpoint
setup_all do
HTTPoison.start()
end
@ -961,7 +963,7 @@ defmodule Mobilizon.Federation.ActivityPub.TransmogrifierTest do
}
expected_tag = %{
"href" => Mobilizon.Web.Endpoint.url() <> "/tags/2hu",
"href" => Endpoint.url() <> "/tags/2hu",
"type" => "Hashtag",
"name" => "#2hu"
}
@ -996,7 +998,7 @@ defmodule Mobilizon.Federation.ActivityPub.TransmogrifierTest do
{:ok, activity, _} = API.Comments.create_comment(%{actor_id: actor.id, text: "#2hu"})
expected_tag = %{
"href" => Mobilizon.Web.Endpoint.url() <> "/tags/2hu",
"href" => Endpoint.url() <> "/tags/2hu",
"type" => "Hashtag",
"name" => "#2hu"
}

View file

@ -11,6 +11,8 @@ defmodule Mobilizon.Federation.WebFingerTest do
alias Mobilizon.Federation.WebFinger
alias Mobilizon.Web.Endpoint
@mastodon_account "tcit@social.tcit.fr"
@mastodon_account_username "tcit"
@pleroma_account "lain@pleroma.soykaf.com"
@ -24,7 +26,7 @@ defmodule Mobilizon.Federation.WebFingerTest do
test "returns a link to the xml lrdd" do
host_info = WebFinger.host_meta()
assert String.contains?(host_info, Mobilizon.Web.Endpoint.url())
assert String.contains?(host_info, Endpoint.url())
end
end
@ -34,7 +36,7 @@ defmodule Mobilizon.Federation.WebFingerTest do
{:ok, result} =
WebFinger.webfinger(
"#{actor.preferred_username}@#{Mobilizon.Web.Endpoint.host()}",
"#{actor.preferred_username}@#{Endpoint.host()}",
"JSON"
)

View file

@ -9,6 +9,8 @@ defmodule Mobilizon.GraphQL.Resolvers.PersonTest do
alias Mobilizon.GraphQL.AbsintheHelpers
alias Mobilizon.Web.Endpoint
@non_existent_username "nonexistent"
describe "Person Resolver" do
@ -120,8 +122,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PersonTest do
assert json_response(res, 200)["data"]["loggedPerson"]["preferredUsername"] ==
actor.preferred_username
assert json_response(res, 200)["data"]["loggedPerson"]["avatar"]["url"] =~
Mobilizon.Web.Endpoint.url()
assert json_response(res, 200)["data"]["loggedPerson"]["avatar"]["url"] =~ Endpoint.url()
end
test "create_person/3 creates a new identity", context do
@ -255,7 +256,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PersonTest do
"The beautiful atlantic way"
assert json_response(res, 200)["data"]["createPerson"]["banner"]["url"] =~
Mobilizon.Web.Endpoint.url() <> "/media/"
Endpoint.url() <> "/media/"
end
test "update_person/3 updates an existing identity", context do
@ -325,7 +326,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PersonTest do
assert res_person["banner"]["id"]
assert res_person["banner"]["name"] == "The beautiful atlantic way"
assert res_person["banner"]["url"] =~ Mobilizon.Web.Endpoint.url() <> "/media/"
assert res_person["banner"]["url"] =~ Endpoint.url() <> "/media/"
end
test "update_person/3 should fail to update a not owned identity", context do

View file

@ -8,6 +8,8 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
alias Mobilizon.GraphQL.AbsintheHelpers
alias Mobilizon.Web.Endpoint
setup %{conn: conn} do
user = insert(:user)
actor = insert(:actor, user: user)
@ -42,8 +44,7 @@ defmodule Mobilizon.GraphQL.Resolvers.PictureTest do
assert json_response(res, 200)["data"]["picture"]["size"] == 13_120
assert json_response(res, 200)["data"]["picture"]["url"] =~
Mobilizon.Web.Endpoint.url()
assert json_response(res, 200)["data"]["picture"]["url"] =~ Endpoint.url()
end
test "picture/3 returns nothing on a non-existent picture", context do

View file

@ -20,6 +20,8 @@ defmodule Mobilizon.Web.ConnCase do
alias Mobilizon.Storage.Repo
alias Mobilizon.Users.User
alias Mobilizon.Web.Auth
using do
quote do
# Import conveniences for testing with connections
@ -30,7 +32,7 @@ defmodule Mobilizon.Web.ConnCase do
@endpoint Mobilizon.Web.Endpoint
def auth_conn(%Plug.Conn{} = conn, %User{} = user) do
{:ok, token, _claims} = Mobilizon.Web.Auth.Guardian.encode_and_sign(user)
{:ok, token, _claims} = Auth.Guardian.encode_and_sign(user)
conn
|> Plug.Conn.put_req_header("authorization", "Bearer #{token}")

View file

@ -8,9 +8,8 @@ defmodule Mobilizon.Factory do
alias Mobilizon.Actors.Actor
alias Mobilizon.Crypto
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.{Endpoint, Upload}
alias Mobilizon.Web.Router.Helpers, as: Routes
alias Mobilizon.Web.Upload
def user_factory do
%Mobilizon.Users.User{
@ -60,7 +59,7 @@ defmodule Mobilizon.Factory do
target_actor: build(:actor),
actor: build(:actor),
id: uuid,
url: "#{Mobilizon.Web.Endpoint.url()}/follows/#{uuid}"
url: "#{Endpoint.url()}/follows/#{uuid}"
}
end

View file

@ -16,8 +16,7 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Web.ActivityPub.ActorView
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.PageView
alias Mobilizon.Web.{Endpoint, PageView}
alias Mobilizon.Web.Router.Helpers, as: Routes
setup_all do
@ -104,7 +103,7 @@ defmodule Mobilizon.Web.ActivityPubControllerTest do
conn =
conn
|> assign(:valid_signature, true)
|> post("#{Mobilizon.Web.Endpoint.url()}/inbox", data)
|> post("#{Endpoint.url()}/inbox", data)
assert "ok" == json_response(conn, 200)
:timer.sleep(500)

View file

@ -11,6 +11,8 @@ defmodule Mobilizon.Web.WebFingerControllerTest do
alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.WebFinger
alias Mobilizon.Web.Endpoint
setup_all do
Mobilizon.Config.put([:instance, :federating], true)
@ -22,7 +24,7 @@ defmodule Mobilizon.Web.WebFingerControllerTest do
assert response(conn, 200) ==
"<?xml version=\"1.0\" encoding=\"UTF-8\"?><XRD xmlns=\"http://docs.oasis-open.org/ns/xri/xrd-1.0\"><Link rel=\"lrdd\" template=\"#{
Mobilizon.Web.Endpoint.url()
Endpoint.url()
}/.well-known/webfinger?resource={uri}\" type=\"application/xrd+xml\" /></XRD>"
assert {"content-type", "application/xrd+xml; charset=utf-8"} in conn.resp_headers

View file

@ -10,7 +10,7 @@ defmodule Mobilizon.Web.MediaProxyTest do
alias Mobilizon.Config
alias Mobilizon.Web.MediaProxyController
alias Mobilizon.Web.{Endpoint, MediaProxyController}
setup do
enabled = Config.get([:media_proxy, :enabled])
@ -35,8 +35,8 @@ defmodule Mobilizon.Web.MediaProxyTest do
end
test "ignores local url" do
local_url = Mobilizon.Web.Endpoint.url() <> "/hello"
local_root = Mobilizon.Web.Endpoint.url()
local_url = Endpoint.url() <> "/hello"
local_root = Endpoint.url()
assert url(local_url) == local_url
assert url(local_root) == local_root
end
@ -47,7 +47,7 @@ defmodule Mobilizon.Web.MediaProxyTest do
assert String.starts_with?(
encoded,
Config.get([:media_proxy, :base_url], Mobilizon.Web.Endpoint.url())
Config.get([:media_proxy, :base_url], Endpoint.url())
)
assert String.ends_with?(encoded, "/logo.png")
@ -84,16 +84,16 @@ defmodule Mobilizon.Web.MediaProxyTest do
end
test "validates signature" do
secret_key_base = Config.get([Mobilizon.Web.Endpoint, :secret_key_base])
secret_key_base = Config.get([Endpoint, :secret_key_base])
on_exit(fn ->
Config.put([Mobilizon.Web.Endpoint, :secret_key_base], secret_key_base)
Config.put([Endpoint, :secret_key_base], secret_key_base)
end)
encoded = url("https://pleroma.social")
Config.put(
[Mobilizon.Web.Endpoint, :secret_key_base],
[Endpoint, :secret_key_base],
"00000000000000000000000000000000000000000000000"
)

View file

@ -8,7 +8,7 @@ defmodule Mobilizon.UploadTest do
alias Mobilizon.Config
alias Mobilizon.Web.Upload
alias Mobilizon.Web.{Endpoint, Upload}
alias Mobilizon.Web.Upload.Uploader
describe "Storing a file with the Local uploader" do
@ -31,7 +31,7 @@ defmodule Mobilizon.UploadTest do
size: 13_227
} = data
assert String.starts_with?(url, Mobilizon.Web.Endpoint.url() <> "/media/")
assert String.starts_with?(url, Endpoint.url() <> "/media/")
end
test "returns a media url with configured base_url" do
@ -59,10 +59,10 @@ defmodule Mobilizon.UploadTest do
filename: "an [image.jpg"
}
{:ok, data} = Upload.store(file, filters: [Mobilizon.Web.Upload.Filter.Dedupe])
{:ok, data} = Upload.store(file, filters: [Upload.Filter.Dedupe])
assert data.url ==
Mobilizon.Web.Endpoint.url() <>
Endpoint.url() <>
"/media/590523d60d3831ec92d05cdd871078409d5780903910efec5cd35ab1b0f19d11.jpg"
end
@ -88,7 +88,7 @@ defmodule Mobilizon.UploadTest do
filename: "an [image.jpg"
}
{:ok, data} = Upload.store(file, filters: [Mobilizon.Web.Upload.Filter.Dedupe])
{:ok, data} = Upload.store(file, filters: [Upload.Filter.Dedupe])
assert data.content_type == "image/jpeg"
end
@ -140,7 +140,7 @@ defmodule Mobilizon.UploadTest do
filename: "an [image.jpg"
}
{:ok, data} = Upload.store(file, filters: [Mobilizon.Web.Upload.Filter.AnonymizeFilename])
{:ok, data} = Upload.store(file, filters: [Upload.Filter.AnonymizeFilename])
refute data.name == "an [image.jpg"
end
@ -212,7 +212,7 @@ defmodule Mobilizon.UploadTest do
content_type: "image/jpeg"
} = data
assert String.starts_with?(url, Mobilizon.Web.Endpoint.url() <> "/media/")
assert String.starts_with?(url, Endpoint.url() <> "/media/")
%URI{path: "/media/" <> path} = URI.parse(url)
{Config.get!([Uploader.Local, :uploads]) <> "/" <> path, url}