2022-09-12 22:18:27 +00:00
import { useEffect , useState , useRef , useContext } from 'react' ;
import SQLite from "react-native-sqlite-storage" ;
2022-12-28 22:55:54 +00:00
const DATABAG _DB = 'db_v090.db' ;
2022-09-12 22:18:27 +00:00
export function useStoreContext ( ) {
2022-09-14 07:27:49 +00:00
const [ state , setState ] = useState ( { } ) ;
2022-09-12 22:18:27 +00:00
const db = useRef ( null ) ;
const updateState = ( value ) => {
setState ( ( s ) => ( { ... s , ... value } ) )
}
2022-09-15 08:03:20 +00:00
const initSession = async ( guid ) => {
2022-12-28 22:55:54 +00:00
await db . current . executeSql ( ` CREATE TABLE IF NOT EXISTS channel_ ${ guid } (channel_id text, revision integer, detail_revision integer, topic_revision integer, topic_marker integer, blocked integer, sync_revision integer, detail text, unsealed_detail text, summary text, unsealed_summary text, offsync integer, read_revision integer, unique(channel_id)) ` ) ;
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` CREATE TABLE IF NOT EXISTS channel_topic_ ${ guid } (channel_id text, topic_id text, revision integer, detail_revision integer, blocked integer, detail text, unsealed_detail text, unique(channel_id, topic_id)) ` ) ;
2022-09-26 21:39:01 +00:00
await db . current . executeSql ( ` CREATE TABLE IF NOT EXISTS card_ ${ guid } (card_id text, revision integer, detail_revision integer, profile_revision integer, detail text, profile text, notified_view integer, notified_article integer, notified_profile integer, notified_channel integer, offsync integer, blocked integer, unique(card_id)) ` ) ;
2022-12-28 22:55:54 +00:00
await db . current . executeSql ( ` CREATE TABLE IF NOT EXISTS card_channel_ ${ guid } (card_id text, channel_id text, revision integer, detail_revision integer, topic_revision integer, topic_marker integer, sync_revision integer, detail text, unsealed_detail text, summary text, unsealed_summary text, offsync integer, blocked integer, read_revision integer, unique(card_id, channel_id)) ` ) ;
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` CREATE TABLE IF NOT EXISTS card_channel_topic_ ${ guid } (card_id text, channel_id text, topic_id text, revision integer, detail_revision integer, blocked integer, detail text, unsealed_detail text, unique(card_id, channel_id, topic_id)) ` ) ;
2022-09-15 08:03:20 +00:00
}
2022-09-12 22:18:27 +00:00
const actions = {
2022-09-14 07:27:49 +00:00
init : async ( ) => {
SQLite . DEBUG ( false ) ;
SQLite . enablePromise ( true ) ;
db . current = await SQLite . openDatabase ( { name : DATABAG _DB , location : "default" } ) ;
await db . current . executeSql ( "CREATE TABLE IF NOT EXISTS app (key text, value text, unique(key));" ) ;
await db . current . executeSql ( "INSERT OR IGNORE INTO app (key, value) values ('session', null);" ) ;
return await getAppValue ( db . current , 'session' ) ;
} ,
2022-09-12 22:18:27 +00:00
setSession : async ( access ) => {
2022-09-15 08:03:20 +00:00
await initSession ( access . guid ) ;
2022-09-12 22:18:27 +00:00
await db . current . executeSql ( "UPDATE app SET value=? WHERE key='session';" , [ encodeObject ( access ) ] ) ;
} ,
clearSession : async ( ) => {
await db . current . executeSql ( "UPDATE app set value=? WHERE key='session';" , [ null ] ) ;
} ,
2022-09-14 19:18:16 +00:00
2022-09-14 07:27:49 +00:00
getProfile : async ( guid ) => {
const dataId = ` ${ guid } _profile ` ;
return await getAppValue ( db . current , dataId , { } ) ;
2022-09-13 18:46:28 +00:00
} ,
2022-09-14 07:27:49 +00:00
setProfile : async ( guid , profile ) => {
const dataId = ` ${ guid } _profile ` ;
2022-09-15 08:03:20 +00:00
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ dataId , encodeObject ( profile ) ] ) ;
2022-10-24 20:11:28 +00:00
} ,
getFirstRun : async ( guid ) => {
2022-10-24 20:46:40 +00:00
const firstRun = await getAppValue ( db . current , "firstrun" , { set : true } ) ;
2022-10-24 20:11:28 +00:00
return firstRun . set ;
} ,
2022-10-24 20:46:40 +00:00
setFirstRun : async ( ) => {
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ "firstrun" , encodeObject ( { set : false } ) ] ) ;
2022-09-13 18:46:28 +00:00
} ,
2022-10-14 22:15:45 +00:00
getCardRequestStatus : async ( guid ) => {
const dataId = ` ${ guid } _card_status ` ;
return await getAppValue ( db . current , dataId , { } ) ;
} ,
setCardRequestStatus : async ( guid , status ) => {
const dataId = ` ${ guid } _card_status ` ;
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ dataId , encodeObject ( status ) ] ) ;
} ,
2022-09-14 07:27:49 +00:00
getProfileRevision : async ( guid ) => {
const dataId = ` ${ guid } _profileRevision ` ;
2022-09-15 08:03:20 +00:00
return await getAppValue ( db . current , dataId , null ) ;
2022-09-13 18:46:28 +00:00
} ,
2022-09-14 07:27:49 +00:00
setProfileRevision : async ( guid , revision ) => {
const dataId = ` ${ guid } _profileRevision ` ;
2022-09-15 08:03:20 +00:00
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ dataId , encodeObject ( revision ) ] ) ;
2022-09-14 19:18:16 +00:00
} ,
getAccountStatus : async ( guid ) => {
const dataId = ` ${ guid } _status ` ;
return await getAppValue ( db . current , dataId , { } ) ;
} ,
setAccountStatus : async ( guid , status ) => {
const dataId = ` ${ guid } _status ` ;
2022-09-15 08:03:20 +00:00
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ dataId , encodeObject ( status ) ] ) ;
2022-09-12 22:18:27 +00:00
} ,
2022-12-08 07:27:27 +00:00
getAccountSealKey : async ( guid ) => {
const dataId = ` ${ guid } _sealkey ` ;
return await getAppValue ( db . current , dataId , { } ) ;
} ,
setAccountSealKey : async ( guid , key ) => {
const dataId = ` ${ guid } _sealkey ` ;
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ dataId , encodeObject ( key ) ] ) ;
} ,
2022-09-14 19:18:16 +00:00
getAccountRevision : async ( guid ) => {
const dataId = ` ${ guid } _accountRevision ` ;
2022-09-15 08:03:20 +00:00
return await getAppValue ( db . current , dataId , null ) ;
2022-09-14 19:18:16 +00:00
} ,
setAccountRevision : async ( guid , revision ) => {
const dataId = ` ${ guid } _accountRevision ` ;
2022-09-15 08:03:20 +00:00
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ dataId , encodeObject ( revision ) ] ) ;
} ,
getCardRevision : async ( guid ) => {
const dataId = ` ${ guid } _cardRevision ` ;
return await getAppValue ( db . current , dataId , null ) ;
} ,
setCardRevision : async ( guid , revision ) => {
const dataId = ` ${ guid } _cardRevision ` ;
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ dataId , encodeObject ( revision ) ] ) ;
2022-09-15 18:26:03 +00:00
} ,
setCardItem : async ( guid , card ) => {
const { id , revision , data } = card ;
2022-09-15 22:12:06 +00:00
await db . current . executeSql ( ` INSERT OR REPLACE INTO card_ ${ guid } (card_id, revision, detail_revision, profile_revision, detail, profile, notified_view, notified_profile, notified_article, notified_channel) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?); ` , [ id , revision , data . detailRevision , data . profileRevision , encodeObject ( data . cardDetail ) , encodeObject ( data . cardProfile ) , null , null , null , null ] ) ;
2022-09-15 18:26:03 +00:00
} ,
clearCardItem : async ( guid , cardId ) => {
await db . current . executeSql ( ` DELETE FROM card_ ${ guid } WHERE card_id=? ` , [ cardId ] ) ;
} ,
setCardItemRevision : async ( guid , cardId , revision ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set revision=? where card_id=? ` , [ revision , cardId ] ) ;
} ,
setCardItemNotifiedView : async ( guid , cardId , notified ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set notified_view=? where card_id=? ` , [ notified , cardId ] ) ;
} ,
2022-09-15 22:12:06 +00:00
setCardItemNotifiedArticle : async ( guid , cardId , notified ) => {
2022-09-15 18:26:03 +00:00
await db . current . executeSql ( ` UPDATE card_ ${ guid } set notified_article=? where card_id=? ` , [ notified , cardId ] ) ;
} ,
setCardItemNotifiedProfile : async ( guid , cardId , notified ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set notified_profile=? where card_id=? ` , [ notified , cardId ] ) ;
} ,
setCardItemNotifiedChannel : async ( guid , cardId , notified ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set notified_channel=? where card_id=? ` , [ notified , cardId ] ) ;
} ,
2022-09-16 06:18:46 +00:00
setCardItemOffsync : async ( guid , cardId ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set offsync=? where card_id=? ` , [ 1 , cardId ] ) ;
} ,
clearCardItemOffsync : async ( guid , cardId ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set offsync=? where card_id=? ` , [ 0 , cardId ] ) ;
} ,
2022-09-26 21:39:01 +00:00
setCardItemBlocked : async ( guid , cardId ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set blocked=? where card_id=? ` , [ 1 , cardId ] ) ;
} ,
clearCardItemBlocked : async ( guid , cardId ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set blocked=? where card_id=? ` , [ 0 , cardId ] ) ;
} ,
2022-09-15 18:26:03 +00:00
setCardItemDetail : async ( guid , cardId , revision , detail ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set detail_revision=?, detail=? where card_id=? ` , [ revision , encodeObject ( detail ) , cardId ] ) ;
} ,
setCardItemProfile : async ( guid , cardId , revision , profile ) => {
await db . current . executeSql ( ` UPDATE card_ ${ guid } set profile_revision=?, profile=? where card_id=? ` , [ revision , encodeObject ( profile ) , cardId ] ) ;
} ,
2022-09-15 22:12:06 +00:00
getCardItemStatus : async ( guid , cardId ) => {
2022-09-20 07:50:53 +00:00
const values = await getAppValues ( db . current , ` SELECT detail, profile, profile_revision, detail_revision, notified_view, notified_article, notified_profile, notified_channel, offsync FROM card_ ${ guid } WHERE card_id=? ` , [ cardId ] ) ;
2022-09-15 18:26:03 +00:00
if ( ! values . length ) {
2022-09-15 22:12:06 +00:00
return null ;
2022-09-15 18:26:03 +00:00
}
return {
2022-09-15 22:12:06 +00:00
detail : decodeObject ( values [ 0 ] . detail ) ,
2022-09-16 06:18:46 +00:00
profile : decodeObject ( values [ 0 ] . profile ) ,
2022-09-20 07:50:53 +00:00
profileRevision : values [ 0 ] . profile _revision ,
detailRevision : values [ 0 ] . detail _revision ,
2022-09-15 18:26:03 +00:00
notifiedView : values [ 0 ] . notified _view ,
notifiedArticle : values [ 0 ] . notified _article ,
notifiedProfile : values [ 0 ] . notified _profile ,
2022-09-20 21:57:58 +00:00
notifiedChannel : values [ 0 ] . notified _channel ,
2022-09-20 07:50:53 +00:00
offsync : values [ 0 ] . offsync ,
2022-09-26 21:39:01 +00:00
blocked : values [ 0 ] . blocked ,
2022-09-15 18:26:03 +00:00
} ;
} ,
2022-09-15 22:12:06 +00:00
getCardItemView : async ( guid , cardId ) => {
const values = await getAppValues ( db . current , ` SELECT revision, detail_revision, profile_revision FROM card_ ${ guid } WHERE card_id=? ` , [ cardId ] ) ;
if ( ! values . length ) {
return null ;
}
return {
revision : values [ 0 ] . revision ,
detailRevision : values [ 0 ] . detail _revision ,
profileRevision : values [ 0 ] . profile _revision ,
} ;
} ,
2022-09-15 18:26:03 +00:00
getCardItems : async ( guid ) => {
2022-09-26 21:39:01 +00:00
const values = await getAppValues ( db . current , ` SELECT card_id, revision, detail_revision, profile_revision, detail, profile, offsync, blocked, notified_view, notified_profile, notified_article, notified_channel FROM card_ ${ guid } ` , [ ] ) ;
2022-09-15 18:26:03 +00:00
return values . map ( card => ( {
cardId : card . card _id ,
revision : card . revision ,
detailRevision : card . detail _revision ,
profileRevision : card . profile _revision ,
detail : decodeObject ( card . detail ) ,
profile : decodeObject ( card . profile ) ,
notifiedView : card . notified _view ,
notifiedProfile : card . notified _profile ,
notifiedArticle : card . notified _article ,
notifiedChannel : card . notified _channel ,
2022-09-26 21:39:01 +00:00
offsync : card . offsync ,
blocked : card . blocked ,
2022-09-15 18:26:03 +00:00
} ) ) ;
} ,
2022-09-15 08:03:20 +00:00
getChannelRevision : async ( guid ) => {
const dataId = ` ${ guid } _channelRevision ` ;
return await getAppValue ( db . current , dataId , null ) ;
} ,
setChannelRevision : async ( guid , revision ) => {
const dataId = ` ${ guid } _channelRevision ` ;
await db . current . executeSql ( "INSERT OR REPLACE INTO app (key, value) values (?, ?);" , [ dataId , encodeObject ( revision ) ] ) ;
} ,
setChannelItem : async ( guid , channel ) => {
const { id , revision , data } = channel ;
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` INSERT OR REPLACE INTO channel_ ${ guid } (channel_id, revision, detail_revision, topic_revision, detail, summary, unsealed_detail, unsealed_summary) values (?, ?, ?, ?, ?, ?, null, null); ` , [ id , revision , data . detailRevision , data . topicRevision , encodeObject ( data . channelDetail ) , encodeObject ( data . channelSummary ) ] ) ;
2022-09-15 08:03:20 +00:00
} ,
clearChannelItem : async ( guid , channelId ) => {
await db . current . executeSql ( ` DELETE FROM channel_ ${ guid } WHERE channel_id=? ` , [ channelId ] ) ;
} ,
setChannelItemRevision : async ( guid , channelId , revision ) => {
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set revision=? where channel_id=? ` , [ revision , channelId ] ) ;
} ,
2022-09-20 20:17:20 +00:00
setChannelItemReadRevision : async ( guid , channelId , revision ) => {
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set read_revision=? where channel_id=? ` , [ revision , channelId ] ) ;
} ,
2022-09-29 06:30:22 +00:00
setChannelItemSyncRevision : async ( guid , channelId , revision ) => {
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set sync_revision=? where channel_id=? ` , [ revision , channelId ] ) ;
} ,
2022-12-28 22:55:54 +00:00
setChannelItemTopicMarker : async ( guid , channelId , marker ) => {
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set topic_marker=? where channel_id=? ` , [ marker , channelId ] ) ;
} ,
2022-10-11 19:18:08 +00:00
setChannelItemBlocked : async ( guid , channelId ) => {
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set blocked=? where channel_id=? ` , [ 1 , channelId ] ) ;
} ,
clearChannelItemBlocked : async ( guid , channelId ) => {
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set blocked=? where channel_id=? ` , [ 0 , channelId ] ) ;
} ,
2022-09-15 08:03:20 +00:00
setChannelItemDetail : async ( guid , channelId , revision , detail ) => {
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set detail_revision=?, detail=?, unsealed_detail=null where channel_id=? ` , [ revision , encodeObject ( detail ) , channelId ] ) ;
2022-09-15 08:03:20 +00:00
} ,
2022-12-14 07:22:20 +00:00
setChannelItemUnsealedDetail : async ( guid , channelId , revision , unsealed ) => {
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set unsealed_detail=? where detail_revision=? AND channel_id=? ` , [ encodeObject ( unsealed ) , revision , channelId ] ) ;
} ,
2022-09-15 08:03:20 +00:00
setChannelItemSummary : async ( guid , channelId , revision , summary ) => {
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set topic_revision=?, summary=?, unsealed_summary=null where channel_id=? ` , [ revision , encodeObject ( summary ) , channelId ] ) ;
2022-09-15 08:03:20 +00:00
} ,
2022-12-17 21:19:07 +00:00
setChannelItemUnsealedSummary : async ( guid , channelId , revision , unsealed ) => {
await db . current . executeSql ( ` UPDATE channel_ ${ guid } set unsealed_summary=? where topic_revision=? AND channel_id=? ` , [ encodeObject ( unsealed ) , revision , channelId ] ) ;
} ,
2022-09-15 08:03:20 +00:00
getChannelItemView : async ( guid , channelId ) => {
const values = await getAppValues ( db . current , ` SELECT revision, detail_revision, topic_revision FROM channel_ ${ guid } WHERE channel_id=? ` , [ channelId ] ) ;
if ( ! values . length ) {
2022-09-15 22:12:06 +00:00
return null ;
2022-09-15 08:03:20 +00:00
}
return {
revision : values [ 0 ] . revision ,
detailRevision : values [ 0 ] . detail _revision ,
topicRevision : values [ 0 ] . topic _revision ,
} ;
} ,
getChannelItems : async ( guid ) => {
2022-12-28 22:55:54 +00:00
const values = await getAppValues ( db . current , ` SELECT channel_id, read_revision, revision, sync_revision, blocked, detail_revision, topic_revision, topic_marker, detail, unsealed_detail, summary, unsealed_summary FROM channel_ ${ guid } ` , [ ] ) ;
2022-09-15 08:03:20 +00:00
return values . map ( channel => ( {
channelId : channel . channel _id ,
revision : channel . revision ,
2022-09-20 20:17:20 +00:00
readRevision : channel . read _revision ,
2022-09-15 08:03:20 +00:00
detailRevision : channel . detail _revision ,
topicRevision : channel . topic _revision ,
2022-12-28 22:55:54 +00:00
topicMarker : channel . topic _marker ,
2022-09-30 05:34:31 +00:00
syncRevision : channel . sync _revision ,
2022-10-11 19:18:08 +00:00
blocked : channel . blocked ,
2022-09-15 08:03:20 +00:00
detail : decodeObject ( channel . detail ) ,
2022-12-13 23:23:38 +00:00
unsealedDetail : decodeObject ( channel . unsealed _detail ) ,
2022-09-15 08:03:20 +00:00
summary : decodeObject ( channel . summary ) ,
2022-12-13 23:23:38 +00:00
unsealedSummary : decodeObject ( channel . unsealed _summary ) ,
2022-09-15 08:03:20 +00:00
} ) ) ;
} ,
2022-09-15 22:12:06 +00:00
2022-09-29 06:30:22 +00:00
getChannelTopicItems : async ( guid , channelId ) => {
2022-12-14 07:22:20 +00:00
const values = await getAppValues ( db . current , ` SELECT topic_id, revision, blocked, detail_revision, detail, unsealed_detail FROM channel_topic_ ${ guid } WHERE channel_id=? ` , [ channelId ] ) ;
2022-09-29 06:30:22 +00:00
return values . map ( topic => ( {
topicId : topic . topic _id ,
revision : topic . revision ,
2022-10-18 20:40:14 +00:00
blocked : topic . blocked ,
2022-09-29 06:30:22 +00:00
detailRevision : topic . detail _revision ,
detail : decodeObject ( topic . detail ) ,
2022-12-13 23:23:38 +00:00
unsealedDetail : decodeObject ( topic . unsealed _detail ) ,
2022-09-29 06:30:22 +00:00
} ) ) ;
} ,
2022-09-30 05:34:31 +00:00
setChannelTopicItem : async ( guid , channelId , topic ) => {
const { id , revision , data } = topic ;
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` INSERT OR REPLACE INTO channel_topic_ ${ guid } (channel_id, topic_id, revision, detail_revision, detail, unsealed_detail) values (?, ?, ?, ?, ?, null); ` , [ channelId , id , revision , data . detailRevision , encodeObject ( data . topicDetail ) ] ) ;
2022-09-29 06:30:22 +00:00
} ,
2022-12-16 07:41:51 +00:00
setChannelTopicItemUnsealedDetail : async ( guid , channelId , topicId , revision , unsealed ) => {
await db . current . executeSql ( ` UPDATE channel_topic_ ${ guid } set unsealed_detail=? where detail_revision=? AND channel_id=? AND topic_id=? ` , [ encodeObject ( unsealed ) , revision , channelId , topicId ] ) ;
} ,
2022-09-29 06:30:22 +00:00
clearChannelTopicItem : async ( guid , channelId , topicId ) => {
await db . current . executeSql ( ` DELETE FROM channel_topic_ ${ guid } WHERE channel_id=? and topic_id=? ` , [ channelId , topicId ] ) ;
} ,
clearChannelTopicItems : async ( guid , channelId ) => {
await db . current . executeSql ( ` DELETE FROM channel_topic_ ${ guid } WHERE channel_id=? ` , [ channelId ] ) ;
} ,
2022-10-18 20:40:14 +00:00
setChannelTopicBlocked : async ( guid , channelId , topicId , blocked ) => {
2022-10-18 21:52:13 +00:00
await db . current . executeSql ( ` UPDATE channel_topic_ ${ guid } set blocked=? WHERE channel_id=? and topic_id=? ` , [ blocked , channelId , topicId ] ) ;
} ,
getChannelTopicBlocked : async ( guid ) => {
const values = await getAppValues ( db . current , ` SELECT channel_id, topic_id, detail FROM channel_topic_ ${ guid } WHERE blocked=? ` , [ 1 ] ) ;
return values . map ( topic => ( {
channelId : topic . channel _id ,
topicId : topic . topic _id ,
detail : decodeObject ( topic . detail ) ,
} ) ) ;
2022-10-18 20:40:14 +00:00
} ,
2022-09-29 06:30:22 +00:00
2022-09-15 22:12:06 +00:00
setCardChannelItem : async ( guid , cardId , channel ) => {
const { id , revision , data } = channel ;
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` INSERT OR REPLACE INTO card_channel_ ${ guid } (card_id, channel_id, revision, detail_revision, topic_revision, detail, summary, unsealed_detail, unsealed_summary) values (?, ?, ?, ?, ?, ?, ?, null, null); ` , [ cardId , id , revision , data . detailRevision , data . topicRevision , encodeObject ( data . channelDetail ) , encodeObject ( data . channelSummary ) ] ) ;
2022-09-15 22:12:06 +00:00
} ,
clearCardChannelItem : async ( guid , cardId , channelId ) => {
await db . current . executeSql ( ` DELETE FROM card_channel_ ${ guid } WHERE card_id=? and channel_id=? ` , [ cardId , channelId ] ) ;
} ,
setCardChannelItemRevision : async ( guid , cardId , channelId , revision ) => {
await db . current . executeSql ( ` UPDATE card_channel_ ${ guid } set revision=? where card_id=? and channel_id=? ` , [ revision , cardId , channelId ] ) ;
} ,
2022-09-20 20:17:20 +00:00
setCardChannelItemReadRevision : async ( guid , cardId , channelId , revision ) => {
await db . current . executeSql ( ` UPDATE card_channel_ ${ guid } set read_revision=? where card_id=? and channel_id=? ` , [ revision , cardId , channelId ] ) ;
} ,
2022-09-29 06:30:22 +00:00
setCardChannelItemSyncRevision : async ( guid , cardId , channelId , revision ) => {
await db . current . executeSql ( ` UPDATE card_channel_ ${ guid } set sync_revision=? where card_id=? and channel_id=? ` , [ revision , cardId , channelId ] ) ;
} ,
2022-12-28 22:55:54 +00:00
setCardChannelItemTopicMarker : async ( guid , cardId , channelId , marker ) => {
await db . current . executeSql ( ` UPDATE card_channel_ ${ guid } set topic_marker=? where card_id=? and channel_id=? ` , [ marker , cardId , channelId ] ) ;
} ,
2022-09-15 22:12:06 +00:00
setCardChannelItemDetail : async ( guid , cardId , channelId , revision , detail ) => {
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` UPDATE card_channel_ ${ guid } set detail_revision=?, detail=?, unsealed_detail=null where card_id=? and channel_id=? ` , [ revision , encodeObject ( detail ) , cardId , channelId ] ) ;
2022-09-15 22:12:06 +00:00
} ,
2022-12-14 07:22:20 +00:00
setCardChannelItemUnsealedDetail : async ( guid , cardId , channelId , revision , unsealed ) => {
await db . current . executeSql ( ` UPDATE card_channel_ ${ guid } set unsealed_detail=? where detail_revision=? AND card_id=? AND channel_id=? ` , [ encodeObject ( unsealed ) , revision , cardId , channelId ] ) ;
} ,
2022-09-15 22:12:06 +00:00
setCardChannelItemSummary : async ( guid , cardId , channelId , revision , summary ) => {
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` UPDATE card_channel_ ${ guid } set topic_revision=?, summary=?, unsealed_summary=null where card_id=? and channel_id=? ` , [ revision , encodeObject ( summary ) , cardId , channelId ] ) ;
2022-09-15 22:12:06 +00:00
} ,
2022-12-17 21:19:07 +00:00
setCardChannelItemUnsealedSummary : async ( guid , cardId , channelId , revision , unsealed ) => {
await db . current . executeSql ( ` UPDATE card_channel_ ${ guid } set unsealed_summary=? where topic_revision=? AND card_id=? AND channel_id=? ` , [ encodeObject ( unsealed ) , revision , cardId , channelId ] ) ;
} ,
2022-09-15 22:12:06 +00:00
getCardChannelItemView : async ( guid , cardId , channelId ) => {
2022-09-20 21:57:58 +00:00
const values = await getAppValues ( db . current , ` SELECT revision, detail_revision, topic_revision FROM card_channel_ ${ guid } WHERE card_id=? and channel_id=? ` , [ cardId , channelId ] ) ;
2022-09-15 22:12:06 +00:00
if ( ! values . length ) {
return null ;
}
return {
revision : values [ 0 ] . revision ,
detailRevision : values [ 0 ] . detail _revision ,
topicRevision : values [ 0 ] . topic _revision ,
} ;
} ,
getCardChannelItems : async ( guid ) => {
2022-12-28 22:55:54 +00:00
const values = await getAppValues ( db . current , ` SELECT card_id, channel_id, read_revision, sync_revision, revision, blocked, detail_revision, topic_revision, topic_marker, detail, unsealed_detail, summary, unsealed_summary FROM card_channel_ ${ guid } ` , [ ] ) ;
2022-09-15 22:12:06 +00:00
return values . map ( channel => ( {
cardId : channel . card _id ,
channelId : channel . channel _id ,
revision : channel . revision ,
2022-09-20 20:17:20 +00:00
readRevision : channel . read _revision ,
2022-09-15 22:12:06 +00:00
detailRevision : channel . detail _revision ,
topicRevision : channel . topic _revision ,
2022-12-28 22:55:54 +00:00
topicMarker : channel . topic _marker ,
2022-09-29 18:31:55 +00:00
syncRevision : channel . sync _revision ,
2022-09-15 22:12:06 +00:00
detail : decodeObject ( channel . detail ) ,
2022-12-13 23:23:38 +00:00
unsealedDetail : decodeObject ( channel . unsealed _detail ) ,
2022-09-15 22:12:06 +00:00
summary : decodeObject ( channel . summary ) ,
2022-12-13 23:23:38 +00:00
unsealedSummary : decodeObject ( channel . unsealed _summary ) ,
2022-09-26 21:39:01 +00:00
blocked : channel . blocked ,
2022-09-15 22:12:06 +00:00
} ) ) ;
} ,
clearCardChannelItems : async ( guid , cardId ) => {
2022-09-16 18:28:54 +00:00
await db . current . executeSql ( ` DELETE FROM card_channel_ ${ guid } WHERE card_id=? ` , [ cardId ] ) ;
2022-09-15 22:12:06 +00:00
} ,
2022-09-29 06:30:22 +00:00
getCardChannelTopicItems : async ( guid , cardId , channelId ) => {
2022-12-14 07:22:20 +00:00
const values = await getAppValues ( db . current , ` SELECT topic_id, revision, blocked, detail_revision, detail, unsealed_detail FROM card_channel_topic_ ${ guid } WHERE card_id=? AND channel_id=? ` , [ cardId , channelId ] ) ;
2022-09-29 06:30:22 +00:00
return values . map ( topic => ( {
topicId : topic . topic _id ,
revision : topic . revision ,
2022-10-18 20:40:14 +00:00
blocked : topic . blocked ,
2022-09-29 06:30:22 +00:00
detailRevision : topic . detail _revision ,
detail : decodeObject ( topic . detail ) ,
2022-12-13 23:23:38 +00:00
unsealedDetail : decodeObject ( topic . unsealed _detail ) ,
2022-09-29 06:30:22 +00:00
} ) ) ;
2022-10-18 21:52:13 +00:00
} ,
2022-09-30 05:34:31 +00:00
setCardChannelTopicItem : async ( guid , cardId , channelId , topic ) => {
const { id , revision , data } = topic ;
2022-12-13 23:23:38 +00:00
await db . current . executeSql ( ` INSERT OR REPLACE INTO card_channel_topic_ ${ guid } (card_id, channel_id, topic_id, revision, detail_revision, detail, unsealed_detail) values (?, ?, ?, ?, ?, ?, null); ` , [ cardId , channelId , id , revision , data . detailRevision , encodeObject ( data . topicDetail ) ] ) ;
2022-09-29 06:30:22 +00:00
} ,
2022-12-16 07:41:51 +00:00
setCardChannelTopicItemUnsealedDetail : async ( guid , cardId , channelId , topicId , revision , unsealed ) => {
await db . current . executeSql ( ` UPDATE card_channel_topic_ ${ guid } set unsealed_detail=? where detail_revision=? AND card_id=? AND channel_id=? AND topic_id=? ` , [ encodeObject ( unsealed ) , revision , cardId , channelId , topicId ] ) ;
} ,
2022-09-29 06:30:22 +00:00
clearCardChannelTopicItem : async ( guid , cardId , channelId , topicId ) => {
2022-09-30 05:34:31 +00:00
await db . current . executeSql ( ` DELETE FROM card_channel_topic_ ${ guid } WHERE card_id=? and channel_id=? and topic_id=? ` , [ cardId , channelId , topicId ] ) ;
2022-09-29 06:30:22 +00:00
} ,
clearCardChannelTopicItems : async ( guid , cardId , channelId ) => {
2022-09-30 05:34:31 +00:00
await db . current . executeSql ( ` DELETE FROM card_channel_topic_ ${ guid } WHERE card_id=? and channel_id=? ` , [ cardId , channelId ] ) ;
2022-09-29 06:30:22 +00:00
} ,
2022-10-18 20:40:14 +00:00
setCardChannelTopicBlocked : async ( guid , cardId , channelId , topicId , blocked ) => {
await db . current . executeSql ( ` UPDATE card_channel_topic_ ${ guid } set blocked=? WHERE card_id=? and channel_id=? and topic_id=? ` , [ blocked ? 1 : 0 , cardId , channelId , topicId ] ) ;
} ,
2022-10-18 21:52:13 +00:00
getCardChannelTopicBlocked : async ( guid ) => {
const values = await getAppValues ( db . current , ` SELECT card_id, channel_id, topic_id, detail FROM card_channel_topic_ ${ guid } WHERE blocked=? ` , [ 1 ] ) ;
return values . map ( topic => ( {
cardId : topic . card _id ,
channelId : topic . channel _id ,
topicId : topic . topic _id ,
detail : decodeObject ( topic . detail ) ,
} ) ) ;
} ,
2022-09-12 22:18:27 +00:00
}
return { state , actions }
}
function decodeObject ( s : string ) {
if ( s == null ) {
return null ;
}
return JSON . parse ( s ) ;
}
function encodeObject ( o : any ) {
if ( o == null ) {
return null ;
}
return JSON . stringify ( o ) ;
}
function hasResult ( res ) {
if ( res === undefined || res [ 0 ] === undefined || res [ 0 ] . rows === undefined || res [ 0 ] . rows . length == 0 ) {
return false ;
}
return true ;
}
2022-09-14 19:18:16 +00:00
function executeSql ( sql : SQLite . SQLiteDatabase , query , params , uset ) {
return new Promise ( ( resolve , reject ) => {
sql . executeSql ( query , params , ( tx , results ) => {
resolve ( results ) ;
} ) ;
} ) ;
}
2022-09-13 18:46:28 +00:00
async function getAppValue ( sql : SQLite . SQLiteDatabase , id : string , unset ) {
2022-09-12 22:18:27 +00:00
const res = await sql . executeSql ( ` SELECT * FROM app WHERE key=' ${ id } '; ` ) ;
if ( hasResult ( res ) ) {
return decodeObject ( res [ 0 ] . rows . item ( 0 ) . value ) ;
}
2022-09-13 18:46:28 +00:00
return unset ;
2022-09-12 22:18:27 +00:00
}
2022-09-15 08:03:20 +00:00
async function getAppValues ( sql : SQLite . SQLiteDatabase , query : string , params ) {
const res = await sql . executeSql ( query , params ) ;
if ( ! hasResult ( res ) ) {
return [ ] ;
}
const values = [ ] ;
for ( let i = 0 ; i < res [ 0 ] . rows . length ; i ++ ) {
values . push ( res [ 0 ] . rows . item ( i ) ) ;
}
return values ;
}
2022-09-12 22:18:27 +00:00