Skip to content

enhance: Allow switching to a more expensive territory #1919

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 21 additions & 7 deletions api/paidAction/itemUpdate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PAID_ACTION_PAYMENT_METHODS, USER_ID } from '@/lib/constants'
import { PAID_ACTION_PAYMENT_METHODS, USER_ID, ITEM_SPAM_INTERVAL, ANON_ITEM_SPAM_INTERVAL } from '@/lib/constants'
import { uploadFees } from '../resolvers/upload'
import { getItemMentions, getMentions, performBotBehavior } from './lib/item'
import { notifyItemMention, notifyMention } from '@/lib/webPush'
Expand All @@ -12,20 +12,34 @@ export const paymentMethods = [
PAID_ACTION_PAYMENT_METHODS.PESSIMISTIC
]

async function getBaseCostDifference (oldSubName, newSubName, { models }) {
async function getSubSwitchFee (oldSubName, newSubName, id, { models, me }) {
if (oldSubName === newSubName) return 0
const oldSub = await models.sub.findUnique({ where: { name: oldSubName }, select: { baseCost: true } })
const newSub = await models.sub.findUnique({ where: { name: newSubName }, select: { baseCost: true } })
return Math.max(0, (newSub?.baseCost ?? 0) - (oldSub?.baseCost ?? 0))

const [oldSub, newSub] = await Promise.all([
models.sub.findUnique({ where: { name: oldSubName }, select: { baseCost: true } }),
models.sub.findUnique({ where: { name: newSubName }, select: { baseCost: true } })
])

const subDifferenceCost = Math.max(0, (newSub?.baseCost ?? 0) - (oldSub?.baseCost ?? 0))
if (subDifferenceCost === 0) return 0

// calculate spam cost exclusively for sub switch
const [{ totalCost }] = await models.$queryRaw`
SELECT ${satsToMsats(subDifferenceCost)}::INTEGER
* POWER(10, item_spam(NULL, ${me?.id ?? USER_ID.anon}::INTEGER,
${me?.id ? ITEM_SPAM_INTERVAL : ANON_ITEM_SPAM_INTERVAL}::INTERVAL))
* ${me ? 1 : 100}::INTEGER as "totalCost"`

return totalCost
Comment on lines +18 to +33
Copy link
Member Author

Choose a reason for hiding this comment

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

It's mostly the same as itemCreate's spam multiplier, another change is parallel sub base cost querying.
I did some tests, following also the loophole scenario you mentioned and it's smooth!

}

export async function getCost ({ subName, id, boost = 0, uploadIds, bio }, { me, models }) {
// the only reason updating items costs anything is when it has new uploads
// or more boost or is switching to a more expensive sub
const old = await models.item.findUnique({ where: { id: parseInt(id) } })
const { totalFeesMsats } = await uploadFees(uploadIds, { models, me })
const baseCostDifference = await getBaseCostDifference(old.subName, subName, { models })
const cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost) + satsToMsats(baseCostDifference)
const subSwitchFee = await getSubSwitchFee(old.subName, subName, id, { models, me })
const cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost) + BigInt(subSwitchFee)

if (cost > 0 && old.invoiceActionState && old.invoiceActionState !== 'PAID') {
throw new Error('creation invoice not paid')
Expand Down
4 changes: 2 additions & 2 deletions pages/items/[id]/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useState } from 'react'
import { useQuery, gql } from '@apollo/client'
import { useRouter } from 'next/router'
import PageLoading from '@/components/page-loading'
import { FeeButtonProvider, postCommentBaseLineItems } from '@/components/fee-button'
import { FeeButtonProvider, postCommentBaseLineItems, postCommentUseRemoteLineItems } from '@/components/fee-button'
import SubSelect from '@/components/sub-select'
import useCanEdit from '@/components/use-can-edit'
import { useMe } from '@/components/me'
Expand Down Expand Up @@ -87,7 +87,7 @@ export default function PostEdit ({ ssrData }) {

return (
<CenterLayout sub={sub}>
<FeeButtonProvider baseLineItems={editLineItems(newSubData?.sub)}>
<FeeButtonProvider baseLineItems={editLineItems(newSubData?.sub)} useRemoteLineItems={postCommentUseRemoteLineItems()}>
<FormType item={item} editThreshold={editThreshold}>
{!item.isJob &&
<SubSelect
Expand Down