Nostr crossposting backlink + content fix (#1251)

* Add backlink in nostr events

* Remove unnecessary async

* Use itemToContent function

* Fix duplicate title in discussion event
This commit is contained in:
ekzyis 2024-07-03 17:11:24 +02:00 committed by GitHub
parent 1a3785a865
commit 7c294478fb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 28 additions and 20 deletions

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')
} }