mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 11:39:17 +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 { getCardProfile } from 'api/getCardProfile';
|
||||||
import { getCardDetail } from 'api/getCardDetail';
|
import { getCardDetail } from 'api/getCardDetail';
|
||||||
|
|
||||||
|
import { getContactChannelTopics } from 'api/getContactChannelTopics';
|
||||||
|
import { getContactChannelDetail } from 'api/getContactChannelDetail';
|
||||||
|
import { getContactChannelSummary } from 'api/getContactChannelSummary';
|
||||||
|
|
||||||
export function useCardContext() {
|
export function useCardContext() {
|
||||||
const [state, setState] = useState({
|
const [state, setState] = useState({
|
||||||
});
|
});
|
||||||
@ -56,27 +60,35 @@ export function useCardContext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const status = await store.actions.getCardItemStatus(guid, card.id);
|
const status = await store.actions.getCardItemStatus(guid, card.id);
|
||||||
|
const cardServer = status.cardProfile.node;
|
||||||
|
const cardToken = status.cardDetail.token;
|
||||||
if (status.detail.status === 'connected') {
|
if (status.detail.status === 'connected') {
|
||||||
|
try {
|
||||||
const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data;
|
const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data;
|
||||||
if (status.notifiedView !== notifiedView) {
|
if (status.notifiedView !== notifiedView) {
|
||||||
// TODO clear contact and channels
|
await store.actions.clearCardChannelItems(guid, card.id);
|
||||||
// TODO get articles
|
await updateCardChannelItems(guid, card.id, cardServer, cardToken, notifiedView, null);
|
||||||
await store.actions.setCardItemNotifiedArticle(guid, card.id, notifiedArticle);
|
|
||||||
// TODO get channels
|
|
||||||
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
|
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
|
||||||
|
|
||||||
await store.actions.setCardItemNotifiedView(guid, card.id, notifiedView);
|
await store.actions.setCardItemNotifiedView(guid, card.id, notifiedView);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (status.notifiedChannel != notifiedChannel) {
|
if (status.notifiedChannel != notifiedChannel) {
|
||||||
// TODO get channel delta
|
await updateCardChannelItems(guid, card.id, cardServer, cardToken, status.notifiedChannel)
|
||||||
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
|
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedView, notifiedChannel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (status.notifiedProflile != notifiedProfile) {
|
if (status.notifiedProflile != notifiedProfile) {
|
||||||
// TODO update contact profile if different
|
// TODO update contact profile if different
|
||||||
await store.actions.setCardItemNotifiedProfile(guid, card.id, notifiedProfile);
|
await store.actions.setCardItemNotifiedProfile(guid, card.id, notifiedProfile);
|
||||||
}
|
}
|
||||||
|
if (status.offsync) {
|
||||||
|
await store.actions.clearCardItemOffsync(guid, cardId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch(err) {
|
||||||
|
console.log(err);
|
||||||
|
await store.actions.setCardItemOffsync(guid, cardId);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
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 = {
|
const actions = {
|
||||||
setSession: async (access) => {
|
setSession: async (access) => {
|
||||||
const { guid, server, appToken } = access;
|
const { guid, server, appToken } = access;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { useEffect, useState, useRef, useContext } from 'react';
|
import { useEffect, useState, useRef, useContext } from 'react';
|
||||||
import SQLite from "react-native-sqlite-storage";
|
import SQLite from "react-native-sqlite-storage";
|
||||||
|
|
||||||
const DATABAG_DB = 'databag_v016.db';
|
const DATABAG_DB = 'databag_v017.db';
|
||||||
|
|
||||||
export function useStoreContext() {
|
export function useStoreContext() {
|
||||||
const [state, setState] = useState({});
|
const [state, setState] = useState({});
|
||||||
@ -12,10 +12,10 @@ export function useStoreContext() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const initSession = async (guid) => {
|
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 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 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, unique(card_id, channel_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))`);
|
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) => {
|
setCardItemNotifiedChannel: async (guid, cardId, notified) => {
|
||||||
await db.current.executeSql(`UPDATE card_${guid} set notified_channel=? where card_id=?`, [notified, cardId]);
|
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) => {
|
setCardItemDetail: async (guid, cardId, revision, detail) => {
|
||||||
await db.current.executeSql(`UPDATE card_${guid} set detail_revision=?, detail=? where card_id=?`, [revision, encodeObject(detail), cardId]);
|
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]);
|
await db.current.executeSql(`UPDATE card_${guid} set profile_revision=?, profile=? where card_id=?`, [revision, encodeObject(profile), cardId]);
|
||||||
},
|
},
|
||||||
getCardItemStatus: async (guid, 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) {
|
if (!values.length) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
detail: decodeObject(values[0].detail),
|
detail: decodeObject(values[0].detail),
|
||||||
|
profile: decodeObject(values[0].profile),
|
||||||
|
offsync: values[0].offsync,
|
||||||
notifiedView: values[0].notified_view,
|
notifiedView: values[0].notified_view,
|
||||||
notifiedArticle: values[0].notified_article,
|
notifiedArticle: values[0].notified_article,
|
||||||
notifiedProfile: values[0].notified_profile,
|
notifiedProfile: values[0].notified_profile,
|
||||||
|
Loading…
Reference in New Issue
Block a user