Skip to content
Snippets Groups Projects

Draft: refactor follows and update_nofications indexing

Open Dan Notestein requested to merge 267-update-notification-cache into develop
Files
17
@@ -351,6 +351,71 @@ END
$function$
;
DROP FUNCTION IF EXISTS hivemind_app.process_hive_post_mentions;
CREATE OR REPLACE FUNCTION hivemind_app.process_hive_post_mentions(_post_id hivemind_app.hive_posts.id%TYPE)
RETURNS SETOF BIGINT
LANGUAGE plpgsql
AS
$function$
BEGIN
-- With clause is inlined, modified call to reptracker_endpoints.get_account_reputation.
-- Reputation is multiplied by 7.5 rather than 9 to bring the max value to 100 rather than 115.
-- In case of reputation being 0, the score is set to 25 rather than 0.
RETURN query
WITH log_account_rep AS
(
SELECT
account_id,
LOG(10, ABS(nullif(reputation, 0))) AS rep,
(CASE WHEN reputation < 0 THEN -1 ELSE 1 END) AS is_neg
FROM reptracker_app.account_reputations
),
calculate_rep AS
(
SELECT
account_id,
GREATEST(lar.rep - 9, 0) * lar.is_neg AS rep
FROM log_account_rep lar
),
final_rep AS
(
SELECT account_id, (cr.rep * 7.5 + 25)::INT AS rep FROM calculate_rep AS cr
),
mentions AS
(
SELECT DISTINCT _post_id AS post_id, T.author_id, ha.id AS account_id, T.block_num
FROM
hivemind_app.hive_accounts ha
INNER JOIN
(
SELECT LOWER( ( SELECT trim( T.mention::text, '{""}') ) ) AS mention, T.author_id, T.block_num
FROM
(
SELECT
hp.id, REGEXP_MATCHES( hpd.body, '(?:^|[^a-zA-Z0-9_!#$%&*@\\/])(?:@)([a-zA-Z0-9\\.-]{1,16}[a-zA-Z0-9])(?![a-z])', 'g') AS mention, hp.author_id, hp.block_num
FROM hivemind_app.hive_posts hp
INNER JOIN hivemind_app.hive_post_data hpd ON hp.id = hpd.id
WHERE hp.id = _post_id
) AS T
) AS T ON ha.name = T.mention
WHERE ha.id != T.author_id
ORDER BY T.block_num, ha.id
)
INSERT INTO hivemind_app.hive_notification_cache
(block_num, type_id, created_at, src, dst, dst_post_id, post_id, score, payload, community, community_title)
SELECT hm.block_num, 16, (SELECT hb.created_at FROM hivemind_app.blocks_view hb WHERE hb.num = (hm.block_num - 1)) AS created_at, hm.author_id, hm.account_id, hm.post_id, hm.post_id, COALESCE(rep.rep, 25), '', '', ''
FROM mentions AS hm
JOIN hivemind_app.hive_accounts AS a ON hm.account_id = a.id
LEFT JOIN final_rep AS rep ON a.haf_id = rep.account_id
WHERE hm.block_num > hivemind_app.block_before_irreversible( '90 days' )
AND COALESCE(rep.rep, 25) > 0
ORDER BY hm.block_num, created_at, hm.author_id, hm.account_id
ON CONFLICT (src, dst, post_id) WHERE type_id=16 DO UPDATE
SET block_num=EXCLUDED.block_num, created_at=EXCLUDED.created_at
RETURNING id;
END;
$function$;
DROP FUNCTION IF EXISTS hivemind_app.delete_hive_post(character varying,character varying,character varying, integer, timestamp)
;
CREATE OR REPLACE FUNCTION hivemind_app.delete_hive_post(
Loading