Skip to content

Commit 14769be

Browse files
committed
feat(socialPoster): add Nostr pubkey tagging with hideNostr check in social poster
1 parent e401c6f commit 14769be

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

lib/nostr.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,3 +201,13 @@ class NDKNip46SignerURLPatch extends NDKNip46Signer {
201201
return super.connectionTokenInit(connectionToken)
202202
}
203203
}
204+
205+
export function getNostrProfile (pubkey) {
206+
try {
207+
const { data } = nip19.decode(pubkey)
208+
return nip19.nprofileEncode({ pubkey: data })
209+
} catch (err) {
210+
console.error('Error decoding or encoding nostrPubkey:', err)
211+
return null
212+
}
213+
}

lib/user.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ export const isMuted = async ({ models, muterId, mutedId }) => {
1010

1111
return !!mute
1212
}
13+
14+
export const getUserById = async ({ models, userId, fields }) => {
15+
const user = await models.user.findUnique({
16+
where: { id: Number(userId) },
17+
select: fields
18+
})
19+
20+
return user
21+
}

worker/socialPoster.js

Lines changed: 20 additions & 11 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

@@ -40,7 +40,7 @@ const RELAYS = [
4040
'wss://relay.primal.net/'
4141
]
4242

43-
async function postToNostr ({ message }) {
43+
async function postToNostr ({ message, postAuthorNostrPubkey }) {
4444
if (!isProd || !process.env.NOSTR_PRIVATE_KEY) {
4545
console.log('Nostr poster not configured')
4646
return
@@ -52,7 +52,7 @@ async function postToNostr ({ message }) {
5252
await nostr.publish({
5353
created_at: Math.floor(new Date().getTime() / 1000),
5454
content: message,
55-
tags: [],
55+
tags: postAuthorNostrPubkey ? [['p', postAuthorNostrPubkey]] : [],
5656
kind: 1
5757
}, {
5858
relays: RELAYS,
@@ -92,21 +92,30 @@ 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 ${item.userName}${postAuthorNostrProfile ? `, nostr profile: nostr:${postAuthorNostrProfile}` : ''} 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 }
111+
})
112+
const postAuthorNostrPubkey = postAuthor?.nostrPubkey
113+
const postAuthorNostrProfile = postAuthorNostrPubkey
114+
? getNostrProfile(postAuthorNostrPubkey)
115+
: null
116+
117+
const message = await itemToMessage({ item, postAuthorNostrProfile })
118+
console.log('Message:', message)
119+
await postToTwitter({ message })
120+
await postToNostr({ message, postAuthorNostrPubkey })
112121
}

0 commit comments

Comments
 (0)