stablize act mutation reference

This commit is contained in:
keyan 2023-12-26 15:55:48 -06:00
parent 8c5c29ee45
commit 67a9fe23cf
3 changed files with 51 additions and 50 deletions

View File

@ -752,13 +752,14 @@ export default {
await ssValidate(amountSchema, { amount: sats }) await ssValidate(amountSchema, { amount: sats })
await assertGofacYourself({ models, headers }) await assertGofacYourself({ models, headers })
// disallow self tips except anons const [item] = await models.$queryRawUnsafe(`
if (me) {
const [item] = await models.$queryRawUnsafe(`
${SELECT} ${SELECT}
FROM "Item" FROM "Item"
WHERE id = $1 AND "userId" = $2`, Number(id), me.id) WHERE id = $1`, Number(id))
if (item) {
// disallow self tips except anons
if (me) {
if (Number(item.userId) === Number(me.id)) {
throw new GraphQLError('cannot zap your self', { extensions: { code: 'BAD_INPUT' } }) throw new GraphQLError('cannot zap your self', { extensions: { code: 'BAD_INPUT' } })
} }
@ -769,7 +770,7 @@ export default {
} }
} }
const { item_act: vote } = await serializeInvoicable( await serializeInvoicable(
models.$queryRaw` models.$queryRaw`
SELECT SELECT
item_act(${Number(id)}::INTEGER, item_act(${Number(id)}::INTEGER,
@ -780,8 +781,9 @@ export default {
notifyZapped({ models, id }) notifyZapped({ models, id })
return { return {
vote, id,
sats sats,
path: item.path
} }
}, },
dontLikeThis: async (parent, { id, sats = DONT_LIKE_THIS_COST, hash, hmac }, { me, lnd, models }) => { dontLikeThis: async (parent, { id, sats = DONT_LIKE_THIS_COST, hash, hmac }, { me, lnd, models }) => {

View File

@ -18,8 +18,9 @@ export default gql`
} }
type ItemActResult { type ItemActResult {
vote: Int! id: ID!
sats: Int! sats: Int!
path: String!
} }
extend type Mutation { extend type Mutation {

View File

@ -56,52 +56,47 @@ const TipPopover = ({ target, show, handleClose }) => (
</Overlay> </Overlay>
) )
function useAct ({ item, setVoteShow = () => {}, setTipShow = () => {} }) { function useAct () {
const me = useMe() const me = useMe()
const update = useCallback((cache, { data: { act: { id, sats, path } } }) => {
cache.modify({
id: `Item:${id}`,
fields: {
sats (existingSats = 0) {
return existingSats + sats
},
meSats: me
? (existingSats = 0) => {
return existingSats + sats
}
: undefined
}
})
// update all ancestors
path.split('.').forEach(aId => {
if (Number(aId) === Number(id)) return
cache.modify({
id: `Item:${aId}`,
fields: {
commentSats (existingCommentSats = 0) {
return existingCommentSats + sats
}
}
})
})
}, [!!me])
return useMutation( return useMutation(
gql` gql`
mutation act($id: ID!, $sats: Int!, $hash: String, $hmac: String) { mutation act($id: ID!, $sats: Int!, $hash: String, $hmac: String) {
act(id: $id, sats: $sats, hash: $hash, hmac: $hmac) { act(id: $id, sats: $sats, hash: $hash, hmac: $hmac) {
id
sats sats
path
} }
}`, { }`, { update }
update (cache, { data: { act: { sats } } }) {
cache.modify({
id: `Item:${item.id}`,
fields: {
sats (existingSats = 0) {
return existingSats + sats
},
meSats: me
? (existingSats = 0) => {
if (sats <= me.privates?.sats) {
if (existingSats === 0) {
setVoteShow(true)
} else {
setTipShow(true)
}
}
return existingSats + sats
}
: undefined
}
})
// update all ancestors
item.path.split('.').forEach(id => {
if (Number(id) === Number(item.id)) return
cache.modify({
id: `Item:${id}`,
fields: {
commentSats (existingCommentSats = 0) {
return existingCommentSats + sats
}
}
})
})
}
}
) )
} }
@ -177,11 +172,14 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
const zap = useDebounceCallback(async (sats) => { const zap = useDebounceCallback(async (sats) => {
if (!sats) return if (!sats) return
const variables = { id: item.id, sats } const variables = { id: item.id, sats }
act({ act({
variables, variables,
optimisticResponse: { optimisticResponse: {
act: { act: {
sats id: item.id,
sats,
path: item.path
} }
} }
}).catch((error) => { }).catch((error) => {
@ -193,7 +191,7 @@ export default function UpVote ({ item, className, pendingSats, setPendingSats }
toaster.danger(error?.message || error?.toString?.()) toaster.danger(error?.message || error?.toString?.())
}) })
setPendingSats(0) setPendingSats(0)
}, 500, [act, toaster, item?.id, showInvoiceModal, setPendingSats]) }, 500, [act, toaster, item?.id, item?.path, showInvoiceModal, setPendingSats])
const disabled = useMemo(() => item?.mine || item?.meForward || item?.deletedAt, const disabled = useMemo(() => item?.mine || item?.meForward || item?.deletedAt,
[item?.mine, item?.meForward, item?.deletedAt]) [item?.mine, item?.meForward, item?.deletedAt])