updated fetch_favorites

This commit is contained in:
Maksim Pechnikov 2019-11-18 16:56:25 +03:00
parent 9da4c88b49
commit 0937895182
4 changed files with 74 additions and 68 deletions

View file

@ -23,6 +23,23 @@ defmodule Pleroma.Object do
timestamps() timestamps()
end end
def with_joined_activity(query, activity_type \\ "Create", join_type \\ :inner) do
object_position = Map.get(query.aliases, :object, 0)
join(query, join_type, [{object, object_position}], a in Activity,
on:
fragment(
"COALESCE(?->'object'->>'id', ?->>'object') = (? ->> 'id') AND (?->>'type' = ?) ",
a.data,
a.data,
object.data,
a.data,
^activity_type
),
as: :object_activity
)
end
def create(data) do def create(data) do
Object.change(%Object{}, %{data: data}) Object.change(%Object{}, %{data: data})
|> Repo.insert() |> Repo.insert()

View file

@ -17,59 +17,59 @@ defmodule Pleroma.Pagination do
def page_keys, do: @page_keys def page_keys, do: @page_keys
def fetch_paginated(query, params, type \\ :keyset) def fetch_paginated(query, params, type \\ :keyset, table_binding \\ nil)
def fetch_paginated(query, %{"total" => true} = params, :keyset) do def fetch_paginated(query, %{"total" => true} = params, :keyset, table_binding) do
total = Repo.aggregate(query, :count, :id) total = Repo.aggregate(query, :count, :id)
%{ %{
total: total, total: total,
items: fetch_paginated(query, Map.drop(params, ["total"]), :keyset) items: fetch_paginated(query, Map.drop(params, ["total"]), :keyset, table_binding)
} }
end end
def fetch_paginated(query, params, :keyset) do def fetch_paginated(query, params, :keyset, table_binding) do
options = cast_params(params) options = cast_params(params)
query query
|> paginate(options, :keyset) |> paginate(options, :keyset, table_binding)
|> Repo.all() |> Repo.all()
|> enforce_order(options) |> enforce_order(options)
end end
def fetch_paginated(query, %{"total" => true} = params, :offset) do def fetch_paginated(query, %{"total" => true} = params, :offset, table_binding) do
total = Repo.aggregate(query, :count, :id) total = Repo.aggregate(query, :count, :id)
%{ %{
total: total, total: total,
items: fetch_paginated(query, Map.drop(params, ["total"]), :offset) items: fetch_paginated(query, Map.drop(params, ["total"]), :offset, table_binding)
} }
end end
def fetch_paginated(query, params, :offset) do def fetch_paginated(query, params, :offset, table_binding) do
options = cast_params(params) options = cast_params(params)
query query
|> paginate(options, :offset) |> paginate(options, :offset, table_binding)
|> Repo.all() |> Repo.all()
end end
def paginate(query, options, method \\ :keyset) def paginate(query, options, method \\ :keyset, table_binding \\ nil)
def paginate(query, options, :keyset) do def paginate(query, options, :keyset, table_binding) do
query query
|> restrict(:min_id, options) |> restrict(:min_id, options, table_binding)
|> restrict(:since_id, options) |> restrict(:since_id, options, table_binding)
|> restrict(:max_id, options) |> restrict(:max_id, options, table_binding)
|> restrict(:order, options) |> restrict(:order, options, table_binding)
|> restrict(:limit, options) |> restrict(:limit, options, table_binding)
end end
def paginate(query, options, :offset) do def paginate(query, options, :offset, table_binding) do
query query
|> restrict(:order, options) |> restrict(:order, options, table_binding)
|> restrict(:offset, options) |> restrict(:offset, options, table_binding)
|> restrict(:limit, options) |> restrict(:limit, options, table_binding)
end end
defp cast_params(params) do defp cast_params(params) do
@ -91,38 +91,46 @@ defp cast_params(params) do
changeset.changes changeset.changes
end end
defp restrict(query, :min_id, %{min_id: min_id}) do defp restrict(query, :min_id, %{min_id: min_id}, table_binding) do
where(query, [q], q.id > ^min_id) where(query, [{q, table_position(query, table_binding)}], q.id > ^min_id)
end end
defp restrict(query, :since_id, %{since_id: since_id}) do defp restrict(query, :since_id, %{since_id: since_id}, table_binding) do
where(query, [q], q.id > ^since_id) where(query, [{q, table_position(query, table_binding)}], q.id > ^since_id)
end end
defp restrict(query, :max_id, %{max_id: max_id}) do defp restrict(query, :max_id, %{max_id: max_id}, table_binding) do
where(query, [q], q.id < ^max_id) where(query, [{q, table_position(query, table_binding)}], q.id < ^max_id)
end end
defp restrict(query, :order, %{min_id: _}) do defp restrict(query, :order, %{min_id: _}, table_binding) do
order_by(query, [u], fragment("? asc nulls last", u.id)) order_by(
query,
[{u, table_position(query, table_binding)}],
fragment("? asc nulls last", u.id)
)
end end
defp restrict(query, :order, _options) do defp restrict(query, :order, _options, table_binding) do
order_by(query, [u], fragment("? desc nulls last", u.id)) order_by(
query,
[{u, table_position(query, table_binding)}],
fragment("? desc nulls last", u.id)
)
end end
defp restrict(query, :offset, %{offset: offset}) do defp restrict(query, :offset, %{offset: offset}, _table_binding) do
offset(query, ^offset) offset(query, ^offset)
end end
defp restrict(query, :limit, options) do defp restrict(query, :limit, options, _table_binding) do
limit = Map.get(options, :limit, @default_limit) limit = Map.get(options, :limit, @default_limit)
query query
|> limit(^limit) |> limit(^limit)
end end
defp restrict(query, _, _), do: query defp restrict(query, _, _, _), do: query
defp enforce_order(result, %{min_id: _}) do defp enforce_order(result, %{min_id: _}) do
result result
@ -130,4 +138,10 @@ defp enforce_order(result, %{min_id: _}) do
end end
defp enforce_order(result, _), do: result defp enforce_order(result, _), do: result
defp table_position(%Ecto.Query{} = query, binding_name) do
Map.get(query.aliases, binding_name, 0)
end
defp table_position(_, _), do: 0
end end

