Skip to content

feat(socialPoster): add Nostr pubkey tagging with hideNostr check in … #2100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/nostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ class NDKNip46SignerURLPatch extends NDKNip46Signer {
return super.connectionTokenInit(connectionToken)
}
}

export function getNostrProfile (pubkey) {
return nip19.nprofileEncode({ pubkey })
}
29 changes: 20 additions & 9 deletions worker/socialPoster.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Nostr from '@/lib/nostr'
import Nostr, { getNostrProfile } from '@/lib/nostr'
import { TwitterApi } from 'twitter-api-v2'
import { msatsToSats, numWithUnits } from '@/lib/format'

Expand Down Expand Up @@ -92,21 +92,32 @@ async function getHottestItem ({ models }) {
return item[0]
}

async function itemToMessage ({ item }) {
async function itemToMessage ({ item, postAuthorNostrProfile }) {
return `${item.title}

by ${item.userName} in ~${item.subName}
by ${postAuthorNostrProfile ? `nostr:${postAuthorNostrProfile}` : `${item.userName}`} in ~${item.subName}
${numWithUnits(msatsToSats(item.msats), { abbreviate: false })} and ${numWithUnits(item.ncomments, { abbreviate: false, unitSingular: 'comment', unitPlural: 'comments' })} so far

https://stacker.news/items/${item.id}`
}

export async function postToSocial ({ models }) {
const item = await getHottestItem({ models })
if (item) {
const message = await itemToMessage({ item })
console.log('Message:', message)
await postToTwitter({ message })
await postToNostr({ message })
}
if (!item) return

const postAuthor = await models.user.findUnique({
where: { id: item.userId, hideNostr: false },
select: { nostrPubkey: true, nostrAuthPubkey: true }
})

const nostrKey = postAuthor?.nostrPubkey || postAuthor?.nostrAuthPubkey
const postAuthorNostrProfile = nostrKey ? getNostrProfile(nostrKey) : null

const twitterMessage = await itemToMessage({ item, postAuthorNostrProfile: null })
const nostrMessage = await itemToMessage({ item, postAuthorNostrProfile })
console.log('Twitter Message:', twitterMessage)
console.log('Nostr Message:', nostrMessage)

await postToTwitter({ message: twitterMessage })
await postToNostr({ message: nostrMessage })
}