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

enhance: Allow switching to a more expensive territory #1919

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
30 changes: 26 additions & 4 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,12 +12,34 @@ export const paymentMethods = [
PAID_ACTION_PAYMENT_METHODS.PESSIMISTIC
]

export async function getCost ({ id, boost = 0, uploadIds, bio }, { me, models }) {
async function getSubSwitchFee (oldSubName, newSubName, id, { models, me }) {
if (oldSubName === newSubName) return 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 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 cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost)
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
8 changes: 0 additions & 8 deletions api/resolvers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -1474,14 +1474,6 @@ export const updateItem = async (parent, { sub: subName, forward, hash, hmac, ..
throw new GqlInputError('item does not belong to you')
}

const differentSub = subName && old.subName !== subName
if (differentSub) {
const sub = await models.sub.findUnique({ where: { name: subName } })
if (sub.baseCost > old.sub.baseCost) {
throw new GqlInputError('cannot change to a more expensive sub')
}
}

// in case they lied about their existing boost
await validateSchema(advSchema, { boost: item.boost }, { models, me, existingBoost: old.boost })

Expand Down
1 change: 1 addition & 0 deletions fragments/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const ITEM_FIELDS = gql`
meMuteSub
meSubscription
nsfw
baseCost
replyCost
}
otsHash
Expand Down
53 changes: 41 additions & 12 deletions pages/items/[id]/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,40 @@ import JobForm from '@/components/job-form'
import { PollForm } from '@/components/poll-form'
import { BountyForm } from '@/components/bounty-form'
import { useState } from 'react'
import { useQuery } from '@apollo/client'
import { useQuery, gql } from '@apollo/client'
import { useRouter } from 'next/router'
import PageLoading from '@/components/page-loading'
import { FeeButtonProvider } 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'

export const getServerSideProps = getGetServerSideProps({
query: ITEM,
notFound: data => !data.item
})

const SUB_BASECOST = gql`
query Sub($name: String!) {
sub(name: $name) {
name
baseCost
}
}
`

export default function PostEdit ({ ssrData }) {
const router = useRouter()
const { data } = useQuery(ITEM, { variables: { id: router.query.id } })
if (!data && !ssrData) return <PageLoading />

const { item } = data || ssrData
const { me } = useMe()
const [sub, setSub] = useState(item.subName)

// we need to fetch the new sub to calculate the cost difference
const { data: newSubData } = useQuery(SUB_BASECOST, { variables: { name: sub } })

const [,, editThreshold] = useCanEdit(item)

let FormType = DiscussionForm
Expand All @@ -45,20 +59,35 @@ export default function PostEdit ({ ssrData }) {
itemType = 'BOUNTY'
}

const existingBoostLineItem = item.boost
? {
existingBoost: {
label: 'old boost',
term: `- ${item.boost}`,
op: '-',
modifier: cost => cost - item.boost
const editLineItems = (newSub) => {
const existingBoostLineItem = item.boost
? {
existingBoost: {
label: 'old boost',
term: `- ${item.boost}`,
op: '-',
modifier: cost => cost - item.boost
}
}
}
: undefined
: undefined

const isSwitchingSub = item.subName !== newSub?.name
const subCostDifference = isSwitchingSub && {
...postCommentBaseLineItems({
baseCost: Math.max(0, (newSub?.baseCost ?? 0) - (item?.sub?.baseCost ?? 0)),
me: !!me
})
}

return {
...subCostDifference,
...existingBoostLineItem
}
}

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