From 730f3be01666f7461fe6748aee17319c247fc6e0 Mon Sep 17 00:00:00 2001 From: Ren Amamiya <123083837+reyamir@users.noreply.github.com> Date: Sun, 7 May 2023 11:13:46 +0700 Subject: [PATCH] add kind 30023 to initial data --- src/app/inital-data/pages/index.page.tsx | 46 +++++++++++++++++++++--- src/app/note/components/user/default.tsx | 3 +- src/app/note/components/user/repost.tsx | 5 +-- src/utils/storage.tsx | 26 ++++++++++++-- 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/src/app/inital-data/pages/index.page.tsx b/src/app/inital-data/pages/index.page.tsx index d401bd22..98f71802 100644 --- a/src/app/inital-data/pages/index.page.tsx +++ b/src/app/inital-data/pages/index.page.tsx @@ -4,6 +4,7 @@ import { READONLY_RELAYS } from '@lume/stores/constants'; import { dateToUnix, hoursAgo } from '@lume/utils/getDate'; import { addToBlacklist, + countTotalLongNotes, countTotalNotes, createChat, createNote, @@ -28,19 +29,31 @@ export function Page() { const account = await getActiveAccount(); const lastLogin = await getLastLogin(); const notes = await countTotalNotes(); + const longNotes = await countTotalLongNotes(); const follows = nip02ToArray(JSON.parse(account.follows)); const query = []; - let since: number; + let sinceNotes: number; + let sinceLongNotes: number; if (notes === 0) { - since = dateToUnix(hoursAgo(24, now.current)); + sinceNotes = dateToUnix(hoursAgo(48, now.current)); } else { if (parseInt(lastLogin) > 0) { - since = parseInt(lastLogin); + sinceNotes = parseInt(lastLogin); } else { - since = dateToUnix(hoursAgo(24, now.current)); + sinceNotes = dateToUnix(hoursAgo(48, now.current)); + } + } + + if (longNotes === 0) { + sinceLongNotes = 0; + } else { + if (parseInt(lastLogin) > 0) { + sinceLongNotes = parseInt(lastLogin); + } else { + sinceLongNotes = 0; } } @@ -48,7 +61,7 @@ export function Page() { query.push({ kinds: [1, 6], authors: follows, - since: since, + since: sinceNotes, until: dateToUnix(now.current), }); @@ -68,6 +81,13 @@ export function Page() { until: dateToUnix(now.current), }); + // kind 30023 (long post) query + query.push({ + kinds: [30023], + since: sinceLongNotes, + until: dateToUnix(now.current), + }); + // subscribe relays unsubscribe = pool.subscribe( query, @@ -113,11 +133,27 @@ export function Page() { if (event.tags[0][0] === 'e') { addToBlacklist(account.id, event.tags[0][1], 43, 1); } + break; // mute user (channel only) case 44: if (event.tags[0][0] === 'p') { addToBlacklist(account.id, event.tags[0][1], 44, 1); } + break; + // long post + case 30023: + // insert event to local database + createNote( + event.id, + account.id, + event.pubkey, + event.kind, + event.tags, + event.content, + event.created_at, + '' + ); + break; default: break; } diff --git a/src/app/note/components/user/default.tsx b/src/app/note/components/user/default.tsx index fea5523d..167b6711 100644 --- a/src/app/note/components/user/default.tsx +++ b/src/app/note/components/user/default.tsx @@ -5,6 +5,7 @@ import { shortenKey } from '@lume/utils/shortenKey'; import dayjs from 'dayjs'; import relativeTime from 'dayjs/plugin/relativeTime'; +import Skeleton from 'react-loading-skeleton'; dayjs.extend(relativeTime); @@ -24,7 +25,7 @@ export const NoteDefaultUser = ({ pubkey, time }: { pubkey: string; time: number
- {user?.display_name || shortenKey(pubkey)} + {user?.display_name || user?.name || }
diff --git a/src/app/note/components/user/repost.tsx b/src/app/note/components/user/repost.tsx index ff1d1888..5579a34b 100644 --- a/src/app/note/components/user/repost.tsx +++ b/src/app/note/components/user/repost.tsx @@ -1,10 +1,10 @@ import { Image } from '@lume/shared/image'; import { DEFAULT_AVATAR, IMGPROXY_URL } from '@lume/stores/constants'; import { useProfile } from '@lume/utils/hooks/useProfile'; -import { shortenKey } from '@lume/utils/shortenKey'; import dayjs from 'dayjs'; import relativeTime from 'dayjs/plugin/relativeTime'; +import Skeleton from 'react-loading-skeleton'; dayjs.extend(relativeTime); @@ -22,8 +22,9 @@ export const NoteRepostUser = ({ pubkey, time }: { pubkey: string; time: number
- {user?.display_name || user?.name || shortenKey(pubkey)}{' '} + {user?.display_name || user?.name || } + {' '} reposted
diff --git a/src/utils/storage.tsx b/src/utils/storage.tsx index 4ab6e656..ad1ec394 100644 --- a/src/utils/storage.tsx +++ b/src/utils/storage.tsx @@ -76,7 +76,14 @@ export async function countTotalChannels() { // count total notes export async function countTotalNotes() { const db = await connect(); - const result = await db.select('SELECT COUNT(*) AS "total" FROM notes;'); + const result = await db.select('SELECT COUNT(*) AS "total" FROM notes WHERE kind IN (1, 6);'); + return result[0].total; +} + +// count total notes +export async function countTotalLongNotes() { + const db = await connect(); + const result = await db.select('SELECT COUNT(*) AS "total" FROM notes WHERE kind = 30023;'); return result[0].total; } @@ -86,7 +93,22 @@ export async function getNotes(time: number, limit: number, offset: number) { const notes: any = { data: null, nextCursor: 0 }; const query: any = await db.select( - `SELECT * FROM notes WHERE created_at <= "${time}" GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";` + `SELECT * FROM notes WHERE created_at <= "${time}" AND kind IN (1, 6) GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";` + ); + + notes['data'] = query; + notes['nextCursor'] = offset + limit; + + return notes; +} + +// get all long notes +export async function getLongNotes(time: number, limit: number, offset: number) { + const db = await connect(); + + const notes: any = { data: null, nextCursor: 0 }; + const query: any = await db.select( + `SELECT * FROM notes WHERE created_at <= "${time}" AND kind = 30023 GROUP BY parent_id ORDER BY created_at DESC LIMIT "${limit}" OFFSET "${offset}";` ); notes['data'] = query;