Skip to content

Commit 6d05682

Browse files
committedMar 5, 2025
issue stackernews#787: exclude posts in own subs from spam detection
1 parent 75d0a8e commit 6d05682

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- exclude posts in own subs from spam detection
2+
CREATE OR REPLACE FUNCTION item_spam(parent_id INTEGER, user_id INTEGER, within INTERVAL)
3+
RETURNS INTEGER
4+
LANGUAGE plpgsql
5+
AS $$
6+
DECLARE
7+
repeats INTEGER;
8+
self_replies INTEGER;
9+
BEGIN
10+
-- no fee escalation
11+
IF within = interval '0' THEN
12+
RETURN 0;
13+
END IF;
14+
15+
SELECT count(*) INTO repeats
16+
FROM "Item"
17+
JOIN "Sub" ON "Sub"."name" = "Item"."subName"
18+
WHERE (
19+
(parent_id IS NULL AND "parentId" IS NULL)
20+
OR
21+
("parentId" = parent_id AND user_id <> (SELECT i."userId" FROM "Item" i WHERE i.id = "Item"."rootId"))
22+
)
23+
AND "Item"."userId" = user_id
24+
AND "bio" = 'f'
25+
AND "Sub"."userId" <> user_id
26+
AND "Item".created_at > now_utc() - within;
27+
28+
IF parent_id IS NULL THEN
29+
RETURN repeats;
30+
END IF;
31+
32+
WITH RECURSIVE base AS (
33+
SELECT "Item".id, "Item"."parentId", "Item"."userId"
34+
FROM "Item"
35+
WHERE id = parent_id
36+
AND "userId" = user_id
37+
AND created_at > now_utc() - within
38+
AND user_id <> (SELECT i."userId" FROM "Item" i WHERE i.id = "Item"."rootId")
39+
UNION ALL
40+
SELECT "Item".id, "Item"."parentId", "Item"."userId"
41+
FROM base p
42+
JOIN "Item" ON "Item".id = p."parentId" AND "Item"."userId" = p."userId" AND "Item".created_at > now_utc() - within)
43+
SELECT count(*) INTO self_replies FROM base;
44+
45+
RETURN repeats + self_replies;
46+
END;
47+
$$;

0 commit comments

Comments
 (0)
Please sign in to comment.