summaryrefslogtreecommitdiff
path: root/priv/repo/migrations/20190122153157_update_activity_visibility.exs
blob: 2bddbe9bd5fd46321e183fcbeef7267ac19be6a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Repo.Migrations.UpdateActivityVisibility do
  use Ecto.Migration
  @disable_ddl_transaction true

  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 PARALLEL SAFE SECURITY DEFINER;
    """

    execute(definition)
  end

  def down do
  end
end