Allow sorting timelines by status publication date

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2023-08-09 19:57:08 +02:00
parent 4440a1974c
commit 82af8b1e92
2 changed files with 38 additions and 5 deletions

View file

@ -1113,6 +1113,11 @@
description: "Only display posts from own domain on local timeline"
}
]
},
%{
key: :order_by_published_at,
type: :boolean,
description: "Order statuses in timelines basing on their declared publication date, instead of insertion date."
}
]
},

View file

@ -1421,17 +1421,43 @@ defp maybe_set_thread_muted_field(query, opts) do
|> Activity.with_set_thread_muted_field(opts[:muting_user] || opts[:user])
end
defp maybe_order(query, %{order: :desc}) do
defp maybe_order(query, %{skip_preload: false, order: :asc}, %{order_by_published_at: true}) do
query
|> order_by(
[activity, object],
fragment(
"case when ?->>'type' = 'Create' then (?->>'published')::timestamptz else ? end asc",
activity.data,
object.data,
activity.inserted_at
)
)
end
defp maybe_order(query, %{skip_preload: false}, %{order_by_published_at: true}) do
query
|> order_by(
[activity, object],
fragment(
"case when ?->>'type' = 'Create' then (?->>'published')::timestamptz else ? end desc",
activity.data,
object.data,
activity.inserted_at
)
)
end
defp maybe_order(query, %{order: :desc}, _config) do
query
|> order_by(desc: :id)
end
defp maybe_order(query, %{order: :asc}) do
defp maybe_order(query, %{order: :asc}, _config) do
query
|> order_by(asc: :id)
end
defp maybe_order(query, _), do: query
defp maybe_order(query, _, _), do: query
defp normalize_fetch_activities_query_opts(opts) do
Enum.reduce([:tag, :tag_all, :tag_reject], opts, fn key, opts ->
@ -1451,6 +1477,7 @@ defp normalize_fetch_activities_query_opts(opts) do
opts
end
end)
|> Map.put(:skip_preload, Map.get(opts, :skip_preload, false))
end
defp fetch_activities_query_ap_ids_ops(opts) do
@ -1482,7 +1509,8 @@ def fetch_activities_query(recipients, opts \\ %{}) do
fetch_activities_query_ap_ids_ops(opts)
config = %{
skip_thread_containment: Config.get([:instance, :skip_thread_containment])
skip_thread_containment: Config.get([:instance, :skip_thread_containment]),
order_by_published_at: Config.get([:instance, :order_by_published_at])
}
query =
@ -1491,7 +1519,7 @@ def fetch_activities_query(recipients, opts \\ %{}) do
|> maybe_preload_bookmarks(opts)
|> maybe_preload_report_notes(opts)
|> maybe_set_thread_muted_field(opts)
|> maybe_order(opts)
|> maybe_order(opts, config)
|> restrict_recipients(recipients, opts[:user])
|> restrict_replies(opts)
|> restrict_since(opts)