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 3 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
14 changes: 11 additions & 3 deletions api/paidAction/itemUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,20 @@ export const paymentMethods = [
PAID_ACTION_PAYMENT_METHODS.PESSIMISTIC
]

export async function getCost ({ id, boost = 0, uploadIds, bio }, { me, models }) {
async function getBaseCostDifference (oldSubName, newSubName, { models }) {
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))
}

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 baseCostDifference = await getBaseCostDifference(old.subName, subName, { models })
const cost = BigInt(totalFeesMsats) + satsToMsats(boost - old.boost) + satsToMsats(baseCostDifference)

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 } 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)}>
<FormType item={item} editThreshold={editThreshold}>
{!item.isJob &&
<SubSelect
Expand Down