/api/v1/favourites: added sorting for activites by adds to favorites

This commit is contained in:
Maksim Pechnikov 2019-11-18 09:44:08 +03:00
parent 4df2d64b9f
commit 4beb3ce5c5
2 changed files with 40 additions and 9 deletions

View File

@ -1055,6 +1055,45 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|> maybe_update_cc(list_memberships, opts["user"]) |> maybe_update_cc(list_memberships, opts["user"])
end end
@doc """
Fetch favorites activities of user with order by sort adds to favorites
"""
@spec fetch_favourites(list(String.t()), User.t(), map(), atom()) :: list(Activity.t())
def fetch_favourites(recipients, user, params \\ %{}, pagination \\ :keyset) do
opts =
%{
"type" => "Create",
"favorited_by" => user.ap_id,
"blocking_user" => user
}
|> Map.merge(params)
recipients
|> fetch_activities_query(opts)
|> order_by_favourites(user)
|> Pagination.fetch_paginated(opts, pagination)
end
# sorts by adds to favorites
#
@spec order_by_favourites(Ecto.Query.t(), User.t()) :: Ecto.Query.t()
defp order_by_favourites(query, user) do
join(query, :inner, [activity, object], a1 in Activity,
on:
fragment(
"(?->>'id') = COALESCE(?->'object'->>'id', ?->>'object') AND (?->>'type' = 'Like') AND (?.actor = ?)",
object.data,
a1.data,
a1.data,
a1.data,
a1,
^user.ap_id
),
as: :like_activity
)
|> order_by([_, _, like_activity], desc: like_activity.updated_at)
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})
when is_list(list_memberships) and length(list_memberships) > 0 do when is_list(list_memberships) and length(list_memberships) > 0 do
Enum.map(activities, fn Enum.map(activities, fn

View File

@ -346,15 +346,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
@doc "GET /api/v1/favourites" @doc "GET /api/v1/favourites"
def favourites(%{assigns: %{user: user}} = conn, params) do def favourites(%{assigns: %{user: user}} = conn, params) do
params = activities = ActivityPub.fetch_favourites([], user, Map.take(params, ["limit"]))
params
|> Map.put("type", "Create")
|> Map.put("favorited_by", user.ap_id)
|> Map.put("blocking_user", user)
activities =
ActivityPub.fetch_activities([], params)
|> Enum.reverse()
conn conn
|> add_link_headers(activities) |> add_link_headers(activities)