1
- import Nostr from '@/lib/nostr'
1
+ import Nostr , { getNostrProfile } from '@/lib/nostr'
2
2
import { TwitterApi } from 'twitter-api-v2'
3
3
import { msatsToSats , numWithUnits } from '@/lib/format'
4
4
@@ -40,7 +40,7 @@ const RELAYS = [
40
40
'wss://relay.primal.net/'
41
41
]
42
42
43
- async function postToNostr ( { message } ) {
43
+ async function postToNostr ( { message, postAuthorNostrPubkey } ) {
44
44
if ( ! isProd || ! process . env . NOSTR_PRIVATE_KEY ) {
45
45
console . log ( 'Nostr poster not configured' )
46
46
return
@@ -52,7 +52,7 @@ async function postToNostr ({ message }) {
52
52
await nostr . publish ( {
53
53
created_at : Math . floor ( new Date ( ) . getTime ( ) / 1000 ) ,
54
54
content : message ,
55
- tags : [ ] ,
55
+ tags : postAuthorNostrPubkey ? [ [ 'p' , postAuthorNostrPubkey ] ] : [ ] ,
56
56
kind : 1
57
57
} , {
58
58
relays : RELAYS ,
@@ -92,21 +92,30 @@ async function getHottestItem ({ models }) {
92
92
return item [ 0 ]
93
93
}
94
94
95
- async function itemToMessage ( { item } ) {
95
+ async function itemToMessage ( { item, postAuthorNostrProfile } ) {
96
96
return `${ item . title }
97
97
98
- by ${ item . userName } in ~${ item . subName }
98
+ by ${ item . userName } ${ postAuthorNostrProfile ? `, nostr profile: nostr: ${ postAuthorNostrProfile } ` : '' } in ~${ item . subName }
99
99
${ numWithUnits ( msatsToSats ( item . msats ) , { abbreviate : false } ) } and ${ numWithUnits ( item . ncomments , { abbreviate : false , unitSingular : 'comment' , unitPlural : 'comments' } ) } so far
100
100
101
101
https://stacker.news/items/${ item . id } `
102
102
}
103
103
104
104
export async function postToSocial ( { models } ) {
105
105
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 } )
112
121
}
0 commit comments