Fixes, update tests

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-01-24 19:45:07 +01:00
parent 7e57c5d0b1
commit 7c36a91657
11 changed files with 36 additions and 89 deletions

View file

@ -818,7 +818,7 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do
end
defp fix_nickname(changeset, domain_id, from_admin) when not is_nil(domain_id) do
with {:domain, domain} <- {:domain, Pleroma.Domain.get(domain_id)},
with {:domain, domain} <- {:domain, Domain.get(domain_id)},
{:domain_allowed, true} <- {:domain_allowed, from_admin || domain.public} do
nickname = get_field(changeset, :nickname)

View file

@ -76,8 +76,7 @@ defp relay_active?(conn, _) do
end
def user(conn, %{"nickname" => nickname}) do
with %User{local: true} = user <-
nickname |> URI.decode() |> User.get_cached_by_nickname_or_id() do
with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
conn
|> put_resp_content_type("application/activity+json")
|> put_view(UserView)
@ -536,7 +535,7 @@ def upload_media(%{assigns: %{user: %User{} = user}} = conn, %{"file" => file} =
with {:ok, object} <-
ActivityPub.upload(
file,
actor: User.ap_id(user),
actor: user.ap_id,
description: Map.get(data, "description")
) do
Logger.debug(inspect(object))

View file

@ -128,8 +128,13 @@ def unfollow(
def create(%{assigns: %{user: admin}, body_params: %{users: users}} = conn, _) do
changesets =
users
|> Enum.map(fn %{nickname: nickname, email: email, password: password, domain: domain} ->
domain = Domain.get(domain)
|> Enum.map(fn %{nickname: nickname, email: email, password: password} = user ->
domain_id = Map.get(user, :domain)
domain =
if domain_id do
Domain.get(domain_id)
end
user_data = %{
nickname: nickname,

View file

@ -6,7 +6,6 @@ defmodule Pleroma.Web.MastodonAPI.MediaController do
use Pleroma.Web, :controller
alias Pleroma.Object
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.Plugs.OAuthScopesPlug
@ -24,7 +23,7 @@ def create(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn,
with {:ok, object} <-
ActivityPub.upload(
file,
actor: User.ap_id(user),
actor: user.ap_id,
description: Map.get(data, :description)
) do
attachment_data = Map.put(object.data, "id", object.id)
@ -40,7 +39,7 @@ def create2(%{assigns: %{user: user}, body_params: %{file: file} = data} = conn,
with {:ok, object} <-
ActivityPub.upload(
file,
actor: User.ap_id(user),
actor: user.ap_id,
description: Map.get(data, :description)
) do
attachment_data = Map.put(object.data, "id", object.id)

View file

@ -36,6 +36,8 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
when action in [:public, :hashtag]
)
plug(Pleroma.Web.Plugs.SetDomainPlug when action in [:public, :hashtag])
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.TimelineOperation
# GET /api/v1/timelines/home
@ -114,7 +116,7 @@ def public(%{assigns: %{user: user}} = conn, params) do
|> Map.put(:instance, params[:instance])
# Restricts unfederated content to authenticated users
|> Map.put(:includes_local_public, not is_nil(user))
|> maybe_put_domain_id(user)
|> maybe_put_domain_id(conn)
|> ActivityPub.fetch_public_activities()
conn
@ -132,7 +134,7 @@ defp fail_on_bad_auth(conn) do
render_error(conn, :unauthorized, "authorization required for timeline view")
end
defp hashtag_fetching(params, user, local_only) do
defp hashtag_fetching(conn, params, user, local_only) do
# Note: not sanitizing tag options at this stage (may be mix-cased, have duplicates etc.)
tags_any =
[params[:tag], params[:any]]
@ -151,7 +153,7 @@ defp hashtag_fetching(params, user, local_only) do
|> Map.put(:tag, tags_any)
|> Map.put(:tag_all, tag_all)
|> Map.put(:tag_reject, tag_reject)
|> maybe_put_domain_id(user)
|> maybe_put_domain_id(conn)
|> ActivityPub.fetch_public_activities()
end
@ -162,7 +164,7 @@ def hashtag(%{assigns: %{user: user}} = conn, params) do
if is_nil(user) and restrict_unauthenticated?(local_only) do
fail_on_bad_auth(conn)
else
activities = hashtag_fetching(params, user, local_only)
activities = hashtag_fetching(conn, params, user, local_only)
conn
|> add_link_headers(activities, %{"local" => local_only})
@ -185,7 +187,7 @@ def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
|> Map.put(:user, user)
|> Map.put(:muting_user, user)
|> Map.put(:local_only, params[:local])
|> ActivityPub.fetch_public_activities()
|> maybe_put_domain_id(conn)
# we must filter the following list for the user to avoid leaking statuses the user
# does not actually have permission to see (for more info, peruse security issue #270).
@ -211,17 +213,18 @@ def list(%{assigns: %{user: user}} = conn, %{list_id: id} = params) do
end
end
defp maybe_put_domain_id(%{local_only: true} = params, user) do
defp maybe_put_domain_id(%{local_only: true} = params, conn) do
separate_timelines = Config.get([:instance, :multitenancy, :separate_timelines])
domain_id = if(user, do: user.domain_id || 0, else: 0)
if separate_timelines do
domain = Map.get(conn, :domain, %{id: 0})
params
|> Map.put(:domain_id, domain_id)
|> Map.put(:domain_id, domain.id)
else
params
end
end
defp maybe_put_domain_id(params, _user), do: params
defp maybe_put_domain_id(params, _conn), do: params
end

View file

@ -207,7 +207,7 @@ defp pleroma_configuration(instance) do
birthday_required: Config.get([:instance, :birthday_required]),
birthday_min_age: Config.get([:instance, :birthday_min_age]),
multitenancy: multitenancy()
},
},
stats: %{mau: Pleroma.User.active_user_count()},
vapid_public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
}

View file

@ -3,21 +3,21 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Plugs.SetDomainPlug do
alias Pleroma.Domain
use Pleroma.Web, :plug
alias Pleroma.Domain
def init(opts), do: opts
@impl true
def perform(%{host: domain} = conn, opts) do
def perform(%{host: domain} = conn, _opts) do
with true <- Pleroma.Config.get([:instance, :multitenancy, :enabled], false),
false <-
domain in [
Pleroma.Config.get([__MODULE__, :domain]),
Pleroma.Web.Endpoint.host()
],
%Domain{domain: domain} <- Domain.get_by_service_domain(domain) do
%Domain{} = domain <- Domain.get_by_service_domain(domain) do
Map.put(conn, :domain, domain)
else
_ -> conn

View file

@ -3,8 +3,6 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Plugs.SetNicknameWithDomainPlug do
alias Pleroma.Domain
use Pleroma.Web, :plug
def init(opts), do: opts
@ -14,8 +12,9 @@ def perform(%{domain: domain, params: params} = conn, opts) do
with key <- Keyword.get(opts, :key, "nickname"),
nickname <- Map.get(params, key),
false <- String.contains?(nickname, "@"),
nickname <- nickname <> "@" <> domain.domain do
Map.put(conn, :params, %{"nickname" => nickname})
nickname <- nickname <> "@" <> domain.domain,
params <- Map.put(params, "nickname", nickname) do
Map.put(conn, :params, params)
else
_ -> conn
end

View file

@ -2,59 +2,6 @@
# Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule TrailingFormatPlug do
@behaviour Plug
def init(options), do: options
def call(conn, opts \\ [])
def call(%{path_info: []} = conn, _opts), do: conn
def call(conn, opts) do
path = conn.path_info |> List.last() |> String.split(".") |> Enum.reverse()
supported_formats = Keyword.get(opts, :supported_formats, nil)
case path do
[_] ->
conn
[format | fragments] ->
if supported_formats == nil || format in supported_formats do
new_path = fragments |> Enum.reverse() |> Enum.join(".")
path_fragments = List.replace_at(conn.path_info, -1, new_path)
params =
Plug.Conn.fetch_query_params(conn).params
|> update_params(new_path, format)
|> Map.put("_format", format)
%{
conn
| path_info: path_fragments,
query_params: params,
params: params
}
else
conn
end
end
end
defp update_params(params, new_path, format) do
wildcard = Enum.find(params, fn {_, v} -> v == "#{new_path}.#{format}" end)
case wildcard do
{key, _} ->
Map.put(params, key, new_path)
_ ->
params
end
end
end
defmodule Pleroma.Web.Plugs.TrailingFormatPlug do
@moduledoc """
This plug is adapted from [`TrailingFormatPlug`](https://github.com/mschae/trailing_format_plug/blob/master/lib/trailing_format_plug.ex).

View file

@ -410,12 +410,11 @@ test "should not return local-only posts for anonymous users" do
end
test "filtering local posts basing on domain", %{conn: conn} do
clear_config([:instance, :multitenancy], %{separate_timelines: false})
clear_config([:instance, :multitenancy], %{enabled: true, separate_timelines: false})
{:ok, domain} = Pleroma.Domain.create(%{domain: "pleroma.example.org"})
user1 = insert(:user)
user2 = insert(:user, %{domain_id: domain.id})
%{id: note1} = insert(:note_activity, user: user1)
@ -429,7 +428,7 @@ test "filtering local posts basing on domain", %{conn: conn} do
|> get("/api/v1/timelines/public?local=true")
|> json_response_and_validate_schema(200)
clear_config([:instance, :multitenancy], %{separate_timelines: true})
clear_config([:instance, :multitenancy, :separate_timelines], true)
assert [%{"id" => ^note1}] =
conn
@ -438,16 +437,12 @@ test "filtering local posts basing on domain", %{conn: conn} do
assert [%{"id" => ^note1}] =
conn
|> assign(:user, user1)
|> assign(:token, insert(:oauth_token, user: user1, scopes: ["read:statuses"]))
|> get("/api/v1/timelines/public?local=true")
|> json_response_and_validate_schema(200)
assert [%{"id" => ^note2}] =
conn
|> assign(:user, user2)
|> assign(:token, insert(:oauth_token, user: user2, scopes: ["read:statuses"]))
|> get("/api/v1/timelines/public?local=true")
|> get("http://pleroma.example.org/api/v1/timelines/public?local=true")
|> json_response_and_validate_schema(200)
end
end

View file

@ -2,7 +2,7 @@
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Workers.ScheduledActivityWorkerTest do
defmodule Pleroma.Workers.CheckDomainsResolveWorkerTest do
use Pleroma.DataCase
alias Pleroma.Domain