Skip to content

Commit

Permalink
In postgrest methods, use functions from hivemind_postgrest_utilities…
Browse files Browse the repository at this point in the history
… schema
  • Loading branch information
asuch committed Jan 8, 2025
1 parent ef43a63 commit 4706f69
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
$$
;
Original file line number Diff line number Diff line change
Expand Up @@ -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
$$
;
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion hive/db/sql_scripts/postgrest/utilities/notifications.sql
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,18 @@ BEGIN
RETURN _msg;
END;
$$
;
;

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$;

0 comments on commit 4706f69

Please sign in to comment.