add blacklist model and refactor channel kind 43 43
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { AccountContext } from '@components/accountProvider';
|
||||
import { ChannelProfile } from '@components/channels/channelProfile';
|
||||
import { FormChannel } from '@components/form/channel';
|
||||
import NewsfeedLayout from '@components/layouts/newsfeed';
|
||||
@@ -9,6 +8,7 @@ import { MESSAGE_RELAYS } from '@stores/constants';
|
||||
|
||||
import { dateToUnix, hoursAgo } from '@utils/getDate';
|
||||
import { usePageContext } from '@utils/hooks/usePageContext';
|
||||
import { arrayObjToPureArr } from '@utils/transform';
|
||||
|
||||
import { EyeClose, MicMute } from 'iconoir-react';
|
||||
import { useSetAtom } from 'jotai';
|
||||
@@ -18,6 +18,16 @@ import useSWRSubscription from 'swr/subscription';
|
||||
|
||||
const ChannelMessages = lazy(() => import('@components/channels/messages'));
|
||||
|
||||
let mutedList: any = [];
|
||||
let hidedList: any = [];
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
const { getBlacklist, getActiveAccount } = await import('@utils/storage');
|
||||
const activeAccount = await getActiveAccount();
|
||||
hidedList = await getBlacklist(activeAccount.id, 43);
|
||||
mutedList = await getBlacklist(activeAccount.id, 44);
|
||||
}
|
||||
|
||||
export function Page() {
|
||||
const pageContext = usePageContext();
|
||||
const searchParams: any = pageContext.urlParsed.search;
|
||||
@@ -26,15 +36,14 @@ export function Page() {
|
||||
const channelPubkey = searchParams.pubkey;
|
||||
|
||||
const pool: any = useContext(RelayContext);
|
||||
const activeAccount: any = useContext(AccountContext);
|
||||
|
||||
const setChannelMessages = useSetAtom(channelMessagesAtom);
|
||||
const resetChannelMessages = useResetAtom(channelMessagesAtom);
|
||||
const resetChannelReply = useResetAtom(channelReplyAtom);
|
||||
|
||||
const now = useRef(new Date());
|
||||
const muted = useRef(new Set());
|
||||
const hided = useRef(new Set());
|
||||
const hided = arrayObjToPureArr(hidedList);
|
||||
const muted = arrayObjToPureArr(mutedList);
|
||||
|
||||
useSWRSubscription(id, () => {
|
||||
// reset channel reply
|
||||
@@ -44,11 +53,6 @@ export function Page() {
|
||||
// subscribe for new messages
|
||||
const unsubscribe = pool.subscribe(
|
||||
[
|
||||
{
|
||||
authors: [activeAccount.pubkey],
|
||||
kinds: [43, 44],
|
||||
since: dateToUnix(hoursAgo(48, now.current)),
|
||||
},
|
||||
{
|
||||
'#e': [id],
|
||||
kinds: [42],
|
||||
@@ -57,18 +61,12 @@ export function Page() {
|
||||
],
|
||||
MESSAGE_RELAYS,
|
||||
(event: { kind: number; tags: string[][]; pubkey: string; id: string }) => {
|
||||
if (event.kind === 44) {
|
||||
muted.current = muted.current.add(event.tags[0][1]);
|
||||
} else if (event.kind === 43) {
|
||||
hided.current = hided.current.add(event.tags[0][1]);
|
||||
if (muted.includes(event.pubkey)) {
|
||||
console.log('muted');
|
||||
} else if (hided.includes(event.id)) {
|
||||
console.log('hided');
|
||||
} else {
|
||||
if (muted.current.has(event.pubkey)) {
|
||||
console.log('muted');
|
||||
} else if (hided.current.has(event.id)) {
|
||||
console.log('hided');
|
||||
} else {
|
||||
setChannelMessages((prev) => [...prev, event]);
|
||||
}
|
||||
setChannelMessages((prev) => [...prev, event]);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { RelayContext } from '@components/relaysProvider';
|
||||
|
||||
import { DEFAULT_RELAYS } from '@stores/constants';
|
||||
import { MESSAGE_RELAYS } from '@stores/constants';
|
||||
|
||||
import { dateToUnix, hoursAgo } from '@utils/getDate';
|
||||
import {
|
||||
addToBlacklist,
|
||||
countTotalNotes,
|
||||
createChat,
|
||||
createNote,
|
||||
@@ -29,13 +30,11 @@ export function Page() {
|
||||
const lastLogin = await getLastLogin();
|
||||
const notes = await countTotalNotes();
|
||||
|
||||
const chats = account.chats?.length || 0;
|
||||
const follows = JSON.parse(tags);
|
||||
const query = [];
|
||||
|
||||
let since: number;
|
||||
|
||||
// kind 1 (notes) query
|
||||
if (notes.total === 0) {
|
||||
since = dateToUnix(hoursAgo(24, now.current));
|
||||
} else {
|
||||
@@ -45,6 +44,8 @@ export function Page() {
|
||||
since = dateToUnix(hoursAgo(24, now.current));
|
||||
}
|
||||
}
|
||||
|
||||
// kind 1 (notes) query
|
||||
query.push({
|
||||
kinds: [1, 6],
|
||||
authors: follows,
|
||||
@@ -52,18 +53,23 @@ export function Page() {
|
||||
until: dateToUnix(now.current),
|
||||
});
|
||||
// kind 4 (chats) query
|
||||
if (chats === 0) {
|
||||
query.push({
|
||||
kinds: [4],
|
||||
'#p': [account.pubkey],
|
||||
since: 0,
|
||||
until: dateToUnix(now.current),
|
||||
});
|
||||
}
|
||||
query.push({
|
||||
kinds: [4],
|
||||
'#p': [account.pubkey],
|
||||
since: 0,
|
||||
until: dateToUnix(now.current),
|
||||
});
|
||||
// kind 43, 43 (mute user, hide message) query
|
||||
query.push({
|
||||
authors: [account.pubkey],
|
||||
kinds: [43, 44],
|
||||
since: 0,
|
||||
until: dateToUnix(now.current),
|
||||
});
|
||||
// subscribe relays
|
||||
const unsubscribe = pool.subscribe(
|
||||
query,
|
||||
DEFAULT_RELAYS,
|
||||
MESSAGE_RELAYS,
|
||||
(event: { kind: number; tags: string[]; id: string; pubkey: string; content: string; created_at: number }) => {
|
||||
switch (event.kind) {
|
||||
// short text note
|
||||
@@ -100,6 +106,16 @@ export function Page() {
|
||||
''
|
||||
);
|
||||
break;
|
||||
// hide message (channel only)
|
||||
case 43:
|
||||
if (event.tags[0][0] === 'e') {
|
||||
addToBlacklist(account.id, event.tags[0][1], 43, 1);
|
||||
}
|
||||
// mute user (channel only)
|
||||
case 44:
|
||||
if (event.tags[0][0] === 'p') {
|
||||
addToBlacklist(account.id, event.tags[0][1], 44, 1);
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user