Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude posts in own subs from spam detection #1946

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
47 changes: 47 additions & 0 deletions prisma/migrations/20250305175301/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- exclude posts in own subs from spam detection
CREATE OR REPLACE FUNCTION item_spam(parent_id INTEGER, user_id INTEGER, within INTERVAL)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
repeats INTEGER;
self_replies INTEGER;
BEGIN
-- no fee escalation
IF within = interval '0' THEN
RETURN 0;
END IF;

SELECT count(*) INTO repeats
FROM "Item"
JOIN "Sub" ON "Sub"."name" = "Item"."subName"
WHERE (
(parent_id IS NULL AND "parentId" IS NULL)
OR
("parentId" = parent_id AND user_id <> (SELECT i."userId" FROM "Item" i WHERE i.id = "Item"."rootId"))
)
AND "Item"."userId" = user_id
AND "bio" = 'f'
AND "Sub"."userId" <> user_id
AND "Item".created_at > now_utc() - within;

IF parent_id IS NULL THEN
RETURN repeats;
END IF;

WITH RECURSIVE base AS (
SELECT "Item".id, "Item"."parentId", "Item"."userId"
FROM "Item"
WHERE id = parent_id
AND "userId" = user_id
AND created_at > now_utc() - within
AND user_id <> (SELECT i."userId" FROM "Item" i WHERE i.id = "Item"."rootId")
UNION ALL
SELECT "Item".id, "Item"."parentId", "Item"."userId"
FROM base p
JOIN "Item" ON "Item".id = p."parentId" AND "Item"."userId" = p."userId" AND "Item".created_at > now_utc() - within)
SELECT count(*) INTO self_replies FROM base;

RETURN repeats + self_replies;
END;
$$;