|
| 1 | +begin; |
| 2 | +set local lock_timeout = '10s'; |
| 3 | +create or replace function public.stack127_touch_directory_records() |
| 4 | +returns trigger language plpgsql security invoker set search_path = pg_catalog as $$ |
| 5 | +declare |
| 6 | + changed jsonb[]; |
| 7 | + value jsonb; |
| 8 | + owner_slug text; |
| 9 | + section_id text; |
| 10 | + slugs text[] := '{}'; |
| 11 | + entities text[] := '{}'; |
| 12 | + entity_slugs text[] := '{}'; |
| 13 | + idx integer; |
| 14 | +begin |
| 15 | + if tg_op = 'INSERT' then |
| 16 | + execute format('select array_agg(value) from (select public.stack127_directory_record_value(%L,to_jsonb(r)) value from new_rows r) records where value is not null',tg_table_name) into changed; |
| 17 | + elsif tg_op = 'DELETE' then |
| 18 | + execute format('select array_agg(value) from (select public.stack127_directory_record_value(%L,to_jsonb(r)) value from old_rows r) records where value is not null',tg_table_name) into changed; |
| 19 | + else |
| 20 | + execute format('with before as (select public.stack127_directory_record_value(%L,to_jsonb(r)) value from old_rows r), |
| 21 | + after as (select public.stack127_directory_record_value(%L,to_jsonb(r)) value from new_rows r), |
| 22 | + changes as ((select value from before except select value from after) union (select value from after except select value from before)) |
| 23 | + select array_agg(value) from changes where value is not null',tg_table_name,tg_table_name) into changed; |
| 24 | + end if; |
| 25 | + if changed is null then return null; end if; |
| 26 | + foreach value in array changed loop |
| 27 | + case tg_table_name |
| 28 | + when 'platform_fee_sections','platform_fee_entries' then |
| 29 | + section_id := case when tg_table_name='platform_fee_sections' then value->>'id' else value->>'section_id' end; |
| 30 | + if tg_table_name='platform_fee_sections' then |
| 31 | + owner_slug := public.stack127_platform_fee_group_slug(value->>'group_name'); |
| 32 | + else |
| 33 | + select public.stack127_platform_fee_group_slug(group_name) into owner_slug from public.platform_fee_sections where id=section_id; |
| 34 | + end if; |
| 35 | + slugs := slugs || array['platform-fees','platform-fees/' || owner_slug]; |
| 36 | + entities := array_append(entities,'platform_fees'); entity_slugs := array_append(entity_slugs,section_id); |
| 37 | + when 'ad_networks','ad_network_publisher_relationships','ad_network_publishers' then |
| 38 | + if tg_table_name <> 'ad_network_publishers' or exists ( |
| 39 | + select 1 from public.ad_network_publisher_relationships rel where rel.publisher_id=(value->>'id')::bigint and rel.status='confirmed' |
| 40 | + ) then slugs := array_append(slugs,'ad-networks'); end if; |
| 41 | + if tg_table_name='ad_networks' then |
| 42 | + owner_slug := value->>'slug'; |
| 43 | + slugs := array_append(slugs,'ad-networks/' || owner_slug); |
| 44 | + entities := array_append(entities,'ad_networks'); entity_slugs := array_append(entity_slugs,owner_slug); |
| 45 | + else |
| 46 | + for owner_slug in |
| 47 | + select network.slug from public.ad_networks network where |
| 48 | + (tg_table_name='ad_network_publisher_relationships' and network.id=(value->>'ad_network_id')::bigint) or |
| 49 | + (tg_table_name='ad_network_publishers' and exists(select 1 from public.ad_network_publisher_relationships rel |
| 50 | + where rel.ad_network_id=network.id and rel.publisher_id=(value->>'id')::bigint and rel.status='confirmed')) |
| 51 | + loop |
| 52 | + slugs := array_append(slugs,'ad-networks/' || owner_slug); |
| 53 | + entities := array_append(entities,'ad_networks'); entity_slugs := array_append(entity_slugs,owner_slug); |
| 54 | + end loop; |
| 55 | + end if; |
| 56 | + when 'launch_directories' then |
| 57 | + slugs := array_append(slugs,'launch-directories'); |
| 58 | + entities := array_append(entities,'launch_directories'); entity_slugs := array_append(entity_slugs,'launch-directories'); |
| 59 | + when 'terminal_apps' then |
| 60 | + slugs := array_append(slugs,'cli-apps'); |
| 61 | + entities := array_append(entities,'cli_apps'); entity_slugs := array_append(entity_slugs,'cli-apps'); |
| 62 | + when 'github_projects' then |
| 63 | + if exists(select 1 from public.self_host_apps app where app.github_project_id=(value->>'id')::uuid and app.published and app.self_hosting_status='self_hostable') then |
| 64 | + slugs := array_append(slugs,'self-host-apps'); |
| 65 | + slugs := slugs || coalesce((select array_agg('self-host-apps/' || app.slug) from public.self_host_apps app |
| 66 | + where app.published and app.self_hosting_status='self_hostable' and ( |
| 67 | + (tg_table_name='github_projects' and app.github_project_id=(value->>'id')::uuid) or |
| 68 | + (tg_table_name='self_host_app_categories' and exists(select 1 from public.self_host_app_category_links link |
| 69 | + where link.app_id=app.id and link.category_id=(value->>'id')::uuid)) |
| 70 | + )), '{}'::text[]); |
| 71 | + entities := array_append(entities,'self_host_catalog'); entity_slugs := array_append(entity_slugs,'all'); |
| 72 | + end if; |
| 73 | + if exists(select 1 from public.terminal_apps app where app.github_project_id=(value->>'id')::uuid and app.published) then |
| 74 | + slugs := array_append(slugs,'cli-apps'); |
| 75 | + entities := array_append(entities,'cli_apps'); entity_slugs := array_append(entity_slugs,'cli-apps'); |
| 76 | + end if; |
| 77 | + when 'self_host_app_categories' then |
| 78 | + if exists(select 1 from public.self_host_app_category_links link join public.self_host_apps app on app.id=link.app_id |
| 79 | + where link.category_id=(value->>'id')::uuid and app.published and app.self_hosting_status='self_hostable') then |
| 80 | + slugs := array_append(slugs,'self-host-apps'); |
| 81 | + slugs := slugs || coalesce((select array_agg('self-host-apps/' || app.slug) from public.self_host_apps app |
| 82 | + where app.published and app.self_hosting_status='self_hostable' and ( |
| 83 | + (tg_table_name='github_projects' and app.github_project_id=(value->>'id')::uuid) or |
| 84 | + (tg_table_name='self_host_app_categories' and exists(select 1 from public.self_host_app_category_links link |
| 85 | + where link.app_id=app.id and link.category_id=(value->>'id')::uuid)) |
| 86 | + )), '{}'::text[]); |
| 87 | + entities := array_append(entities,'self_host_catalog'); entity_slugs := array_append(entity_slugs,'all'); |
| 88 | + end if; |
| 89 | + else |
| 90 | + if tg_table_name='self_host_apps' then owner_slug := value->>'slug'; |
| 91 | + else |
| 92 | + select app.slug into owner_slug from public.self_host_apps app where app.id=(value->>'app_id')::uuid |
| 93 | + and app.published and app.self_hosting_status='self_hostable'; |
| 94 | + end if; |
| 95 | + if owner_slug is not null then |
| 96 | + slugs := slugs || array['self-host-apps','self-host-apps/' || owner_slug]; |
| 97 | + entities := array_append(entities,'self_host_app'); entity_slugs := array_append(entity_slugs,owner_slug); |
| 98 | + end if; |
| 99 | + end case; |
| 100 | + end loop; |
| 101 | + for owner_slug in select distinct unnest(slugs) order by 1 loop |
| 102 | + perform public.stack127_touch_page(owner_slug,tg_table_name); |
| 103 | + end loop; |
| 104 | + for idx in select distinct on (entities[n],entity_slugs[n]) n from generate_subscripts(entities,1) n loop |
| 105 | + perform public.stack127_enqueue_revalidation(entities[idx],entity_slugs[idx],tg_table_name); |
| 106 | + end loop; |
| 107 | + return null; |
| 108 | +end; |
| 109 | +$$; |
| 110 | + |
| 111 | +-- The license directory combines code facts with counts from published apps. |
| 112 | +create function public.stack127_touch_directory_license_counts() |
| 113 | +returns trigger language plpgsql security invoker set search_path = pg_catalog as $$ |
| 114 | +declare before_counts jsonb := '[]'; after_counts jsonb := '[]'; |
| 115 | +begin |
| 116 | + if tg_op <> 'INSERT' then |
| 117 | + select coalesce(jsonb_agg(to_jsonb(counts) order by license,license_spdx_id),'[]') into before_counts |
| 118 | + from (select license,license_spdx_id,count(*) count from old_rows |
| 119 | + where published and self_hosting_status='self_hostable' group by license,license_spdx_id) counts; |
| 120 | + end if; |
| 121 | + if tg_op <> 'DELETE' then |
| 122 | + select coalesce(jsonb_agg(to_jsonb(counts) order by license,license_spdx_id),'[]') into after_counts |
| 123 | + from (select license,license_spdx_id,count(*) count from new_rows |
| 124 | + where published and self_hosting_status='self_hostable' group by license,license_spdx_id) counts; |
| 125 | + end if; |
| 126 | + if before_counts is distinct from after_counts then |
| 127 | + perform public.stack127_touch_page('software-licenses-for-self-hosting','license_app_counts'); |
| 128 | + end if; |
| 129 | + return null; |
| 130 | +end; |
| 131 | +$$; |
| 132 | +revoke all on function public.stack127_touch_directory_license_counts() from public, anon, authenticated; |
| 133 | +grant execute on function public.stack127_touch_directory_license_counts() to service_role; |
| 134 | +create trigger stack127_touch_license_counts_insert after insert on public.self_host_apps |
| 135 | +referencing new table as new_rows for each statement execute function public.stack127_touch_directory_license_counts(); |
| 136 | +create trigger stack127_touch_license_counts_update after update on public.self_host_apps |
| 137 | +referencing old table as old_rows new table as new_rows for each statement execute function public.stack127_touch_directory_license_counts(); |
| 138 | +create trigger stack127_touch_license_counts_delete after delete on public.self_host_apps |
| 139 | +referencing old table as old_rows for each statement execute function public.stack127_touch_directory_license_counts(); |
| 140 | +commit; |
0 commit comments