add published_at field to activities

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2023-08-12 12:40:36 +02:00
parent 7da517d23a
commit 06e2225bf4
5 changed files with 52 additions and 32 deletions

View file

@ -32,6 +32,7 @@ defmodule Pleroma.Activity do
field(:actor, :string)
field(:recipients, {:array, :string}, default: [])
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
# id to order / paginate this field by

View file

@ -1421,30 +1421,14 @@ 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, %{skip_preload: false, order: :asc}, %{order_by_published_at: true}) do
defp maybe_order(query, %{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
)
)
|> order_by(asc: :published_at)
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
|> order_by(
[activity, object],
fragment(
"case when ?->>'type' = 'Create' then (?->>'published')::timestamptz else ? end desc",
activity.data,
object.data,
activity.inserted_at
)
)
|> order_by(desc: :published_at)
end
defp maybe_order(query, %{order: :desc}, _config) do

View file

@ -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
use Ecto.Migration

View file

@ -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

View file

@ -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