diff --git a/api/paidAction/itemCreate.js b/api/paidAction/itemCreate.js index d6fb603f7..f36ea60b6 100644 --- a/api/paidAction/itemCreate.js +++ b/api/paidAction/itemCreate.js @@ -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[])) diff --git a/api/resolvers/item.js b/api/resolvers/item.js index b1a53e544..f4527104f 100644 --- a/api/resolvers/item.js +++ b/api/resolvers/item.js @@ -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 }, diff --git a/api/typeDefs/item.js b/api/typeDefs/item.js index 730f61830..34a6dbb0e 100644 --- a/api/typeDefs/item.js +++ b/api/typeDefs/item.js @@ -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 { diff --git a/components/fee-button.js b/components/fee-button.js index d490c4499..98b56c7b9 100644 --- a/components/fee-button.js +++ b/components/fee-button.js @@ -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 }` + )) return function useRemoteLineItems () { const [line, setLine] = useState({}) diff --git a/components/post.js b/components/post.js index 6245d1a16..7be73d8fb 100644 --- a/components/post.js +++ b/components/post.js @@ -150,7 +150,7 @@ export function PostForm ({ type, sub, children }) { return ( {children} diff --git a/components/reply.js b/components/reply.js index ec12d0a5f..b45996a72 100644 --- a/components/reply.js +++ b/components/reply.js @@ -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}") }` }, data => { return { itemRepetition: (data?.itemRepetition || 0) + 1 @@ -162,7 +162,7 @@ export default forwardRef(function Reply ({
(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; +$$;