Merge branch 'activity-pub-use-atoms-as-keys' into 'develop'

Use atoms as keys in `ActivityPub.fetch_*` options

See merge request pleroma/pleroma!2629
This commit is contained in:
lain 2020-06-08 09:11:55 +00:00
commit a1aa9fa913
27 changed files with 532 additions and 625 deletions

View file

@ -52,12 +52,12 @@ defmodule Pleroma.LoadTesting.Fetcher do
defp opts_for_home_timeline(user) do defp opts_for_home_timeline(user) do
%{ %{
"blocking_user" => user, blocking_user: user,
"count" => "20", count: "20",
"muting_user" => user, muting_user: user,
"type" => ["Create", "Announce"], type: ["Create", "Announce"],
"user" => user, user: user,
"with_muted" => "true" with_muted: true
} }
end end
@ -70,17 +70,17 @@ defmodule Pleroma.LoadTesting.Fetcher do
ActivityPub.fetch_activities(recipients, opts) |> Enum.reverse() |> List.last() ActivityPub.fetch_activities(recipients, opts) |> Enum.reverse() |> List.last()
second_page_last = second_page_last =
ActivityPub.fetch_activities(recipients, Map.put(opts, "max_id", first_page_last.id)) ActivityPub.fetch_activities(recipients, Map.put(opts, :max_id, first_page_last.id))
|> Enum.reverse() |> Enum.reverse()
|> List.last() |> List.last()
third_page_last = third_page_last =
ActivityPub.fetch_activities(recipients, Map.put(opts, "max_id", second_page_last.id)) ActivityPub.fetch_activities(recipients, Map.put(opts, :max_id, second_page_last.id))
|> Enum.reverse() |> Enum.reverse()
|> List.last() |> List.last()
forth_page_last = forth_page_last =
ActivityPub.fetch_activities(recipients, Map.put(opts, "max_id", third_page_last.id)) ActivityPub.fetch_activities(recipients, Map.put(opts, :max_id, third_page_last.id))
|> Enum.reverse() |> Enum.reverse()
|> List.last() |> List.last()
@ -90,19 +90,19 @@ defmodule Pleroma.LoadTesting.Fetcher do
}, },
inputs: %{ inputs: %{
"1 page" => opts, "1 page" => opts,
"2 page" => Map.put(opts, "max_id", first_page_last.id), "2 page" => Map.put(opts, :max_id, first_page_last.id),
"3 page" => Map.put(opts, "max_id", second_page_last.id), "3 page" => Map.put(opts, :max_id, second_page_last.id),
"4 page" => Map.put(opts, "max_id", third_page_last.id), "4 page" => Map.put(opts, :max_id, third_page_last.id),
"5 page" => Map.put(opts, "max_id", forth_page_last.id), "5 page" => Map.put(opts, :max_id, forth_page_last.id),
"1 page only media" => Map.put(opts, "only_media", "true"), "1 page only media" => Map.put(opts, :only_media, true),
"2 page only media" => "2 page only media" =>
Map.put(opts, "max_id", first_page_last.id) |> Map.put("only_media", "true"), Map.put(opts, :max_id, first_page_last.id) |> Map.put(:only_media, true),
"3 page only media" => "3 page only media" =>
Map.put(opts, "max_id", second_page_last.id) |> Map.put("only_media", "true"), Map.put(opts, :max_id, second_page_last.id) |> Map.put(:only_media, true),
"4 page only media" => "4 page only media" =>
Map.put(opts, "max_id", third_page_last.id) |> Map.put("only_media", "true"), Map.put(opts, :max_id, third_page_last.id) |> Map.put(:only_media, true),
"5 page only media" => "5 page only media" =>
Map.put(opts, "max_id", forth_page_last.id) |> Map.put("only_media", "true") Map.put(opts, :max_id, forth_page_last.id) |> Map.put(:only_media, true)
}, },
formatters: formatters() formatters: formatters()
) )
@ -110,12 +110,12 @@ defmodule Pleroma.LoadTesting.Fetcher do
defp opts_for_direct_timeline(user) do defp opts_for_direct_timeline(user) do
%{ %{
:visibility => "direct", visibility: "direct",
"blocking_user" => user, blocking_user: user,
"count" => "20", count: "20",
"type" => "Create", type: "Create",
"user" => user, user: user,
"with_muted" => "true" with_muted: true
} }
end end
@ -130,7 +130,7 @@ defmodule Pleroma.LoadTesting.Fetcher do
|> Pagination.fetch_paginated(opts) |> Pagination.fetch_paginated(opts)
|> List.last() |> List.last()
opts2 = Map.put(opts, "max_id", first_page_last.id) opts2 = Map.put(opts, :max_id, first_page_last.id)
second_page_last = second_page_last =
recipients recipients
@ -138,7 +138,7 @@ defmodule Pleroma.LoadTesting.Fetcher do
|> Pagination.fetch_paginated(opts2) |> Pagination.fetch_paginated(opts2)
|> List.last() |> List.last()
opts3 = Map.put(opts, "max_id", second_page_last.id) opts3 = Map.put(opts, :max_id, second_page_last.id)
third_page_last = third_page_last =
recipients recipients
@ -146,7 +146,7 @@ defmodule Pleroma.LoadTesting.Fetcher do
|> Pagination.fetch_paginated(opts3) |> Pagination.fetch_paginated(opts3)
|> List.last() |> List.last()
opts4 = Map.put(opts, "max_id", third_page_last.id) opts4 = Map.put(opts, :max_id, third_page_last.id)
forth_page_last = forth_page_last =
recipients recipients
@ -165,7 +165,7 @@ defmodule Pleroma.LoadTesting.Fetcher do
"2 page" => opts2, "2 page" => opts2,
"3 page" => opts3, "3 page" => opts3,
"4 page" => opts4, "4 page" => opts4,
"5 page" => Map.put(opts4, "max_id", forth_page_last.id) "5 page" => Map.put(opts4, :max_id, forth_page_last.id)
}, },
formatters: formatters() formatters: formatters()
) )
@ -173,34 +173,34 @@ defmodule Pleroma.LoadTesting.Fetcher do
defp opts_for_public_timeline(user) do defp opts_for_public_timeline(user) do
%{ %{
"type" => ["Create", "Announce"], type: ["Create", "Announce"],
"local_only" => false, local_only: false,
"blocking_user" => user, blocking_user: user,
"muting_user" => user muting_user: user
} }
end end
defp opts_for_public_timeline(user, :local) do defp opts_for_public_timeline(user, :local) do
%{ %{
"type" => ["Create", "Announce"], type: ["Create", "Announce"],
"local_only" => true, local_only: true,
"blocking_user" => user, blocking_user: user,
"muting_user" => user muting_user: user
} }
end end
defp opts_for_public_timeline(user, :tag) do defp opts_for_public_timeline(user, :tag) do
%{ %{
"blocking_user" => user, blocking_user: user,
"count" => "20", count: "20",
"local_only" => nil, local_only: nil,
"muting_user" => user, muting_user: user,
"tag" => ["tag"], tag: ["tag"],
"tag_all" => [], tag_all: [],
"tag_reject" => [], tag_reject: [],
"type" => "Create", type: "Create",
"user" => user, user: user,
"with_muted" => "true" with_muted: true
} }
end end
@ -223,7 +223,7 @@ defmodule Pleroma.LoadTesting.Fetcher do
end end
defp fetch_public_timeline(user, :only_media) do defp fetch_public_timeline(user, :only_media) do
opts = opts_for_public_timeline(user) |> Map.put("only_media", "true") opts = opts_for_public_timeline(user) |> Map.put(:only_media, true)
fetch_public_timeline(opts, "public timeline only media") fetch_public_timeline(opts, "public timeline only media")
end end
@ -245,15 +245,13 @@ defmodule Pleroma.LoadTesting.Fetcher do
user = User.get_by_id(user.id) user = User.get_by_id(user.id)
opts = Map.put(opts, "blocking_user", user) opts = Map.put(opts, :blocking_user, user)
Benchee.run( Benchee.run(%{
%{
"public timeline with user block" => fn -> "public timeline with user block" => fn ->
ActivityPub.fetch_public_activities(opts) ActivityPub.fetch_public_activities(opts)
end end
}, })
)
domains = domains =
Enum.reduce(remote_non_friends, [], fn non_friend, domains -> Enum.reduce(remote_non_friends, [], fn non_friend, domains ->
@ -269,30 +267,28 @@ defmodule Pleroma.LoadTesting.Fetcher do
end) end)
user = User.get_by_id(user.id) user = User.get_by_id(user.id)
opts = Map.put(opts, "blocking_user", user) opts = Map.put(opts, :blocking_user, user)
Benchee.run( Benchee.run(%{
%{ "public timeline with domain block" => fn ->
"public timeline with domain block" => fn opts ->
ActivityPub.fetch_public_activities(opts) ActivityPub.fetch_public_activities(opts)
end end
} })
)
end end
defp fetch_public_timeline(opts, title) when is_binary(title) do defp fetch_public_timeline(opts, title) when is_binary(title) do
first_page_last = ActivityPub.fetch_public_activities(opts) |> List.last() first_page_last = ActivityPub.fetch_public_activities(opts) |> List.last()
second_page_last = second_page_last =
ActivityPub.fetch_public_activities(Map.put(opts, "max_id", first_page_last.id)) ActivityPub.fetch_public_activities(Map.put(opts, :max_id, first_page_last.id))
|> List.last() |> List.last()
third_page_last = third_page_last =
ActivityPub.fetch_public_activities(Map.put(opts, "max_id", second_page_last.id)) ActivityPub.fetch_public_activities(Map.put(opts, :max_id, second_page_last.id))
|> List.last() |> List.last()
forth_page_last = forth_page_last =
ActivityPub.fetch_public_activities(Map.put(opts, "max_id", third_page_last.id)) ActivityPub.fetch_public_activities(Map.put(opts, :max_id, third_page_last.id))
|> List.last() |> List.last()
Benchee.run( Benchee.run(
@ -303,17 +299,17 @@ defmodule Pleroma.LoadTesting.Fetcher do
}, },
inputs: %{ inputs: %{
"1 page" => opts, "1 page" => opts,
"2 page" => Map.put(opts, "max_id", first_page_last.id), "2 page" => Map.put(opts, :max_id, first_page_last.id),
"3 page" => Map.put(opts, "max_id", second_page_last.id), "3 page" => Map.put(opts, :max_id, second_page_last.id),
"4 page" => Map.put(opts, "max_id", third_page_last.id), "4 page" => Map.put(opts, :max_id, third_page_last.id),
"5 page" => Map.put(opts, "max_id", forth_page_last.id) "5 page" => Map.put(opts, :max_id, forth_page_last.id)
}, },
formatters: formatters() formatters: formatters()
) )
end end
defp opts_for_notifications do defp opts_for_notifications do
%{"count" => "20", "with_muted" => "true"} %{count: "20", with_muted: true}
end end
defp fetch_notifications(user) do defp fetch_notifications(user) do
@ -322,15 +318,15 @@ defmodule Pleroma.LoadTesting.Fetcher do
first_page_last = MastodonAPI.get_notifications(user, opts) |> List.last() first_page_last = MastodonAPI.get_notifications(user, opts) |> List.last()
second_page_last = second_page_last =
MastodonAPI.get_notifications(user, Map.put(opts, "max_id", first_page_last.id)) MastodonAPI.get_notifications(user, Map.put(opts, :max_id, first_page_last.id))
|> List.last() |> List.last()
third_page_last = third_page_last =
MastodonAPI.get_notifications(user, Map.put(opts, "max_id", second_page_last.id)) MastodonAPI.get_notifications(user, Map.put(opts, :max_id, second_page_last.id))
|> List.last() |> List.last()
forth_page_last = forth_page_last =
MastodonAPI.get_notifications(user, Map.put(opts, "max_id", third_page_last.id)) MastodonAPI.get_notifications(user, Map.put(opts, :max_id, third_page_last.id))
|> List.last() |> List.last()
Benchee.run( Benchee.run(
@ -341,10 +337,10 @@ defmodule Pleroma.LoadTesting.Fetcher do
}, },
inputs: %{ inputs: %{
"1 page" => opts, "1 page" => opts,
"2 page" => Map.put(opts, "max_id", first_page_last.id), "2 page" => Map.put(opts, :max_id, first_page_last.id),
"3 page" => Map.put(opts, "max_id", second_page_last.id), "3 page" => Map.put(opts, :max_id, second_page_last.id),
"4 page" => Map.put(opts, "max_id", third_page_last.id), "4 page" => Map.put(opts, :max_id, third_page_last.id),
"5 page" => Map.put(opts, "max_id", forth_page_last.id) "5 page" => Map.put(opts, :max_id, forth_page_last.id)
}, },
formatters: formatters() formatters: formatters()
) )
@ -354,13 +350,13 @@ defmodule Pleroma.LoadTesting.Fetcher do
first_page_last = ActivityPub.fetch_favourites(user) |> List.last() first_page_last = ActivityPub.fetch_favourites(user) |> List.last()
second_page_last = second_page_last =
ActivityPub.fetch_favourites(user, %{"max_id" => first_page_last.id}) |> List.last() ActivityPub.fetch_favourites(user, %{:max_id => first_page_last.id}) |> List.last()
third_page_last = third_page_last =
ActivityPub.fetch_favourites(user, %{"max_id" => second_page_last.id}) |> List.last() ActivityPub.fetch_favourites(user, %{:max_id => second_page_last.id}) |> List.last()
forth_page_last = forth_page_last =
ActivityPub.fetch_favourites(user, %{"max_id" => third_page_last.id}) |> List.last() ActivityPub.fetch_favourites(user, %{:max_id => third_page_last.id}) |> List.last()
Benchee.run( Benchee.run(
%{ %{
@ -370,10 +366,10 @@ defmodule Pleroma.LoadTesting.Fetcher do
}, },
inputs: %{ inputs: %{
"1 page" => %{}, "1 page" => %{},
"2 page" => %{"max_id" => first_page_last.id}, "2 page" => %{:max_id => first_page_last.id},
"3 page" => %{"max_id" => second_page_last.id}, "3 page" => %{:max_id => second_page_last.id},
"4 page" => %{"max_id" => third_page_last.id}, "4 page" => %{:max_id => third_page_last.id},
"5 page" => %{"max_id" => forth_page_last.id} "5 page" => %{:max_id => forth_page_last.id}
}, },
formatters: formatters() formatters: formatters()
) )
@ -381,8 +377,8 @@ defmodule Pleroma.LoadTesting.Fetcher do
defp opts_for_long_thread(user) do defp opts_for_long_thread(user) do
%{ %{
"blocking_user" => user, blocking_user: user,
"user" => user user: user
} }
end end
@ -392,9 +388,9 @@ defmodule Pleroma.LoadTesting.Fetcher do
opts = opts_for_long_thread(user) opts = opts_for_long_thread(user)
private_input = {private.data["context"], Map.put(opts, "exclude_id", private.id)} private_input = {private.data["context"], Map.put(opts, :exclude_id, private.id)}
public_input = {public.data["context"], Map.put(opts, "exclude_id", public.id)} public_input = {public.data["context"], Map.put(opts, :exclude_id, public.id)}
Benchee.run( Benchee.run(
%{ %{
@ -514,13 +510,13 @@ defmodule Pleroma.LoadTesting.Fetcher do
public_context = public_context =
ActivityPub.fetch_activities_for_context( ActivityPub.fetch_activities_for_context(
public.data["context"], public.data["context"],
Map.put(fetch_opts, "exclude_id", public.id) Map.put(fetch_opts, :exclude_id, public.id)
) )
private_context = private_context =
ActivityPub.fetch_activities_for_context( ActivityPub.fetch_activities_for_context(
private.data["context"], private.data["context"],
Map.put(fetch_opts, "exclude_id", private.id) Map.put(fetch_opts, :exclude_id, private.id)
) )
Benchee.run( Benchee.run(
@ -551,14 +547,14 @@ defmodule Pleroma.LoadTesting.Fetcher do
end, end,
"Public timeline with reply filtering - following" => fn -> "Public timeline with reply filtering - following" => fn ->
public_params public_params
|> Map.put("reply_visibility", "following") |> Map.put(:reply_visibility, "following")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
end, end,
"Public timeline with reply filtering - self" => fn -> "Public timeline with reply filtering - self" => fn ->
public_params public_params
|> Map.put("reply_visibility", "self") |> Map.put(:reply_visibility, "self")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
end end
}, },
@ -577,16 +573,16 @@ defmodule Pleroma.LoadTesting.Fetcher do
"Home timeline with reply filtering - following" => fn -> "Home timeline with reply filtering - following" => fn ->
private_params = private_params =
private_params private_params
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> Map.put("reply_visibility", "following") |> Map.put(:reply_visibility, "following")
ActivityPub.fetch_activities(recipients, private_params) ActivityPub.fetch_activities(recipients, private_params)
end, end,
"Home timeline with reply filtering - self" => fn -> "Home timeline with reply filtering - self" => fn ->
private_params = private_params =
private_params private_params
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> Map.put("reply_visibility", "self") |> Map.put(:reply_visibility, "self")
ActivityPub.fetch_activities(recipients, private_params) ActivityPub.fetch_activities(recipients, private_params)
end end

View file

@ -100,14 +100,14 @@ defmodule Mix.Tasks.Pleroma.Benchmarks.Tags do
_activities = _activities =
params params
|> Map.put("type", "Create") |> Map.put(:type, "Create")
|> Map.put("local_only", local_only) |> Map.put(:local_only, local_only)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> Map.put("tag", tags) |> Map.put(:tag, tags)
|> Map.put("tag_all", tag_all) |> Map.put(:tag_all, tag_all)
|> Map.put("tag_reject", tag_reject) |> Map.put(:tag_reject, tag_reject)
|> Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities() |> Pleroma.Web.ActivityPub.ActivityPub.fetch_public_activities()
end end
end end

View file

@ -92,10 +92,10 @@ defmodule Pleroma.BBS.Handler do
params = params =
%{} %{}
|> Map.put("type", ["Create"]) |> Map.put(:type, ["Create"])
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
activities = activities =
[user.ap_id | Pleroma.User.following(user)] [user.ap_id | Pleroma.User.following(user)]

View file

@ -163,8 +163,8 @@ defmodule Pleroma.Conversation.Participation do
|> Enum.map(fn participation -> |> Enum.map(fn participation ->
activity_id = activity_id =
ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{ ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
"user" => user, user: user,
"blocking_user" => user blocking_user: user
}) })
%{ %{

View file

@ -23,12 +23,12 @@ defmodule Pleroma.Pagination do
@spec fetch_paginated(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()] @spec fetch_paginated(Ecto.Query.t(), map(), type(), atom() | nil) :: [Ecto.Schema.t()]
def fetch_paginated(query, params, type \\ :keyset, table_binding \\ nil) def fetch_paginated(query, params, type \\ :keyset, table_binding \\ nil)
def fetch_paginated(query, %{"total" => true} = params, :keyset, table_binding) do def fetch_paginated(query, %{total: true} = params, :keyset, table_binding) do
total = Repo.aggregate(query, :count, :id) total = Repo.aggregate(query, :count, :id)
%{ %{
total: total, total: total,
items: fetch_paginated(query, Map.drop(params, ["total"]), :keyset, table_binding) items: fetch_paginated(query, Map.drop(params, [:total]), :keyset, table_binding)
} }
end end
@ -41,7 +41,7 @@ defmodule Pleroma.Pagination do
|> enforce_order(options) |> enforce_order(options)
end end
def fetch_paginated(query, %{"total" => true} = params, :offset, table_binding) do def fetch_paginated(query, %{total: true} = params, :offset, table_binding) do
total = total =
query query
|> Ecto.Query.exclude(:left_join) |> Ecto.Query.exclude(:left_join)
@ -49,7 +49,7 @@ defmodule Pleroma.Pagination do
%{ %{
total: total, total: total,
items: fetch_paginated(query, Map.drop(params, ["total"]), :offset, table_binding) items: fetch_paginated(query, Map.drop(params, [:total]), :offset, table_binding)
} }
end end
@ -90,12 +90,6 @@ defmodule Pleroma.Pagination do
skip_order: :boolean skip_order: :boolean
} }
params =
Enum.reduce(params, %{}, fn
{key, _value}, acc when is_atom(key) -> Map.drop(acc, [key])
{key, value}, acc -> Map.put(acc, key, value)
end)
changeset = cast({%{}, param_types}, params, Map.keys(param_types)) changeset = cast({%{}, param_types}, params, Map.keys(param_types))
changeset.changes changeset.changes
end end

View file

@ -67,16 +67,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
{recipients, to, cc} {recipients, to, cc}
end end
defp check_actor_is_active(actor) do defp check_actor_is_active(nil), do: true
if not is_nil(actor) do
with user <- User.get_cached_by_ap_id(actor), defp check_actor_is_active(actor) when is_binary(actor) do
false <- user.deactivated do case User.get_cached_by_ap_id(actor) do
true %User{deactivated: deactivated} -> not deactivated
else _ -> false
_e -> false
end
else
true
end end
end end
@ -87,7 +83,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp check_remote_limit(_), do: true defp check_remote_limit(_), do: true
def increase_note_count_if_public(actor, object) do defp increase_note_count_if_public(actor, object) do
if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor} if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
end end
@ -95,7 +91,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor} if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
end end
def increase_replies_count_if_reply(%{ defp increase_replies_count_if_reply(%{
"object" => %{"inReplyTo" => reply_ap_id} = object, "object" => %{"inReplyTo" => reply_ap_id} = object,
"type" => "Create" "type" => "Create"
}) do }) do
@ -104,19 +100,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
end end
def increase_replies_count_if_reply(_create_data), do: :noop defp increase_replies_count_if_reply(_create_data), do: :noop
def decrease_replies_count_if_reply(%Object{ defp increase_poll_votes_if_vote(%{
data: %{"inReplyTo" => reply_ap_id} = object
}) do
if is_public?(object) do
Object.decrease_replies_count(reply_ap_id)
end
end
def decrease_replies_count_if_reply(_object), do: :noop
def increase_poll_votes_if_vote(%{
"object" => %{"inReplyTo" => reply_ap_id, "name" => name}, "object" => %{"inReplyTo" => reply_ap_id, "name" => name},
"type" => "Create", "type" => "Create",
"actor" => actor "actor" => actor
@ -124,7 +110,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
Object.increase_vote_count(reply_ap_id, name, actor) Object.increase_vote_count(reply_ap_id, name, actor)
end end
def increase_poll_votes_if_vote(_create_data), do: :noop defp increase_poll_votes_if_vote(_create_data), do: :noop
@spec persist(map(), keyword()) :: {:ok, Activity.t() | Object.t()} @spec persist(map(), keyword()) :: {:ok, Activity.t() | Object.t()}
def persist(object, meta) do def persist(object, meta) do
@ -198,8 +184,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp create_or_bump_conversation(activity, actor) do defp create_or_bump_conversation(activity, actor) do
with {:ok, conversation} <- Conversation.create_or_bump_for(activity), with {:ok, conversation} <- Conversation.create_or_bump_for(activity),
%User{} = user <- User.get_cached_by_ap_id(actor), %User{} = user <- User.get_cached_by_ap_id(actor) do
Participation.mark_as_read(user, conversation) do Participation.mark_as_read(user, conversation)
{:ok, conversation} {:ok, conversation}
end end
end end
@ -221,13 +207,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
def stream_out_participations(%Object{data: %{"context" => context}}, user) do def stream_out_participations(%Object{data: %{"context" => context}}, user) do
with %Conversation{} = conversation <- Conversation.get_for_ap_id(context), with %Conversation{} = conversation <- Conversation.get_for_ap_id(context) do
conversation = Repo.preload(conversation, :participations), conversation = Repo.preload(conversation, :participations)
last_activity_id = last_activity_id =
fetch_latest_activity_id_for_context(conversation.ap_id, %{ fetch_latest_activity_id_for_context(conversation.ap_id, %{
"user" => user, user: user,
"blocking_user" => user blocking_user: user
}) do })
if last_activity_id do if last_activity_id do
stream_out_participations(conversation.participations) stream_out_participations(conversation.participations)
end end
@ -261,12 +249,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
published = params[:published] published = params[:published]
quick_insert? = Config.get([:env]) == :benchmark quick_insert? = Config.get([:env]) == :benchmark
with create_data <- create_data =
make_create_data( make_create_data(
%{to: to, actor: actor, published: published, context: context, object: object}, %{to: to, actor: actor, published: published, context: context, object: object},
additional additional
), )
{:ok, activity} <- insert(create_data, local, fake),
with {:ok, activity} <- insert(create_data, local, fake),
{:fake, false, activity} <- {:fake, fake, activity}, {:fake, false, activity} <- {:fake, fake, activity},
_ <- increase_replies_count_if_reply(create_data), _ <- increase_replies_count_if_reply(create_data),
_ <- increase_poll_votes_if_vote(create_data), _ <- increase_poll_votes_if_vote(create_data),
@ -294,12 +283,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
local = !(params[:local] == false) local = !(params[:local] == false)
published = params[:published] published = params[:published]
with listen_data <- listen_data =
make_listen_data( make_listen_data(
%{to: to, actor: actor, published: published, context: context, object: object}, %{to: to, actor: actor, published: published, context: context, object: object},
additional additional
), )
{:ok, activity} <- insert(listen_data, local),
with {:ok, activity} <- insert(listen_data, local),
_ <- notify_and_stream(activity), _ <- notify_and_stream(activity),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
{:ok, activity} {:ok, activity}
@ -317,14 +307,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
@spec accept_or_reject(String.t(), map()) :: {:ok, Activity.t()} | {:error, any()} @spec accept_or_reject(String.t(), map()) :: {:ok, Activity.t()} | {:error, any()}
def accept_or_reject(type, %{to: to, actor: actor, object: object} = params) do defp accept_or_reject(type, %{to: to, actor: actor, object: object} = params) do
local = Map.get(params, :local, true) local = Map.get(params, :local, true)
activity_id = Map.get(params, :activity_id, nil) activity_id = Map.get(params, :activity_id, nil)
with data <- data =
%{"to" => to, "type" => type, "actor" => actor.ap_id, "object" => object} %{"to" => to, "type" => type, "actor" => actor.ap_id, "object" => object}
|> Maps.put_if_present("id", activity_id), |> Maps.put_if_present("id", activity_id)
{:ok, activity} <- insert(data, local),
with {:ok, activity} <- insert(data, local),
_ <- notify_and_stream(activity), _ <- notify_and_stream(activity),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
{:ok, activity} {:ok, activity}
@ -336,15 +327,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
local = !(params[:local] == false) local = !(params[:local] == false)
activity_id = params[:activity_id] activity_id = params[:activity_id]
with data <- %{ data =
%{
"to" => to, "to" => to,
"cc" => cc, "cc" => cc,
"type" => "Update", "type" => "Update",
"actor" => actor, "actor" => actor,
"object" => object "object" => object
}, }
data <- Maps.put_if_present(data, "id", activity_id), |> Maps.put_if_present("id", activity_id)
{:ok, activity} <- insert(data, local),
with {:ok, activity} <- insert(data, local),
_ <- notify_and_stream(activity), _ <- notify_and_stream(activity),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
{:ok, activity} {:ok, activity}
@ -361,8 +354,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
defp do_follow(follower, followed, activity_id, local) do defp do_follow(follower, followed, activity_id, local) do
with data <- make_follow_data(follower, followed, activity_id), data = make_follow_data(follower, followed, activity_id)
{:ok, activity} <- insert(data, local),
with {:ok, activity} <- insert(data, local),
_ <- notify_and_stream(activity), _ <- notify_and_stream(activity),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
{:ok, activity} {:ok, activity}
@ -406,13 +400,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp do_block(blocker, blocked, activity_id, local) do defp do_block(blocker, blocked, activity_id, local) do
unfollow_blocked = Config.get([:activitypub, :unfollow_blocked]) unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
if unfollow_blocked do if unfollow_blocked and fetch_latest_follow(blocker, blocked) do
follow_activity = fetch_latest_follow(blocker, blocked) unfollow(blocker, blocked, nil, local)
if follow_activity, do: unfollow(blocker, blocked, nil, local)
end end
with block_data <- make_block_data(blocker, blocked, activity_id), block_data = make_block_data(blocker, blocked, activity_id)
{:ok, activity} <- insert(block_data, local),
with {:ok, activity} <- insert(block_data, local),
_ <- notify_and_stream(activity), _ <- notify_and_stream(activity),
:ok <- maybe_federate(activity) do :ok <- maybe_federate(activity) do
{:ok, activity} {:ok, activity}
@ -491,8 +485,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
public = [Constants.as_public()] public = [Constants.as_public()]
recipients = recipients =
if opts["user"], if opts[:user],
do: [opts["user"].ap_id | User.following(opts["user"])] ++ public, do: [opts[:user].ap_id | User.following(opts[:user])] ++ public,
else: public else: public
from(activity in Activity) from(activity in Activity)
@ -500,7 +494,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> maybe_preload_bookmarks(opts) |> maybe_preload_bookmarks(opts)
|> maybe_set_thread_muted_field(opts) |> maybe_set_thread_muted_field(opts)
|> restrict_blocked(opts) |> restrict_blocked(opts)
|> restrict_recipients(recipients, opts["user"]) |> restrict_recipients(recipients, opts[:user])
|> where( |> where(
[activity], [activity],
fragment( fragment(
@ -527,7 +521,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
FlakeId.Ecto.CompatType.t() | nil FlakeId.Ecto.CompatType.t() | nil
def fetch_latest_activity_id_for_context(context, opts \\ %{}) do def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
context context
|> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts)) |> fetch_activities_for_context_query(Map.merge(%{skip_preload: true}, opts))
|> limit(1) |> limit(1)
|> select([a], a.id) |> select([a], a.id)
|> Repo.one() |> Repo.one()
@ -535,24 +529,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
@spec fetch_public_or_unlisted_activities(map(), Pagination.type()) :: [Activity.t()] @spec fetch_public_or_unlisted_activities(map(), Pagination.type()) :: [Activity.t()]
def fetch_public_or_unlisted_activities(opts \\ %{}, pagination \\ :keyset) do def fetch_public_or_unlisted_activities(opts \\ %{}, pagination \\ :keyset) do
opts = Map.drop(opts, ["user"]) opts = Map.delete(opts, :user)
query = fetch_activities_query([Constants.as_public()], opts) [Constants.as_public()]
|> fetch_activities_query(opts)
query = |> restrict_unlisted(opts)
if opts["restrict_unlisted"] do |> Pagination.fetch_paginated(opts, pagination)
restrict_unlisted(query)
else
query
end
Pagination.fetch_paginated(query, opts, pagination)
end end
@spec fetch_public_activities(map(), Pagination.type()) :: [Activity.t()] @spec fetch_public_activities(map(), Pagination.type()) :: [Activity.t()]
def fetch_public_activities(opts \\ %{}, pagination \\ :keyset) do def fetch_public_activities(opts \\ %{}, pagination \\ :keyset) do
opts opts
|> Map.put("restrict_unlisted", true) |> Map.put(:restrict_unlisted, true)
|> fetch_public_or_unlisted_activities(pagination) |> fetch_public_or_unlisted_activities(pagination)
end end
@ -561,7 +549,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_visibility(query, %{visibility: visibility}) defp restrict_visibility(query, %{visibility: visibility})
when is_list(visibility) do when is_list(visibility) do
if Enum.all?(visibility, &(&1 in @valid_visibilities)) do if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
query =
from( from(
a in query, a in query,
where: where:
@ -573,8 +560,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
^visibility ^visibility
) )
) )
query
else else
Logger.error("Could not restrict visibility to #{visibility}") Logger.error("Could not restrict visibility to #{visibility}")
end end
@ -596,7 +581,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_visibility(query, _visibility), do: query defp restrict_visibility(query, _visibility), do: query
defp exclude_visibility(query, %{"exclude_visibilities" => visibility}) defp exclude_visibility(query, %{exclude_visibilities: visibility})
when is_list(visibility) do when is_list(visibility) do
if Enum.all?(visibility, &(&1 in @valid_visibilities)) do if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
from( from(
@ -616,7 +601,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
end end
defp exclude_visibility(query, %{"exclude_visibilities" => visibility}) defp exclude_visibility(query, %{exclude_visibilities: visibility})
when visibility in @valid_visibilities do when visibility in @valid_visibilities do
from( from(
a in query, a in query,
@ -631,7 +616,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
) )
end end
defp exclude_visibility(query, %{"exclude_visibilities" => visibility}) defp exclude_visibility(query, %{exclude_visibilities: visibility})
when visibility not in [nil | @valid_visibilities] do when visibility not in [nil | @valid_visibilities] do
Logger.error("Could not exclude visibility to #{visibility}") Logger.error("Could not exclude visibility to #{visibility}")
query query
@ -642,14 +627,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _), defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
do: query do: query
defp restrict_thread_visibility( defp restrict_thread_visibility(query, %{user: %User{skip_thread_containment: true}}, _),
query,
%{"user" => %User{skip_thread_containment: true}},
_
),
do: query do: query
defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do defp restrict_thread_visibility(query, %{user: %User{ap_id: ap_id}}, _) do
from( from(
a in query, a in query,
where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data) where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
@ -661,87 +642,79 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
def fetch_user_abstract_activities(user, reading_user, params \\ %{}) do def fetch_user_abstract_activities(user, reading_user, params \\ %{}) do
params = params =
params params
|> Map.put("user", reading_user) |> Map.put(:user, reading_user)
|> Map.put("actor_id", user.ap_id) |> Map.put(:actor_id, user.ap_id)
recipients = %{
user_activities_recipients(%{ godmode: params[:godmode],
"godmode" => params["godmode"], reading_user: reading_user
"reading_user" => reading_user }
}) |> user_activities_recipients()
|> fetch_activities(params)
fetch_activities(recipients, params)
|> Enum.reverse() |> Enum.reverse()
end end
def fetch_user_activities(user, reading_user, params \\ %{}) do def fetch_user_activities(user, reading_user, params \\ %{}) do
params = params =
params params
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("user", reading_user) |> Map.put(:user, reading_user)
|> Map.put("actor_id", user.ap_id) |> Map.put(:actor_id, user.ap_id)
|> Map.put("pinned_activity_ids", user.pinned_activities) |> Map.put(:pinned_activity_ids, user.pinned_activities)
params = params =
if User.blocks?(reading_user, user) do if User.blocks?(reading_user, user) do
params params
else else
params params
|> Map.put("blocking_user", reading_user) |> Map.put(:blocking_user, reading_user)
|> Map.put("muting_user", reading_user) |> Map.put(:muting_user, reading_user)
end end
recipients = %{
user_activities_recipients(%{ godmode: params[:godmode],
"godmode" => params["godmode"], reading_user: reading_user
"reading_user" => reading_user }
}) |> user_activities_recipients()
|> fetch_activities(params)
fetch_activities(recipients, params)
|> Enum.reverse() |> Enum.reverse()
end end
def fetch_statuses(reading_user, params) do def fetch_statuses(reading_user, params) do
params = params = Map.put(params, :type, ["Create", "Announce"])
params
|> Map.put("type", ["Create", "Announce"])
recipients = %{
user_activities_recipients(%{ godmode: params[:godmode],
"godmode" => params["godmode"], reading_user: reading_user
"reading_user" => reading_user }
}) |> user_activities_recipients()
|> fetch_activities(params, :offset)
fetch_activities(recipients, params, :offset)
|> Enum.reverse() |> Enum.reverse()
end end
defp user_activities_recipients(%{"godmode" => true}) do defp user_activities_recipients(%{godmode: true}), do: []
[]
end
defp user_activities_recipients(%{"reading_user" => reading_user}) do defp user_activities_recipients(%{reading_user: reading_user}) do
if reading_user do if reading_user do
[Constants.as_public()] ++ [reading_user.ap_id | User.following(reading_user)] [Constants.as_public(), reading_user.ap_id | User.following(reading_user)]
else else
[Constants.as_public()] [Constants.as_public()]
end end
end end
defp restrict_since(query, %{"since_id" => ""}), do: query defp restrict_since(query, %{since_id: ""}), do: query
defp restrict_since(query, %{"since_id" => since_id}) do defp restrict_since(query, %{since_id: since_id}) do
from(activity in query, where: activity.id > ^since_id) from(activity in query, where: activity.id > ^since_id)
end end
defp restrict_since(query, _), do: query defp restrict_since(query, _), do: query
defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do defp restrict_tag_reject(_query, %{tag_reject: _tag_reject, skip_preload: true}) do
raise "Can't use the child object without preloading!" raise "Can't use the child object without preloading!"
end end
defp restrict_tag_reject(query, %{"tag_reject" => tag_reject}) defp restrict_tag_reject(query, %{tag_reject: [_ | _] = tag_reject}) do
when is_list(tag_reject) and tag_reject != [] do
from( from(
[_activity, object] in query, [_activity, object] in query,
where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject) where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
@ -750,12 +723,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_tag_reject(query, _), do: query defp restrict_tag_reject(query, _), do: query
defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do defp restrict_tag_all(_query, %{tag_all: _tag_all, skip_preload: true}) do
raise "Can't use the child object without preloading!" raise "Can't use the child object without preloading!"
end end
defp restrict_tag_all(query, %{"tag_all" => tag_all}) defp restrict_tag_all(query, %{tag_all: [_ | _] = tag_all}) do
when is_list(tag_all) and tag_all != [] do
from( from(
[_activity, object] in query, [_activity, object] in query,
where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all) where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
@ -764,18 +736,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_tag_all(query, _), do: query defp restrict_tag_all(query, _), do: query
defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do defp restrict_tag(_query, %{tag: _tag, skip_preload: true}) do
raise "Can't use the child object without preloading!" raise "Can't use the child object without preloading!"
end end
defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do defp restrict_tag(query, %{tag: tag}) when is_list(tag) do
from( from(
[_activity, object] in query, [_activity, object] in query,
where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag) where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
) )
end end
defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do defp restrict_tag(query, %{tag: tag}) when is_binary(tag) do
from( from(
[_activity, object] in query, [_activity, object] in query,
where: fragment("(?)->'tag' \\? (?)", object.data, ^tag) where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
@ -798,35 +770,35 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
) )
end end
defp restrict_local(query, %{"local_only" => true}) do defp restrict_local(query, %{local_only: true}) do
from(activity in query, where: activity.local == true) from(activity in query, where: activity.local == true)
end end
defp restrict_local(query, _), do: query defp restrict_local(query, _), do: query
defp restrict_actor(query, %{"actor_id" => actor_id}) do defp restrict_actor(query, %{actor_id: actor_id}) do
from(activity in query, where: activity.actor == ^actor_id) from(activity in query, where: activity.actor == ^actor_id)
end end
defp restrict_actor(query, _), do: query defp restrict_actor(query, _), do: query
defp restrict_type(query, %{"type" => type}) when is_binary(type) do defp restrict_type(query, %{type: type}) when is_binary(type) do
from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type)) from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
end end
defp restrict_type(query, %{"type" => type}) do defp restrict_type(query, %{type: type}) do
from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type)) from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
end end
defp restrict_type(query, _), do: query defp restrict_type(query, _), do: query
defp restrict_state(query, %{"state" => state}) do defp restrict_state(query, %{state: state}) do
from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state)) from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
end end
defp restrict_state(query, _), do: query defp restrict_state(query, _), do: query
defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do defp restrict_favorited_by(query, %{favorited_by: ap_id}) do
from( from(
[_activity, object] in query, [_activity, object] in query,
where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id) where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id)
@ -835,11 +807,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_favorited_by(query, _), do: query defp restrict_favorited_by(query, _), do: query
defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do defp restrict_media(_query, %{only_media: _val, skip_preload: true}) do
raise "Can't use the child object without preloading!" raise "Can't use the child object without preloading!"
end end
defp restrict_media(query, %{"only_media" => val}) when val in [true, "true", "1"] do defp restrict_media(query, %{only_media: true}) do
from( from(
[_activity, object] in query, [_activity, object] in query,
where: fragment("not (?)->'attachment' = (?)", object.data, ^[]) where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
@ -848,7 +820,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_media(query, _), do: query defp restrict_media(query, _), do: query
defp restrict_replies(query, %{"exclude_replies" => val}) when val in [true, "true", "1"] do defp restrict_replies(query, %{exclude_replies: true}) do
from( from(
[_activity, object] in query, [_activity, object] in query,
where: fragment("?->>'inReplyTo' is null", object.data) where: fragment("?->>'inReplyTo' is null", object.data)
@ -856,8 +828,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
defp restrict_replies(query, %{ defp restrict_replies(query, %{
"reply_filtering_user" => user, reply_filtering_user: user,
"reply_visibility" => "self" reply_visibility: "self"
}) do }) do
from( from(
[activity, object] in query, [activity, object] in query,
@ -872,8 +844,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
defp restrict_replies(query, %{ defp restrict_replies(query, %{
"reply_filtering_user" => user, reply_filtering_user: user,
"reply_visibility" => "following" reply_visibility: "following"
}) do }) do
from( from(
[activity, object] in query, [activity, object] in query,
@ -892,16 +864,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_replies(query, _), do: query defp restrict_replies(query, _), do: query
defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val in [true, "true", "1"] do defp restrict_reblogs(query, %{exclude_reblogs: true}) do
from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data)) from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
end end
defp restrict_reblogs(query, _), do: query defp restrict_reblogs(query, _), do: query
defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query defp restrict_muted(query, %{with_muted: true}), do: query
defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do defp restrict_muted(query, %{muting_user: %User{} = user} = opts) do
mutes = opts["muted_users_ap_ids"] || User.muted_users_ap_ids(user) mutes = opts[:muted_users_ap_ids] || User.muted_users_ap_ids(user)
query = query =
from([activity] in query, from([activity] in query,
@ -909,7 +881,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes) where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
) )
unless opts["skip_preload"] do unless opts[:skip_preload] do
from([thread_mute: tm] in query, where: is_nil(tm.user_id)) from([thread_mute: tm] in query, where: is_nil(tm.user_id))
else else
query query
@ -918,8 +890,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_muted(query, _), do: query defp restrict_muted(query, _), do: query
defp restrict_blocked(query, %{"blocking_user" => %User{} = user} = opts) do defp restrict_blocked(query, %{blocking_user: %User{} = user} = opts) do
blocked_ap_ids = opts["blocked_users_ap_ids"] || User.blocked_users_ap_ids(user) blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user)
domain_blocks = user.domain_blocks || [] domain_blocks = user.domain_blocks || []
following_ap_ids = User.get_friends_ap_ids(user) following_ap_ids = User.get_friends_ap_ids(user)
@ -965,7 +937,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_blocked(query, _), do: query defp restrict_blocked(query, _), do: query
defp restrict_unlisted(query) do defp restrict_unlisted(query, %{restrict_unlisted: true}) do
from( from(
activity in query, activity in query,
where: where:
@ -977,19 +949,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
) )
end end
# TODO: when all endpoints migrated to OpenAPI compare `pinned` with `true` (boolean) only, defp restrict_unlisted(query, _), do: query
# the same for `restrict_media/2`, `restrict_replies/2`, 'restrict_reblogs/2'
# and `restrict_muted/2`
defp restrict_pinned(query, %{"pinned" => pinned, "pinned_activity_ids" => ids}) defp restrict_pinned(query, %{pinned: true, pinned_activity_ids: ids}) do
when pinned in [true, "true", "1"] do
from(activity in query, where: activity.id in ^ids) from(activity in query, where: activity.id in ^ids)
end end
defp restrict_pinned(query, _), do: query defp restrict_pinned(query, _), do: query
defp restrict_muted_reblogs(query, %{"muting_user" => %User{} = user} = opts) do defp restrict_muted_reblogs(query, %{muting_user: %User{} = user} = opts) do
muted_reblogs = opts["reblog_muted_users_ap_ids"] || User.reblog_muted_users_ap_ids(user) muted_reblogs = opts[:reblog_muted_users_ap_ids] || User.reblog_muted_users_ap_ids(user)
from( from(
activity in query, activity in query,
@ -1005,7 +974,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_muted_reblogs(query, _), do: query defp restrict_muted_reblogs(query, _), do: query
defp restrict_instance(query, %{"instance" => instance}) do defp restrict_instance(query, %{instance: instance}) do
users = users =
from( from(
u in User, u in User,
@ -1019,7 +988,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp restrict_instance(query, _), do: query defp restrict_instance(query, _), do: query
defp exclude_poll_votes(query, %{"include_poll_votes" => true}), do: query defp exclude_poll_votes(query, %{include_poll_votes: true}), do: query
defp exclude_poll_votes(query, _) do defp exclude_poll_votes(query, _) do
if has_named_binding?(query, :object) do if has_named_binding?(query, :object) do
@ -1031,7 +1000,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
end end
defp exclude_invisible_actors(query, %{"invisible_actors" => true}), do: query defp exclude_invisible_actors(query, %{invisible_actors: true}), do: query
defp exclude_invisible_actors(query, _opts) do defp exclude_invisible_actors(query, _opts) do
invisible_ap_ids = invisible_ap_ids =
@ -1042,38 +1011,38 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
from([activity] in query, where: activity.actor not in ^invisible_ap_ids) from([activity] in query, where: activity.actor not in ^invisible_ap_ids)
end end
defp exclude_id(query, %{"exclude_id" => id}) when is_binary(id) do defp exclude_id(query, %{exclude_id: id}) when is_binary(id) do
from(activity in query, where: activity.id != ^id) from(activity in query, where: activity.id != ^id)
end end
defp exclude_id(query, _), do: query defp exclude_id(query, _), do: query
defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query defp maybe_preload_objects(query, %{skip_preload: true}), do: query
defp maybe_preload_objects(query, _) do defp maybe_preload_objects(query, _) do
query query
|> Activity.with_preloaded_object() |> Activity.with_preloaded_object()
end end
defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query defp maybe_preload_bookmarks(query, %{skip_preload: true}), do: query
defp maybe_preload_bookmarks(query, opts) do defp maybe_preload_bookmarks(query, opts) do
query query
|> Activity.with_preloaded_bookmark(opts["user"]) |> Activity.with_preloaded_bookmark(opts[:user])
end end
defp maybe_preload_report_notes(query, %{"preload_report_notes" => true}) do defp maybe_preload_report_notes(query, %{preload_report_notes: true}) do
query query
|> Activity.with_preloaded_report_notes() |> Activity.with_preloaded_report_notes()
end end
defp maybe_preload_report_notes(query, _), do: query defp maybe_preload_report_notes(query, _), do: query
defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query defp maybe_set_thread_muted_field(query, %{skip_preload: true}), do: query
defp maybe_set_thread_muted_field(query, opts) do defp maybe_set_thread_muted_field(query, opts) do
query query
|> Activity.with_set_thread_muted_field(opts["muting_user"] || opts["user"]) |> Activity.with_set_thread_muted_field(opts[:muting_user] || opts[:user])
end end
defp maybe_order(query, %{order: :desc}) do defp maybe_order(query, %{order: :desc}) do
@ -1089,24 +1058,23 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp maybe_order(query, _), do: query defp maybe_order(query, _), do: query
defp fetch_activities_query_ap_ids_ops(opts) do defp fetch_activities_query_ap_ids_ops(opts) do
source_user = opts["muting_user"] source_user = opts[:muting_user]
ap_id_relationships = if source_user, do: [:mute, :reblog_mute], else: [] ap_id_relationships = if source_user, do: [:mute, :reblog_mute], else: []
ap_id_relationships = ap_id_relationships =
ap_id_relationships ++ if opts[:blocking_user] && opts[:blocking_user] == source_user do
if opts["blocking_user"] && opts["blocking_user"] == source_user do [:block | ap_id_relationships]
[:block]
else else
[] ap_id_relationships
end end
preloaded_ap_ids = User.outgoing_relationships_ap_ids(source_user, ap_id_relationships) preloaded_ap_ids = User.outgoing_relationships_ap_ids(source_user, ap_id_relationships)
restrict_blocked_opts = Map.merge(%{"blocked_users_ap_ids" => preloaded_ap_ids[:block]}, opts) restrict_blocked_opts = Map.merge(%{blocked_users_ap_ids: preloaded_ap_ids[:block]}, opts)
restrict_muted_opts = Map.merge(%{"muted_users_ap_ids" => preloaded_ap_ids[:mute]}, opts) restrict_muted_opts = Map.merge(%{muted_users_ap_ids: preloaded_ap_ids[:mute]}, opts)
restrict_muted_reblogs_opts = restrict_muted_reblogs_opts =
Map.merge(%{"reblog_muted_users_ap_ids" => preloaded_ap_ids[:reblog_mute]}, opts) Map.merge(%{reblog_muted_users_ap_ids: preloaded_ap_ids[:reblog_mute]}, opts)
{restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts} {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts}
end end
@ -1125,7 +1093,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> maybe_preload_report_notes(opts) |> maybe_preload_report_notes(opts)
|> maybe_set_thread_muted_field(opts) |> maybe_set_thread_muted_field(opts)
|> maybe_order(opts) |> maybe_order(opts)
|> restrict_recipients(recipients, opts["user"]) |> restrict_recipients(recipients, opts[:user])
|> restrict_replies(opts) |> restrict_replies(opts)
|> restrict_tag(opts) |> restrict_tag(opts)
|> restrict_tag_reject(opts) |> restrict_tag_reject(opts)
@ -1152,12 +1120,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
def fetch_activities(recipients, opts \\ %{}, pagination \\ :keyset) do def fetch_activities(recipients, opts \\ %{}, pagination \\ :keyset) do
list_memberships = Pleroma.List.memberships(opts["user"]) list_memberships = Pleroma.List.memberships(opts[:user])
fetch_activities_query(recipients ++ list_memberships, opts) fetch_activities_query(recipients ++ list_memberships, opts)
|> Pagination.fetch_paginated(opts, pagination) |> Pagination.fetch_paginated(opts, pagination)
|> Enum.reverse() |> Enum.reverse()
|> maybe_update_cc(list_memberships, opts["user"]) |> maybe_update_cc(list_memberships, opts[:user])
end end
@doc """ @doc """
@ -1173,16 +1141,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> select([_like, object, activity], %{activity | object: object}) |> select([_like, object, activity], %{activity | object: object})
|> order_by([like, _, _], desc_nulls_last: like.id) |> order_by([like, _, _], desc_nulls_last: like.id)
|> Pagination.fetch_paginated( |> Pagination.fetch_paginated(
Map.merge(params, %{"skip_order" => true}), Map.merge(params, %{skip_order: true}),
pagination, pagination,
:object_activity :object_activity
) )
end end
defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id}) defp maybe_update_cc(activities, [_ | _] = list_memberships, %User{ap_id: user_ap_id}) do
when is_list(list_memberships) and length(list_memberships) > 0 do
Enum.map(activities, fn Enum.map(activities, fn
%{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 -> %{data: %{"bcc" => [_ | _] = bcc}} = activity ->
if Enum.any?(bcc, &(&1 in list_memberships)) do if Enum.any?(bcc, &(&1 in list_memberships)) do
update_in(activity.data["cc"], &[user_ap_id | &1]) update_in(activity.data["cc"], &[user_ap_id | &1])
else else
@ -1196,7 +1163,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp maybe_update_cc(activities, _, _), do: activities defp maybe_update_cc(activities, _, _), do: activities
def fetch_activities_bounded_query(query, recipients, recipients_with_public) do defp fetch_activities_bounded_query(query, recipients, recipients_with_public) do
from(activity in query, from(activity in query,
where: where:
fragment("? && ?", activity.recipients, ^recipients) or fragment("? && ?", activity.recipients, ^recipients) or
@ -1266,8 +1233,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
%{"type" => "Emoji"} -> true %{"type" => "Emoji"} -> true
_ -> false _ -> false
end) end)
|> Enum.reduce(%{}, fn %{"icon" => %{"url" => url}, "name" => name}, acc -> |> Map.new(fn %{"icon" => %{"url" => url}, "name" => name} ->
Map.put(acc, String.trim(name, ":"), url) {String.trim(name, ":"), url}
end) end)
locked = data["manuallyApprovesFollowers"] || false locked = data["manuallyApprovesFollowers"] || false
@ -1313,7 +1280,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
} }
# nickname can be nil because of virtual actors # nickname can be nil because of virtual actors
user_data =
if data["preferredUsername"] do if data["preferredUsername"] do
Map.put( Map.put(
user_data, user_data,
@ -1323,8 +1289,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
else else
Map.put(user_data, :nickname, nil) Map.put(user_data, :nickname, nil)
end end
{:ok, user_data}
end end
def fetch_follow_information_for_user(user) do def fetch_follow_information_for_user(user) do
@ -1399,9 +1363,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
defp collection_private(_data), do: {:ok, true} defp collection_private(_data), do: {:ok, true}
def user_data_from_user_object(data) do def user_data_from_user_object(data) do
with {:ok, data} <- MRF.filter(data), with {:ok, data} <- MRF.filter(data) do
{:ok, data} <- object_to_user_data(data) do {:ok, object_to_user_data(data)}
{:ok, data}
else else
e -> {:error, e} e -> {:error, e}
end end
@ -1409,15 +1372,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
def fetch_and_prepare_user_from_ap_id(ap_id) do def fetch_and_prepare_user_from_ap_id(ap_id) do
with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id), with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
{:ok, data} <- user_data_from_user_object(data), {:ok, data} <- user_data_from_user_object(data) do
data <- maybe_update_follow_information(data) do {:ok, maybe_update_follow_information(data)}
{:ok, data}
else else
{:error, "Object has been deleted"} = e -> {:error, "Object has been deleted" = e} ->
Logger.debug("Could not decode user at fetch #{ap_id}, #{inspect(e)}") Logger.debug("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
{:error, e} {:error, e}
e -> {:error, e} ->
Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}") Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
{:error, e} {:error, e}
end end
@ -1440,8 +1402,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> Repo.insert() |> Repo.insert()
|> User.set_cache() |> User.set_cache()
end end
else
e -> {:error, e}
end end
end end
end end
@ -1455,7 +1415,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
# filter out broken threads # filter out broken threads
def contain_broken_threads(%Activity{} = activity, %User{} = user) do defp contain_broken_threads(%Activity{} = activity, %User{} = user) do
entire_thread_visible_for_user?(activity, user) entire_thread_visible_for_user?(activity, user)
end end
@ -1466,7 +1426,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
def fetch_direct_messages_query do def fetch_direct_messages_query do
Activity Activity
|> restrict_type(%{"type" => "Create"}) |> restrict_type(%{type: "Create"})
|> restrict_visibility(%{visibility: "direct"}) |> restrict_visibility(%{visibility: "direct"})
|> order_by([activity], asc: activity.id) |> order_by([activity], asc: activity.id)
end end

View file

@ -238,6 +238,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
params params
|> Map.drop(["nickname", "page"]) |> Map.drop(["nickname", "page"])
|> Map.put("include_poll_votes", true) |> Map.put("include_poll_votes", true)
|> Map.new(fn {k, v} -> {String.to_existing_atom(k), v} end)
activities = ActivityPub.fetch_user_activities(user, for_user, params) activities = ActivityPub.fetch_user_activities(user, for_user, params)
@ -354,6 +355,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
|> Map.drop(["nickname", "page"]) |> Map.drop(["nickname", "page"])
|> Map.put("blocking_user", user) |> Map.put("blocking_user", user)
|> Map.put("user", user) |> Map.put("user", user)
|> Map.new(fn {k, v} -> {String.to_existing_atom(k), v} end)
activities = activities =
[user.ap_id | User.following(user)] [user.ap_id | User.following(user)]

View file

@ -245,7 +245,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
Inserts a full object if it is contained in an activity. Inserts a full object if it is contained in an activity.
""" """
def insert_full_object(%{"object" => %{"type" => type} = object_data} = map) def insert_full_object(%{"object" => %{"type" => type} = object_data} = map)
when is_map(object_data) and type in @supported_object_types do when type in @supported_object_types do
with {:ok, object} <- Object.create(object_data) do with {:ok, object} <- Object.create(object_data) do
map = Map.put(map, "object", object.data["id"]) map = Map.put(map, "object", object.data["id"])
@ -741,13 +741,12 @@ defmodule Pleroma.Web.ActivityPub.Utils do
def get_reports(params, page, page_size) do def get_reports(params, page, page_size) do
params = params =
params params
|> Map.new(fn {key, value} -> {to_string(key), value} end) |> Map.put(:type, "Flag")
|> Map.put("type", "Flag") |> Map.put(:skip_preload, true)
|> Map.put("skip_preload", true) |> Map.put(:preload_report_notes, true)
|> Map.put("preload_report_notes", true) |> Map.put(:total, true)
|> Map.put("total", true) |> Map.put(:limit, page_size)
|> Map.put("limit", page_size) |> Map.put(:offset, (page - 1) * page_size)
|> Map.put("offset", (page - 1) * page_size)
ActivityPub.fetch_activities([], params, :offset) ActivityPub.fetch_activities([], params, :offset)
end end

View file

@ -228,10 +228,10 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
activities = activities =
ActivityPub.fetch_statuses(nil, %{ ActivityPub.fetch_statuses(nil, %{
"instance" => instance, instance: instance,
"limit" => page_size, limit: page_size,
"offset" => (page - 1) * page_size, offset: (page - 1) * page_size,
"exclude_reblogs" => !with_reblogs && "true" exclude_reblogs: not with_reblogs
}) })
conn conn
@ -248,9 +248,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
activities = activities =
ActivityPub.fetch_user_activities(user, nil, %{ ActivityPub.fetch_user_activities(user, nil, %{
"limit" => page_size, limit: page_size,
"godmode" => godmode, godmode: godmode,
"exclude_reblogs" => !with_reblogs && "true" exclude_reblogs: not with_reblogs
}) })
conn conn

View file

@ -29,11 +29,11 @@ defmodule Pleroma.Web.AdminAPI.StatusController do
def index(%{assigns: %{user: _admin}} = conn, params) do def index(%{assigns: %{user: _admin}} = conn, params) do
activities = activities =
ActivityPub.fetch_statuses(nil, %{ ActivityPub.fetch_statuses(nil, %{
"godmode" => params.godmode, godmode: params.godmode,
"local_only" => params.local_only, local_only: params.local_only,
"limit" => params.page_size, limit: params.page_size,
"offset" => (params.page - 1) * params.page_size, offset: (params.page - 1) * params.page_size,
"exclude_reblogs" => not params.with_reblogs exclude_reblogs: not params.with_reblogs
}) })
render(conn, "index.json", activities: activities, as: :activity) render(conn, "index.json", activities: activities, as: :activity)

View file

@ -18,7 +18,7 @@ defmodule Pleroma.Web.AdminAPI.ReportView do
%{ %{
reports: reports:
reports[:items] reports[:items]
|> Enum.map(&Report.extract_report_info(&1)) |> Enum.map(&Report.extract_report_info/1)
|> Enum.map(&render(__MODULE__, "show.json", &1)) |> Enum.map(&render(__MODULE__, "show.json", &1))
|> Enum.reverse(), |> Enum.reverse(),
total: reports[:total] total: reports[:total]

View file

@ -93,8 +93,7 @@ defmodule Pleroma.Web.ControllerHelper do
end end
def assign_account_by_id(conn, _) do def assign_account_by_id(conn, _) do
# TODO: use `conn.params[:id]` only after moving to OpenAPI case Pleroma.User.get_cached_by_id(conn.params.id) do
case Pleroma.User.get_cached_by_id(conn.params[:id] || conn.params["id"]) do
%Pleroma.User{} = account -> assign(conn, :account, account) %Pleroma.User{} = account -> assign(conn, :account, account)
nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt() nil -> Pleroma.Web.MastodonAPI.FallbackController.call(conn, {:error, :not_found}) |> halt()
end end

View file

@ -13,8 +13,8 @@ defmodule Pleroma.Web.Feed.TagController do
{format, tag} = parse_tag(raw_tag) {format, tag} = parse_tag(raw_tag)
activities = activities =
%{"type" => ["Create"], "tag" => tag} %{type: ["Create"], tag: tag}
|> Pleroma.Maps.put_if_present("max_id", params["max_id"]) |> Pleroma.Maps.put_if_present(:max_id, params["max_id"])
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
conn conn

View file

@ -50,10 +50,10 @@ defmodule Pleroma.Web.Feed.UserController do
with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do with {_, %User{} = user} <- {:fetch_user, User.get_cached_by_nickname(nickname)} do
activities = activities =
%{ %{
"type" => ["Create"], type: ["Create"],
"actor_id" => user.ap_id actor_id: user.ap_id
} }
|> Pleroma.Maps.put_if_present("max_id", params["max_id"]) |> Pleroma.Maps.put_if_present(:max_id, params["max_id"])
|> ActivityPub.fetch_public_or_unlisted_activities() |> ActivityPub.fetch_public_or_unlisted_activities()
conn conn

View file

@ -244,9 +244,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
params = params =
params params
|> Map.delete(:tagged) |> Map.delete(:tagged)
|> Enum.filter(&(not is_nil(&1))) |> Map.put(:tag, params[:tagged])
|> Map.new(fn {key, value} -> {to_string(key), value} end)
|> Map.put("tag", params[:tagged])
activities = ActivityPub.fetch_user_activities(user, reading_user, params) activities = ActivityPub.fetch_user_activities(user, reading_user, params)

View file

@ -21,7 +21,6 @@ defmodule Pleroma.Web.MastodonAPI.ConversationController do
@doc "GET /api/v1/conversations" @doc "GET /api/v1/conversations"
def index(%{assigns: %{user: user}} = conn, params) do def index(%{assigns: %{user: user}} = conn, params) do
params = stringify_pagination_params(params)
participations = Participation.for_user_with_last_activity_id(user, params) participations = Participation.for_user_with_last_activity_id(user, params)
conn conn
@ -37,20 +36,4 @@ defmodule Pleroma.Web.MastodonAPI.ConversationController do
render(conn, "participation.json", participation: participation, for: user) render(conn, "participation.json", participation: participation, for: user)
end end
end end
defp stringify_pagination_params(params) do
atom_keys =
Pleroma.Pagination.page_keys()
|> Enum.map(&String.to_atom(&1))
str_keys =
params
|> Map.take(atom_keys)
|> Enum.map(fn {key, value} -> {to_string(key), value} end)
|> Enum.into(%{})
params
|> Map.delete(atom_keys)
|> Map.merge(str_keys)
end
end end

View file

@ -359,9 +359,9 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
with %Activity{} = activity <- Activity.get_by_id(id) do with %Activity{} = activity <- Activity.get_by_id(id) do
activities = activities =
ActivityPub.fetch_activities_for_context(activity.data["context"], %{ ActivityPub.fetch_activities_for_context(activity.data["context"], %{
"blocking_user" => user, blocking_user: user,
"user" => user, user: user,
"exclude_id" => activity.id exclude_id: activity.id
}) })
render(conn, "context.json", activity: activity, activities: activities, user: user) render(conn, "context.json", activity: activity, activities: activities, user: user)
@ -370,11 +370,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
@doc "GET /api/v1/favourites" @doc "GET /api/v1/favourites"
def favourites(%{assigns: %{user: %User{} = user}} = conn, params) do def favourites(%{assigns: %{user: %User{} = user}} = conn, params) do
params =
params
|> Map.new(fn {key, value} -> {to_string(key), value} end)
|> Map.take(Pleroma.Pagination.page_keys())
activities = ActivityPub.fetch_favourites(user, params) activities = ActivityPub.fetch_favourites(user, params)
conn conn

View file

@ -44,12 +44,11 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
def home(%{assigns: %{user: user}} = conn, params) do def home(%{assigns: %{user: user}} = conn, params) do
params = params =
params params
|> Map.new(fn {key, value} -> {to_string(key), value} end) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:blocking_user, user)
|> Map.put("blocking_user", user) |> Map.put(:muting_user, user)
|> Map.put("muting_user", user) |> Map.put(:reply_filtering_user, user)
|> Map.put("reply_filtering_user", user) |> Map.put(:user, user)
|> Map.put("user", user)
activities = activities =
[user.ap_id | User.following(user)] [user.ap_id | User.following(user)]
@ -69,10 +68,9 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
def direct(%{assigns: %{user: user}} = conn, params) do def direct(%{assigns: %{user: user}} = conn, params) do
params = params =
params params
|> Map.new(fn {key, value} -> {to_string(key), value} end) |> Map.put(:type, "Create")
|> Map.put("type", "Create") |> Map.put(:blocking_user, user)
|> Map.put("blocking_user", user) |> Map.put(:user, user)
|> Map.put("user", user)
|> Map.put(:visibility, "direct") |> Map.put(:visibility, "direct")
activities = activities =
@ -91,9 +89,7 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
# GET /api/v1/timelines/public # GET /api/v1/timelines/public
def public(%{assigns: %{user: user}} = conn, params) do def public(%{assigns: %{user: user}} = conn, params) do
params = Map.new(params, fn {key, value} -> {to_string(key), value} end) local_only = params[:local]
local_only = params["local"]
cfg_key = cfg_key =
if local_only do if local_only do
@ -109,11 +105,11 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
else else
activities = activities =
params params
|> Map.put("type", ["Create"]) |> Map.put(:type, ["Create"])
|> Map.put("local_only", local_only) |> Map.put(:local_only, local_only)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
conn conn
@ -128,39 +124,38 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
defp hashtag_fetching(params, user, local_only) do defp hashtag_fetching(params, user, local_only) do
tags = tags =
[params["tag"], params["any"]] [params[:tag], params[:any]]
|> List.flatten() |> List.flatten()
|> Enum.uniq() |> Enum.uniq()
|> Enum.filter(& &1) |> Enum.reject(&is_nil/1)
|> Enum.map(&String.downcase(&1)) |> Enum.map(&String.downcase/1)
tag_all = tag_all =
params params
|> Map.get("all", []) |> Map.get(:all, [])
|> Enum.map(&String.downcase(&1)) |> Enum.map(&String.downcase/1)
tag_reject = tag_reject =
params params
|> Map.get("none", []) |> Map.get(:none, [])
|> Enum.map(&String.downcase(&1)) |> Enum.map(&String.downcase/1)
_activities = _activities =
params params
|> Map.put("type", "Create") |> Map.put(:type, "Create")
|> Map.put("local_only", local_only) |> Map.put(:local_only, local_only)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> Map.put("tag", tags) |> Map.put(:tag, tags)
|> Map.put("tag_all", tag_all) |> Map.put(:tag_all, tag_all)
|> Map.put("tag_reject", tag_reject) |> Map.put(:tag_reject, tag_reject)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
end end
# GET /api/v1/timelines/tag/:tag # GET /api/v1/timelines/tag/:tag
def hashtag(%{assigns: %{user: user}} = conn, params) do def hashtag(%{assigns: %{user: user}} = conn, params) do
params = Map.new(params, fn {key, value} -> {to_string(key), value} end) local_only = params[:local]
local_only = params["local"]
activities = hashtag_fetching(params, user, local_only) activities = hashtag_fetching(params, user, local_only)
conn conn

View file

@ -24,8 +24,8 @@ defmodule Pleroma.Web.MastodonAPI.ConversationView do
last_activity_id = last_activity_id =
with nil <- participation.last_activity_id do with nil <- participation.last_activity_id do
ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{ ActivityPub.fetch_latest_activity_id_for_context(participation.conversation.ap_id, %{
"user" => user, user: user,
"blocking_user" => user blocking_user: user
}) })
end end

View file

@ -126,10 +126,9 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do def favourites(%{assigns: %{user: for_user, account: user}} = conn, params) do
params = params =
params params
|> Map.new(fn {key, value} -> {to_string(key), value} end) |> Map.put(:type, "Create")
|> Map.put("type", "Create") |> Map.put(:favorited_by, user.ap_id)
|> Map.put("favorited_by", user.ap_id) |> Map.put(:blocking_user, for_user)
|> Map.put("blocking_user", for_user)
recipients = recipients =
if for_user do if for_user do

View file

@ -42,15 +42,14 @@ defmodule Pleroma.Web.PleromaAPI.ConversationController do
Participation.get(participation_id, preload: [:conversation]) do Participation.get(participation_id, preload: [:conversation]) do
params = params =
params params
|> Map.new(fn {key, value} -> {to_string(key), value} end) |> Map.put(:blocking_user, user)
|> Map.put("blocking_user", user) |> Map.put(:muting_user, user)
|> Map.put("muting_user", user) |> Map.put(:user, user)
|> Map.put("user", user)
activities = activities =
participation.conversation.ap_id participation.conversation.ap_id
|> ActivityPub.fetch_activities_for_context_query(params) |> ActivityPub.fetch_activities_for_context_query(params)
|> Pleroma.Pagination.fetch_paginated(Map.put(params, "total", false)) |> Pleroma.Pagination.fetch_paginated(Map.put(params, :total, false))
|> Enum.reverse() |> Enum.reverse()
conn conn

View file

@ -36,10 +36,7 @@ defmodule Pleroma.Web.PleromaAPI.ScrobbleController do
def index(%{assigns: %{user: reading_user}} = conn, %{id: id} = params) do def index(%{assigns: %{user: reading_user}} = conn, %{id: id} = params) do
with %User{} = user <- User.get_cached_by_nickname_or_id(id, for: reading_user) do with %User{} = user <- User.get_cached_by_nickname_or_id(id, for: reading_user) do
params = params = Map.put(params, :type, ["Listen"])
params
|> Map.new(fn {key, value} -> {to_string(key), value} end)
|> Map.put("type", ["Listen"])
activities = ActivityPub.fetch_user_abstract_activities(user, reading_user, params) activities = ActivityPub.fetch_user_abstract_activities(user, reading_user, params)

View file

@ -111,8 +111,14 @@ defmodule Pleroma.Web.StaticFE.StaticFEController do
%User{} = user -> %User{} = user ->
meta = Metadata.build_tags(%{user: user}) meta = Metadata.build_tags(%{user: user})
params =
params
|> Map.take(@page_keys)
|> Map.new(fn {k, v} -> {String.to_existing_atom(k), v} end)
timeline = timeline =
ActivityPub.fetch_user_activities(user, nil, Map.take(params, @page_keys)) user
|> ActivityPub.fetch_user_activities(nil, params)
|> Enum.map(&represent/1) |> Enum.map(&represent/1)
prev_page_id = prev_page_id =

View file

@ -21,7 +21,7 @@ defmodule Pleroma.PaginationTest do
id = Enum.at(notes, 2).id |> Integer.to_string() id = Enum.at(notes, 2).id |> Integer.to_string()
%{total: total, items: paginated} = %{total: total, items: paginated} =
Pagination.fetch_paginated(Object, %{"min_id" => id, "total" => true}) Pagination.fetch_paginated(Object, %{min_id: id, total: true})
assert length(paginated) == 2 assert length(paginated) == 2
assert total == 5 assert total == 5
@ -31,7 +31,7 @@ defmodule Pleroma.PaginationTest do
id = Enum.at(notes, 2).id |> Integer.to_string() id = Enum.at(notes, 2).id |> Integer.to_string()
%{total: total, items: paginated} = %{total: total, items: paginated} =
Pagination.fetch_paginated(Object, %{"since_id" => id, "total" => true}) Pagination.fetch_paginated(Object, %{since_id: id, total: true})
assert length(paginated) == 2 assert length(paginated) == 2
assert total == 5 assert total == 5
@ -41,7 +41,7 @@ defmodule Pleroma.PaginationTest do
id = Enum.at(notes, 1).id |> Integer.to_string() id = Enum.at(notes, 1).id |> Integer.to_string()
%{total: total, items: paginated} = %{total: total, items: paginated} =
Pagination.fetch_paginated(Object, %{"max_id" => id, "total" => true}) Pagination.fetch_paginated(Object, %{max_id: id, total: true})
assert length(paginated) == 1 assert length(paginated) == 1
assert total == 5 assert total == 5
@ -50,7 +50,7 @@ defmodule Pleroma.PaginationTest do
test "paginates by min_id & limit", %{notes: notes} do test "paginates by min_id & limit", %{notes: notes} do
id = Enum.at(notes, 2).id |> Integer.to_string() id = Enum.at(notes, 2).id |> Integer.to_string()
paginated = Pagination.fetch_paginated(Object, %{"min_id" => id, "limit" => 1}) paginated = Pagination.fetch_paginated(Object, %{min_id: id, limit: 1})
assert length(paginated) == 1 assert length(paginated) == 1
end end
@ -64,13 +64,13 @@ defmodule Pleroma.PaginationTest do
end end
test "paginates by limit" do test "paginates by limit" do
paginated = Pagination.fetch_paginated(Object, %{"limit" => 2}, :offset) paginated = Pagination.fetch_paginated(Object, %{limit: 2}, :offset)
assert length(paginated) == 2 assert length(paginated) == 2
end end
test "paginates by limit & offset" do test "paginates by limit & offset" do
paginated = Pagination.fetch_paginated(Object, %{"limit" => 2, "offset" => 4}, :offset) paginated = Pagination.fetch_paginated(Object, %{limit: 2, offset: 4}, :offset)
assert length(paginated) == 1 assert length(paginated) == 1
end end

View file

@ -62,11 +62,11 @@ defmodule Mix.Tasks.Pleroma.RelayTest do
[undo_activity] = [undo_activity] =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
"type" => "Undo", type: "Undo",
"actor_id" => follower_id, actor_id: follower_id,
"limit" => 1, limit: 1,
"skip_preload" => true, skip_preload: true,
"invisible_actors" => true invisible_actors: true
}) })
assert undo_activity.data["type"] == "Undo" assert undo_activity.data["type"] == "Undo"

View file

@ -1122,7 +1122,7 @@ defmodule Pleroma.UserTest do
assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] == assert [%{activity | thread_muted?: CommonAPI.thread_muted?(user2, activity)}] ==
ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{ ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
"user" => user2 user: user2
}) })
{:ok, _user} = User.deactivate(user) {:ok, _user} = User.deactivate(user)
@ -1132,7 +1132,7 @@ defmodule Pleroma.UserTest do
assert [] == assert [] ==
ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{ ActivityPub.fetch_activities([user2.ap_id | User.following(user2)], %{
"user" => user2 user: user2
}) })
end end
end end

View file

@ -82,30 +82,28 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"}) {:ok, private_activity} = CommonAPI.post(user, %{status: ".", visibility: "private"})
activities = activities = ActivityPub.fetch_activities([], %{visibility: "direct", actor_id: user.ap_id})
ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
assert activities == [direct_activity] assert activities == [direct_activity]
activities = activities =
ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id}) ActivityPub.fetch_activities([], %{visibility: "unlisted", actor_id: user.ap_id})
assert activities == [unlisted_activity] assert activities == [unlisted_activity]
activities = activities =
ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id}) ActivityPub.fetch_activities([], %{visibility: "private", actor_id: user.ap_id})
assert activities == [private_activity] assert activities == [private_activity]
activities = activities = ActivityPub.fetch_activities([], %{visibility: "public", actor_id: user.ap_id})
ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
assert activities == [public_activity] assert activities == [public_activity]
activities = activities =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
:visibility => ~w[private public], visibility: ~w[private public],
"actor_id" => user.ap_id actor_id: user.ap_id
}) })
assert activities == [public_activity, private_activity] assert activities == [public_activity, private_activity]
@ -126,8 +124,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
activities = activities =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
"exclude_visibilities" => "direct", exclude_visibilities: "direct",
"actor_id" => user.ap_id actor_id: user.ap_id
}) })
assert public_activity in activities assert public_activity in activities
@ -137,8 +135,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
activities = activities =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
"exclude_visibilities" => "unlisted", exclude_visibilities: "unlisted",
"actor_id" => user.ap_id actor_id: user.ap_id
}) })
assert public_activity in activities assert public_activity in activities
@ -148,8 +146,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
activities = activities =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
"exclude_visibilities" => "private", exclude_visibilities: "private",
"actor_id" => user.ap_id actor_id: user.ap_id
}) })
assert public_activity in activities assert public_activity in activities
@ -159,8 +157,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
activities = activities =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
"exclude_visibilities" => "public", exclude_visibilities: "public",
"actor_id" => user.ap_id actor_id: user.ap_id
}) })
refute public_activity in activities refute public_activity in activities
@ -193,23 +191,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"}) {:ok, status_two} = CommonAPI.post(user, %{status: ". #essais"})
{:ok, status_three} = CommonAPI.post(user, %{status: ". #test #reject"}) {:ok, status_three} = CommonAPI.post(user, %{status: ". #test #reject"})
fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"}) fetch_one = ActivityPub.fetch_activities([], %{type: "Create", tag: "test"})
fetch_two = fetch_two = ActivityPub.fetch_activities([], %{type: "Create", tag: ["test", "essais"]})
ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
fetch_three = fetch_three =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
"type" => "Create", type: "Create",
"tag" => ["test", "essais"], tag: ["test", "essais"],
"tag_reject" => ["reject"] tag_reject: ["reject"]
}) })
fetch_four = fetch_four =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
"type" => "Create", type: "Create",
"tag" => ["test"], tag: ["test"],
"tag_all" => ["test", "reject"] tag_all: ["test", "reject"]
}) })
assert fetch_one == [status_one, status_three] assert fetch_one == [status_one, status_three]
@ -375,7 +372,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
_listen_activity_2 = insert(:listen) _listen_activity_2 = insert(:listen)
_listen_activity_3 = insert(:listen) _listen_activity_3 = insert(:listen)
timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]}) timeline = ActivityPub.fetch_activities([], %{type: ["Listen"]})
assert length(timeline) == 3 assert length(timeline) == 3
end end
@ -507,7 +504,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]}) {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user}) activities = ActivityPub.fetch_activities_for_context("2hu", %{blocking_user: user})
assert activities == [activity_two, activity] assert activities == [activity_two, activity]
end end
end end
@ -520,8 +517,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
booster = insert(:user) booster = insert(:user)
{:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]}) {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
activities = activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_three)
@ -529,8 +525,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]}) {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
activities = activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_three)
@ -541,16 +536,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
%Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id) %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
activity_three = Activity.get_by_id(activity_three.id) activity_three = Activity.get_by_id(activity_three.id)
activities = activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
refute Enum.member?(activities, activity_three) refute Enum.member?(activities, activity_three)
refute Enum.member?(activities, boost_activity) refute Enum.member?(activities, boost_activity)
assert Enum.member?(activities, activity_one) assert Enum.member?(activities, activity_one)
activities = activities = ActivityPub.fetch_activities([], %{blocking_user: nil, skip_preload: true})
ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_three)
@ -573,7 +566,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"}) {:ok, activity_four} = CommonAPI.post(blockee, %{status: "hey! @#{blocker.nickname}"})
activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker}) activities = ActivityPub.fetch_activities([], %{blocking_user: blocker})
assert Enum.member?(activities, activity_one) assert Enum.member?(activities, activity_one)
refute Enum.member?(activities, activity_two) refute Enum.member?(activities, activity_two)
@ -595,7 +588,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, activity_three} = CommonAPI.repeat(activity_two.id, friend) {:ok, activity_three} = CommonAPI.repeat(activity_two.id, friend)
activities = activities =
ActivityPub.fetch_activities([], %{"blocking_user" => blocker}) ActivityPub.fetch_activities([], %{blocking_user: blocker})
|> Enum.map(fn act -> act.id end) |> Enum.map(fn act -> act.id end)
assert Enum.member?(activities, activity_one.id) assert Enum.member?(activities, activity_one.id)
@ -611,8 +604,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
user = insert(:user) user = insert(:user)
{:ok, user} = User.block_domain(user, domain) {:ok, user} = User.block_domain(user, domain)
activities = activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
refute activity in activities refute activity in activities
@ -620,8 +612,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
ActivityPub.follow(user, followed_user) ActivityPub.follow(user, followed_user)
{:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user) {:ok, repeat_activity} = CommonAPI.repeat(activity.id, followed_user)
activities = activities = ActivityPub.fetch_activities([], %{blocking_user: user, skip_preload: true})
ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
refute repeat_activity in activities refute repeat_activity in activities
end end
@ -641,8 +632,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
note = insert(:note, %{data: %{"actor" => domain_user.ap_id}}) note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
activity = insert(:note_activity, %{note: note}) activity = insert(:note_activity, %{note: note})
activities = activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
assert activity in activities assert activity in activities
@ -653,8 +643,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
bad_activity = insert(:note_activity, %{note: bad_note}) bad_activity = insert(:note_activity, %{note: bad_note})
{:ok, repeat_activity} = CommonAPI.repeat(bad_activity.id, domain_user) {:ok, repeat_activity} = CommonAPI.repeat(bad_activity.id, domain_user)
activities = activities = ActivityPub.fetch_activities([], %{blocking_user: blocker, skip_preload: true})
ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
refute repeat_activity in activities refute repeat_activity in activities
end end
@ -669,8 +658,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
activity_one_actor = User.get_by_ap_id(activity_one.data["actor"]) activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
{:ok, _user_relationships} = User.mute(user, activity_one_actor) {:ok, _user_relationships} = User.mute(user, activity_one_actor)
activities = activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_three)
@ -679,9 +667,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
# Calling with 'with_muted' will deliver muted activities, too. # Calling with 'with_muted' will deliver muted activities, too.
activities = activities =
ActivityPub.fetch_activities([], %{ ActivityPub.fetch_activities([], %{
"muting_user" => user, muting_user: user,
"with_muted" => true, with_muted: true,
"skip_preload" => true skip_preload: true
}) })
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
@ -690,8 +678,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, _user_mute} = User.unmute(user, activity_one_actor) {:ok, _user_mute} = User.unmute(user, activity_one_actor)
activities = activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_three)
@ -703,15 +690,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
%Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id) %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
activity_three = Activity.get_by_id(activity_three.id) activity_three = Activity.get_by_id(activity_three.id)
activities = activities = ActivityPub.fetch_activities([], %{muting_user: user, skip_preload: true})
ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
refute Enum.member?(activities, activity_three) refute Enum.member?(activities, activity_three)
refute Enum.member?(activities, boost_activity) refute Enum.member?(activities, boost_activity)
assert Enum.member?(activities, activity_one) assert Enum.member?(activities, activity_one)
activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true}) activities = ActivityPub.fetch_activities([], %{muting_user: nil, skip_preload: true})
assert Enum.member?(activities, activity_two) assert Enum.member?(activities, activity_two)
assert Enum.member?(activities, activity_three) assert Enum.member?(activities, activity_three)
@ -727,7 +713,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, _activity_two} = CommonAPI.add_mute(user, activity_two) {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user}) assert [_activity_one] = ActivityPub.fetch_activities([], %{muting_user: user})
end end
test "returns thread muted activities when with_muted is set" do test "returns thread muted activities when with_muted is set" do
@ -739,7 +725,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, _activity_two} = CommonAPI.add_mute(user, activity_two) {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
assert [_activity_two, _activity_one] = assert [_activity_two, _activity_one] =
ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true}) ActivityPub.fetch_activities([], %{muting_user: user, with_muted: true})
end end
test "does include announces on request" do test "does include announces on request" do
@ -761,7 +747,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user}) {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
{:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user}) {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
[activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"}) [activity] = ActivityPub.fetch_user_activities(user, nil, %{exclude_reblogs: true})
assert activity == expected_activity assert activity == expected_activity
end end
@ -804,7 +790,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
expected_activities = ActivityBuilder.insert_list(10) expected_activities = ActivityBuilder.insert_list(10)
since_id = List.last(activities).id since_id = List.last(activities).id
activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id}) activities = ActivityPub.fetch_public_activities(%{since_id: since_id})
assert collect_ids(activities) == collect_ids(expected_activities) assert collect_ids(activities) == collect_ids(expected_activities)
assert length(activities) == 10 assert length(activities) == 10
@ -819,7 +805,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
|> ActivityBuilder.insert_list() |> ActivityBuilder.insert_list()
|> List.first() |> List.first()
activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id}) activities = ActivityPub.fetch_public_activities(%{max_id: max_id})
assert length(activities) == 20 assert length(activities) == 20
assert collect_ids(activities) == collect_ids(expected_activities) assert collect_ids(activities) == collect_ids(expected_activities)
@ -831,8 +817,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
later_activities = ActivityBuilder.insert_list(10) later_activities = ActivityBuilder.insert_list(10)
activities = activities = ActivityPub.fetch_public_activities(%{page: "2", page_size: "20"}, :offset)
ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
assert length(activities) == 20 assert length(activities) == 20
@ -848,7 +833,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, activity} = CommonAPI.repeat(activity.id, booster) {:ok, activity} = CommonAPI.repeat(activity.id, booster)
activities = ActivityPub.fetch_activities([], %{"muting_user" => user}) activities = ActivityPub.fetch_activities([], %{muting_user: user})
refute Enum.any?(activities, fn %{id: id} -> id == activity.id end) refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
end end
@ -862,7 +847,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, activity} = CommonAPI.repeat(activity.id, booster) {:ok, activity} = CommonAPI.repeat(activity.id, booster)
activities = ActivityPub.fetch_activities([], %{"muting_user" => user}) activities = ActivityPub.fetch_activities([], %{muting_user: user})
assert Enum.any?(activities, fn %{id: id} -> id == activity.id end) assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
end end
@ -1066,7 +1051,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
assert length(activities) == 3 assert length(activities) == 3
activities = activities =
ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{"user" => user1}) ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{user: user1})
|> Enum.map(fn a -> a.id end) |> Enum.map(fn a -> a.id end)
assert [public_activity.id, private_activity_1.id] == activities assert [public_activity.id, private_activity_1.id] == activities
@ -1115,7 +1100,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
CommonAPI.pin(activity_three.id, user) CommonAPI.pin(activity_three.id, user)
user = refresh_record(user) user = refresh_record(user)
activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"}) activities = ActivityPub.fetch_user_activities(user, nil, %{pinned: true})
assert 3 = length(activities) assert 3 = length(activities)
end end
@ -1226,7 +1211,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
activity = Repo.preload(activity, :bookmark) activity = Repo.preload(activity, :bookmark)
activity = %Activity{activity | thread_muted?: !!activity.thread_muted?} activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity] assert ActivityPub.fetch_activities([], %{user: user}) == [activity]
end end
def data_uri do def data_uri do
@ -1400,7 +1385,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id] assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
result = ActivityPub.fetch_favourites(user, %{"limit" => 2}) result = ActivityPub.fetch_favourites(user, %{limit: 2})
assert Enum.map(result, & &1.id) == [a1.id, a5.id] assert Enum.map(result, & &1.id) == [a1.id, a5.id]
end end
end end
@ -1470,7 +1455,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
{:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id}) {:ok, _reply} = CommonAPI.post(user, %{status: "yeah", in_reply_to_status_id: activity.id})
[result] = ActivityPub.fetch_public_activities(%{"exclude_replies" => "true"}) [result] = ActivityPub.fetch_public_activities(%{exclude_replies: true})
assert result.id == activity.id assert result.id == activity.id
@ -1483,11 +1468,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
test "public timeline", %{users: %{u1: user}} do test "public timeline", %{users: %{u1: user}} do
activities_ids = activities_ids =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("local_only", false) |> Map.put(:local_only, false)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
|> Enum.map(& &1.id) |> Enum.map(& &1.id)
@ -1504,12 +1489,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
} do } do
activities_ids = activities_ids =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("local_only", false) |> Map.put(:local_only, false)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("reply_visibility", "following") |> Map.put(:reply_visibility, "following")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
|> Enum.map(& &1.id) |> Enum.map(& &1.id)
@ -1531,12 +1516,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
} do } do
activities_ids = activities_ids =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("local_only", false) |> Map.put(:local_only, false)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("reply_visibility", "self") |> Map.put(:reply_visibility, "self")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
|> Enum.map(& &1.id) |> Enum.map(& &1.id)
@ -1555,11 +1540,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
} do } do
params = params =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
activities_ids = activities_ids =
ActivityPub.fetch_activities([user.ap_id | User.following(user)], params) ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
@ -1593,12 +1578,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
} do } do
params = params =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> Map.put("reply_visibility", "following") |> Map.put(:reply_visibility, "following")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
activities_ids = activities_ids =
ActivityPub.fetch_activities([user.ap_id | User.following(user)], params) ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
@ -1632,12 +1617,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
} do } do
params = params =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> Map.put("reply_visibility", "self") |> Map.put(:reply_visibility, "self")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
activities_ids = activities_ids =
ActivityPub.fetch_activities([user.ap_id | User.following(user)], params) ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
@ -1666,11 +1651,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
test "public timeline", %{users: %{u1: user}} do test "public timeline", %{users: %{u1: user}} do
activities_ids = activities_ids =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("local_only", false) |> Map.put(:local_only, false)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
|> Enum.map(& &1.id) |> Enum.map(& &1.id)
@ -1680,13 +1665,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
activities_ids = activities_ids =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("local_only", false) |> Map.put(:local_only, false)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("reply_visibility", "following") |> Map.put(:reply_visibility, "following")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
|> Enum.map(& &1.id) |> Enum.map(& &1.id)
@ -1696,13 +1681,13 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
activities_ids = activities_ids =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("local_only", false) |> Map.put(:local_only, false)
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("reply_visibility", "self") |> Map.put(:reply_visibility, "self")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> ActivityPub.fetch_public_activities() |> ActivityPub.fetch_public_activities()
|> Enum.map(& &1.id) |> Enum.map(& &1.id)
@ -1712,10 +1697,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
test "home timeline", %{users: %{u1: user}} do test "home timeline", %{users: %{u1: user}} do
params = params =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
activities_ids = activities_ids =
ActivityPub.fetch_activities([user.ap_id | User.following(user)], params) ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
@ -1727,12 +1712,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
params = params =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> Map.put("reply_visibility", "following") |> Map.put(:reply_visibility, "following")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
activities_ids = activities_ids =
ActivityPub.fetch_activities([user.ap_id | User.following(user)], params) ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
@ -1751,12 +1736,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
} do } do
params = params =
%{} %{}
|> Map.put("type", ["Create", "Announce"]) |> Map.put(:type, ["Create", "Announce"])
|> Map.put("blocking_user", user) |> Map.put(:blocking_user, user)
|> Map.put("muting_user", user) |> Map.put(:muting_user, user)
|> Map.put("user", user) |> Map.put(:user, user)
|> Map.put("reply_visibility", "self") |> Map.put(:reply_visibility, "self")
|> Map.put("reply_filtering_user", user) |> Map.put(:reply_filtering_user, user)
activities_ids = activities_ids =
ActivityPub.fetch_activities([user.ap_id | User.following(user)], params) ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)