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