fixing card channel sync

This commit is contained in:
Roland Osborne 2022-09-20 14:57:58 -07:00
parent 433781c5fa
commit c615d73fbf
4 changed files with 57 additions and 20 deletions

View File

@ -97,7 +97,7 @@ export function useCardContext() {
const setCardChannelItem = (cardId, channel) => {
let card = cards.current.get(cardId);
if (card) {
card.channels.set(channel.id, channel);
card.channels.set(channel.channelId, channel);
cards.current.set(cardId, card);
}
}
@ -118,7 +118,7 @@ export function useCardContext() {
if (card) {
let channel = card.channels.get(channelId);
if (channel) {
channel.summary = detail;
channel.summary = summary;
channel.topicRevision = revision;
card.channels.set(channelId, channel);
cards.current.set(cardId, card);
@ -135,7 +135,18 @@ export function useCardContext() {
cards.current.set(cardId, card);
}
}
}
}
const setCardChannelReadRevision = (cardId, channelId, revision) => {
let card = cards.current.get(cardId);
if (card) {
let channel = card.channels.get(channelId);
if (channel) {
channel.readRevision = revision;
card.channels.set(channelId, channel);
cards.current.set(cardId, card);
}
}
}
const clearCardChannel = (cardId, channelId) => {
let card = cards.current.get(cardId);
if (card) {
@ -154,7 +165,6 @@ export function useCardContext() {
// get and store
const delta = await getCards(server, appToken, setRevision.current);
for (let card of delta) {
if (card.data) {
if (card.data.cardDetail && card.data.cardProfile) {
@ -172,6 +182,7 @@ export function useCardContext() {
setCard(assembled.id, assembled);
}
else {
const { detailRevision, profileRevision } = card.data;
if (view.detailRevision != detailRevision) {
const detail = await getCardDetail(server, appToken, card.id);
await store.actions.setCardItemDetail(guid, card.id, detailRevision, detail);
@ -202,8 +213,8 @@ export function useCardContext() {
}
else {
if (status.notifiedChannel != notifiedChannel) {
await updateCardChannelItems(card.id, cardServer, cardToken, status.notifiedChannel)
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedView, notifiedChannel);
await updateCardChannelItems(card.id, cardServer, cardToken, status.notifiedView, status.notifiedChannel)
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
}
}
if (status.notifiedProflile != notifiedProfile) {
@ -260,19 +271,19 @@ export function useCardContext() {
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);
assembled.data.channelDetail = await getContactChannelDetail(cardServer, cardToken, channel.id);
assembled.data.channelSummary = await getContactChannelSummary(cardServer, cardToken, channel.id);
await store.actions.setCardChannelItem(guid, cardId, assembled);
setCardChannel(cardId, assembled);
}
else {
if (view.detailRevision != detailRevision) {
const detail = await getChannelDetail(cardServer, cardToken, channel.id);
const detail = await getContactChannelDetail(cardServer, cardToken, channel.id);
await store.actions.setCardChannelItemDetail(guid, cardId, channel.id, detailRevision, detail);
setCardChannelDetail(cardId, channel.id, detail, detailRevision);
}
if (view.topicRevision != topicRevision) {
const summary = await getChannelSummary(cardServer, channel.id);
const summary = await getContactChannelSummary(cardServer, cardToken, channel.id);
await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, topicRevision, summary);
setCardChannelSummary(cardId, channel.id, summary, topicRevision);
}
@ -314,6 +325,11 @@ export function useCardContext() {
curRevision.current = rev;
sync();
},
setReadRevision: async (cardId, channelId, rev) => {
await store.actions.setCardChannelItemReadRevision(session.current.guid, cardId, channelId, rev);
setCardChannelReadRevision(cardId, channelId, rev);
updateState({ cards: cards.current });
},
getCardLogo: (cardId, revision) => {
const { server, appToken } = session.current;
return getCardImageUrl(server, appToken, cardId, revision);

View File

@ -1,7 +1,7 @@
import { useEffect, useState, useRef, useContext } from 'react';
import SQLite from "react-native-sqlite-storage";
const DATABAG_DB = 'databag_v026.db';
const DATABAG_DB = 'databag_v027.db';
export function useStoreContext() {
const [state, setState] = useState({});
@ -125,7 +125,7 @@ export function useStoreContext() {
notifiedView: values[0].notified_view,
notifiedArticle: values[0].notified_article,
notifiedProfile: values[0].notified_profile,
notifiedChannel: values[0].notified_cahnnel,
notifiedChannel: values[0].notified_channel,
offsync: values[0].offsync,
};
},
@ -227,7 +227,7 @@ export function useStoreContext() {
await db.current.executeSql(`UPDATE card_channel_${guid} set topic_revision=?, summary=? where card_id=? and channel_id=?`, [revision, encodeObject(summary), cardId, channelId]);
},
getCardChannelItemView: async (guid, cardId, channelId) => {
const values = await getAppValues(db.current, `SELECT revision, detail_revision, topic_revision FROM card_channel_${guid} WHERE channel_id=?`, [cardId, channelId]);
const values = await getAppValues(db.current, `SELECT revision, detail_revision, topic_revision FROM card_channel_${guid} WHERE card_id=? and channel_id=?`, [cardId, channelId]);
if (!values.length) {
return null;
}

View File

@ -8,6 +8,7 @@ export function useChannelItem(item) {
const [state, setState] = useState({});
const channel = useContext(ChannelContext);
const card = useContext(CardContext);
const updateState = (value) => {
setState((s) => ({ ...s, ...value }));
@ -15,7 +16,12 @@ export function useChannelItem(item) {
const actions = {
setRead: () => {
channel.actions.setReadRevision(item.channelId, item.revision);
if (item.cardId) {
card.actions.setReadRevision(item.cardId, item.channelId, item.revision);
}
else {
channel.actions.setReadRevision(item.channelId, item.revision);
}
},
};

View File

@ -3,6 +3,7 @@ import { useWindowDimensions } from 'react-native';
import { useNavigate } from 'react-router-dom';
import { CardContext } from 'context/CardContext';
import { ChannelContext } from 'context/ChannelContext';
import { ProfileContext } from 'context/ProfileContext';
import { AppContext } from 'context/AppContext';
import config from 'constants/Config';
@ -16,6 +17,7 @@ export function useChannels() {
const items = useRef([]);
const channel = useContext(ChannelContext);
const card = useContext(CardContext);
const profile = useContext(ProfileContext);
const app = useContext(AppContext);
const updateState = (value) => {
@ -44,9 +46,16 @@ export function useChannels() {
}
let contacts = [];
if (item.cardId) {
contacts.push(card.state.cards.get(item.cardId));
}
if (item?.detail?.members) {
item.detail.members.forEach(guid => {
contacts.push(getCard(guid));
const profileGuid = profile.state.profile.guid;
if (profileGuid !== guid) {
contacts.push(getCard(guid));
}
})
}
@ -82,7 +91,7 @@ export function useChannels() {
if (contact?.profile?.name) {
names.push(contact.profile.name);
}
else {
else if (contact?.profile?.handle) {
names.push(contact?.profile?.handle);
}
}
@ -103,12 +112,17 @@ export function useChannels() {
}
}
return { channelId: item.channelId, contacts, logo, subject, message, updated, revision: item.revision };
return { cardId: item.cardId, channelId: item.channelId, contacts, logo, subject, message, updated, revision: item.revision };
}
useEffect(() => {
let channels = Array.from(channel.state.channels.values())
channels.sort((a, b) => {
let merged = [];
card.state.cards.forEach((card, cardId, map) => {
merged.push(...Array.from(card.channels.values()));
});
merged.push(...Array.from(channel.state.channels.values()));
merged.sort((a, b) => {
const aCreated = a?.summary?.lastTopic?.created;
const bCreated = b?.summary?.lastTopic?.created;
if (aCreated === bCreated) {
@ -119,7 +133,8 @@ export function useChannels() {
}
return -1;
});
updateState({ channels: channels.map(item => setChannelEntry(item)) });
updateState({ channels: merged.map(item => setChannelEntry(item)) });
}, [channel, card]);
const actions = {