stacker.news/components/use-crossposter.js
Austin Kelsay 565e939245
Nostr crossposting all item types (#779)
* crosspost-item

* crosspost old items, update with nEventId

* Updating noteId encoding, cleaning up a little

* Fixing item-info condition, cleaning up

* Linting

* Add createdAt variable back

* Change instances of eventId to noteId

* Adding upsertNoteId mutation

* Cleaning up updateItem, using toasts to communivate success/failure in crosspost-item

* Linting

* Move crosspost to share button, make sure only OP can crosspost

* Lint

* Simplify conditions

* user might have no nostr extension installed

Co-authored-by: ekzyis <27162016+ekzyis@users.noreply.github.com>

* change upsertNoteId to updateNoteID for resolver and mutations, change isOp to mine, remove unused noteId params

* Basic setup for crossposting poll / link items

* post rebase fixes and Bounty and job crossposts

* Job crossposting working

* adding back accidentally removed import

* Lint / rebase

* Outsource as much crossposting logic from discussion-form into use-crossposter as possible

* Fix incorrect property for user relays, fix itemId param in updateNoteId

* Fix toast messages / error cases in use-crossposter

* Update item forms to for updated use-crossposter hook

* CrosspostDropdownItem in share updated to accomodate use-crossposter update

* Encode paramaterized replacable event id's in naddress format with nostr-tools, bounty to follw nip-99 spec

* Increase timeout on relay connection / cleaning up

* No longer crossposting job

* Add blastr, fix crosspost button in item-info for polls/discussions, finish removing job crosspostr code

* Fix toaster error, create reusable crossposterror function to surface toaster

* Cleaning up / comments / linting

* Update copy

* Simplify CrosspostdropdownItem, keep replies from being crossposted

* Moved query for missing item fields when crossposting to use-crossposter hook

* Remove unneeded param in CrosspostDropdownItem, lint

* Small fixes post rebase

* Remove unused import

* fix nostr-tools version, fix package-lock.json

* Update components/item-info.js

Co-authored-by: ekzyis <ek@stacker.news>

* Remove unused param, determine poll item type from pollCost field, add mutiny strfry relay to defaults

* Update toaster implementations, use no-cache for item query, restructure crosspostItem to use await with try catch

* crosspost info modal that lives under adv-post-form now has dynamic crossposting info

* Move determineItemType into handleEventCreation, mover item/event handing outside of do ... while loop

* Lint

* Reconcile skip method with onCancel function in toaster

* Handle failedRelays being undefined

* determine item type from router.query.type if available otherwise use item fields

* Initiliaze failerRelays as undefined but handle error explicitly

* Lint

* Fix crosspost default value for link, poll, bounty forms

---------

Co-authored-by: ekzyis <27162016+ekzyis@users.noreply.github.com>
Co-authored-by: ekzyis <ek@stacker.news>
Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com>
2024-02-21 19:18:36 -06:00

257 lines
6.1 KiB
JavaScript

import { useCallback } from 'react'
import { useToast } from './toast'
import { Button } from 'react-bootstrap'
import { DEFAULT_CROSSPOSTING_RELAYS, crosspost, callWithTimeout } from '../lib/nostr'
import { gql, useMutation, useQuery, useLazyQuery } from '@apollo/client'
import { SETTINGS } from '../fragments/users'
import { ITEM_FULL_FIELDS, POLL_FIELDS } from '../fragments/items'
async function discussionToEvent (item) {
const createdAt = Math.floor(Date.now() / 1000)
return {
created_at: createdAt,
kind: 30023,
content: item.text,
tags: [
['d', item.id.toString()],
['title', item.title],
['published_at', createdAt.toString()]
]
}
}
async function linkToEvent (item) {
const createdAt = Math.floor(Date.now() / 1000)
return {
created_at: createdAt,
kind: 1,
content: `${item.title} \n ${item.url}`,
tags: []
}
}
async function pollToEvent (item) {
const createdAt = Math.floor(Date.now() / 1000)
const expiresAt = createdAt + 86400
return {
created_at: createdAt,
kind: 1,
content: item.text,
tags: [
['poll', 'single', expiresAt.toString(), item.title, ...item.poll.options.map(op => op?.option.toString())]
]
}
}
async function bountyToEvent (item) {
const createdAt = Math.floor(Date.now() / 1000)
return {
created_at: createdAt,
kind: 30402,
content: item.text,
tags: [
['d', item.id.toString()],
['title', item.title],
['location', `https://stacker.news/items/${item.id}`],
['price', item.bounty.toString(), 'SATS'],
['t', 'bounty'],
['published_at', createdAt.toString()]
]
}
}
export default function useCrossposter () {
const toaster = useToast()
const { data } = useQuery(SETTINGS)
const userRelays = data?.settings?.privates?.nostrRelays || []
const relays = [...DEFAULT_CROSSPOSTING_RELAYS, ...userRelays]
const [fetchItem] = useLazyQuery(
gql`
${ITEM_FULL_FIELDS}
${POLL_FIELDS}
query Item($id: ID!) {
item(id: $id) {
...ItemFullFields
...PollFields
}
}`, {
fetchPolicy: 'no-cache'
}
)
const [updateNoteId] = useMutation(
gql`
mutation updateNoteId($id: ID!, $noteId: String!) {
updateNoteId(id: $id, noteId: $noteId) {
id
noteId
}
}`
)
const relayError = (failedRelays) => {
return new Promise(resolve => {
const handleSkip = () => {
resolve('skip')
removeToast()
}
const removeToast = toaster.danger(
<>
Crossposting failed for {failedRelays.join(', ')} <br />
<Button
variant='link' onClick={() => {
resolve('retry')
setTimeout(() => {
removeToast()
}, 1000)
}}
>Retry
</Button>
{' | '}
<Button
variant='link' onClick={handleSkip}
>Skip
</Button>
</>,
{
onCancel: () => handleSkip()
}
)
})
}
const crosspostError = (errorMessage) => {
return toaster.danger(`Error crossposting: ${errorMessage}`)
}
async function handleEventCreation (item) {
const determineItemType = (item) => {
const typeMap = {
url: 'link',
bounty: 'bounty',
pollCost: 'poll'
}
for (const [key, type] of Object.entries(typeMap)) {
if (item[key]) {
return type
}
}
// Default
return 'discussion'
}
const itemType = determineItemType(item)
switch (itemType) {
case 'discussion':
return await discussionToEvent(item)
case 'link':
return await linkToEvent(item)
case 'bounty':
return await bountyToEvent(item)
case 'poll':
return await pollToEvent(item)
default:
return crosspostError('Unknown item type')
}
}
const fetchItemData = async (itemId) => {
try {
const { data } = await fetchItem({ variables: { id: itemId } })
return data?.item
} catch (e) {
console.error(e)
return null
}
}
const crosspostItem = async item => {
let failedRelays
let allSuccessful = false
let noteId
const event = await handleEventCreation(item)
if (!event) return { allSuccessful, noteId }
do {
try {
const result = await crosspost(event, failedRelays || relays)
if (result.error) {
failedRelays = []
throw new Error(result.error)
}
noteId = result.noteId
failedRelays = result?.failedRelays?.map(relayObj => relayObj.relay) || []
if (failedRelays.length > 0) {
const userAction = await relayError(failedRelays)
if (userAction === 'skip') {
toaster.success('Skipped failed relays.')
// wait 2 seconds then break
await new Promise(resolve => setTimeout(resolve, 2000))
break
}
} else {
allSuccessful = true
}
} catch (error) {
await crosspostError(error.message)
// wait 2 seconds to show error then break
await new Promise(resolve => setTimeout(resolve, 2000))
return { allSuccessful, noteId }
}
} while (failedRelays.length > 0)
return { allSuccessful, noteId }
}
const handleCrosspost = useCallback(async (itemId) => {
try {
const pubkey = await callWithTimeout(() => window.nostr.getPublicKey(), 10000)
if (!pubkey) throw new Error('failed to get pubkey')
} catch (e) {
throw new Error(`Nostr extension error: ${e.message}`)
}
let noteId
try {
if (itemId) {
const item = await fetchItemData(itemId)
const crosspostResult = await crosspostItem(item)
noteId = crosspostResult?.noteId
if (noteId) {
await updateNoteId({
variables: {
id: itemId,
noteId
}
})
}
}
} catch (e) {
console.error(e)
await crosspostError(e.message)
}
}, [updateNoteId, relays, toaster])
return handleCrosspost
}