From 9ff0723ea91fc207fd8984412c287a111ed60d2c Mon Sep 17 00:00:00 2001
From: Dan Notestein <dan@syncad.com>
Date: Tue, 24 Dec 2024 20:20:27 -0500
Subject: [PATCH] add new get_followers

---
 hive/db/schema.py                             |  1 +
 .../new_condenser_api_get_followers.sql       | 73 +++++++++++++++++++
 2 files changed, 74 insertions(+)
 create mode 100644 hive/db/sql_scripts/postgrest/condenser_api/new_condenser_api_get_followers.sql

diff --git a/hive/db/schema.py b/hive/db/schema.py
index 393212a75..3ca25ea96 100644
--- a/hive/db/schema.py
+++ b/hive/db/schema.py
@@ -706,6 +706,7 @@ def setup_runtime_code(db):
         "postgrest/bridge_api/bridge_api_list_pop_communities.sql",
         "postgrest/condenser_api/extract_parameters_for_get_following_and_followers.sql",
         "postgrest/condenser_api/condenser_api_get_followers.sql",
+        "postgrest/condenser_api/new_condenser_api_get_followers.sql",
         "postgrest/condenser_api/condenser_api_get_following.sql",
         "postgrest/utilities/find_subscription_id.sql",
         "postgrest/bridge_api/bridge_api_get_profiles.sql",
diff --git a/hive/db/sql_scripts/postgrest/condenser_api/new_condenser_api_get_followers.sql b/hive/db/sql_scripts/postgrest/condenser_api/new_condenser_api_get_followers.sql
new file mode 100644
index 000000000..8ab7592f5
--- /dev/null
+++ b/hive/db/sql_scripts/postgrest/condenser_api/new_condenser_api_get_followers.sql
@@ -0,0 +1,73 @@
+DROP FUNCTION IF EXISTS hivemind_endpoints.new_condenser_api_get_followers;
+CREATE FUNCTION hivemind_endpoints.new_condenser_api_get_followers(IN _params JSONB)
+RETURNS JSONB
+LANGUAGE 'plpgsql'
+STABLE
+AS
+$$
+DECLARE
+  _account TEXT;
+  _account_id INT;
+  _start TEXT;
+  _start_id INT;
+  _follow_type TEXT;
+  _limit INT;
+  _result JSONB;
+BEGIN
+  _params := hivemind_postgrest_utilities.validate_json_arguments( _params, '{"account": "string", "start" : "string", "type" : "string", "limit" : "number"}',  4,   NULL  );
+
+  _account := params->'account';
+  _account_id := hivemind_postgrest_utilities.find_account_id( _account, TRUE );
+  if (_account_id = 0) then
+    raise_parameter_validation_exception('Invalid account');
+  end if;
+
+  _start := params->'start';
+  _start_id := hivemind_postgrest_utilities.find_account_id( _start, TRUE );
+  if (_start_id = 0) then
+    raise_parameter_validation_exception('Invalid start account');
+  end if;
+
+  _follow_type := hivemind_postgrest_utilities.parse_argument_from_json(_params, 'type', FALSE);
+  _limit = (_params->'limit')::INT;
+
+  IF _follow_type = 'blog' THEN
+    _result := (
+      SELECT jsonb_agg(
+        jsonb_build_object(
+          'following', _account,
+          'follower', ha.name,
+          'what', '[blog]',
+        )
+      )
+      FROM {SCHEMA_NAME}.follows f
+      JOIN {SCHEMA_NAME}.hive_accounts ha ON ha.id = f.follower
+      WHERE f.following = _account_id AND ha.id < _start_id
+      ORDER BY f.follower DESC
+      LIMIT _limit
+    );
+ELSIF _follow_type = 'ignore' THEN
+    _result := (
+      SELECT jsonb_agg(
+        jsonb_build_object(
+          'following', _account,
+          'follower', ha.name,
+          'what', _f'[ignore]',
+        )
+      )
+      FROM {SCHEMA_NAME}.muted m
+      JOIN {SCHEMA_NAME}.hive_accounts ha ON ha.id = m.follower
+      WHERE m.following = _account_id AND ha.id < _start_id
+      ORDER BY f.follower DESC
+      LIMIT _limit
+    );
+  ELSE
+    RAISE EXCEPTION '%', hivemind_postgrest_utilities.raise_parameter_validation_exception(
+      'Unsupported follow_type, valid values: blog, ignore'
+    );
+  END IF;
+
+  RETURN COALESCE(_result, '[]'::jsonb);
+END
+$$
+;
-- 
GitLab