-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathMessageEditor.vue
More file actions
318 lines (288 loc) · 8.58 KB
/
MessageEditor.vue
File metadata and controls
318 lines (288 loc) · 8.58 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<template>
<div class="message-editor">
<div class="input-section" @click="$refs.textarea.focus()">
<AutoSizeTextarea
v-model="content" ref="textarea" :placeholder="placeholder" :disabled="publishing" :rows="1"
@submit="publishMessage" submit-on-enter />
<div class="inline-controls">
<div class="inline-controls-item">
<BaseIcon icon="emoji" />
<q-menu ref="menuEmojiPicker">
<EmojiPicker @select="onEmojiSelected" />
</q-menu>
</div>
</div>
</div>
<div class="controls">
<div class="controls-submit">
<q-btn icon="send" :loading="publishing" :disable="!hasContent()" color="primary" @click="publishMessage" round />
</div>
</div>
</div>
</template>
<script>
import BaseIcon from 'components/BaseIcon/index.vue'
import EmojiPicker from 'components/CreatePost/EmojiPicker.vue'
import AutoSizeTextarea from 'components/CreatePost/AutoSizeTextarea.vue'
import { useAppStore } from 'stores/App'
import { useNostrStore } from 'src/nostr/NostrStore'
import EventBuilder from 'src/nostr/EventBuilder'
import Event from 'src/nostr/model/Event'
import { $t } from 'src/boot/i18n'
import { Account } from 'src/nostr/Account'
import { generatePrivateKey, getPublicKey } from 'nostr-tools'
export default {
name: 'MessageEditor',
components: {
AutoSizeTextarea,
BaseIcon,
EmojiPicker,
},
props: {
recipient: {
type: String,
required: true,
},
placeholder: {
type: String,
default: 'Message',
},
autofocus: {
type: Boolean,
default: false,
},
},
emits: ['publish'],
data() {
return {
content: '',
publishing: false,
}
},
setup() {
return {
app: useAppStore(),
nostr: useNostrStore(),
}
},
methods: {
hasContent() {
return this.content.trim().length > 0
},
onEmojiSelected(emoji) {
if (emoji.native) {
this.$refs.menuEmojiPicker.hide()
this.$refs.textarea.insertText(emoji.native)
}
},
focus() {
this.$refs.textarea.focus()
},
reset() {
this.content = ''
},
async publishMessage() {
this.publishing = true
// Save the content property, as it's subject to be reset
const content = this.content
let skConversation = window.localStorage.getItem(this.recipient)
let pkConversation = skConversation && getPublicKey(skConversation)
// If we haven't generated a conversation sk/pk we should now
// and then send our handshake message
if (!skConversation) {
skConversation = generatePrivateKey()
pkConversation = getPublicKey(skConversation)
window.localStorage.setItem(this.recipient, skConversation)
// Subscribe to the conversation
const subConv = window.clientSubscribe({
kinds: [4],
authors: [pkConversation],
limit: 0,
}, `dm:${Date.now()}`)
subConv.on('event', async event => {
console.log('CONVERSATION EVENT', event)
const account = new Account({
pubkey: pkConversation,
privkey: skConversation
})
let plaintext = await account.decrypt(
this.recipient,
event.content
)
if (plaintext) {
const event2 = new Event(JSON.parse(plaintext))
window.hiPhilipp(event2)
}
})
// The content body of the handshake message is a JSON
// object with three properties {"pubkey":"", "convkey":"", "sig":""}
// with the real pubkey of the user trying to message you,
// the conversation pubkey where the messages are being posted to,
// and a signature for a fake event that looks as follows:
const handshakeEvent = new EventBuilder({
kind: 0,
pubkey: this.app.myPubkey,
content: '',
tags: [['p', pkConversation]]
}).build()
handshakeEvent.created_at = 0 // Override default created_at
if (!(await this.app.signEvent(handshakeEvent))) return
console.log('GENERATED HANDSHAKE', handshakeEvent)
const handshakeMessage = JSON.stringify({
pubkey: this.app.myPubkey,
convkey: pkConversation,
sig: handshakeEvent.sig
})
// The handshake event is signed with a one-time-use pubkey/privkey
// pair
const skHandshake = generatePrivateKey()
const pkHandshake = getPublicKey(skHandshake)
const handshakeAccount = new Account({
pubkey: pkHandshake,
privkey: skHandshake
})
const ciphertext = await handshakeAccount.encrypt(
this.recipient,
`INCOGNITO DIRECT MESSAGE\n\nYour client doesn't support incognito direct messages\n\n---------\n${handshakeMessage}`
)
if (!ciphertext) return
const event = EventBuilder.message(
pkHandshake,
this.recipient,
ciphertext
).build()
if (!(await handshakeAccount.sign(event))) return
console.log('Handshake event:', event)
if (await this.nostr.publish(event)) {
this.reset()
this.$nextTick(this.focus.bind(this))
this.$emit('publish', event)
} else {
this.$q.notify({
message: $t(`Failed to send message`),
color: 'negative',
})
}
} else {
pkConversation = getPublicKey(skConversation)
}
// Send our message using the conversation key to a random key
{
// All private messages have an outer "decoy" shell NIP-04
// event with the conversation pubkey as sender and a random
// recipient.
// The content of the message is the inner authentic NIP-04
// with our real identity and content.
// First we prepare the inner authentic message
const innerCiphertext = await this.app.activeAccount.encrypt(
this.recipient,
content
)
const innerEvent = EventBuilder.message(
this.app.myPubkey,
this.recipient,
innerCiphertext
).build()
if (!(await this.app.activeAccount.sign(innerEvent))) return
// Next, we prepare the outer message
const conversationAccount = new Account({
pubkey: pkConversation,
privkey: skConversation
})
const outerCiphertext = await conversationAccount.encrypt(
this.recipient,
JSON.stringify(innerEvent)
)
console.log('Inner event:', innerEvent)
const randomRecipient = generatePrivateKey()
const outerEvent = EventBuilder.message(
pkConversation,
randomRecipient,
outerCiphertext
).build()
if (!(await conversationAccount.sign(outerEvent))) return
console.log('Outer event:', outerEvent)
if (await this.nostr.publish(outerEvent)) {
this.reset()
this.$nextTick(this.focus.bind(this))
this.$emit('publish', outerEvent)
} else {
this.$q.notify({
message: $t(`Failed to send message`),
color: 'negative',
})
}
}
this.publishing = false
},
},
mounted() {
if (this.autofocus) {
this.$nextTick(this.focus.bind(this))
}
},
}
</script>
<style lang="scss" scoped>
@import "assets/theme/colors.scss";
.message-editor {
display: flex;
align-items: flex-end;
padding: 0 1rem;
width: 100%;
.input-section {
width: 100%;
background-color: rgba($color: $color-dark-gray, $alpha: 0.2);
border-radius: 1rem;
position: relative;
padding: 12px 36px 12px 1rem;
margin-right: 0.5rem;
textarea {
display: block;
width: 100%;
max-height: 10rem;
line-height: 18px;
padding: 0;
overflow: hidden;
background-color: transparent;
color: #fff;
appearance: none;
-webkit-appearance: none;
resize: none;
border: none;
&:focus {
border: none;
outline: none;
}
}
}
.inline-controls {
position: absolute;
right: 4px;
bottom: 5px;
&-item {
width: 32px;
height: 32px;
border-radius: 999px;
cursor: pointer;
padding: 5px;
svg {
width: 100%;
fill: $color-primary;
}
&:hover {
background-color: rgba($color: $color-primary, $alpha: 0.3);
}
}
}
.controls {}
}
</style>
<style lang="scss">
@import "assets/theme/colors.scss";
.message-editor {
.controls .controls-submit i.q-icon {
margin-left: 4px;
}
}
</style>