-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store events locally via SQLite (#38)
* Add expo-sqlite * Add DB test and demo * clean up a require cycle * and another * Consolidate auth and authActions - no more require cycles * clean up RelayTest * show DatabaseTest * cleanup * relaypool exports * clear nostr hook stuff from useAuthed for now * useRelayPool in dbtest * ya * small button showing number of relays * Extract RelayIndicator * shh * shhh * createTable calls * databaseReport on how many rows * try/catch * useRelayPool refactor options, allow connectNow * DatabaseTest connect to relayPool * Start new simple NostrEvent * useDatabase return db * handleEvent just make NostrEvent * parse events * 6 channels saved * initial arc_channel_messages maybe * saving initial channel messages * saving kind 40 and 42s * fix useAuthed and hook that up * wow that was a waste of time * ok useInterval works thx * starting to simplify useRelayPool - need to globalize context via zustand * fix addOrModifyRelay * looks unfucked now * finally connectedRelays looks right * ensure only create 1 sub array even if multiple hooks * almost subscribing * k dupe whatever - we got stuff * saving a bunch more * move relay check elsewhere * addrelay in the addrelay * yeah looking good * k much smoother * connecting in authednavigator and got events * useGlobalFeed now looks in database * sort msgs chrono * fetch notes from localdb on interval * usermetadata * add saveUserMeta * add viewrelays screen from settings * hilarious hack saved a user * got all usermetadata ehhahahahah * simpler getFriendMetadata * start with a FirstLoad screen eheheheh * rejigger * return tablecounts * set up some stuff * firstload * ok * ok saving a bunch * no reposts or reactions for now * use zustand store * inefficient but works * initial hydration looks good * fix user hydration * add comments to mergeSimilarAndRemoveEmptyFilters * yea the relaypool filter merger was removing the contact filter * nocache works - now we are receiving kind0s * axe getfriendemtadata * ok finally * hmm * users and posts tick up * properly dedupe * dedupe the multi adds * alllmost ready * simplify useglobalfeed * consolidate initial subscriptions * harmonize initial sub syntax * axe old usenostrhook * connecting usermetadata yo * homefeed looks good with user metadata * sort all dem msgs descending chrono * firstload cleanup * cleanup * makesubs async * clean up useRelayPool * ya * lastfetch table and helpers * and adding channels * ok channelmsgs works like that too * just nav to next only if eheheh * addUsers use latest metadata i think * and adduser * ok looking good * filter out friends we already got * saving lastfetched and not querying again * start cleanup * cleanup * fix user metadata * axe prev profilescreen * console shh * axe relaytest * axe prev databasetestdemo * refactor useUserMetadataForChannel maybe * refactor useMessagesForChannel maybe * fix various type errors down to 0 * axe old loadtestpaint - tests pass * getting user metadata slowly ehehehe * fix get msgs * console cleanup * niiiiiice * remove require cycles * put channelscreen in keyboardavoidingview * bump version to 0.0.2 * add a few more bitcoiners * navheader padding fixes * dont autofocus relay field cuz it focuses when hidden * createaccountscreen padding stuff * no underscore in number * keyboardawarescrollview * bump ios build num * hey install the keyboardthing * limit globalfeed to 50 fornow * replace old nostr class w direct relayinit until we're authed * bump android build version * in a channel, only get the last 3 days of msgs * ok thats a lot better * last buildnums
- Loading branch information
1 parent
e93360f
commit 7f9348d
Showing
79 changed files
with
1,993 additions
and
1,280 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import * as SQLite from 'expo-sqlite' | ||
|
||
export const createTables = async (db: SQLite.WebSQLDatabase) => { | ||
try { | ||
db.transaction(async (tx) => { | ||
for (const createTableCall of createTableCalls) { | ||
tx.executeSql(createTableCall) | ||
} | ||
console.log('Database initialized.') | ||
}) | ||
} catch (error) { | ||
console.error('Error creating tables', error) | ||
} | ||
} | ||
|
||
const createTableCalls = [ | ||
`CREATE TABLE IF NOT EXISTS arc_channels( | ||
id TEXT PRIMARY KEY NOT NULL, | ||
pubkey TEXT NOT NULL, | ||
sig TEXT NOT NULL, | ||
name TEXT NOT NULL, | ||
about TEXT NOT NULL, | ||
picture TEXT NOT NULL, | ||
created_at INT NOT NULL | ||
);`, | ||
`CREATE TABLE IF NOT EXISTS arc_channel_messages( | ||
id TEXT PRIMARY KEY NOT NULL, | ||
content TEXT NOT NULL, | ||
created_at INT NOT NULL, | ||
kind INT NOT NULL, | ||
pubkey TEXT NOT NULL, | ||
sig TEXT NOT NULL, | ||
tags TEXT NOT NULL, | ||
channel_id TEXT NOT NULL, | ||
reply_event_id TEXT | ||
);`, | ||
`CREATE TABLE IF NOT EXISTS arc_direct_messages( | ||
id TEXT PRIMARY KEY NOT NULL, | ||
content TEXT NOT NULL, | ||
created_at INT NOT NULL, | ||
kind INT NOT NULL, | ||
pubkey TEXT NOT NULL, | ||
sig TEXT NOT NULL, | ||
tags TEXT NOT NULL, | ||
conversation_id TEXT NOT NULL, | ||
read BOOLEAN DEFAULT FALSE | ||
);`, | ||
`CREATE TABLE IF NOT EXISTS arc_notes( | ||
id TEXT PRIMARY KEY NOT NULL, | ||
content TEXT NOT NULL, | ||
created_at INT NOT NULL, | ||
kind INT NOT NULL, | ||
pubkey TEXT NOT NULL, | ||
sig TEXT NOT NULL, | ||
tags TEXT NOT NULL, | ||
main_event_id TEXT, | ||
reply_event_id TEXT, | ||
user_mentioned BOOLEAN DEFAULT FALSE, | ||
seen BOOLEAN DEFAULT FALSE | ||
);`, | ||
`CREATE TABLE IF NOT EXISTS arc_reactions( | ||
id TEXT PRIMARY KEY NOT NULL, | ||
content TEXT NOT NULL, | ||
created_at INT NOT NULL, | ||
kind INT NOT NULL, | ||
pubkey TEXT NOT NULL, | ||
sig TEXT NOT NULL, | ||
tags TEXT NOT NULL, | ||
event_id TEXT NOT NULL, | ||
type INT NOT NULL | ||
);`, | ||
`CREATE TABLE IF NOT EXISTS arc_relays( | ||
url TEXT PRIMARY KEY NOT NULL, | ||
pet INTEGER | ||
);`, | ||
`CREATE TABLE IF NOT EXISTS arc_users( | ||
id TEXT PRIMARY KEY NOT NULL, | ||
pubkey TEXT NOT NULL, | ||
name TEXT, | ||
display_name TEXT, | ||
picture TEXT, | ||
about TEXT, | ||
main_relay TEXT, | ||
contact BOOLEAN DEFAULT FALSE, | ||
follower BOOLEAN DEFAULT FALSE, | ||
lnurl TEXT, | ||
created_at INT DEFAULT 0 | ||
);`, | ||
`CREATE TABLE IF NOT EXISTS last_fetch ( | ||
type TEXT PRIMARY KEY NOT NULL UNIQUE, | ||
last_fetch INT NOT NULL | ||
);`, | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import * as SQLite from 'expo-sqlite' | ||
|
||
export const databaseReport = async (db: SQLite.WebSQLDatabase) => { | ||
const tableCounts: { [table: string]: number } = {} | ||
return new Promise((resolve, reject) => { | ||
try { | ||
db.transaction((tx) => { | ||
tx.executeSql( | ||
'SELECT name from sqlite_master WHERE type = "table"', | ||
[], | ||
(_, { rows: { _array } }) => { | ||
const tables = _array.map((t) => t.name) | ||
tables.forEach((table) => { | ||
tx.executeSql( | ||
`SELECT COUNT(*) as count FROM ${table}`, | ||
[], | ||
(_, { rows: { _array } }) => { | ||
const count = _array[0].count | ||
console.log(` ${count} records in ${table}`) | ||
tableCounts[table] = count | ||
if (Object.keys(tableCounts).length === tables.length) { | ||
resolve(tableCounts) | ||
} | ||
}, | ||
(_, error) => { | ||
console.log(`Error querying ${table}`, error) | ||
reject(error) | ||
return false | ||
} | ||
) | ||
}) | ||
}, | ||
(_, error) => { | ||
console.log('Error querying sqlite_master', error) | ||
reject(error) | ||
return false | ||
} | ||
) | ||
}) | ||
} catch (error) { | ||
console.log('Error in databaseReport', error) | ||
reject(error) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useStore } from 'app/stores' | ||
import { db } from 'lib/database/useDatabase' | ||
|
||
export const hydrateStoreFromDatabase = async () => { | ||
const { addUsers, addNotes, addChannels, addChannelMessages } = | ||
useStore.getState() | ||
|
||
db.transaction((tx) => { | ||
tx.executeSql( | ||
'SELECT * FROM arc_users', | ||
[], | ||
(_, { rows: { _array } }) => { | ||
const users = _array.map((user) => { | ||
return user | ||
}) | ||
addUsers(users) | ||
}, | ||
(_, error) => { | ||
console.log('Error querying arc_users', error) | ||
return false | ||
} | ||
) | ||
|
||
tx.executeSql( | ||
'SELECT * FROM arc_notes', | ||
[], | ||
(_, { rows: { _array } }) => { | ||
const notes = _array.map((note) => { | ||
return note | ||
}) | ||
addNotes(notes) | ||
}, | ||
(_, error) => { | ||
console.log('Error querying arc_notes', error) | ||
return false | ||
} | ||
) | ||
|
||
tx.executeSql( | ||
'SELECT * FROM arc_channels', | ||
[], | ||
(_, { rows: { _array } }) => { | ||
const channels = _array.map((channel) => channel) | ||
addChannels(channels) | ||
}, | ||
(_, error) => { | ||
console.log('Error querying arc_channels', error) | ||
return false | ||
} | ||
) | ||
|
||
tx.executeSql( | ||
'SELECT * FROM arc_channel_messages', | ||
[], | ||
(_, { rows: { _array } }) => { | ||
const channelMessages = _array.map((channelMessage) => channelMessage) | ||
addChannelMessages(channelMessages) | ||
}, | ||
(_, error) => { | ||
console.log('Error querying arc_channel_messages', error) | ||
return false | ||
} | ||
) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './hydrateStoreFromDatabase' | ||
export * from './lastFetch' | ||
export * from './useDatabase' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { db } from 'lib/database/useDatabase' | ||
|
||
export const setLastFetch = (type: string, timestamp: number) => { | ||
db.transaction((tx) => { | ||
tx.executeSql( | ||
`INSERT OR REPLACE INTO last_fetch (type, last_fetch) VALUES (?, ?)`, | ||
[type, timestamp], | ||
(_, result) => {}, | ||
(_, error) => { | ||
console.log('Error setting last fetch timestamp:', error) | ||
return false | ||
} | ||
) | ||
}) | ||
} | ||
|
||
export const getLastFetch = (type: string) => { | ||
return new Promise((resolve) => { | ||
db.transaction((tx) => { | ||
tx.executeSql( | ||
`SELECT last_fetch FROM last_fetch WHERE type = ?`, | ||
[type], | ||
(_, result) => { | ||
if (result.rows.length > 0) { | ||
resolve(result.rows.item(0).last_fetch) | ||
} else { | ||
resolve(undefined) | ||
} | ||
}, | ||
(_, error) => { | ||
console.log('Error getting last fetch timestamp:', error) | ||
resolve(undefined) | ||
return false | ||
} | ||
) | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import * as SQLite from 'expo-sqlite' | ||
import { useEffect } from 'react' | ||
|
||
import { createTables } from './createTables' | ||
|
||
const openDatabase = () => SQLite.openDatabase('arc53.db') | ||
export const db = openDatabase() | ||
|
||
let did = false | ||
|
||
export const useDatabase = () => { | ||
useEffect(() => { | ||
if (did) return | ||
createTables(db) | ||
did = true | ||
}, []) | ||
|
||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
import { useStore } from 'stores' | ||
import { useStore } from 'app/stores' | ||
|
||
export const useGlobalFeed = () => { | ||
const events = useStore((s) => s.events) | ||
const filteredEvents = events | ||
.filter((e) => e.kind === 1) | ||
.filter((e) => e.content !== '') | ||
.sort((a, b) => b.created_at - a.created_at) | ||
.filter((e, i, a) => a.findIndex((t) => t.id === e.id) === i) | ||
return filteredEvents | ||
const notes = useStore((state) => state.notes).sort( | ||
(a, b) => b.created_at - a.created_at | ||
) | ||
return notes | ||
} |
Oops, something went wrong.