logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20190109152453_add_visibility_function.exs (1510B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Repo.Migrations.AddVisibilityFunction do
  5. use Ecto.Migration
  6. @disable_ddl_transaction true
  7. def up do
  8. definition = """
  9. create or replace function activity_visibility(actor varchar, recipients varchar[], data jsonb) returns varchar as $$
  10. DECLARE
  11. fa varchar;
  12. public varchar := 'https://www.w3.org/ns/activitystreams#Public';
  13. BEGIN
  14. SELECT COALESCE(users.follower_address, '') into fa from users where users.ap_id = actor;
  15. IF data->'to' ? public THEN
  16. RETURN 'public';
  17. ELSIF data->'cc' ? public THEN
  18. RETURN 'unlisted';
  19. ELSIF ARRAY[fa] && recipients THEN
  20. RETURN 'private';
  21. ELSIF not(ARRAY[fa, public] && recipients) THEN
  22. RETURN 'direct';
  23. ELSE
  24. RETURN 'unknown';
  25. END IF;
  26. END;
  27. $$ LANGUAGE plpgsql IMMUTABLE;
  28. """
  29. execute(definition)
  30. create(
  31. index(:activities, ["activity_visibility(actor, recipients, data)"],
  32. name: :activities_visibility_index,
  33. concurrently: true
  34. )
  35. )
  36. end
  37. def down do
  38. drop_if_exists(
  39. index(:activities, ["activity_visibility(actor, recipients, data)"],
  40. name: :activities_visibility_index
  41. )
  42. )
  43. execute(
  44. "drop function if exists activity_visibility(actor varchar, recipients varchar[], data jsonb)"
  45. )
  46. end
  47. end