From 95e4a27b699fec8839467fe2435371da5eada1e7 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Tue, 25 Apr 2023 12:51:56 +0700 Subject: [PATCH] add blacklist model and refactor channel kind 43 43 --- .../20230425050745_add_blacklist_model.sql | 11 +++++ src-tauri/src/main.rs | 6 +++ src/pages/channel/index.page.tsx | 38 +++++++++--------- src/pages/index.page.tsx | 40 +++++++++++++------ src/utils/storage.tsx | 23 +++++++++++ src/utils/transform.tsx | 10 +++++ 6 files changed, 96 insertions(+), 32 deletions(-) create mode 100644 src-tauri/migrations/20230425050745_add_blacklist_model.sql diff --git a/src-tauri/migrations/20230425050745_add_blacklist_model.sql b/src-tauri/migrations/20230425050745_add_blacklist_model.sql new file mode 100644 index 00000000..67b25c7a --- /dev/null +++ b/src-tauri/migrations/20230425050745_add_blacklist_model.sql @@ -0,0 +1,11 @@ +-- Add migration script here +-- create blacklist table +CREATE TABLE + blacklist ( + id INTEGER NOT NULL PRIMARY KEY, + account_id INTEGER NOT NULL, + content TEXT NOT NULL UNIQUE, + status INTEGER NOT NULL DEFAULT 0, + kind INTEGER NOT NULL, + FOREIGN KEY (account_id) REFERENCES accounts (id) + ); \ No newline at end of file diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 8c2a1942..b4c1185b 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -81,6 +81,12 @@ fn main() { sql: include_str!("../migrations/20230425024708_add_default_channels.sql"), kind: MigrationKind::Up, }, + Migration { + version: 20230425050745, + description: "create blacklist", + sql: include_str!("../migrations/20230425050745_add_blacklist_model.sql"), + kind: MigrationKind::Up, + }, ], ) .build(), diff --git a/src/pages/channel/index.page.tsx b/src/pages/channel/index.page.tsx index 9535a7d5..5fbd9e2a 100644 --- a/src/pages/channel/index.page.tsx +++ b/src/pages/channel/index.page.tsx @@ -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]); } } ); diff --git a/src/pages/index.page.tsx b/src/pages/index.page.tsx index b1367642..93465d77 100644 --- a/src/pages/index.page.tsx +++ b/src/pages/index.page.tsx @@ -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; } diff --git a/src/utils/storage.tsx b/src/utils/storage.tsx index a7bb29f5..4f308e50 100644 --- a/src/utils/storage.tsx +++ b/src/utils/storage.tsx @@ -170,3 +170,26 @@ export async function updateLastLogin(value: number) { const db = await connect(); return await db.execute(`UPDATE settings SET value = ${value} WHERE key = "last_login";`); } + +// get blacklist by kind and account id +export async function getBlacklist(account_id: number, kind: number) { + const db = await connect(); + return await db.select(`SELECT content FROM blacklist WHERE account_id = "${account_id}" AND kind = "${kind}";`); +} + +// add to blacklist +export async function addToBlacklist(account_id: number, content: string, kind: number, status?: number) { + const db = await connect(); + return await db.execute('INSERT OR IGNORE INTO blacklist (account_id, content, kind, status) VALUES (?, ?, ?, ?);', [ + account_id, + content, + kind, + status || 1, + ]); +} + +// update item in blacklist +export async function updateItemInBlacklist(content: string, status: number) { + const db = await connect(); + return await db.execute(`UPDATE blacklist SET status = "${status}" WHERE content = "${content}";`); +} diff --git a/src/utils/transform.tsx b/src/utils/transform.tsx index a2cf3921..1dd1d51e 100644 --- a/src/utils/transform.tsx +++ b/src/utils/transform.tsx @@ -20,6 +20,16 @@ export const arrayToNIP02 = (arr: string[]) => { return nip02_arr; }; +// convert array object to pure array +export const arrayObjToPureArr = (arr: any) => { + const pure_arr = []; + arr.forEach((item) => { + pure_arr.push(item.content); + }); + + return pure_arr; +}; + // get parent id from event tags export const getParentID = (arr: string[], fallback: string) => { const tags = destr(arr);