2023-07-24 18:35:05 +00:00
|
|
|
import Button from 'react-bootstrap/Button'
|
|
|
|
import InputGroup from 'react-bootstrap/InputGroup'
|
Allow zapping, posting and commenting without funds or an account (#336)
* Add anon zaps
* Add anon comments and posts (link, discussion, poll)
* Use payment hash instead of invoice id as proof of payment
Our invoice IDs can be enumerated.
So there is a - even though very rare - chance that an attacker could find a paid invoice which is not used yet and use it for himself.
Random payment hashes prevent this.
Also, since we delete invoices after use, using database IDs as proof of payments are not suitable.
If a user tells us an invoice ID after we deleted it, we can no longer tell if the invoice was paid or not since the LN node only knows about payment hashes but nothing about the database IDs.
* Allow pay per invoice for stackers
The modal which pops up if the stacker does not have enough sats now has two options: "fund wallet" and "pay invoice"
* Fix onSuccess called twice
For some reason, when calling `showModal`, `useMemo` in modal.js and the code for the modal component (here: <Invoice>) is called twice.
This leads to the `onSuccess` callback being called twice and one failing since the first one deletes the invoice.
* Keep invoice modal open if focus is lost
* Skip anon user during trust calculation
* Add error handling
* Skip 'invoice not found' errors
* Remove duplicate insufficient funds handling
* Fix insufficient funds error detection
* Fix invoice amount for comments
* Allow pay per invoice for bounty and job posts
* Also strike on payment after short press
* Fix unexpected token 'export'
* Fix eslint
* Remove unused id param
* Fix comment copy-paste error
* Rename to useInvoiceable
* Fix unexpected token 'export'
* Fix onConfirmation called at every render
* Add invoice HMAC
This prevents entities which know the invoice hash (like all LN nodes on the payment path) from using the invoice hash on SN.
Only the user which created the invoice knows the HMAC and thus can use the invoice hash.
* make anon posting less hidden, add anon info button explainer
* Fix anon users can't zap other anon users
* Always show repeat and contacts on action error
* Keep track of modal stack
* give anon an icon
* add generic date pivot helper
* make anon user's invoices expire in 5 minutes
* fix forgotten find and replace
* use datePivot more places
* add sat amounts to invoices
* reduce anon invoice expiration to 3 minutes
* don't abbreviate
* Fix [object Object] as error message
Any errors thrown here are already objects of shape { message: string }
* Fix empty invoice creation attempts
I stumbled across this while checking if anons can edit their items.
I monkey patched the code to make it possible (so they can see the 'edit' button) and tried to edit an item but I got this error:
Variable "$amount" of required type "Int!" was not provided.
I fixed this even though this function should never be called without an amount anyway. It will return a sane error in that case now.
* anon func mods, e.g. inv limits
* anon tips should be denormalized
* remove redundant meTotalSats
* correct overlay zap text for anon
* exclude anon from trust graph before algo runs
* remove balance limit on anon
* give anon a bio and remove cowboy hat/top stackers;
* make anon hat appear on profile
* concat hash and hmac and call it a token
* Fix localStorage cleared because error were swallowed
* fix qr layout shift
* restyle fund error modal
* Catch invoice errors in fund error modal
* invoice check backoff
* anon info typo
* make invoice expiration times have saner defaults
* add comma to anon info
* use builtin copy input label
---------
Co-authored-by: ekzyis <ek@stacker.news>
Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2023-08-11 23:50:57 +00:00
|
|
|
import React, { useState, useRef, useEffect, useCallback } from 'react'
|
2021-12-13 19:49:34 +00:00
|
|
|
import { Form, Input, SubmitButton } from './form'
|
2021-09-12 16:55:38 +00:00
|
|
|
import { useMe } from './me'
|
2024-03-20 00:37:31 +00:00
|
|
|
import UpBolt from '@/svgs/bolt.svg'
|
2024-09-19 18:13:14 +00:00
|
|
|
import { amountSchema, boostSchema } from '@/lib/validate'
|
2024-05-28 17:18:54 +00:00
|
|
|
import { useToast } from './toast'
|
2023-12-27 02:27:52 +00:00
|
|
|
import { useLightning } from './lightning'
|
2024-07-27 03:37:03 +00:00
|
|
|
import { nextTip, defaultTipIncludingRandom } from './upvote'
|
2024-05-28 17:18:54 +00:00
|
|
|
import { ZAP_UNDO_DELAY_MS } from '@/lib/constants'
|
2024-07-01 17:02:29 +00:00
|
|
|
import { usePaidMutation } from './use-paid-mutation'
|
|
|
|
import { ACT_MUTATION } from '@/fragments/paidAction'
|
2024-09-12 18:05:11 +00:00
|
|
|
import { meAnonSats } from '@/lib/apollo'
|
2024-09-19 18:13:14 +00:00
|
|
|
import { BoostItemInput } from './adv-post-form'
|
2024-09-24 14:38:48 +00:00
|
|
|
import { useWallet } from '../wallets'
|
2021-09-10 18:55:36 +00:00
|
|
|
|
2024-05-28 17:18:54 +00:00
|
|
|
const defaultTips = [100, 1000, 10_000, 100_000]
|
2023-05-14 23:33:49 +00:00
|
|
|
|
2023-05-14 23:11:31 +00:00
|
|
|
const Tips = ({ setOValue }) => {
|
2023-05-14 23:34:51 +00:00
|
|
|
const tips = [...getCustomTips(), ...defaultTips].sort((a, b) => a - b)
|
2024-04-16 20:33:00 +00:00
|
|
|
return tips.map((num, i) =>
|
2023-05-14 23:11:31 +00:00
|
|
|
<Button
|
|
|
|
size='sm'
|
2024-04-16 20:33:00 +00:00
|
|
|
className={`${i > 0 ? 'ms-2' : ''} mb-2`}
|
2023-05-14 23:11:31 +00:00
|
|
|
key={num}
|
|
|
|
onClick={() => { setOValue(num) }}
|
|
|
|
>
|
|
|
|
<UpBolt
|
2023-07-24 18:35:05 +00:00
|
|
|
className='me-1'
|
2023-05-14 23:11:31 +00:00
|
|
|
width={14}
|
|
|
|
height={14}
|
|
|
|
/>{num}
|
|
|
|
</Button>)
|
|
|
|
}
|
|
|
|
|
2023-07-25 14:14:45 +00:00
|
|
|
const getCustomTips = () => JSON.parse(window.localStorage.getItem('custom-tips')) || []
|
2023-05-14 23:11:31 +00:00
|
|
|
|
|
|
|
const addCustomTip = (amount) => {
|
2023-05-14 23:33:49 +00:00
|
|
|
if (defaultTips.includes(amount)) return
|
2023-05-14 23:11:31 +00:00
|
|
|
let customTips = Array.from(new Set([amount, ...getCustomTips()]))
|
|
|
|
if (customTips.length > 3) {
|
|
|
|
customTips = customTips.slice(0, 3)
|
|
|
|
}
|
2023-07-25 14:14:45 +00:00
|
|
|
window.localStorage.setItem('custom-tips', JSON.stringify(customTips))
|
2023-05-14 23:11:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 17:18:54 +00:00
|
|
|
const setItemMeAnonSats = ({ id, amount }) => {
|
2024-09-12 18:05:11 +00:00
|
|
|
const reactiveVar = meAnonSats[id]
|
|
|
|
const existingAmount = reactiveVar()
|
|
|
|
reactiveVar(existingAmount + amount)
|
|
|
|
|
|
|
|
// save for next page load
|
2024-05-28 17:18:54 +00:00
|
|
|
const storageKey = `TIP-item:${id}`
|
|
|
|
window.localStorage.setItem(storageKey, existingAmount + amount)
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:13:14 +00:00
|
|
|
function BoostForm ({ step, onSubmit, children, item, oValue, inputRef, act = 'BOOST' }) {
|
|
|
|
return (
|
|
|
|
<Form
|
|
|
|
initial={{
|
|
|
|
amount: step
|
|
|
|
}}
|
|
|
|
schema={boostSchema}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
>
|
|
|
|
<BoostItemInput
|
|
|
|
label='add boost'
|
|
|
|
act
|
|
|
|
name='amount'
|
|
|
|
type='number'
|
|
|
|
innerRef={inputRef}
|
|
|
|
overrideValue={oValue}
|
|
|
|
sub={item.sub}
|
|
|
|
step={step}
|
|
|
|
required
|
|
|
|
autoFocus
|
|
|
|
item={item}
|
|
|
|
/>
|
|
|
|
{children}
|
|
|
|
<div className='d-flex mt-3'>
|
|
|
|
<SubmitButton variant='success' className='ms-auto mt-1 px-4' value={act}>
|
|
|
|
boost
|
|
|
|
</SubmitButton>
|
|
|
|
</div>
|
|
|
|
</Form>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function ItemAct ({ onClose, item, act = 'TIP', step, children, abortSignal }) {
|
2024-07-01 17:02:29 +00:00
|
|
|
const inputRef = useRef(null)
|
2024-09-12 18:05:11 +00:00
|
|
|
const { me } = useMe()
|
2024-09-25 18:32:52 +00:00
|
|
|
const wallet = useWallet()
|
2024-07-01 17:02:29 +00:00
|
|
|
const [oValue, setOValue] = useState()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
inputRef.current?.focus()
|
|
|
|
}, [onClose, item.id])
|
|
|
|
|
2024-09-19 18:13:14 +00:00
|
|
|
const actor = useAct()
|
2024-07-01 19:56:17 +00:00
|
|
|
const strike = useLightning()
|
2024-07-01 17:02:29 +00:00
|
|
|
|
2024-07-04 17:30:42 +00:00
|
|
|
const onSubmit = useCallback(async ({ amount }) => {
|
2024-07-01 17:02:29 +00:00
|
|
|
if (abortSignal && zapUndoTrigger({ me, amount })) {
|
|
|
|
onClose?.()
|
|
|
|
try {
|
|
|
|
await abortSignal.pause({ me, amount })
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof ActCanceledError) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-25 18:32:52 +00:00
|
|
|
|
|
|
|
const onPaid = () => {
|
|
|
|
strike()
|
|
|
|
onClose?.()
|
|
|
|
if (!me) setItemMeAnonSats({ id: item.id, amount })
|
|
|
|
}
|
|
|
|
|
|
|
|
const closeImmediately = !!wallet || me?.privates?.sats > Number(amount)
|
|
|
|
if (closeImmediately) {
|
|
|
|
onPaid()
|
|
|
|
}
|
|
|
|
|
2024-09-19 18:13:14 +00:00
|
|
|
const { error } = await actor({
|
2024-07-01 17:02:29 +00:00
|
|
|
variables: {
|
|
|
|
id: item.id,
|
|
|
|
sats: Number(amount),
|
2024-09-19 18:13:14 +00:00
|
|
|
act
|
2024-07-01 17:02:29 +00:00
|
|
|
},
|
|
|
|
optimisticResponse: me
|
|
|
|
? {
|
|
|
|
act: {
|
2024-07-01 19:56:17 +00:00
|
|
|
__typename: 'ItemActPaidAction',
|
2024-07-01 17:02:29 +00:00
|
|
|
result: {
|
2024-09-19 18:13:14 +00:00
|
|
|
id: item.id, sats: Number(amount), act, path: item.path
|
2024-07-01 17:02:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
// don't close modal immediately because we want the QR modal to stack
|
2024-09-25 18:32:52 +00:00
|
|
|
onPaid: closeImmediately ? undefined : onPaid
|
2024-07-01 17:02:29 +00:00
|
|
|
})
|
2024-07-09 18:10:41 +00:00
|
|
|
if (error) throw error
|
2024-07-01 17:02:29 +00:00
|
|
|
addCustomTip(Number(amount))
|
2024-09-25 18:32:52 +00:00
|
|
|
}, [me, actor, !!wallet, act, item.id, onClose, abortSignal, strike])
|
2024-07-01 17:02:29 +00:00
|
|
|
|
2024-09-19 18:13:14 +00:00
|
|
|
return act === 'BOOST'
|
|
|
|
? <BoostForm step={step} onSubmit={onSubmit} item={item} oValue={oValue} inputRef={inputRef} act={act}>{children}</BoostForm>
|
|
|
|
: (
|
|
|
|
<Form
|
|
|
|
initial={{
|
|
|
|
amount: defaultTipIncludingRandom(me?.privates) || defaultTips[0]
|
|
|
|
}}
|
|
|
|
schema={amountSchema}
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
>
|
|
|
|
<Input
|
|
|
|
label='amount'
|
|
|
|
name='amount'
|
|
|
|
type='number'
|
|
|
|
innerRef={inputRef}
|
|
|
|
overrideValue={oValue}
|
|
|
|
step={step}
|
|
|
|
required
|
|
|
|
autoFocus
|
|
|
|
append={<InputGroup.Text className='text-monospace'>sats</InputGroup.Text>}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<Tips setOValue={setOValue} />
|
|
|
|
</div>
|
|
|
|
{children}
|
|
|
|
<div className='d-flex mt-3'>
|
|
|
|
<SubmitButton variant={act === 'DONT_LIKE_THIS' ? 'danger' : 'success'} className='ms-auto mt-1 px-4' value={act}>
|
|
|
|
{act === 'DONT_LIKE_THIS' ? 'downzap' : 'zap'}
|
|
|
|
</SubmitButton>
|
|
|
|
</div>
|
|
|
|
</Form>)
|
2024-07-01 17:02:29 +00:00
|
|
|
}
|
2024-05-28 17:18:54 +00:00
|
|
|
|
2024-07-01 17:02:29 +00:00
|
|
|
function modifyActCache (cache, { result, invoice }) {
|
|
|
|
if (!result) return
|
|
|
|
const { id, sats, path, act } = result
|
2024-05-28 17:18:54 +00:00
|
|
|
cache.modify({
|
|
|
|
id: `Item:${id}`,
|
|
|
|
fields: {
|
|
|
|
sats (existingSats = 0) {
|
|
|
|
if (act === 'TIP') {
|
|
|
|
return existingSats + sats
|
|
|
|
}
|
|
|
|
return existingSats
|
|
|
|
},
|
|
|
|
meSats: (existingSats = 0) => {
|
|
|
|
if (act === 'TIP') {
|
|
|
|
return existingSats + sats
|
|
|
|
}
|
|
|
|
return existingSats
|
|
|
|
},
|
2024-07-01 17:02:29 +00:00
|
|
|
meDontLikeSats: (existingSats = 0) => {
|
|
|
|
if (act === 'DONT_LIKE_THIS') {
|
|
|
|
return existingSats + sats
|
|
|
|
}
|
|
|
|
return existingSats
|
2024-09-19 18:13:14 +00:00
|
|
|
},
|
|
|
|
boost: (existingBoost = 0) => {
|
|
|
|
if (act === 'BOOST') {
|
|
|
|
return existingBoost + sats
|
|
|
|
}
|
|
|
|
return existingBoost
|
2024-07-01 17:02:29 +00:00
|
|
|
}
|
2024-05-28 17:18:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (act === 'TIP') {
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2024-03-25 00:46:12 +00:00
|
|
|
}
|
|
|
|
|
2024-07-01 17:02:29 +00:00
|
|
|
export function useAct ({ query = ACT_MUTATION, ...options } = {}) {
|
|
|
|
// because the mutation name we use varies,
|
|
|
|
// we need to extract the result/invoice from the response
|
|
|
|
const getPaidActionResult = data => Object.values(data)[0]
|
2023-12-26 22:51:47 +00:00
|
|
|
|
2024-07-01 17:02:29 +00:00
|
|
|
const [act] = usePaidMutation(query, {
|
2024-09-25 18:32:52 +00:00
|
|
|
waitFor: inv => inv?.satsReceived > 0,
|
2024-07-01 17:02:29 +00:00
|
|
|
...options,
|
|
|
|
update: (cache, { data }) => {
|
|
|
|
const response = getPaidActionResult(data)
|
|
|
|
if (!response) return
|
|
|
|
modifyActCache(cache, response)
|
|
|
|
options?.update?.(cache, { data })
|
|
|
|
},
|
|
|
|
onPayError: (e, cache, { data }) => {
|
|
|
|
const response = getPaidActionResult(data)
|
|
|
|
if (!response || !response.result) return
|
|
|
|
const { result: { sats } } = response
|
|
|
|
const negate = { ...response, result: { ...response.result, sats: -1 * sats } }
|
|
|
|
modifyActCache(cache, negate)
|
|
|
|
options?.onPayError?.(e, cache, { data })
|
|
|
|
},
|
|
|
|
onPaid: (cache, { data }) => {
|
|
|
|
const response = getPaidActionResult(data)
|
|
|
|
if (!response) return
|
|
|
|
options?.onPaid?.(cache, { data })
|
2023-12-26 22:51:47 +00:00
|
|
|
}
|
2024-07-01 17:02:29 +00:00
|
|
|
})
|
2024-05-28 17:18:54 +00:00
|
|
|
return act
|
2023-12-26 22:51:47 +00:00
|
|
|
}
|
2023-12-27 02:27:52 +00:00
|
|
|
|
|
|
|
export function useZap () {
|
2024-09-24 14:38:48 +00:00
|
|
|
const wallet = useWallet()
|
2024-07-01 17:02:29 +00:00
|
|
|
const act = useAct()
|
2024-07-01 19:56:17 +00:00
|
|
|
const strike = useLightning()
|
2023-12-27 02:27:52 +00:00
|
|
|
const toaster = useToast()
|
2024-02-22 00:48:42 +00:00
|
|
|
|
2024-09-24 14:38:48 +00:00
|
|
|
return useCallback(async ({ item, me, abortSignal }) => {
|
2024-07-01 17:02:29 +00:00
|
|
|
const meSats = (item?.meSats || 0)
|
2023-12-27 02:27:52 +00:00
|
|
|
|
2024-02-27 00:09:09 +00:00
|
|
|
// add current sats to next tip since idempotent zaps use desired total zap not difference
|
2024-07-01 17:02:29 +00:00
|
|
|
const sats = nextTip(meSats, { ...me?.privates })
|
2023-12-27 02:27:52 +00:00
|
|
|
|
2024-05-28 17:18:54 +00:00
|
|
|
const variables = { id: item.id, sats, act: 'TIP' }
|
2024-07-01 19:56:17 +00:00
|
|
|
const optimisticResponse = { act: { __typename: 'ItemActPaidAction', result: { path: item.path, ...variables } } }
|
2024-05-28 17:18:54 +00:00
|
|
|
|
2023-12-27 02:27:52 +00:00
|
|
|
try {
|
2024-07-01 17:02:29 +00:00
|
|
|
await abortSignal.pause({ me, amount: sats })
|
2024-07-01 19:56:17 +00:00
|
|
|
strike()
|
2024-09-24 14:38:48 +00:00
|
|
|
// batch zaps if wallet is enabled or using fee credits so they can be executed serially in a single request
|
|
|
|
const { error } = await act({ variables, optimisticResponse, context: { batch: !!wallet || me?.privates?.sats > sats } })
|
2024-07-09 18:10:41 +00:00
|
|
|
if (error) throw error
|
2023-12-27 02:27:52 +00:00
|
|
|
} catch (error) {
|
2024-07-09 18:10:41 +00:00
|
|
|
if (error instanceof ActCanceledError) {
|
2023-12-27 02:27:52 +00:00
|
|
|
return
|
|
|
|
}
|
2024-05-28 17:18:54 +00:00
|
|
|
|
|
|
|
const reason = error?.message || error?.toString?.()
|
2024-07-09 18:10:41 +00:00
|
|
|
toaster.danger(reason)
|
2024-05-28 17:18:54 +00:00
|
|
|
}
|
2024-09-24 14:38:48 +00:00
|
|
|
}, [act, toaster, strike, !!wallet])
|
2024-05-28 17:18:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ActCanceledError extends Error {
|
|
|
|
constructor () {
|
|
|
|
super('act canceled')
|
|
|
|
this.name = 'ActCanceledError'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class ZapUndoController extends AbortController {
|
2024-07-01 17:02:29 +00:00
|
|
|
constructor ({ onStart = () => {}, onDone = () => {} }) {
|
2024-05-28 17:18:54 +00:00
|
|
|
super()
|
2024-07-01 17:02:29 +00:00
|
|
|
this.signal.start = onStart
|
|
|
|
this.signal.done = onDone
|
2024-05-28 17:18:54 +00:00
|
|
|
this.signal.pause = async ({ me, amount }) => {
|
|
|
|
if (zapUndoTrigger({ me, amount })) {
|
2024-08-02 22:41:47 +00:00
|
|
|
await zapUndo(this.signal, amount)
|
2024-05-28 17:18:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const zapUndoTrigger = ({ me, amount }) => {
|
|
|
|
if (!me) return false
|
|
|
|
const enabled = me.privates.zapUndos !== null
|
|
|
|
return enabled ? amount >= me.privates.zapUndos : false
|
|
|
|
}
|
|
|
|
|
2024-08-02 22:41:47 +00:00
|
|
|
const zapUndo = async (signal, amount) => {
|
2024-05-28 17:18:54 +00:00
|
|
|
return await new Promise((resolve, reject) => {
|
2024-08-02 22:41:47 +00:00
|
|
|
signal.start(amount)
|
2024-05-28 17:18:54 +00:00
|
|
|
const abortHandler = () => {
|
|
|
|
reject(new ActCanceledError())
|
|
|
|
signal.done()
|
|
|
|
signal.removeEventListener('abort', abortHandler)
|
2023-12-27 02:27:52 +00:00
|
|
|
}
|
2024-05-28 17:18:54 +00:00
|
|
|
signal.addEventListener('abort', abortHandler)
|
|
|
|
setTimeout(() => {
|
|
|
|
resolve()
|
|
|
|
signal.done()
|
|
|
|
signal.removeEventListener('abort', abortHandler)
|
|
|
|
}, ZAP_UNDO_DELAY_MS)
|
2023-12-27 02:27:52 +00:00
|
|
|
})
|
|
|
|
}
|