Compare commits

..

2 Commits

Author SHA1 Message Date
ekzyis
7c294478fb
Nostr crossposting backlink + content fix (#1251)
* Add backlink in nostr events

* Remove unnecessary async

* Use itemToContent function

* Fix duplicate title in discussion event
2024-07-03 10:11:24 -05:00
keyan
1a3785a865 only assume insufficient funds in paid action if the error says so 2024-07-03 09:10:04 -05:00
2 changed files with 30 additions and 22 deletions

View File

@ -58,8 +58,8 @@ export default async function performPaidAction (actionType, args, context) {
} catch (e) { } catch (e) {
console.error('fee credit action failed', e) console.error('fee credit action failed', e)
// if we fail to do the action with fee credits, but the cost is 0, we should bail // if we fail with fee credits, but not because of insufficient funds, bail
if (context.cost === 0n) { if (!e.message.includes('\\"users\\" violates check constraint \\"msats_positive\\"')) {
throw e throw e
} }

View File

@ -6,13 +6,29 @@ import { gql, useMutation, useQuery, useLazyQuery } from '@apollo/client'
import { SETTINGS } from '@/fragments/users' import { SETTINGS } from '@/fragments/users'
import { ITEM_FULL_FIELDS, POLL_FIELDS } from '@/fragments/items' import { ITEM_FULL_FIELDS, POLL_FIELDS } from '@/fragments/items'
async function discussionToEvent (item) { function itemToContent (item, { includeTitle = true } = {}) {
let content = includeTitle ? item.title : ''
if (item.url) {
content += `\n${item.url}`
}
if (item.text) {
content += `\n\n${item.text}`
}
content += `\n\noriginally posted at https://stacker.news/items/${item.id}`
return content.trim()
}
function discussionToEvent (item) {
const createdAt = Math.floor(Date.now() / 1000) const createdAt = Math.floor(Date.now() / 1000)
return { return {
created_at: createdAt, created_at: createdAt,
kind: 30023, kind: 30023,
content: item.text, content: itemToContent(item, { includeTitle: false }),
tags: [ tags: [
['d', item.id.toString()], ['d', item.id.toString()],
['title', item.title], ['title', item.title],
@ -21,25 +37,18 @@ async function discussionToEvent (item) {
} }
} }
async function linkToEvent (item) { function linkToEvent (item) {
const createdAt = Math.floor(Date.now() / 1000) const createdAt = Math.floor(Date.now() / 1000)
let contentField
if (item.text) {
contentField = `${item.title}\n${item.url}\n\n${item.text}`
} else {
contentField = `${item.title}\n${item.url}`
}
return { return {
created_at: createdAt, created_at: createdAt,
kind: 1, kind: 1,
content: contentField, content: itemToContent(item),
tags: [] tags: []
} }
} }
async function pollToEvent (item) { function pollToEvent (item) {
const createdAt = Math.floor(Date.now() / 1000) const createdAt = Math.floor(Date.now() / 1000)
const expiresAt = createdAt + 86400 const expiresAt = createdAt + 86400
@ -47,20 +56,20 @@ async function pollToEvent (item) {
return { return {
created_at: createdAt, created_at: createdAt,
kind: 1, kind: 1,
content: item.text, content: itemToContent(item),
tags: [ tags: [
['poll', 'single', expiresAt.toString(), item.title, ...item.poll.options.map(op => op?.option.toString())] ['poll', 'single', expiresAt.toString(), item.title, ...item.poll.options.map(op => op?.option.toString())]
] ]
} }
} }
async function bountyToEvent (item) { function bountyToEvent (item) {
const createdAt = Math.floor(Date.now() / 1000) const createdAt = Math.floor(Date.now() / 1000)
return { return {
created_at: createdAt, created_at: createdAt,
kind: 30402, kind: 30402,
content: item.text, content: itemToContent(item),
tags: [ tags: [
['d', item.id.toString()], ['d', item.id.toString()],
['title', item.title], ['title', item.title],
@ -158,16 +167,15 @@ export default function useCrossposter () {
} }
const itemType = determineItemType(item) const itemType = determineItemType(item)
switch (itemType) { switch (itemType) {
case 'discussion': case 'discussion':
return await discussionToEvent(item) return discussionToEvent(item)
case 'link': case 'link':
return await linkToEvent(item) return linkToEvent(item)
case 'bounty': case 'bounty':
return await bountyToEvent(item) return bountyToEvent(item)
case 'poll': case 'poll':
return await pollToEvent(item) return pollToEvent(item)
default: default:
return crosspostError('Unknown item type') return crosspostError('Unknown item type')
} }