View file

@ -1058,40 +1058,16 @@ def fetch_activities(recipients, opts \\ %{}, pagination \\ :keyset) do
@doc """ @doc """
Fetch favorites activities of user with order by sort adds to favorites 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()) @spec fetch_favourites(User.t(), map(), atom()) :: list(Activity.t())
def fetch_favourites(recipients, user, params \\ %{}, pagination \\ :keyset) do def fetch_favourites(user, params \\ %{}, pagination \\ :keyset) do
opts = user.ap_id
%{ |> Activity.Queries.by_actor()
"type" => "Create", |> Activity.Queries.by_type("Like")
"favorited_by" => user.ap_id, |> Activity.with_joined_object()
"blocking_user" => user |> Object.with_joined_activity()
} |> select([_like, object, activity], %{activity | object: object})
|> Map.merge(params) |> order_by([like, _, _], desc: like.updated_at)
|> Pagination.fetch_paginated(params, pagination, :object_activity)
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 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})

View file

@ -348,7 +348,6 @@ def context(%{assigns: %{user: user}} = conn, %{"id" => id}) do
def favourites(%{assigns: %{user: user}} = conn, params) do def favourites(%{assigns: %{user: user}} = conn, params) do
activities = activities =
ActivityPub.fetch_favourites( ActivityPub.fetch_favourites(
[],
user, user,
Map.take(params, Pleroma.Pagination.page_keys()) Map.take(params, Pleroma.Pagination.page_keys())
) )