|
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 |
|
@@ -92,21 +92,32 @@ 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 ${postAuthorNostrProfile ? `nostr:${postAuthorNostrProfile}` : `${item.userName}`} 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, 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 }) |
112 | 123 | }
|
0 commit comments