Merge branch 'refactor/add_link_headers' into 'develop'

Refactor `add_link_headers/7` -> `add_link_headers/3`

See merge request pleroma/pleroma!1636
This commit is contained in:
lain 2019-09-13 17:06:38 +00:00
commit 1be09bde2d
3 changed files with 50 additions and 100 deletions

View file

@ -34,79 +34,38 @@ defmodule Pleroma.Web.ControllerHelper do
defp param_to_integer(_, default), do: default defp param_to_integer(_, default), do: default
def add_link_headers( def add_link_headers(conn, activities, extra_params \\ %{}) do
conn, case List.last(activities) do
method, %{id: max_id} ->
activities, params =
param \\ nil, conn.params
params \\ %{}, |> Map.drop(Map.keys(conn.path_params))
func3 \\ nil, |> Map.drop(["since_id", "max_id", "min_id"])
func4 \\ nil |> Map.merge(extra_params)
) do
params =
conn.params
|> Map.drop(["since_id", "max_id", "min_id"])
|> Map.merge(params)
last = List.last(activities) limit =
params
|> Map.get("limit", "20")
|> String.to_integer()
func3 = func3 || (&mastodon_api_url/3) min_id =
func4 = func4 || (&mastodon_api_url/4) if length(activities) <= limit do
activities
|> List.first()
|> Map.get(:id)
else
activities
|> Enum.at(limit * -1)
|> Map.get(:id)
end
if last do next_url = current_url(conn, Map.merge(params, %{max_id: max_id}))
max_id = last.id prev_url = current_url(conn, Map.merge(params, %{min_id: min_id}))
limit = put_resp_header(conn, "link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
params
|> Map.get("limit", "20")
|> String.to_integer()
min_id = _ ->
if length(activities) <= limit do conn
activities
|> List.first()
|> Map.get(:id)
else
activities
|> Enum.at(limit * -1)
|> Map.get(:id)
end
{next_url, prev_url} =
if param do
{
func4.(
Pleroma.Web.Endpoint,
method,
param,
Map.merge(params, %{max_id: max_id})
),
func4.(
Pleroma.Web.Endpoint,
method,
param,
Map.merge(params, %{min_id: min_id})
)
}
else
{
func3.(
Pleroma.Web.Endpoint,
method,
Map.merge(params, %{max_id: max_id})
),
func3.(
Pleroma.Web.Endpoint,
method,
Map.merge(params, %{min_id: min_id})
)
}
end
conn
|> put_resp_header("link", "<#{next_url}>; rel=\"next\", <#{prev_url}>; rel=\"prev\"")
else
conn
end end
end end
end end

View file

@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
use Pleroma.Web, :controller use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper, import Pleroma.Web.ControllerHelper,
only: [json_response: 3, add_link_headers: 5, add_link_headers: 4, add_link_headers: 3] only: [json_response: 3, add_link_headers: 2, add_link_headers: 3]
alias Ecto.Changeset alias Ecto.Changeset
alias Pleroma.Activity alias Pleroma.Activity
@ -365,7 +365,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|> Enum.reverse() |> Enum.reverse()
conn conn
|> add_link_headers(:home_timeline, activities) |> add_link_headers(activities)
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{activities: activities, for: user, as: :activity}) |> render("index.json", %{activities: activities, for: user, as: :activity})
end end
@ -384,7 +384,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|> Enum.reverse() |> Enum.reverse()
conn conn
|> add_link_headers(:public_timeline, activities, false, %{"local" => local_only}) |> add_link_headers(activities, %{"local" => local_only})
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{activities: activities, for: user, as: :activity}) |> render("index.json", %{activities: activities, for: user, as: :activity})
end end
@ -398,7 +398,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
activities = ActivityPub.fetch_user_activities(user, reading_user, params) activities = ActivityPub.fetch_user_activities(user, reading_user, params)
conn conn
|> add_link_headers(:user_statuses, activities, params["id"]) |> add_link_headers(activities)
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{ |> render("index.json", %{
activities: activities, activities: activities,
@ -422,7 +422,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|> Pagination.fetch_paginated(params) |> Pagination.fetch_paginated(params)
conn conn
|> add_link_headers(:dm_timeline, activities) |> add_link_headers(activities)
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{activities: activities, for: user, as: :activity}) |> render("index.json", %{activities: activities, for: user, as: :activity})
end end
@ -537,7 +537,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
def scheduled_statuses(%{assigns: %{user: user}} = conn, params) do def scheduled_statuses(%{assigns: %{user: user}} = conn, params) do
with scheduled_activities <- MastodonAPI.get_scheduled_activities(user, params) do with scheduled_activities <- MastodonAPI.get_scheduled_activities(user, params) do
conn conn
|> add_link_headers(:scheduled_statuses, scheduled_activities) |> add_link_headers(scheduled_activities)
|> put_view(ScheduledActivityView) |> put_view(ScheduledActivityView)
|> render("index.json", %{scheduled_activities: scheduled_activities}) |> render("index.json", %{scheduled_activities: scheduled_activities})
end end
@ -720,7 +720,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
notifications = MastodonAPI.get_notifications(user, params) notifications = MastodonAPI.get_notifications(user, params)
conn conn
|> add_link_headers(:notifications, notifications) |> add_link_headers(notifications)
|> put_view(NotificationView) |> put_view(NotificationView)
|> render("index.json", %{notifications: notifications, for: user}) |> render("index.json", %{notifications: notifications, for: user})
end end
@ -908,7 +908,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|> Enum.reverse() |> Enum.reverse()
conn conn
|> add_link_headers(:hashtag_timeline, activities, params["tag"], %{"local" => local_only}) |> add_link_headers(activities, %{"local" => local_only})
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{activities: activities, for: user, as: :activity}) |> render("index.json", %{activities: activities, for: user, as: :activity})
end end
@ -924,7 +924,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end end
conn conn
|> add_link_headers(:followers, followers, user) |> add_link_headers(followers)
|> put_view(AccountView) |> put_view(AccountView)
|> render("accounts.json", %{for: for_user, users: followers, as: :user}) |> render("accounts.json", %{for: for_user, users: followers, as: :user})
end end
@ -941,7 +941,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end end
conn conn
|> add_link_headers(:following, followers, user) |> add_link_headers(followers)
|> put_view(AccountView) |> put_view(AccountView)
|> render("accounts.json", %{for: for_user, users: followers, as: :user}) |> render("accounts.json", %{for: for_user, users: followers, as: :user})
end end
@ -1166,7 +1166,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|> Enum.reverse() |> Enum.reverse()
conn conn
|> add_link_headers(:favourites, activities) |> add_link_headers(activities)
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{activities: activities, for: user, as: :activity}) |> render("index.json", %{activities: activities, for: user, as: :activity})
end end
@ -1193,7 +1193,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|> Enum.reverse() |> Enum.reverse()
conn conn
|> add_link_headers(:favourites, activities) |> add_link_headers(activities)
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{activities: activities, for: for_user, as: :activity}) |> render("index.json", %{activities: activities, for: for_user, as: :activity})
else else
@ -1214,7 +1214,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
|> Enum.map(fn b -> Map.put(b.activity, :bookmark, Map.delete(b, :activity)) end) |> Enum.map(fn b -> Map.put(b.activity, :bookmark, Map.delete(b, :activity)) end)
conn conn
|> add_link_headers(:bookmarks, bookmarks) |> add_link_headers(bookmarks)
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{activities: activities, for: user, as: :activity}) |> render("index.json", %{activities: activities, for: user, as: :activity})
end end
@ -1654,7 +1654,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end) end)
conn conn
|> add_link_headers(:conversations, participations) |> add_link_headers(participations)
|> json(conversations) |> json(conversations)
end end

