From: Maksim Pechnikov Date: Mon, 18 Nov 2019 06:44:08 +0000 (+0300) Subject: /api/v1/favourites: added sorting for activites by adds to favorites X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=4beb3ce5c57e6caf03365d32078ea58fd0c93434;p=akkoma /api/v1/favourites: added sorting for activites by adds to favorites --- diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index d0c014e9d..95d97615c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -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 diff --git a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex index 74b223cf4..cdc4cec9c 100644 --- a/lib/pleroma/web/mastodon_api/controllers/status_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/status_controller.ex @@ -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)