Fix image fees undefined and not enforced if logged in (#909)

* Fix image fees not enforced

* Fix image fees undefined

* Add comment about return value
This commit is contained in:
ekzyis 2024-03-13 22:08:55 +01:00 committed by GitHub
parent 9b5e63cb9c
commit 96d3034c66
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 6 additions and 4 deletions

View File

@ -15,6 +15,8 @@ export function uploadIdsFromText (text, { models }) {
} }
export async function imageFeesInfo (s3Keys, { models, me }) { export async function imageFeesInfo (s3Keys, { models, me }) {
// returns info object in this format:
// { bytes24h: int, bytesUnpaid: int, nUnpaid: int, imageFeeMsats: BigInt }
const [info] = await models.$queryRawUnsafe('SELECT * FROM image_fees_info($1::INTEGER, $2::INTEGER[])', me ? me.id : ANON_USER_ID, s3Keys) const [info] = await models.$queryRawUnsafe('SELECT * FROM image_fees_info($1::INTEGER, $2::INTEGER[])', me ? me.id : ANON_USER_ID, s3Keys)
const imageFee = msatsToSats(info.imageFeeMsats) const imageFee = msatsToSats(info.imageFeeMsats)
const totalFeesMsats = info.nUnpaid * Number(info.imageFeeMsats) const totalFeesMsats = info.nUnpaid * Number(info.imageFeeMsats)

View File

@ -1285,7 +1285,7 @@ export const updateItem = async (parent, { sub: subName, forward, options, ...it
const fwdUsers = await getForwardUsers(models, forward) const fwdUsers = await getForwardUsers(models, forward)
const uploadIds = uploadIdsFromText(item.text, { models }) const uploadIds = uploadIdsFromText(item.text, { models })
const { fees: imgFees } = await imageFeesInfo(uploadIds, { models, me }) const { totalFees: imgFees } = await imageFeesInfo(uploadIds, { models, me })
item = await serializeInvoicable( item = await serializeInvoicable(
models.$queryRawUnsafe(`${SELECT} FROM update_item($1::JSONB, $2::JSONB, $3::JSONB, $4::INTEGER[]) AS "Item"`, models.$queryRawUnsafe(`${SELECT} FROM update_item($1::JSONB, $2::JSONB, $3::JSONB, $4::INTEGER[]) AS "Item"`,
@ -1321,9 +1321,9 @@ export const createItem = async (parent, { forward, options, ...item }, { me, mo
} }
const uploadIds = uploadIdsFromText(item.text, { models }) const uploadIds = uploadIdsFromText(item.text, { models })
const { fees: imgFees } = await imageFeesInfo(uploadIds, { models, me }) const { totalFees: imgFees } = await imageFeesInfo(uploadIds, { models, me })
let enforceFee let enforceFee = 0
if (!me) { if (!me) {
if (item.parentId) { if (item.parentId) {
enforceFee = ANON_FEE_MULTIPLIER enforceFee = ANON_FEE_MULTIPLIER
@ -1331,8 +1331,8 @@ export const createItem = async (parent, { forward, options, ...item }, { me, mo
const sub = await models.sub.findUnique({ where: { name: item.subName } }) const sub = await models.sub.findUnique({ where: { name: item.subName } })
enforceFee = sub.baseCost * ANON_FEE_MULTIPLIER + (item.boost || 0) enforceFee = sub.baseCost * ANON_FEE_MULTIPLIER + (item.boost || 0)
} }
enforceFee += imgFees
} }
enforceFee += imgFees
item = await serializeInvoicable( item = await serializeInvoicable(
models.$queryRawUnsafe( models.$queryRawUnsafe(