-
Notifications
You must be signed in to change notification settings - Fork 0
/
join-chat.ts
76 lines (69 loc) · 2.25 KB
/
join-chat.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
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { createNoStrAccount, EncryptionTools } from "../utils";
import { NostrQueries, useNostrPublishMutation } from "../nostr";
import { useContext } from "react";
import { Kind } from "nostr-tools";
import { ChatContext } from "../chat-context-provider";
import { useSaveKeys } from "../api";
const crypto = require("crypto");
/**
* Custom React hook for joining a chat with some side effects.
*
* This hook manages the process of joining a chat, resetting chat state, and uploading
* a public key for secure communication.
*
* @param onSuccess A callback function to be called upon successful completion of chat join.
* @param meta A metaobject which could contain any information related to the Nostr keys record
*
* @returns A function from the `useMutation` hook, which can be used to initiate the chat join process.
*/
export function useJoinChat(
onSuccess?: () => void,
meta?: Record<string, unknown>,
) {
const queryClient = useQueryClient();
const { activeUsername, storage } = useContext(ChatContext);
const { mutateAsync: updateProfile } = useNostrPublishMutation(
["chats/update-nostr-profile"],
Kind.Metadata,
() => {},
);
const { mutateAsync: uploadKeys } = useSaveKeys();
return useMutation({
mutationKey: ["chat-join-chat"],
mutationFn: async (pin: string) => {
const keys = createNoStrAccount();
storage?.setItem("ecency_nostr_pr_" + activeUsername, pin);
const initialVector = crypto.randomBytes(16);
const encryptedKey = EncryptionTools.encrypt(
keys.priv,
pin,
initialVector,
);
await uploadKeys({
pubkey: keys.pub,
key: encryptedKey,
iv: initialVector.toString("base64"),
meta: meta ?? {},
});
queryClient.setQueryData(
[NostrQueries.PUBLIC_KEY, activeUsername],
keys.pub,
);
queryClient.setQueryData(
[NostrQueries.PRIVATE_KEY, activeUsername],
keys.priv,
);
await updateProfile({
tags: [["p", keys.pub]],
eventMetadata: {
name: activeUsername!,
about: "",
picture: "",
joinedChannels: [],
},
});
},
onSuccess,
});
}