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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/paidAction/itemCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function getCost ({ subName, parentId, uploadIds, boost = 0, bio },
const [{ cost }] = await models.$queryRaw`
SELECT ${baseCost}::INTEGER
* POWER(10, item_spam(${parseInt(parentId)}::INTEGER, ${me?.id ?? USER_ID.anon}::INTEGER,
${me?.id && !bio ? ITEM_SPAM_INTERVAL : ANON_ITEM_SPAM_INTERVAL}::INTERVAL))
${me?.id && !bio ? ITEM_SPAM_INTERVAL : ANON_ITEM_SPAM_INTERVAL}::INTERVAL, ${subName}::TEXT))
* ${me ? 1 : 100}::INTEGER
+ (SELECT "nUnpaid" * "uploadFeesMsats"
FROM upload_fees(${me?.id || USER_ID.anon}::INTEGER, ${uploadIds}::INTEGER[]))
Expand Down
6 changes: 3 additions & 3 deletions api/resolvers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ function typeClause (type) {

export default {
Query: {
itemRepetition: async (parent, { parentId }, { me, models }) => {
itemRepetition: async (parent, { parentId, sub }, { me, models }) => {
if (!me) return 0
// how many of the parents starting at parentId belong to me
const [{ item_spam: count }] = await models.$queryRawUnsafe(`SELECT item_spam($1::INTEGER, $2::INTEGER, '${ITEM_SPAM_INTERVAL}')`,
Number(parentId), Number(me.id))
const [{ item_spam: count }] = await models.$queryRawUnsafe(`SELECT item_spam($1::INTEGER, $2::INTEGER, '${ITEM_SPAM_INTERVAL}', $3::TEXT)`,
Number(parentId), Number(me.id), sub)

return count
},
Expand Down
2 changes: 1 addition & 1 deletion api/typeDefs/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default gql`
search(q: String, sub: String, cursor: String, what: String, sort: String, when: String, from: String, to: String): Items
auctionPosition(sub: String, id: ID, boost: Int): Int!
boostPosition(sub: String, id: ID, boost: Int): BoostPositions!
itemRepetition(parentId: ID): Int!
itemRepetition(parentId: ID, sub: String): Int!
}

type BoostPositions {
Expand Down
13 changes: 9 additions & 4 deletions components/fee-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ export function postCommentBaseLineItems ({ baseCost = 1, comment = false, me })
}
}

export function postCommentUseRemoteLineItems ({ parentId } = {}) {
const query = parentId
? gql`{ itemRepetition(parentId: "${parentId}") }`
: gql`{ itemRepetition }`
export function postCommentUseRemoteLineItems ({ sub, parentId } = {}) {
const query = parentId && sub
? gql`{ itemRepetition(parentId: "${parentId}", sub: "${sub}") }`
: (parentId
? gql`{ itemRepetition(parentId: "${parentId}") }`
: (sub
? gql`{ itemRepetition(sub: "${sub}") }`
: gql`{ itemRepetition }`
))
Comment on lines +41 to +48
Copy link
Member

@ekzyis ekzyis Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in the other comment, this should use GraphQL variables.


return function useRemoteLineItems () {
const [line, setLine] = useState({})
Expand Down
2 changes: 1 addition & 1 deletion components/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ export function PostForm ({ type, sub, children }) {
return (
<FeeButtonProvider
baseLineItems={sub ? postCommentBaseLineItems({ baseCost: sub.baseCost, me: !!me }) : undefined}
useRemoteLineItems={postCommentUseRemoteLineItems()}
useRemoteLineItems={postCommentUseRemoteLineItems({ sub: sub?.name })}
>
<FormType sub={sub}>{children}</FormType>
</FeeButtonProvider>
Expand Down
4 changes: 2 additions & 2 deletions components/reply.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default forwardRef(function Reply ({
// no lag for itemRepetition
if (!item.mine && me) {
cache.updateQuery({
query: gql`{ itemRepetition(parentId: "${parentId}") }`
query: gql`{ itemRepetition(parentId: "${parentId}", sub: "${sub?.name}") }`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this as-is, assuming that was a hint how to solve the other comment.

Copy link
Member

@ekzyis ekzyis Mar 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant that you have the same issue with searching for updating the query with a sub named 'undefined' here, too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}, data => {
return {
itemRepetition: (data?.itemRepetition || 0) + 1
Expand Down Expand Up @@ -162,7 +162,7 @@ export default forwardRef(function Reply ({
<div className={styles.reply}>
<FeeButtonProvider
baseLineItems={postCommentBaseLineItems({ baseCost: sub?.replyCost ?? 1, comment: true, me: !!me })}
useRemoteLineItems={postCommentUseRemoteLineItems({ parentId: item.id, me: !!me })}
useRemoteLineItems={postCommentUseRemoteLineItems({ parentId: item.id, sub: sub?.name, me: !!me })}
>
<Form
initial={{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
-- exclude posts in own subs from spam detection
CREATE OR REPLACE FUNCTION item_spam(parent_id INTEGER, user_id INTEGER, within INTERVAL, sub_name TEXT)
RETURNS INTEGER
LANGUAGE plpgsql
AS $$
DECLARE
repeats INTEGER;
self_replies INTEGER;
BEGIN
-- no fee escalation
IF within = interval '0' THEN
RETURN 0;
END IF;

IF sub_name IS NOT NULL AND user_id = (SELECT "Sub"."userId" FROM "Sub" WHERE "Sub"."name" = sub_name) THEN
RETURN 0;
END IF;

SELECT count(*) INTO repeats
FROM "Item"
LEFT 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"."name" IS NULL OR "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;
$$;