From 4706f69dbb800f21e66a9d78f31e972be13b1b33 Mon Sep 17 00:00:00 2001
From: asuch <asuch@syncad.com>
Date: Wed, 8 Jan 2025 13:35:33 +0100
Subject: [PATCH] In postgrest methods, use functions from
 hivemind_postgrest_utilities schema

---
 .../bridge_api_account_notifications.sql      |   2 +-
 ..._api_get_relationship_between_accounts.sql | 117 ++++++++++++------
 .../bridge_api_list_pop_communities.sql       |  29 ++++-
 .../bridge_api_post_notifications.sql         |   2 +-
 .../postgrest/utilities/notifications.sql     |  16 ++-
 5 files changed, 118 insertions(+), 48 deletions(-)

diff --git a/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_account_notifications.sql b/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_account_notifications.sql
index 808cf33d6..9215bba80 100644
--- a/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_account_notifications.sql
+++ b/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_account_notifications.sql
@@ -92,7 +92,7 @@ BEGIN
           SELECT
             CASE
                 WHEN hnv.type_id != 16 THEN 0 --evrything else than mentions (only optimization)
-                ELSE hivemind_app.get_number_of_mentions_in_post( hnv.post_id )
+                ELSE hivemind_postgrest_utilities.get_number_of_mentions_in_post( hnv.post_id )
             END as mentions
         ) as hm
         LIMIT _limit
diff --git a/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_get_relationship_between_accounts.sql b/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_get_relationship_between_accounts.sql
index 727f8a4db..b61e28243 100644
--- a/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_get_relationship_between_accounts.sql
+++ b/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_get_relationship_between_accounts.sql
@@ -6,57 +6,92 @@ STABLE
 AS
 $$
 DECLARE
-  _account1 TEXT;
-  _account2 TEXT;
-  _observer TEXT;
+  _account1_id INT;
+  _account2_id INT;
+  _observer_id INT;
   _debug BOOLEAN;
-  _result JSONB;
-  _state INT;
-  _blacklisted BOOLEAN;
-  _follow_blacklists BOOLEAN;
-  _follow_muted BOOLEAN;
-  _created_at TIMESTAMP;
-  _block_num INT;
 BEGIN
   _params = hivemind_postgrest_utilities.validate_json_arguments(_params, '{"account1": "string", "account2": "string", "observer": "string", "debug": "boolean"}', 4, NULL);
 
-  _account1 = hivemind_postgrest_utilities.parse_argument_from_json(_params, 'account1', True);
-  _account1 = hivemind_postgrest_utilities.valid_account(_account1);
+  _account1_id = 
+    hivemind_postgrest_utilities.find_account_id(
+      hivemind_postgrest_utilities.valid_account(
+        hivemind_postgrest_utilities.parse_argument_from_json(_params, 'account1', True),
+        False),
+    True);
 
-  _account2 = hivemind_postgrest_utilities.parse_argument_from_json(_params, 'account2', True);
-  _account2 = hivemind_postgrest_utilities.valid_account(_account2);
-
-  _observer = hivemind_postgrest_utilities.parse_argument_from_json(_params, 'observer', False);
-  PERFORM hivemind_postgrest_utilities.valid_account(_observer, True);
+  _account2_id = 
+    hivemind_postgrest_utilities.find_account_id(
+      hivemind_postgrest_utilities.valid_account(
+        hivemind_postgrest_utilities.parse_argument_from_json(_params, 'account2', True),
+        False),
+    True);
+  
+  _observer_id = hivemind_postgrest_utilities.find_account_id(
+    hivemind_postgrest_utilities.valid_account(
+      hivemind_postgrest_utilities.parse_argument_from_json(_params, 'observer', False),
+      True),
+    True);
 
   _debug = hivemind_postgrest_utilities.parse_argument_from_json(_params, 'debug', False);
 
-  SELECT state,
-         COALESCE(blacklisted, FALSE),
-         COALESCE(follow_blacklists, FALSE),
-         COALESCE(follow_muted, FALSE),
-         created_at,
-         block_num
-  INTO _state, _blacklisted, _follow_blacklists, _follow_muted, _created_at, _block_num
-  FROM hivemind_app.bridge_get_relationship_between_accounts(_account1, _account2)
-  LIMIT 1;
-
-  _result := jsonb_build_object(
-      'follows', CASE WHEN _state = 1 THEN TRUE ELSE FALSE END,
-      'ignores', CASE WHEN _state = 2 THEN TRUE ELSE FALSE END,
-      'blacklists', _blacklisted,
-      'follows_blacklists', _follow_blacklists,
-      'follows_muted', _follow_muted
-  );
-
-  IF _debug IS NOT NULL AND _debug THEN
-      _result := _result || jsonb_build_object(
-          'created_at', COALESCE(to_char(_created_at, 'YYYY-MM-DD"T"HH24:MI:SS'), NULL),
-          'block_num', _block_num
-      );
+  IF _debug IS NULL THEN
+    _debug = False;
   END IF;
 
