2022-02-25 23:11:42 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-01 04:11:47 -07:00
|
|
|
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
|
2022-02-25 23:11:42 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-03-03 05:35:32 -07:00
|
|
|
defmodule Pleroma.Repo.Migrations.UpdateUserNoteCounters do
|
|
|
|
use Ecto.Migration
|
|
|
|
|
|
|
|
@public "https://www.w3.org/ns/activitystreams#Public"
|
|
|
|
|
|
|
|
def up do
|
2019-10-08 06:16:39 -06:00
|
|
|
execute("""
|
2019-03-03 05:35:32 -07:00
|
|
|
WITH public_note_count AS (
|
|
|
|
SELECT
|
|
|
|
data->>'actor' AS actor,
|
|
|
|
count(id) AS count
|
|
|
|
FROM objects
|
|
|
|
WHERE data->>'type' = 'Note' AND (
|
|
|
|
data->'cc' ? '#{@public}' OR data->'to' ? '#{@public}'
|
|
|
|
)
|
|
|
|
GROUP BY data->>'actor'
|
|
|
|
)
|
|
|
|
UPDATE users AS u
|
|
|
|
SET "info" = jsonb_set(u.info, '{note_count}', o.count::varchar::jsonb, true)
|
|
|
|
FROM public_note_count AS o
|
|
|
|
WHERE u.ap_id = o.actor
|
2019-10-08 06:16:39 -06:00
|
|
|
""")
|
2019-03-03 05:35:32 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def down do
|
2019-10-08 06:16:39 -06:00
|
|
|
execute("""
|
2019-03-03 05:35:32 -07:00
|
|
|
WITH public_note_count AS (
|
|
|
|
SELECT
|
|
|
|
data->>'actor' AS actor,
|
|
|
|
count(id) AS count
|
|
|
|
FROM objects
|
|
|
|
WHERE data->>'type' = 'Note'
|
|
|
|
GROUP BY data->>'actor'
|
|
|
|
)
|
|
|
|
UPDATE users AS u
|
|
|
|
SET "info" = jsonb_set(u.info, '{note_count}', o.count::varchar::jsonb, true)
|
|
|
|
FROM public_note_count AS o
|
|
|
|
WHERE u.ap_id = o.actor
|
2019-10-08 06:16:39 -06:00
|
|
|
""")
|
2019-03-03 05:35:32 -07:00
|
|
|
end
|
|
|
|
end
|