View file

@ -5,7 +5,7 @@
defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
use Pleroma.Web, :controller use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 7] import Pleroma.Web.ControllerHelper, only: [add_link_headers: 2]
alias Pleroma.Conversation.Participation alias Pleroma.Conversation.Participation
alias Pleroma.Notification alias Pleroma.Notification
@ -27,31 +27,22 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
%{assigns: %{user: user}} = conn, %{assigns: %{user: user}} = conn,
%{"id" => participation_id} = params %{"id" => participation_id} = params
) do ) do
params = participation = Participation.get(participation_id, preload: [:conversation])
params
|> Map.put("blocking_user", user)
|> Map.put("muting_user", user)
|> Map.put("user", user)
participation =
participation_id
|> Participation.get(preload: [:conversation])
if user.id == participation.user_id do if user.id == participation.user_id do
params =
params
|> Map.put("blocking_user", user)
|> Map.put("muting_user", user)
|> Map.put("user", user)
activities = activities =
participation.conversation.ap_id participation.conversation.ap_id
|> ActivityPub.fetch_activities_for_context(params) |> ActivityPub.fetch_activities_for_context(params)
|> Enum.reverse() |> Enum.reverse()
conn conn
|> add_link_headers( |> add_link_headers(activities)
:conversation_statuses,
activities,
participation_id,
params,
nil,
&pleroma_api_url/4
)
|> put_view(StatusView) |> put_view(StatusView)
|> render("index.json", %{activities: activities, for: user, as: :activity}) |> render("index.json", %{activities: activities, for: user, as: :activity})
end end