diff --git a/api/paidAction/itemCreate.js b/api/paidAction/itemCreate.js index 8d284657..be7a77d6 100644 --- a/api/paidAction/itemCreate.js +++ b/api/paidAction/itemCreate.js @@ -13,9 +13,33 @@ export const paymentMethods = [ PAID_ACTION_PAYMENT_METHODS.PESSIMISTIC ] +export const DEFAULT_ITEM_COST = 1000n + +export async function getBaseCost ({ models, bio, parentId, subName }) { + if (bio) return DEFAULT_ITEM_COST + + if (parentId) { + // the subname is stored in the root item of the thread + const parent = await models.item.findFirst({ + where: { id: Number(parentId) }, + include: { + root: { include: { sub: true } }, + sub: true + } + }) + + const root = parent.root ?? parent + + if (!root.sub) return DEFAULT_ITEM_COST + return satsToMsats(root.sub.replyCost) + } + + const sub = await models.sub.findUnique({ where: { name: subName } }) + return satsToMsats(sub.baseCost) +} + export async function getCost ({ subName, parentId, uploadIds, boost = 0, bio }, { models, me }) { - const sub = (parentId || bio) ? null : await models.sub.findUnique({ where: { name: subName } }) - const baseCost = sub ? satsToMsats(sub.baseCost) : 1000n + const baseCost = await getBaseCost({ models, bio, parentId, subName }) // cost = baseCost * 10^num_items_in_10m * 100 (anon) or 1 (user) + upload fees + boost const [{ cost }] = await models.$queryRaw` diff --git a/api/typeDefs/sub.js b/api/typeDefs/sub.js index 29fc8c7f..a7b00aee 100644 --- a/api/typeDefs/sub.js +++ b/api/typeDefs/sub.js @@ -16,6 +16,7 @@ export default gql` extend type Mutation { upsertSub(oldName: String, name: String!, desc: String, baseCost: Int!, + replyCost: Int!, postTypes: [String!]!, billingType: String!, billingAutoRenew: Boolean!, moderated: Boolean!, nsfw: Boolean!): SubPaidAction! @@ -24,7 +25,7 @@ export default gql` toggleSubSubscription(name: String!): Boolean! transferTerritory(subName: String!, userName: String!): Sub unarchiveTerritory(name: String!, desc: String, baseCost: Int!, - postTypes: [String!]!, + replyCost: Int!, postTypes: [String!]!, billingType: String!, billingAutoRenew: Boolean!, moderated: Boolean!, nsfw: Boolean!): SubPaidAction! } @@ -45,6 +46,7 @@ export default gql` billedLastAt: Date! billPaidUntil: Date baseCost: Int! + replyCost: Int! status: String! moderated: Boolean! moderatedCount: Int! diff --git a/components/reply.js b/components/reply.js index 0928ccdf..ec12d0a5 100644 --- a/components/reply.js +++ b/components/reply.js @@ -161,7 +161,7 @@ export default forwardRef(function Reply ({ {reply &&