/api/v1/favourites: added sorting for activites by adds to favorites
This commit is contained in:
parent
4df2d64b9f
commit
4beb3ce5c5
@ -1055,6 +1055,45 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
||||
|> maybe_update_cc(list_memberships, opts["user"])
|
||||
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})
|
||||
when is_list(list_memberships) and length(list_memberships) > 0 do
|
||||
Enum.map(activities, fn
|
||||
|
@ -346,15 +346,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusController do
|
||||
|
||||
@doc "GET /api/v1/favourites"
|
||||
def favourites(%{assigns: %{user: user}} = conn, params) do
|
||||
params =
|
||||
params
|
||||
|> Map.put("type", "Create")
|
||||
|> Map.put("favorited_by", user.ap_id)
|
||||
|> Map.put("blocking_user", user)
|
||||
|
||||
activities =
|
||||
ActivityPub.fetch_activities([], params)
|
||||
|> Enum.reverse()
|
||||
activities = ActivityPub.fetch_favourites([], user, Map.take(params, ["limit"]))
|
||||
|
||||
conn
|
||||
|> add_link_headers(activities)
|
||||
|
Loading…
Reference in New Issue
Block a user