add blacklist model and refactor channel kind 43 43

This commit is contained in:
Ren Amamiya 2023-04-25 12:51:56 +07:00
parent cf26aa504e
commit 95e4a27b69
6 changed files with 96 additions and 32 deletions

View File

@ -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)
);

View File

@ -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(),

View File

@ -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]);
}
}
);

View File

@ -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;
}

View File

@ -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}";`);
}

View File

@ -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);