use Pleroma.Web, :view
alias Pleroma.User
+ alias Pleroma.Web.AdminAPI
alias Pleroma.Web.AdminAPI.AccountView
+ alias Pleroma.Web.MastodonAPI
alias Pleroma.Web.MediaProxy
def render("index.json", %{users: users, count: count, page_size: page_size}) do
}
end
+ def merge_account_views(%User{} = user) do
+ MastodonAPI.AccountView.render("show.json", %{user: user, skip_relationships: true})
+ |> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
+ end
+
+ def merge_account_views(_), do: %{}
+
defp parse_error([]), do: ""
defp parse_error(errors) do
alias Pleroma.Web.AdminAPI
alias Pleroma.Web.AdminAPI.Report
alias Pleroma.Web.CommonAPI.Utils
- alias Pleroma.Web.MastodonAPI
alias Pleroma.Web.MastodonAPI.StatusView
+ defdelegate merge_account_views(user), to: AdminAPI.AccountView
+
def render("index.json", %{reports: reports}) do
%{
reports:
created_at: Utils.to_masto_date(inserted_at)
}
end
-
- defp merge_account_views(%User{} = user) do
- MastodonAPI.AccountView.render("show.json", %{user: user, skip_relationships: true})
- |> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
- end
-
- defp merge_account_views(_), do: %{}
end
require Pleroma.Constants
- alias Pleroma.User
alias Pleroma.Web.AdminAPI
alias Pleroma.Web.MastodonAPI
- alias Pleroma.Web.MastodonAPI.StatusView
+
+ defdelegate merge_account_views(user), to: AdminAPI.AccountView
def render("index.json", opts) do
safe_render_many(opts.activities, __MODULE__, "show.json", opts)
end
def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
- user = StatusView.get_user(activity.data["actor"])
+ user = MastodonAPI.StatusView.get_user(activity.data["actor"])
- StatusView.render("show.json", opts)
+ MastodonAPI.StatusView.render("show.json", opts)
|> Map.merge(%{account: merge_account_views(user)})
end
-
- defp merge_account_views(%User{} = user) do
- MastodonAPI.AccountView.render("show.json", %{user: user, skip_relationships: true})
- |> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
- end
-
- defp merge_account_views(_), do: %{}
end
def put_if_exist(map, _key, nil), do: map
def put_if_exist(map, key, value), do: Map.put(map, key, value)
-
- @doc "Whether to skip `account.pleroma.relationship` rendering for statuses/notifications"
- def skip_relationships?(params) do
- not truthy_param?(params["with_relationships"])
- end
end
add_link_headers: 2,
truthy_param?: 1,
assign_account_by_id: 2,
- json_response: 3,
- skip_relationships?: 1
+ json_response: 3
]
alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
|> render("index.json",
activities: activities,
for: reading_user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
else
_e -> render_error(conn, :not_found, "Can't find user")
defmodule Pleroma.Web.MastodonAPI.NotificationController do
use Pleroma.Web, :controller
- import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2, skip_relationships?: 1]
+ import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
alias Pleroma.Notification
alias Pleroma.Plugs.OAuthScopesPlug
|> add_link_headers(notifications)
|> render("index.json",
notifications: notifications,
- for: user,
- skip_relationships: skip_relationships?(params)
+ for: user
)
end
defmodule Pleroma.Web.MastodonAPI.SearchController do
use Pleroma.Web, :controller
- import Pleroma.Web.ControllerHelper, only: [skip_relationships?: 1]
-
alias Pleroma.Activity
alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.Plugs.RateLimiter
defp search_options(params, user) do
[
- skip_relationships: skip_relationships?(params),
resolve: params[:resolve],
following: params[:following],
limit: params[:limit],
StatusView.render("index.json",
activities: statuses,
for: options[:for_user],
- as: :activity,
- skip_relationships: options[:skip_relationships]
+ as: :activity
)
end
use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper,
- only: [try_render: 3, add_link_headers: 2, skip_relationships?: 1]
+ only: [try_render: 3, add_link_headers: 2]
require Ecto.Query
`ids` query param is required
"""
- def index(%{assigns: %{user: user}} = conn, %{"ids" => ids} = params) do
+ def index(%{assigns: %{user: user}} = conn, %{"ids" => ids} = _params) do
limit = 100
activities =
render(conn, "index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
end
|> render("index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
end
|> render("index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
end
end
use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper,
- only: [add_link_headers: 2, add_link_headers: 3, truthy_param?: 1, skip_relationships?: 1]
+ only: [add_link_headers: 2, add_link_headers: 3, truthy_param?: 1]
alias Pleroma.Pagination
alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
|> render("index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
end
|> render("index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
end
|> render("index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
end
end
|> render("index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
end
render(conn, "index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
else
_e -> render_error(conn, :forbidden, "Error.")
alias Pleroma.Web.MastodonAPI.StatusView
def render("index.json", %{notifications: notifications, for: reading_user} = opts) do
- opts = Map.merge(%{skip_relationships: true}, opts)
-
activities = Enum.map(notifications, & &1.activity)
parent_activities =
|> Enum.filter(& &1)
|> Kernel.++(move_activities_targets)
- UserRelationship.view_relationships_option(reading_user, actors,
- source_mutes_only: opts[:skip_relationships]
- )
+ UserRelationship.view_relationships_option(reading_user, actors, source_mutes_only: true)
end
opts =
for: reading_user
} = opts
) do
- opts = Map.merge(%{skip_relationships: true}, opts)
-
actor = User.get_cached_by_ap_id(activity.data["actor"])
parent_activity_fn = fn ->
mastodon_type = Activity.mastodon_notification_type(activity)
- render_opts = %{
- relationships: opts[:relationships],
- skip_relationships: opts[:skip_relationships]
- }
+ # Note: :relationships contain user mutes (needed for :muted flag in :status)
+ status_render_opts = %{relationships: opts[:relationships]}
+
+ account_render_opts = %{skip_relationships: true}
with %{id: _} = account <-
AccountView.render(
"show.json",
- Map.merge(render_opts, %{user: actor, for: reading_user})
+ Map.merge(account_render_opts, %{user: actor, for: reading_user})
) do
response = %{
id: to_string(notification.id),
case mastodon_type do
"mention" ->
- put_status(response, activity, reading_user, render_opts)
+ put_status(response, activity, reading_user, status_render_opts)
"favourite" ->
- put_status(response, parent_activity_fn.(), reading_user, render_opts)
+ put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
"reblog" ->
- put_status(response, parent_activity_fn.(), reading_user, render_opts)
+ put_status(response, parent_activity_fn.(), reading_user, status_render_opts)
"move" ->
- # Note: :skip_relationships option being applied to _account_ rendering (here)
- put_target(response, activity, reading_user, render_opts)
+ put_target(response, activity, reading_user, account_render_opts)
"pleroma:emoji_reaction" ->
response
- |> put_status(parent_activity_fn.(), reading_user, render_opts)
+ |> put_status(parent_activity_fn.(), reading_user, status_render_opts)
|> put_emoji(activity)
type when type in ["follow", "follow_request"] ->
end
def render("index.json", opts) do
- opts = Map.merge(%{skip_relationships: true}, opts)
-
reading_user = opts[:for]
# To do: check AdminAPIControllerTest on the reasons behind nil activities in the list
|> Enum.map(&get_user(&1.data["actor"], false))
|> Enum.filter(& &1)
- UserRelationship.view_relationships_option(reading_user, actors,
- source_mutes_only: opts[:skip_relationships]
- )
+ UserRelationship.view_relationships_option(reading_user, actors, source_mutes_only: true)
end
opts =
"show.json",
%{activity: %{data: %{"type" => "Announce", "object" => _object}} = activity} = opts
) do
- opts = Map.merge(%{skip_relationships: true}, opts)
-
user = get_user(activity.data["actor"])
created_at = Utils.to_masto_date(activity.data["published"])
activity_object = Object.normalize(activity)
AccountView.render("show.json", %{
user: user,
for: opts[:for],
- relationships: opts[:relationships],
- skip_relationships: opts[:skip_relationships]
+ skip_relationships: true
}),
in_reply_to_id: nil,
in_reply_to_account_id: nil,
end
def render("show.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
- opts = Map.merge(%{skip_relationships: true}, opts)
-
object = Object.normalize(activity)
user = get_user(activity.data["actor"])
AccountView.render("show.json", %{
user: user,
for: opts[:for],
- relationships: opts[:relationships],
- skip_relationships: opts[:skip_relationships]
+ skip_relationships: true
}),
in_reply_to_id: reply_to && to_string(reply_to.id),
in_reply_to_account_id: reply_to_user && to_string(reply_to_user.id),
use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper,
- only: [json_response: 3, add_link_headers: 2, assign_account_by_id: 2, skip_relationships?: 1]
+ only: [json_response: 3, add_link_headers: 2, assign_account_by_id: 2]
alias Ecto.Changeset
alias Pleroma.Plugs.EnsurePublicOrAuthenticatedPlug
|> render("index.json",
activities: activities,
for: for_user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
end
defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
use Pleroma.Web, :controller
- import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2, skip_relationships?: 1]
+ import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
alias Pleroma.Activity
alias Pleroma.Conversation.Participation
|> render("index.json",
activities: activities,
for: user,
- as: :activity,
- skip_relationships: skip_relationships?(params)
+ as: :activity
)
else
_error ->
end
end
- def mark_notifications_as_read(%{assigns: %{user: user}} = conn, %{"max_id" => max_id} = params) do
+ def mark_notifications_as_read(%{assigns: %{user: user}} = conn, %{"max_id" => max_id}) do
with notifications <- Notification.set_read_up_to(user, max_id) do
notifications = Enum.take(notifications, 80)
|> put_view(NotificationView)
|> render("index.json",
notifications: notifications,
- for: user,
- skip_relationships: skip_relationships?(params)
+ for: user
)
end
end
describe "home" do
setup do: oauth_access(["read:statuses"])
- test "does NOT render account/pleroma/relationship by default", %{
+ test "does NOT embed account/pleroma/relationship in statuses", %{
user: user,
conn: conn
} do
end)
end
- test "embeds account relationships with `with_relationships=true`", %{user: user, conn: conn} do
- uri = "/api/v1/timelines/home?with_relationships=true"
-
- following = insert(:user, nickname: "followed")
- third_user = insert(:user, nickname: "repeated")
-
- {:ok, _activity} = CommonAPI.post(following, %{"status" => "post"})
- {:ok, activity} = CommonAPI.post(third_user, %{"status" => "repeated post"})
- {:ok, _, _} = CommonAPI.repeat(activity.id, following)
-
- ret_conn = get(conn, uri)
-
- assert Enum.empty?(json_response(ret_conn, :ok))
-
- {:ok, _user} = User.follow(user, following)
-
- ret_conn = get(conn, uri)
-
- assert [
- %{
- "reblog" => %{
- "content" => "repeated post",
- "account" => %{
- "pleroma" => %{
- "relationship" => %{"following" => false, "followed_by" => false}
- }
- }
- },
- "account" => %{
- "pleroma" => %{
- "relationship" => %{"following" => true}
- }
- }
- },
- %{
- "content" => "post",
- "account" => %{
- "acct" => "followed",
- "pleroma" => %{
- "relationship" => %{"following" => true}
- }
- }
- }
- ] = json_response(ret_conn, :ok)
-
- {:ok, _user} = User.follow(third_user, user)
-
- ret_conn = get(conn, uri)
-
- assert [
- %{
- "reblog" => %{
- "content" => "repeated post",
- "account" => %{
- "acct" => "repeated",
- "pleroma" => %{
- "relationship" => %{"following" => false, "followed_by" => true}
- }
- }
- },
- "account" => %{
- "pleroma" => %{
- "relationship" => %{"following" => true}
- }
- }
- },
- %{
- "content" => "post",
- "account" => %{
- "acct" => "followed",
- "pleroma" => %{
- "relationship" => %{"following" => true}
- }
- }
- }
- ] = json_response(ret_conn, :ok)
- end
-
test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do
{:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
{:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})