Skip to content

Commit ea8a28a

Browse files
brymuthuumn
andauthored
feat(socialPoster): add Nostr pubkey tagging with hideNostr check in social poster (#2100)
Co-authored-by: Keyan <[email protected]>
1 parent 4c2f059 commit ea8a28a

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

lib/nostr.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,7 @@ class NDKNip46SignerURLPatch extends NDKNip46Signer {
201201
return super.connectionTokenInit(connectionToken)
202202
}
203203
}
204+
205+
export function getNostrProfile (pubkey) {
206+
return nip19.nprofileEncode({ pubkey })
207+
}

worker/socialPoster.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Nostr from '@/lib/nostr'
1+
import Nostr, { getNostrProfile } from '@/lib/nostr'
22
import { TwitterApi } from 'twitter-api-v2'
33
import { msatsToSats, numWithUnits } from '@/lib/format'
44

@@ -92,21 +92,32 @@ async function getHottestItem ({ models }) {
9292
return item[0]
9393
}
9494

95-
async function itemToMessage ({ item }) {
95+
async function itemToMessage ({ item, postAuthorNostrProfile }) {
9696
return `${item.title}
9797
98-
by ${item.userName} in ~${item.subName}
98+
by ${postAuthorNostrProfile ? `nostr:${postAuthorNostrProfile}` : `${item.userName}`} in ~${item.subName}
9999
${numWithUnits(msatsToSats(item.msats), { abbreviate: false })} and ${numWithUnits(item.ncomments, { abbreviate: false, unitSingular: 'comment', unitPlural: 'comments' })} so far
100100
101101
https://stacker.news/items/${item.id}`
102102
}
103103

104104
export async function postToSocial ({ models }) {
105105
const item = await getHottestItem({ models })
106-
if (item) {
107-
const message = await itemToMessage({ item })
108-
console.log('Message:', message)
109-
await postToTwitter({ message })
110-
await postToNostr({ message })
111-
}
106+
if (!item) return
107+
108+
const postAuthor = await models.user.findUnique({
109+
where: { id: item.userId, hideNostr: false },
110+
select: { nostrPubkey: true, nostrAuthPubkey: true }
111+
})
112+
113+
const nostrKey = postAuthor?.nostrPubkey || postAuthor?.nostrAuthPubkey
114+
const postAuthorNostrProfile = nostrKey ? getNostrProfile(nostrKey) : null
115+
116+
const twitterMessage = await itemToMessage({ item, postAuthorNostrProfile: null })
117+
const nostrMessage = await itemToMessage({ item, postAuthorNostrProfile })
118+
console.log('Twitter Message:', twitterMessage)
119+
console.log('Nostr Message:', nostrMessage)
120+
121+
await postToTwitter({ message: twitterMessage })
122+
await postToNostr({ message: nostrMessage })
112123
}

0 commit comments

Comments
 (0)