mirror of
https://github.com/balzack/databag.git
synced 2025-02-11 19:19:16 +00:00
saving card channels
This commit is contained in:
parent
630d8a99bb
commit
16c2385f2d
@ -4,6 +4,10 @@ import { getCards } from 'api/getCards';
|
||||
import { getCardProfile } from 'api/getCardProfile';
|
||||
import { getCardDetail } from 'api/getCardDetail';
|
||||
|
||||
import { getContactChannelTopics } from 'api/getContactChannelTopics';
|
||||
import { getContactChannelDetail } from 'api/getContactChannelDetail';
|
||||
import { getContactChannelSummary } from 'api/getContactChannelSummary';
|
||||
|
||||
export function useCardContext() {
|
||||
const [state, setState] = useState({
|
||||
});
|
||||
@ -56,27 +60,35 @@ export function useCardContext() {
|
||||
}
|
||||
|
||||
const status = await store.actions.getCardItemStatus(guid, card.id);
|
||||
const cardServer = status.cardProfile.node;
|
||||
const cardToken = status.cardDetail.token;
|
||||
if (status.detail.status === 'connected') {
|
||||
const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data;
|
||||
if (status.notifiedView !== notifiedView) {
|
||||
// TODO clear contact and channels
|
||||
// TODO get articles
|
||||
await store.actions.setCardItemNotifiedArticle(guid, card.id, notifiedArticle);
|
||||
// TODO get channels
|
||||
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
|
||||
|
||||
await store.actions.setCardItemNotifiedView(guid, card.id, notifiedView);
|
||||
}
|
||||
else {
|
||||
if (status.notifiedChannel != notifiedChannel) {
|
||||
// TODO get channel delta
|
||||
try {
|
||||
const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data;
|
||||
if (status.notifiedView !== notifiedView) {
|
||||
await store.actions.clearCardChannelItems(guid, card.id);
|
||||
await updateCardChannelItems(guid, card.id, cardServer, cardToken, notifiedView, null);
|
||||
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
|
||||
await store.actions.setCardItemNotifiedView(guid, card.id, notifiedView);
|
||||
}
|
||||
else {
|
||||
if (status.notifiedChannel != notifiedChannel) {
|
||||
await updateCardChannelItems(guid, card.id, cardServer, cardToken, status.notifiedChannel)
|
||||
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedView, notifiedChannel);
|
||||
}
|
||||
}
|
||||
if (status.notifiedProflile != notifiedProfile) {
|
||||
// TODO update contact profile if different
|
||||
await store.actions.setCardItemNotifiedProfile(guid, card.id, notifiedProfile);
|
||||
}
|
||||
if (status.offsync) {
|
||||
await store.actions.clearCardItemOffsync(guid, cardId);
|
||||
}
|
||||
}
|
||||
if (status.notifiedProflile != notifiedProfile) {
|
||||
// TODO update contact profile if different
|
||||
await store.actions.setCardItemNotifiedProfile(guid, card.id, notifiedProfile);
|
||||
}
|
||||
catch(err) {
|
||||
console.log(err);
|
||||
await store.actions.setCardItemOffsync(guid, cardId);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
@ -100,6 +112,43 @@ export function useCardContext() {
|
||||
}
|
||||
};
|
||||
|
||||
const updateCardChannelItems = async (cardId, cardServer, cardToken, notifiedView, notifiedChannel) => {
|
||||
const { guid } = session.current;
|
||||
const delta = await getContactChannels(cardServer, cardToken, notifiedView, notifiedChannel);
|
||||
for (let channel of delta) {
|
||||
if (channel.data) {
|
||||
if (channel.data.channelDetail && channel.data.channelSummary) {
|
||||
await store.actions.setCardChannelItem(guid, cardId, channel);
|
||||
}
|
||||
else {
|
||||
const { detailRevision, topicRevision, channelDetail, channelSummary } = channel.data;
|
||||
const view = await store.actions.getCardChannelItemView(guid, cardId, channel.id);
|
||||
if (view == null) {
|
||||
console.log('alert: expected channel not synced');
|
||||
let assembled = JSON.parse(JSON.stringify(channel));
|
||||
assembled.data.channelDetail = await getChannelDetail(cardServer, cardToken, channel.id);
|
||||
assembled.data.channelSummary = await getChannelSummary(cardServer, cardToken, channel.id);
|
||||
await store.actions.setCardChannelItem(guid, cardId, assembled);
|
||||
}
|
||||
else {
|
||||
if (view.detailRevision != detailRevision) {
|
||||
const detail = await getChannelDetail(cardServer, cardToken, channel.id);
|
||||
await store.actions.setCardChannelItemDetail(guid, cardId, channel.id, detailRevision, detail);
|
||||
}
|
||||
if (view.topicRevision != topicRevision) {
|
||||
const summary = await getChannelSummary(cardServer, channel.id);
|
||||
await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, topicRevision, summary);
|
||||
}
|
||||
await store.actions.setCardChannelItemRevision(guid, cardId, channel.revision);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
await store.actions.clearCardChannelItem(guid, cardId, channel.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const actions = {
|
||||
setSession: async (access) => {
|
||||
const { guid, server, appToken } = access;
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { useEffect, useState, useRef, useContext } from 'react';
|
||||
import SQLite from "react-native-sqlite-storage";
|
||||
|
||||
const DATABAG_DB = 'databag_v016.db';
|
||||
const DATABAG_DB = 'databag_v017.db';
|
||||
|
||||
export function useStoreContext() {
|
||||
const [state, setState] = useState({});
|
||||
@ -12,10 +12,10 @@ export function useStoreContext() {
|
||||
}
|
||||
|
||||
const initSession = async (guid) => {
|
||||
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_${guid} (channel_id text, revision integer, detail_revision integer, topic_revision integer, detail text, summary text, unique(channel_id))`);
|
||||
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_${guid} (channel_id text, revision integer, detail_revision integer, topic_revision integer, detail text, summary text, offsync integer, unique(channel_id))`);
|
||||
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS channel_topic_${guid} (channel_id text, topic_id text, revision integer, detail_revision integer, detail text, unique(channel_id, topic_id))`);
|
||||
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, unique(card_id))`);
|
||||
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS contact_channel_${guid} (card_id text, channel_id text, revision integer, detail_revision integer, topic_revision integer, detail text, summary text, unique(card_id, channel_id))`);
|
||||
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, unique(card_id))`);
|
||||
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS contact_channel_${guid} (card_id text, channel_id text, revision integer, detail_revision integer, topic_revision integer, detail text, summary text, offsync integer, unique(card_id, channel_id))`);
|
||||
await db.current.executeSql(`CREATE TABLE IF NOT EXISTS contact_channel_topic_${guid} (card_id text, channel_id text, topic_id text, revision integer, detail_revision integer, detail text, unique(card_id, channel_id, topic_id))`);
|
||||
}
|
||||
|
||||
@ -100,6 +100,12 @@ export function useStoreContext() {
|
||||
setCardItemNotifiedChannel: async (guid, cardId, notified) => {
|
||||
await db.current.executeSql(`UPDATE card_${guid} set notified_channel=? where card_id=?`, [notified, cardId]);
|
||||
},
|
||||
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]);
|
||||
},
|
||||
setCardItemDetail: async (guid, cardId, revision, detail) => {
|
||||
await db.current.executeSql(`UPDATE card_${guid} set detail_revision=?, detail=? where card_id=?`, [revision, encodeObject(detail), cardId]);
|
||||
},
|
||||
@ -107,12 +113,14 @@ export function useStoreContext() {
|
||||
await db.current.executeSql(`UPDATE card_${guid} set profile_revision=?, profile=? where card_id=?`, [revision, encodeObject(profile), cardId]);
|
||||
},
|
||||
getCardItemStatus: async (guid, cardId) => {
|
||||
const values = await getAppValues(db.current, `SELECT detail, notified_view, notified_article, notified_profile, notified_channel FROM card_${guid} WHERE card_id=?`, [cardId]);
|
||||
const values = await getAppValues(db.current, `SELECT detail, profile, notified_view, notified_article, notified_profile, notified_channel, offsync FROM card_${guid} WHERE card_id=?`, [cardId]);
|
||||
if (!values.length) {
|
||||
return null;
|
||||
}
|
||||
return {
|
||||
detail: decodeObject(values[0].detail),
|
||||
profile: decodeObject(values[0].profile),
|
||||
offsync: values[0].offsync,
|
||||
notifiedView: values[0].notified_view,
|
||||
notifiedArticle: values[0].notified_article,
|
||||
notifiedProfile: values[0].notified_profile,
|
||||
|
Loading…
Reference in New Issue
Block a user