unify idempotent act and act

This commit is contained in:
keyan 2023-12-27 10:15:18 -06:00
parent 73ad93f2bb
commit 6170853d72
3 changed files with 26 additions and 60 deletions

View File

@ -1,6 +1,6 @@
import { GraphQLError } from 'graphql'
import { ensureProtocol, removeTracking } from '../../lib/url'
import { serializeInvoicable } from './serial'
import serialize, { serializeInvoicable } from './serial'
import { decodeCursor, LIMIT, nextCursorEncoded } from '../../lib/cursor'
import { getMetadata, metadataRuleSets } from 'page-metadata-parser'
import { ruleSet as publicationDateRuleSet } from '../../lib/timedate-scraper'
@ -752,7 +752,7 @@ export default {
return id
},
act: async (parent, { id, sats, act = 'TIP', hash, hmac }, { me, models, lnd, headers }) => {
act: async (parent, { id, sats, act = 'TIP', idempotent, hash, hmac }, { me, models, lnd, headers }) => {
await ssValidate(actSchema, { sats, act })
await assertGofacYourself({ models, headers })
@ -776,59 +776,27 @@ export default {
}
}
await serializeInvoicable(
models.$queryRaw`
if (idempotent) {
await serialize(
models,
models.$queryRaw`
SELECT
item_act(${Number(id)}::INTEGER,
${me?.id || ANON_USER_ID}::INTEGER, ${act}::"ItemActType", ${Number(sats)}::INTEGER)`,
{ me, models, lnd, hash, hmac, enforceFee: sats }
)
notifyZapped({ models, id })
return {
id,
sats,
act,
path: item.path
item_act(${Number(id)}::INTEGER, ${me.id}::INTEGER, ${act}::"ItemActType",
(SELECT ${Number(sats)}::INTEGER - COALESCE(sum(msats) / 1000, 0)
FROM "ItemAct"
WHERE act IN ('TIP', 'FEE')
AND "itemId" = ${Number(id)}::INTEGER
AND "userId" = ${me.id}::INTEGER)::INTEGER)`
)
} else {
await serializeInvoicable(
models.$queryRaw`
SELECT
item_act(${Number(id)}::INTEGER,
${me?.id || ANON_USER_ID}::INTEGER, ${act}::"ItemActType", ${Number(sats)}::INTEGER)`,
{ me, models, lnd, hash, hmac, enforceFee: sats }
)
}
},
idempotentAct: async (parent, { id, sats, act = 'TIP', hash, hmac }, { me, models, lnd, headers }) => {
if (!me) {
throw new GraphQLError('you must be logged in', { extensions: { code: 'FORBIDDEN' } })
}
await ssValidate(actSchema, { sats, act })
await assertGofacYourself({ models, headers })
const [item] = await models.$queryRawUnsafe(`
${SELECT}
FROM "Item"
WHERE id = $1`, Number(id))
if (Number(item.userId) === Number(me.id)) {
throw new GraphQLError('cannot zap your self', { extensions: { code: 'BAD_INPUT' } })
}
// Disallow tips if me is one of the forward user recipients
if (act === 'TIP') {
const existingForwards = await models.itemForward.findMany({ where: { itemId: Number(id) } })
if (existingForwards.some(fwd => Number(fwd.userId) === Number(me.id))) {
throw new GraphQLError('cannot zap a post for which you are forwarded zaps', { extensions: { code: 'BAD_INPUT' } })
}
}
await serializeInvoicable(
models.$queryRaw`
SELECT
item_act(${Number(id)}::INTEGER, ${me.id}::INTEGER, ${act}::"ItemActType",
(SELECT ${Number(sats)}::INTEGER - COALESCE(sum(msats) / 1000, 0)
FROM "ItemAct"
WHERE act IN ('TIP', 'FEE')
AND "itemId" = ${Number(id)}::INTEGER
AND "userId" = ${me.id}::INTEGER)::INTEGER)`,
{ me, models, lnd, hash, hmac, enforceFee: sats }
)
notifyZapped({ models, id })

View File

@ -36,8 +36,7 @@ export default gql`
upsertPoll(id: ID, sub: String, title: String!, text: String, options: [String!]!, boost: Int, forward: [ItemForwardInput], hash: String, hmac: String): Item!
updateNoteId(id: ID!, noteId: String!): Item!
upsertComment(id:ID, text: String!, parentId: ID, hash: String, hmac: String): Item!
act(id: ID!, sats: Int, act: String, hash: String, hmac: String): ItemActResult!
idempotentAct(id: ID!, sats: Int, hash: String, hmac: String): ItemActResult!
act(id: ID!, sats: Int, act: String, idempotent: Boolean, hash: String, hmac: String): ItemActResult!
pollVote(id: ID!, hash: String, hmac: String): ID!
}

View File

@ -173,7 +173,7 @@ export function useAct ({ onUpdate } = {}) {
export function useZap () {
const update = useCallback((cache, args) => {
const { data: { idempotentAct: { id, sats, path } } } = args
const { data: { act: { id, sats, path } } } = args
// determine how much we increased existing sats by by checking the
// difference between result sats and meSats
@ -221,8 +221,8 @@ export function useZap () {
const [zap] = useMutation(
gql`
mutation idempotentAct($id: ID!, $sats: Int!, $hash: String, $hmac: String) {
idempotentAct(id: $id, sats: $sats, hash: $hash, hmac: $hmac) {
mutation idempotentAct($id: ID!, $sats: Int!) {
act(id: $id, sats: $sats, idempotent: true) {
id
sats
path
@ -241,7 +241,6 @@ export function useZap () {
}, [act, strike])
return useCallback(async ({ item, me }) => {
console.log(item)
const meSats = (item?.meSats || 0)
// what should our next tip be?
@ -259,7 +258,7 @@ export function useZap () {
await zap({
variables,
optimisticResponse: {
idempotentAct: {
act: {
path: item.path,
...variables
}