add published_at field to activities
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
7da517d23a
commit
06e2225bf4
5 changed files with 52 additions and 32 deletions
|
@ -32,6 +32,7 @@ defmodule Pleroma.Activity do
|
||||||
field(:actor, :string)
|
field(:actor, :string)
|
||||||
field(:recipients, {:array, :string}, default: [])
|
field(:recipients, {:array, :string}, default: [])
|
||||||
field(:thread_muted?, :boolean, virtual: true)
|
field(:thread_muted?, :boolean, virtual: true)
|
||||||
|
field(:published_at, :naive_datetime)
|
||||||
|
|
||||||
# A field that can be used if you need to join some kind of other
|
# A field that can be used if you need to join some kind of other
|
||||||
# id to order / paginate this field by
|
# id to order / paginate this field by
|
||||||
|
|
|
@ -1421,30 +1421,14 @@ defp maybe_set_thread_muted_field(query, opts) do
|
||||||
|> Activity.with_set_thread_muted_field(opts[:muting_user] || opts[:user])
|
|> Activity.with_set_thread_muted_field(opts[:muting_user] || opts[:user])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_order(query, %{skip_preload: false, order: :asc}, %{order_by_published_at: true}) do
|
defp maybe_order(query, %{order: :asc}, %{order_by_published_at: true}) do
|
||||||
query
|
query
|
||||||
|> order_by(
|
|> order_by(asc: :published_at)
|
||||||
[activity, object],
|
|
||||||
fragment(
|
|
||||||
"case when ?->>'type' = 'Create' then (?->>'published')::timestamptz else ? end asc",
|
|
||||||
activity.data,
|
|
||||||
object.data,
|
|
||||||
activity.inserted_at
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_order(query, %{skip_preload: false}, %{order_by_published_at: true}) do
|
defp maybe_order(query, _opts, %{order_by_published_at: true}) do
|
||||||
query
|
query
|
||||||
|> order_by(
|
|> order_by(desc: :published_at)
|
||||||
[activity, object],
|
|
||||||
fragment(
|
|
||||||
"case when ?->>'type' = 'Create' then (?->>'published')::timestamptz else ? end desc",
|
|
||||||
activity.data,
|
|
||||||
object.data,
|
|
||||||
activity.inserted_at
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp maybe_order(query, %{order: :desc}, _config) do
|
defp maybe_order(query, %{order: :desc}, _config) do
|
||||||
|
|
|
@ -1,3 +1,7 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.Repo.Migrations.AddPreferredNameToHashtags do
|
defmodule Pleroma.Repo.Migrations.AddPreferredNameToHashtags do
|
||||||
use Ecto.Migration
|
use Ecto.Migration
|
||||||
|
|
||||||
|
|
|
@ -1,12 +0,0 @@
|
||||||
defmodule Pleroma.Repo.Migrations.AddPublishedIndexToActivities do
|
|
||||||
use Ecto.Migration
|
|
||||||
|
|
||||||
def change do
|
|
||||||
create_if_not_exists(
|
|
||||||
index(:activities, ["case when data->>'type' = 'Create' then (data->'object'->>'published')::timestamptz else inserted_at end"],
|
|
||||||
name: :activities_published,
|
|
||||||
concurrently: true
|
|
||||||
)
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Repo.Migrations.AddPublishedAtToActivities do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def up do
|
||||||
|
alter table(:activities) do
|
||||||
|
add_if_not_exists(:published_at, :naive_datetime)
|
||||||
|
end
|
||||||
|
|
||||||
|
create_if_not_exists(index(:activities, [:published_at]))
|
||||||
|
|
||||||
|
execute("WITH dates as (
|
||||||
|
select activities.id as id,
|
||||||
|
(
|
||||||
|
CASE
|
||||||
|
WHEN activities.data->>'type' = 'Create' THEN (objects.data->>'published')::timestamptz
|
||||||
|
ELSE activities.inserted_at
|
||||||
|
end
|
||||||
|
) as published_at
|
||||||
|
FROM activities
|
||||||
|
INNER JOIN objects ON COALESCE(
|
||||||
|
activities.data->'object'->>'id',
|
||||||
|
activities.data->>'object'
|
||||||
|
) = objects.data->>'id'
|
||||||
|
WHERE activities.data->>'type' IN ('Create', 'Announce')
|
||||||
|
)
|
||||||
|
UPDATE activities
|
||||||
|
SET published_at = dates.published_at
|
||||||
|
FROM dates
|
||||||
|
WHERE activities.id = dates.id")
|
||||||
|
end
|
||||||
|
|
||||||
|
def down do
|
||||||
|
alter table(:activities) do
|
||||||
|
remove_if_exists(:published_at, :naive_datetime)
|
||||||
|
end
|
||||||
|
|
||||||
|
drop_if_exists(index(:activities, [:published_at]))
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue