akkoma/priv/repo/migrations/20190109152453_add_visibility_function.exs

51 lines
1.3 KiB
Elixir
Raw Normal View History

2019-01-09 15:45:09 +00:00
defmodule Pleroma.Repo.Migrations.AddVisibilityFunction do
use Ecto.Migration
2019-01-09 15:55:05 +00:00
@disable_ddl_transaction true
2019-01-09 15:45:09 +00:00
def up do
definition = """
create or replace function activity_visibility(actor varchar, recipients varchar[], data jsonb) returns varchar as $$
DECLARE
fa varchar;
public varchar := 'https://www.w3.org/ns/activitystreams#Public';
BEGIN
SELECT COALESCE(users.follower_address, '') into fa from users where users.ap_id = actor;
IF data->'to' ? public THEN
RETURN 'public';
ELSIF data->'cc' ? public THEN
RETURN 'unlisted';
ELSIF ARRAY[fa] && recipients THEN
RETURN 'private';
ELSIF not(ARRAY[fa, public] && recipients) THEN
RETURN 'direct';
ELSE
RETURN 'unknown';
END IF;
END;
$$ LANGUAGE plpgsql IMMUTABLE;
"""
execute(definition)
create(
index(:activities, ["activity_visibility(actor, recipients, data)"],
2019-01-09 15:55:05 +00:00
name: :activities_visibility_index,
concurrently: true
2019-01-09 15:45:09 +00:00
)
)
end
def down do
2019-07-01 01:08:07 +00:00
drop_if_exists(
2019-01-09 15:45:09 +00:00
index(:activities, ["activity_visibility(actor, recipients, data)"],
name: :activities_visibility_index
)
)
2019-10-08 12:16:39 +00:00
execute(
"drop function if exists activity_visibility(actor varchar, recipients varchar[], data jsonb)"
)
2019-01-09 15:45:09 +00:00
end
end