-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
232 lines (202 loc) · 6.49 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import '@logseq/libs'
import { format } from 'date-fns'
import { generatePrivateKey, getPublicKey, nip19, relayInit, nip04, nip42, finishEvent } from 'nostr-tools'
import { NAV_BAR_ICON, PLUGIN_NAMESPACE, RELAY_LIST, UUID_SEED } from './constants'
import { v5 as uuidv5 } from 'uuid'
import { AppUserConfigs, PageEntity, SettingSchemaDesc } from '@logseq/libs/dist/LSPlugin.user'
const delay = async (t = 100): Promise<void> => {
await new Promise((resolve) => setTimeout(resolve, t))
}
let config: AppUserConfigs
const settingsTemplate: SettingSchemaDesc[] = [
{
key: "nostrSyncPrivateKey",
type: "string",
default: "",
title: "Your Logseq private key",
description:
"Should follow nsec format",
},
{
key: "nostrSyncRelays",
type: "string",
default: '[]',
title: "Relays",
description: 'Should follow JSON array format'
},
{
key: "nostrSyncAllowedPubkey",
type: "string",
default: '',
title: "Allowed Pubkey",
description: 'Will only fetch messages from this Pubkey. If left empty, will fetch from any source.'
},
]
logseq.useSettingsSchema(settingsTemplate)
const getJournalPage = async (unixtime: number): Promise<PageEntity | null> => {
const journalName = format(new Date(unixtime * 1000), config.preferredDateFormat)
const page = logseq.Editor.getPage(journalName)
if (page === null) {
await logseq.Editor.createPage(
journalName,
{},
{
createFirstBlock: true,
redirect: false,
journal: true
}
)
}
return await page
}
const getDecodedSecretKey: () => Promise<string> = async () => {
const nsecDecoded = await nip19.decode(logseq.settings?.nostrSyncPrivateKey)
if (nsecDecoded.type === 'nsec') {
return nsecDecoded.data as string
}
return ''
}
const syncRelay = async (relayUrl: string): Promise<void> => {
const relay = relayInit(`wss://${relayUrl}`)
relay.on('connect', () => {
logseq.UI.showMsg(`connected to ${relay.url}`, 'success')
})
relay.on('error', () => {
logseq.UI.showMsg(`failed to connect to ${relay.url}`, 'warning')
})
await relay.connect()
await delay(1000)
const secretKey = await getDecodedSecretKey()
const publicKey = getPublicKey(secretKey)
config = await logseq.App.getUserConfigs()
const pets = [publicKey]
if (logseq.settings?.nostrSyncAllowedPubkey !== '') {
const allowedPubKey = nip19.decode(logseq.settings?.nostrSyncAllowedPubkey)
if (allowedPubKey.type === 'npub') {
pets.push(allowedPubKey.data as string)
} else if (allowedPubKey.type === 'nprofile') {
pets.push(allowedPubKey.data.pubkey as string)
}
}
relay.on('auth', (challenge) => {
nip42.authenticate({ relay, sign: (e) => finishEvent(e, secretKey), challenge })
})
await delay(500)
const sub = relay.sub([
{
kinds: [4],
'#p': pets
}
])
sub.on('event', async (event) => {
try {
const message = await nip04.decrypt(
await getDecodedSecretKey(),
event.pubkey,
event.content
)
const page = await getJournalPage(event.created_at)
if (page !== null) {
const customUUID: string = uuidv5(event.id, UUID_SEED)
const existingBlock = await logseq.Editor.getBlock(customUUID)
if (existingBlock === null && page.uuid !== null) {
await logseq.Editor.insertBlock(page.uuid, `${message} #${PLUGIN_NAMESPACE}`, {
before: true,
customUUID
})
}
} else {
logseq.UI.showMsg('Journal not found', 'warning')
}
} catch (e: unknown) {
logseq.UI.showMsg(e.toString(), 'warning')
console.error(e)
}
})
sub.on('eose', () => {
sub.unsub()
})
await delay(10000)
}
const setup = async (): Promise<void> => {
const targetPage = await logseq.Editor.createPage(PLUGIN_NAMESPACE)
if (targetPage === null) {
logseq.UI.showMsg('Page error', 'warning')
return
} else {
logseq.App.pushState('page', targetPage)
}
const pageBlocksTree = await logseq.Editor.getCurrentPageBlocksTree()
let tagetBlockUuid = pageBlocksTree[0]?.uuid
const content = '🚀 Generating PubKey ...'
if (tagetBlockUuid !== undefined) {
await logseq.Editor.updateBlock(tagetBlockUuid, content)
} else {
const newBlock = await logseq.Editor.insertBlock(targetPage.name, content, { before: true })
tagetBlockUuid = newBlock?.uuid ?? tagetBlockUuid
}
const privateKey = generatePrivateKey()
const relays: string[] = []
while (relays.length < 4) {
const randomPosition = Math.floor(Math.random() * RELAY_LIST.length)
const relayUrl = RELAY_LIST[randomPosition]
if (!relays.includes(relayUrl)) {
relays.push(relayUrl)
}
}
const publicKey = getPublicKey(privateKey)
const nostrNsec = nip19.nsecEncode(privateKey)
const nostrNpub = nip19.nprofileEncode({ pubkey: publicKey, relays })
logseq.updateSettings({ nostrSyncPrivateKey: nostrNsec, nostrSyncRelays: JSON.stringify(relays) })
if (publicKey !== null) {
await logseq.Editor.updateBlock(tagetBlockUuid, 'This is the public key of your Logseq client:')
await logseq.Editor.insertBlock(targetPage.name, nostrNpub, { before: true })
await logseq.Editor.insertBlock(
targetPage.name,
'All private messages sent to this public key will be downloaded to Logseq.',
{ before: true }
)
await logseq.Editor.insertBlock(
targetPage.name,
'⚠️ This generated private key is NOT securely stored:',
{ before: true }
)
await logseq.Editor.insertBlock(targetPage.name, nostrNsec, { before: true })
}
}
/**
* main entry
* @param baseInfo
*/
const main = (): void => {
logseq.provideModel({
async syncNostr () {
try {
if (logseq.settings?.nostrSyncPrivateKey !== '') {
logseq.UI.showMsg('Connecting', 'info')
const relays = JSON.parse(logseq.settings?.nostrSyncRelays)
if (relays !== undefined && relays.length > 0) {
relays.forEach((name: string) => {
syncRelay(name).catch((e) => {
logseq.UI.showMsg(e.toString(), 'warning')
})
})
}
} else {
setup().catch((e) => {
logseq.UI.showMsg(e.toString(), 'warning')
})
}
} catch (e: unknown) {
logseq.UI.showMsg(e.toString(), 'warning')
console.error(e)
}
}
})
logseq.App.registerUIItem('toolbar', {
key: 'logseq-nostr',
template: NAV_BAR_ICON
})
}
// bootstrap
logseq.ready(main).catch(console.error)