-  RETURN _result;
+  RETURN COALESCE(
+    ( SELECT 
+      CASE WHEN NOT _debug THEN 
+        jsonb_build_object( -- bridge_api_get_relationship_between_accounts
+          'follows', CASE WHEN row.state = 1 THEN TRUE ELSE FALSE END,
+          'ignores', CASE WHEN row.state = 2 THEN TRUE ELSE FALSE END,
+          'blacklists', row.blacklisted,
+          'follows_blacklists', row.follow_blacklists,
+          'follows_muted', row.follow_muted
+      ) ELSE
+        jsonb_build_object( -- bridge_api_get_relationship_between_accounts with debug
+          'follows', CASE WHEN row.state = 1 THEN TRUE ELSE FALSE END,
+          'ignores', CASE WHEN row.state = 2 THEN TRUE ELSE FALSE END,
+          'blacklists', row.blacklisted,
+          'follows_blacklists', row.follow_blacklists,
+          'follows_muted', row.follow_muted,
+          'created_at', COALESCE(to_char(row.created_at, 'YYYY-MM-DD"T"HH24:MI:SS'), NULL),
+          'block_num', row.block_num
+        )
+      END
+      FROM (
+      SELECT
+        hf.state,
+        COALESCE(hf.blacklisted, False) AS blacklisted,
+        COALESCE(hf.follow_blacklists, FALSE) AS follow_blacklists,
+        COALESCE(hf.follow_muted, FALSE) AS follow_muted,
+        hf.created_at,
+        hf.block_num
+      FROM
+        hivemind_app.hive_follows hf
+      WHERE
+        hf.follower = _account1_id AND hf.following = _account2_id
+      LIMIT 1
+    ) row ),
+      CASE WHEN NOT _debug THEN
+        jsonb_build_object( -- bridge_api_get_relationship_between_accounts null
+          'follows', FALSE,
+          'ignores', FALSE,
+          'blacklists', FALSE,
+          'follows_blacklists', FALSE,
+          'follows_muted', FALSE
+      ) ELSE
+        jsonb_build_object( -- bridge_api_get_relationship_between_accounts null with debug
+          'follows', FALSE,
+          'ignores', FALSE,
+          'blacklists', FALSE,
+          'follows_blacklists', FALSE,
+          'follows_muted', FALSE,
+          'created_at', NULL,
+          'block_num', NULL
+        )
+      END
+  );
 END
 $$
 ;
\ No newline at end of file
diff --git a/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_list_pop_communities.sql b/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_list_pop_communities.sql
index c23bae364..f431b51d4 100644
--- a/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_list_pop_communities.sql
+++ b/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_list_pop_communities.sql
@@ -7,17 +7,38 @@ AS
 $$
 DECLARE
     _limit INTEGER;
-    _response JSONB;
+    _head_block_time TIMESTAMP;
 BEGIN
     _params = hivemind_postgrest_utilities.validate_json_arguments(_params, '{"limit": "number"}', 0, NULL);
 
     _limit := hivemind_postgrest_utilities.parse_integer_argument_from_json(_params, 'limit', False);
     _limit := hivemind_postgrest_utilities.valid_number(_limit, 25, 1, 25, 'limit');
 
-    SELECT jsonb_agg(jsonb_build_array(name, title) ORDER BY newsubs DESC, id DESC) INTO _response
-    FROM hivemind_app.bridge_list_pop_communities(_limit);
+    _head_block_time := hivemind_app.head_block_time();
 
-    RETURN _response;
+    RETURN (
+      SELECT
+        jsonb_agg( jsonb_build_array(name, title) ORDER BY newsubs DESC, id DESC)
+      FROM
+      (
+        SELECT
+          hc.id,
+          hc.name,
+          hc.title,
+          stats.newsubs
+        FROM hivemind_app.hive_communities hc
+        JOIN (
+          SELECT
+            community_id,
+            COUNT(*) newsubs
+          FROM hivemind_app.hive_subscriptions
+          WHERE created_at > _head_block_time - INTERVAL '1 MONTH'
+          GROUP BY community_id
+        ) stats ON stats.community_id = hc.id
+        ORDER BY stats.newsubs DESC, hc.id DESC
+        LIMIT _limit
+      )
+    );
 END
 $$
 ;
\ No newline at end of file
diff --git a/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_post_notifications.sql b/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_post_notifications.sql
index 0bf460902..5642cf3c1 100644
--- a/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_post_notifications.sql
+++ b/hive/db/sql_scripts/postgrest/bridge_api/bridge_api_post_notifications.sql
@@ -84,7 +84,7 @@ BEGIN
           SELECT
             CASE
               WHEN hnv.type_id != 16 THEN 0 --evrything else than mentions (only optimization)
-              ELSE hivemind_app.get_number_of_mentions_in_post( hnv.post_id )
+              ELSE hivemind_postgrest_utilities.get_number_of_mentions_in_post( hnv.post_id )
             END as mentions
         ) as hm
         ORDER BY hnv.id DESC
diff --git a/hive/db/sql_scripts/postgrest/utilities/notifications.sql b/hive/db/sql_scripts/postgrest/utilities/notifications.sql
index 64a171492..cfa3e2434 100644
--- a/hive/db/sql_scripts/postgrest/utilities/notifications.sql
+++ b/hive/db/sql_scripts/postgrest/utilities/notifications.sql
@@ -100,4 +100,18 @@ BEGIN
     RETURN _msg;
 END;
 $$
-;
\ No newline at end of file
+;
+
+DROP FUNCTION IF EXISTS hivemind_postgrest_utilities.get_number_of_mentions_in_post;
+CREATE FUNCTION hivemind_postgrest_utilities.get_number_of_mentions_in_post( _post_id hivemind_app.hive_posts.id%TYPE )
+RETURNS INTEGER
+LANGUAGE 'plpgsql'
+STABLE
+AS
+$BODY$
+BEGIN
+  RETURN (
+    SELECT COUNT(*) FROM hivemind_app.hive_mentions hm WHERE hm.post_id = _post_id
+  );
+END
+$BODY$;
\ No newline at end of file
-- 
GitLab