-
Notifications
You must be signed in to change notification settings - Fork 0
/
hide-message-in-channel.ts
58 lines (54 loc) · 1.77 KB
/
hide-message-in-channel.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
import { useMutation, useQueryClient } from "@tanstack/react-query";
import { Channel, NostrQueries, useNostrPublishMutation } from "../nostr";
import { Kind } from "nostr-tools";
import { ChatQueries } from "../queries";
import { useContext } from "react";
import { ChatContext } from "../chat-context-provider";
interface Payload {
messageId: string;
reason?: string;
status: 0 | 1; //0 is hidden 1 is shown
}
/**
* Use to hide specific user's message by community team member
* @note Only community team member's mute event will be applied in messages query.
* All other event owners will be ignored
* @param channel Current channel
*/
export function useHideMessageInChannel(channel?: Channel) {
const queryClient = useQueryClient();
const { activeUsername } = useContext(ChatContext);
const hideMessageRequest = useNostrPublishMutation(
["chats", "hide-message", channel?.name],
Kind.ChannelHideMessage,
() => {},
);
return useMutation({
mutationKey: ["chats/hide-message-in-channel", channel?.name],
mutationFn: async ({ messageId, reason, status }: Payload) => {
await hideMessageRequest.mutateAsync({
eventMetadata: JSON.stringify({
reason: reason ?? "Hidden by community team",
}),
tags: [
["e", messageId],
["e", channel!.id],
["status", status.toString()],
],
});
return { messageId, status };
},
onSuccess: async () => {
await queryClient.invalidateQueries({
queryKey: [
ChatQueries.HIDDEN_CHANNEL_MESSAGES,
activeUsername,
channel?.id,
],
});
await queryClient.invalidateQueries({
queryKey: [NostrQueries.PUBLIC_MESSAGES, activeUsername, channel?.id],
});
},
});
}