2022-09-15 08:03:20 +00:00
|
|
|
import { useState, useRef, useContext } from 'react';
|
|
|
|
import { StoreContext } from 'context/StoreContext';
|
2022-10-07 05:52:28 +00:00
|
|
|
import { UploadContext } from 'context/UploadContext';
|
2022-10-31 07:32:27 +00:00
|
|
|
import { addFlag } from 'api/addFlag';
|
2022-10-25 07:06:39 +00:00
|
|
|
import { getCard } from 'api/getCard';
|
2022-09-15 18:26:03 +00:00
|
|
|
import { getCards } from 'api/getCards';
|
|
|
|
import { getCardProfile } from 'api/getCardProfile';
|
2022-10-23 06:57:44 +00:00
|
|
|
import { setCardProfile } from 'api/setCardProfile';
|
2022-09-15 18:26:03 +00:00
|
|
|
import { getCardDetail } from 'api/getCardDetail';
|
2022-09-15 08:03:20 +00:00
|
|
|
|
2022-10-23 06:57:44 +00:00
|
|
|
import { getContactProfile } from 'api/getContactProfile';
|
2022-09-16 18:28:54 +00:00
|
|
|
import { getContactChannels } from 'api/getContactChannels';
|
2022-09-16 06:18:46 +00:00
|
|
|
import { getContactChannelDetail } from 'api/getContactChannelDetail';
|
|
|
|
import { getContactChannelSummary } from 'api/getContactChannelSummary';
|
2022-09-20 07:50:53 +00:00
|
|
|
import { getCardImageUrl } from 'api/getCardImageUrl';
|
2022-09-16 06:18:46 +00:00
|
|
|
|
2022-09-26 19:14:06 +00:00
|
|
|
import { addCard } from 'api/addCard';
|
|
|
|
import { removeCard } from 'api/removeCard';
|
|
|
|
import { setCardConnecting, setCardConnected, setCardConfirmed } from 'api/setCardStatus';
|
|
|
|
import { getCardOpenMessage } from 'api/getCardOpenMessage';
|
|
|
|
import { setCardOpenMessage } from 'api/setCardOpenMessage';
|
|
|
|
import { getCardCloseMessage } from 'api/getCardCloseMessage';
|
|
|
|
import { setCardCloseMessage } from 'api/setCardCloseMessage';
|
|
|
|
|
2022-09-29 18:31:55 +00:00
|
|
|
import { getContactChannelTopic } from 'api/getContactChannelTopic';
|
|
|
|
import { getContactChannelTopics } from 'api/getContactChannelTopics';
|
|
|
|
import { getContactChannelTopicAssetUrl } from 'api/getContactChannelTopicAssetUrl';
|
|
|
|
import { addContactChannelTopic } from 'api/addContactChannelTopic';
|
|
|
|
import { setContactChannelTopicSubject } from 'api/setContactChannelTopicSubject';
|
|
|
|
import { removeContactChannel } from 'api/removeContactChannel';
|
|
|
|
import { removeContactChannelTopic } from 'api/removeContactChannelTopic';
|
|
|
|
|
2022-11-14 21:30:58 +00:00
|
|
|
import { getContactChannelNotifications } from 'api/getContactChannelNotifications';
|
|
|
|
import { setContactChannelNotifications } from 'api/setContactChannelNotifications';
|
|
|
|
|
2022-09-15 08:03:20 +00:00
|
|
|
export function useCardContext() {
|
|
|
|
const [state, setState] = useState({
|
2022-09-20 05:39:53 +00:00
|
|
|
cards: new Map(),
|
2022-10-14 22:15:45 +00:00
|
|
|
requestRevision: null,
|
2022-09-15 08:03:20 +00:00
|
|
|
});
|
|
|
|
const store = useContext(StoreContext);
|
2022-10-07 05:52:28 +00:00
|
|
|
const upload = useContext(UploadContext);
|
2022-09-15 08:03:20 +00:00
|
|
|
|
|
|
|
const session = useRef(null);
|
|
|
|
const curRevision = useRef(null);
|
|
|
|
const setRevision = useRef(null);
|
|
|
|
const syncing = useRef(false);
|
2022-09-20 05:39:53 +00:00
|
|
|
const cards = useRef(new Map());
|
|
|
|
const cardChannels = useRef(new Map());
|
2022-10-25 07:06:39 +00:00
|
|
|
const resync = useRef([]);
|
2022-09-15 08:03:20 +00:00
|
|
|
|
|
|
|
const updateState = (value) => {
|
|
|
|
setState((s) => ({ ...s, ...value }))
|
|
|
|
}
|
|
|
|
|
2022-10-25 07:06:39 +00:00
|
|
|
const getCardEntry = (cardId) => {
|
2022-09-29 22:21:18 +00:00
|
|
|
const card = cards.current.get(cardId);
|
|
|
|
if (!card) {
|
2022-10-25 07:06:39 +00:00
|
|
|
throw new Error('card not found');
|
2022-09-29 22:21:18 +00:00
|
|
|
}
|
|
|
|
return card;
|
|
|
|
}
|
|
|
|
|
2022-09-20 07:50:53 +00:00
|
|
|
const setCard = (cardId, card) => {
|
|
|
|
let updated = cards.current.get(cardId);
|
|
|
|
if (updated == null) {
|
|
|
|
updated = { channels: new Map() };
|
|
|
|
}
|
|
|
|
cards.current.set(cardId, {
|
|
|
|
...updated,
|
|
|
|
cardId: cardId,
|
|
|
|
revision: card?.revision,
|
|
|
|
detailRevision: card?.data?.detailRevision,
|
|
|
|
profileRevision: card?.data?.profileRevision,
|
|
|
|
detail: card?.data?.cardDetail,
|
|
|
|
profile: card?.data?.cardProfile,
|
|
|
|
notifiedView: card?.data?.notifiedView,
|
|
|
|
notifiedProfile: card?.data?.notifiedProfile,
|
|
|
|
notifiedArtile: card?.data?.notifiedArticle,
|
|
|
|
notifiedChannel: card?.data?.notifiedChannel,
|
|
|
|
});
|
|
|
|
}
|
2022-09-20 05:39:53 +00:00
|
|
|
const setCardDetail = (cardId, detail, revision) => {
|
|
|
|
let card = cards.current.get(cardId);
|
2022-09-20 07:50:53 +00:00
|
|
|
if (card) {
|
|
|
|
card.detail = detail;
|
|
|
|
card.detailRevision = revision;
|
2022-09-20 05:39:53 +00:00
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
2022-10-23 06:57:44 +00:00
|
|
|
const setCardIdentity = (cardId, profile, revision) => {
|
2022-09-20 05:39:53 +00:00
|
|
|
let card = cards.current.get(cardId);
|
2022-09-20 07:50:53 +00:00
|
|
|
if (card) {
|
|
|
|
card.profile = profile;
|
|
|
|
card.profileRevision = revision;
|
2022-09-20 05:39:53 +00:00
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const setCardRevision = (cardId, revision) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
card.revision = revision;
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const setCardOffsync = (cardId, offsync) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
card.offsync = offsync;
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 21:39:01 +00:00
|
|
|
const setCardBlocked = (cardId, blocked) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
card.blocked = blocked;
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 05:39:53 +00:00
|
|
|
const clearCardChannels = (cardId) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
card.channels = new Map();
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const setCardChannel = (cardId, channel) => {
|
2022-09-20 07:50:53 +00:00
|
|
|
setCardChannelItem(cardId, {
|
|
|
|
cardId: cardId,
|
|
|
|
channelId: channel?.id,
|
|
|
|
revision: channel?.revision,
|
|
|
|
detailRevision: channel?.data?.detailRevision,
|
|
|
|
topicRevision: channel?.data?.topicRevision,
|
|
|
|
detail: channel?.data?.channelDetail,
|
2022-12-13 23:23:38 +00:00
|
|
|
unsealed_detail: null,
|
2022-09-20 07:50:53 +00:00
|
|
|
summary: channel?.data?.channelSummary,
|
2022-12-13 23:23:38 +00:00
|
|
|
unsealed_summary: null,
|
2022-09-20 07:50:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
const setCardChannelItem = (cardId, channel) => {
|
2022-09-20 05:39:53 +00:00
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
2022-09-20 21:57:58 +00:00
|
|
|
card.channels.set(channel.channelId, channel);
|
2022-09-20 05:39:53 +00:00
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const setCardChannelDetail = (cardId, channelId, detail, revision) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
let channel = card.channels.get(channelId);
|
2022-09-20 07:50:53 +00:00
|
|
|
if (channel) {
|
|
|
|
channel.detail = detail;
|
2022-12-13 23:23:38 +00:00
|
|
|
channel.unsealed_detail = null;
|
2022-09-20 07:50:53 +00:00
|
|
|
channel.detailRevision = revision;
|
2022-09-20 05:39:53 +00:00
|
|
|
card.channels.set(channelId, channel);
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const setCardChannelSummary = (cardId, channelId, summary, revision) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
let channel = card.channels.get(channelId);
|
2022-09-20 07:50:53 +00:00
|
|
|
if (channel) {
|
2022-09-20 21:57:58 +00:00
|
|
|
channel.summary = summary;
|
2022-12-13 23:23:38 +00:00
|
|
|
channel.unsealed_summary = null;
|
2022-09-20 07:50:53 +00:00
|
|
|
channel.topicRevision = revision;
|
2022-09-20 05:39:53 +00:00
|
|
|
card.channels.set(channelId, channel);
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const setCardChannelRevision = (cardId, channelId, revision) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
let channel = card.channels.get(channelId);
|
|
|
|
if (channel) {
|
|
|
|
channel.revision = revision;
|
|
|
|
card.channels.set(channelId, channel);
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 21:57:58 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-29 18:31:55 +00:00
|
|
|
const setCardChannelSyncRevision = (cardId, channelId, revision) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
let channel = card.channels.get(channelId);
|
|
|
|
if (channel) {
|
|
|
|
channel.syncRevision = revision;
|
|
|
|
card.channels.set(channelId, channel);
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-11 19:18:08 +00:00
|
|
|
const setCardChannelBlocked = (cardId, channelId, blocked) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
let channel = card.channels.get(channelId);
|
|
|
|
if (channel) {
|
|
|
|
channel.blocked = blocked;
|
|
|
|
card.channels.set(channelId, channel);
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-20 05:39:53 +00:00
|
|
|
const clearCardChannel = (cardId, channelId) => {
|
|
|
|
let card = cards.current.get(cardId);
|
|
|
|
if (card) {
|
|
|
|
card.channels.delete(channelId);
|
|
|
|
cards.current.set(cardId, card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 08:03:20 +00:00
|
|
|
const sync = async () => {
|
2022-10-25 07:06:39 +00:00
|
|
|
if (!syncing.current && (setRevision.current !== curRevision.current || resync.current.length > 0)) {
|
2022-09-15 08:03:20 +00:00
|
|
|
syncing.current = true;
|
2022-10-25 07:06:39 +00:00
|
|
|
const { server, appToken, guid } = session.current;
|
2022-09-15 08:03:20 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const revision = curRevision.current;
|
|
|
|
|
|
|
|
// get and store
|
2022-09-15 18:26:03 +00:00
|
|
|
const delta = await getCards(server, appToken, setRevision.current);
|
|
|
|
for (let card of delta) {
|
|
|
|
if (card.data) {
|
|
|
|
if (card.data.cardDetail && card.data.cardProfile) {
|
|
|
|
await store.actions.setCardItem(guid, card);
|
2022-09-20 07:50:53 +00:00
|
|
|
setCard(card.id, card);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const view = await store.actions.getCardItemView(guid, card.id);
|
2022-09-15 22:12:06 +00:00
|
|
|
if (view == null) {
|
|
|
|
let assembled = JSON.parse(JSON.stringify(card));
|
|
|
|
assembled.data.cardDetail = await getCardDetail(server, appToken, card.id);
|
|
|
|
assembled.data.cardProfile = await getCardProfile(server, appToken, card.id);
|
|
|
|
await store.actions.setCardItem(guid, assembled);
|
2022-09-20 07:50:53 +00:00
|
|
|
setCard(assembled.id, assembled);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
2022-09-15 22:12:06 +00:00
|
|
|
else {
|
2022-09-20 21:57:58 +00:00
|
|
|
const { detailRevision, profileRevision } = card.data;
|
2022-09-15 22:12:06 +00:00
|
|
|
if (view.detailRevision != detailRevision) {
|
|
|
|
const detail = await getCardDetail(server, appToken, card.id);
|
|
|
|
await store.actions.setCardItemDetail(guid, card.id, detailRevision, detail);
|
2022-09-20 05:39:53 +00:00
|
|
|
setCardDetail(card.id, detail, detailRevision);
|
2022-09-15 22:12:06 +00:00
|
|
|
}
|
|
|
|
if (view.profileRevision != profileRevision) {
|
|
|
|
const profile = await getCardProfile(server, appToken, card.id);
|
|
|
|
await store.actions.setCardItemProfile(guid, card.id, profileRevision, profile);
|
2022-10-23 06:57:44 +00:00
|
|
|
setCardIdentity(card.id, profile, profileRevision);
|
2022-09-15 22:12:06 +00:00
|
|
|
}
|
2022-09-20 05:39:53 +00:00
|
|
|
await store.actions.setCardItemRevision(guid, card.id, card.revision);
|
|
|
|
setCardRevision(card.id, card.revision);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
2022-09-15 22:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-10-25 07:06:39 +00:00
|
|
|
await syncCard(card);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-09-15 22:12:06 +00:00
|
|
|
await store.actions.clearCardChannelItems(guid, card.id);
|
2022-09-15 18:26:03 +00:00
|
|
|
await store.actions.clearCardItem(guid, card.id);
|
2022-09-20 05:39:53 +00:00
|
|
|
cards.current.delete(card.id);
|
2022-09-15 18:26:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-15 08:03:20 +00:00
|
|
|
|
|
|
|
setRevision.current = revision;
|
2022-09-15 18:26:03 +00:00
|
|
|
await store.actions.setCardRevision(guid, revision);
|
2022-09-15 08:03:20 +00:00
|
|
|
}
|
|
|
|
catch(err) {
|
2022-09-20 07:50:53 +00:00
|
|
|
console.log("card2:", err);
|
2022-09-15 08:03:20 +00:00
|
|
|
syncing.current = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-25 07:06:39 +00:00
|
|
|
if (resync.current.length > 0) {
|
|
|
|
const ids = resync.current;
|
|
|
|
resync.current = [];
|
|
|
|
|
|
|
|
for(let i = 0; i < ids.length; i++) {
|
|
|
|
const item = cards.current.get(ids[i]);
|
|
|
|
if (item) {
|
|
|
|
const card = await getCard(server, appToken, ids[i]);
|
|
|
|
await syncCard(card);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-20 05:39:53 +00:00
|
|
|
updateState({ cards: cards.current });
|
2022-09-15 08:03:20 +00:00
|
|
|
syncing.current = false;
|
|
|
|
sync();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-25 07:06:39 +00:00
|
|
|
const syncCard = async (card) => {
|
|
|
|
|
|
|
|
const { server, appToken, guid } = session.current;
|
|
|
|
const status = await store.actions.getCardItemStatus(guid, card.id);
|
|
|
|
const cardServer = status.profile.node;
|
|
|
|
const cardToken = status.profile.guid + '.' + status.detail.token;
|
|
|
|
if (status.detail.status === 'connected') {
|
|
|
|
try {
|
|
|
|
const { notifiedView, notifiedProfile, notifiedArticle, notifiedChannel } = card.data;
|
|
|
|
if (status.notifiedView !== notifiedView) {
|
|
|
|
await store.actions.clearCardChannelItems(guid, card.id);
|
2022-12-14 05:55:14 +00:00
|
|
|
clearCardChannels(card.id);
|
2022-10-25 07:06:39 +00:00
|
|
|
await updateCardChannelItems(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(card.id, cardServer, cardToken, status.notifiedView, status.notifiedChannel)
|
|
|
|
await store.actions.setCardItemNotifiedChannel(guid, card.id, notifiedChannel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status.notifiedProfile != notifiedProfile) {
|
|
|
|
const message = await getContactProfile(cardServer, cardToken);
|
|
|
|
await setCardProfile(server, appToken, card.id, message);
|
|
|
|
await store.actions.setCardItemNotifiedProfile(guid, card.id, notifiedProfile);
|
|
|
|
}
|
|
|
|
if (status.offsync) {
|
|
|
|
await store.actions.clearCardItemOffsync(guid, card.id);
|
|
|
|
setCardOffsync(card.id, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(err) {
|
|
|
|
console.log("card1:", err);
|
|
|
|
await store.actions.setCardItemOffsync(guid, card.id);
|
|
|
|
setCardOffsync(card.id, 1);
|
|
|
|
}
|
|
|
|
}
|
2022-11-16 17:50:42 +00:00
|
|
|
else {
|
|
|
|
await store.actions.clearCardChannelItems(guid, card.id);
|
|
|
|
clearCardChannels(card.id);
|
|
|
|
}
|
2022-10-25 07:06:39 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 06:18:46 +00:00
|
|
|
const updateCardChannelItems = async (cardId, cardServer, cardToken, notifiedView, notifiedChannel) => {
|
|
|
|
const { guid } = session.current;
|
|
|
|
const delta = await getContactChannels(cardServer, cardToken, notifiedView, notifiedChannel);
|
2022-12-14 05:55:14 +00:00
|
|
|
|
2022-09-16 06:18:46 +00:00
|
|
|
for (let channel of delta) {
|
|
|
|
if (channel.data) {
|
|
|
|
if (channel.data.channelDetail && channel.data.channelSummary) {
|
|
|
|
await store.actions.setCardChannelItem(guid, cardId, channel);
|
2022-09-20 05:39:53 +00:00
|
|
|
setCardChannel(cardId, channel);
|
2022-09-16 06:18:46 +00:00
|
|
|
}
|
|
|
|
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));
|
2022-09-20 21:57:58 +00:00
|
|
|
assembled.data.channelDetail = await getContactChannelDetail(cardServer, cardToken, channel.id);
|
|
|
|
assembled.data.channelSummary = await getContactChannelSummary(cardServer, cardToken, channel.id);
|
2022-09-16 06:18:46 +00:00
|
|
|
await store.actions.setCardChannelItem(guid, cardId, assembled);
|
2022-09-20 05:39:53 +00:00
|
|
|
setCardChannel(cardId, assembled);
|
2022-09-16 06:18:46 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (view.detailRevision != detailRevision) {
|
2022-09-20 21:57:58 +00:00
|
|
|
const detail = await getContactChannelDetail(cardServer, cardToken, channel.id);
|
2022-09-16 06:18:46 +00:00
|
|
|
await store.actions.setCardChannelItemDetail(guid, cardId, channel.id, detailRevision, detail);
|
2022-09-20 05:39:53 +00:00
|
|
|
setCardChannelDetail(cardId, channel.id, detail, detailRevision);
|
2022-09-16 06:18:46 +00:00
|
|
|
}
|
|
|
|
if (view.topicRevision != topicRevision) {
|
2022-09-20 21:57:58 +00:00
|
|
|
const summary = await getContactChannelSummary(cardServer, cardToken, channel.id);
|
2022-09-16 06:18:46 +00:00
|
|
|
await store.actions.setCardChannelItemSummary(guid, cardId, channel.id, topicRevision, summary);
|
2022-09-20 05:39:53 +00:00
|
|
|
setCardChannelSummary(cardId, channel.id, summary, topicRevision);
|
2022-09-16 06:18:46 +00:00
|
|
|
}
|
2022-10-06 22:34:29 +00:00
|
|
|
await store.actions.setCardChannelItemRevision(guid, cardId, channel.id, channel.revision);
|
2022-09-20 05:39:53 +00:00
|
|
|
setCardChannelRevision(cardId, channel.id, channel.revision);
|
2022-09-16 06:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
await store.actions.clearCardChannelItem(guid, cardId, channel.id);
|
2022-09-20 05:39:53 +00:00
|
|
|
clearCardChannel(cardId, channel.id);
|
2022-09-16 06:18:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-15 08:03:20 +00:00
|
|
|
const actions = {
|
|
|
|
setSession: async (access) => {
|
|
|
|
const { guid, server, appToken } = access;
|
2022-09-20 05:39:53 +00:00
|
|
|
cards.current = new Map();
|
2022-10-14 22:15:45 +00:00
|
|
|
const status = await store.actions.getCardRequestStatus(guid);
|
|
|
|
updateState({ requestRevision: status.revision });
|
2022-09-20 05:39:53 +00:00
|
|
|
const cardItems = await store.actions.getCardItems(guid);
|
|
|
|
for (item of cardItems) {
|
|
|
|
cards.current.set(item.cardId, { ...item, channels: new Map() });
|
|
|
|
}
|
|
|
|
const cardChannelItems = await store.actions.getCardChannelItems(guid);
|
|
|
|
for (item of cardChannelItems) {
|
2022-09-20 07:50:53 +00:00
|
|
|
setCardChannelItem(item.cardId, item);
|
2022-09-20 05:39:53 +00:00
|
|
|
}
|
2022-09-15 08:03:20 +00:00
|
|
|
const revision = await store.actions.getCardRevision(guid);
|
2022-09-20 05:39:53 +00:00
|
|
|
updateState({ cards: cards.current });
|
2022-09-15 08:03:20 +00:00
|
|
|
setRevision.current = revision;
|
|
|
|
curRevision.current = revision;
|
|
|
|
session.current = access;
|
|
|
|
},
|
2022-10-14 22:15:45 +00:00
|
|
|
setRequestRevision: async (revision) => {
|
|
|
|
const { guid } = session.current
|
|
|
|
await store.actions.setCardRequestStatus(guid, { revision });
|
|
|
|
updateState({ requestRevision: revision });
|
|
|
|
},
|
2022-09-15 08:03:20 +00:00
|
|
|
clearSession: () => {
|
|
|
|
session.current = {};
|
|
|
|
updateState({ account: null });
|
|
|
|
},
|
|
|
|
setRevision: (rev) => {
|
|
|
|
curRevision.current = rev;
|
|
|
|
sync();
|
|
|
|
},
|
2022-10-04 18:52:22 +00:00
|
|
|
setChannelReadRevision: async (cardId, channelId, rev) => {
|
2022-09-20 21:57:58 +00:00
|
|
|
await store.actions.setCardChannelItemReadRevision(session.current.guid, cardId, channelId, rev);
|
|
|
|
setCardChannelReadRevision(cardId, channelId, rev);
|
|
|
|
updateState({ cards: cards.current });
|
|
|
|
},
|
2022-09-20 07:50:53 +00:00
|
|
|
getCardLogo: (cardId, revision) => {
|
|
|
|
const { server, appToken } = session.current;
|
|
|
|
return getCardImageUrl(server, appToken, cardId, revision);
|
2022-09-25 05:56:41 +00:00
|
|
|
},
|
|
|
|
getByGuid: (guid) => {
|
|
|
|
let card;
|
|
|
|
cards.current.forEach((value, key, map) => {
|
|
|
|
if (value?.profile?.guid === guid) {
|
|
|
|
card = value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return card;
|
|
|
|
},
|
2022-09-26 19:14:06 +00:00
|
|
|
addCard: async (message) => {
|
|
|
|
const { server, appToken } = session.current;
|
|
|
|
return await addCard(server, appToken, message);
|
|
|
|
},
|
|
|
|
removeCard: async (cardId) => {
|
|
|
|
const { server, appToken } = session.current;
|
|
|
|
return await removeCard(server, appToken, cardId);
|
|
|
|
},
|
|
|
|
setCardConnecting: async (cardId) => {
|
|
|
|
const { server, appToken } = session.current;
|
|
|
|
return await setCardConnecting(server, appToken, cardId);
|
|
|
|
},
|
|
|
|
setCardConnected: async (cardId, token, rev) => {
|
|
|
|
const { server, appToken } = session.current;
|
|
|
|
return await setCardConnected(server, appToken, cardId, token,
|
|
|
|
rev.viewRevision, rev.articleRevision, rev.channelRevision, rev.profileRevision);
|
|
|
|
},
|
|
|
|
setCardConfirmed: async (cardId) => {
|
|
|
|
const { server, appToken } = session.current;
|
|
|
|
return await setCardConfirmed(server, appToken, cardId);
|
|
|
|
},
|
|
|
|
getCardOpenMessage: async (cardId) => {
|
|
|
|
const { server, appToken } = session.current;
|
|
|
|
return await getCardOpenMessage(server, appToken, cardId);
|
|
|
|
},
|
|
|
|
setCardOpenMessage: async (server, message) => {
|
|
|
|
return await setCardOpenMessage(server, message);
|
|
|
|
},
|
|
|
|
getCardCloseMessage: async (cardId) => {
|
|
|
|
const { server, appToken } = session.current;
|
|
|
|
return await getCardCloseMessage(server, appToken, cardId);
|
|
|
|
},
|
|
|
|
setCardCloseMessage: async (server, message) => {
|
|
|
|
return await setCardCloseMessage(server, message);
|
|
|
|
},
|
2022-09-26 21:39:01 +00:00
|
|
|
setCardBlocked: async (cardId) => {
|
|
|
|
const { guid } = session.current;
|
|
|
|
setCardBlocked(cardId, true);
|
|
|
|
await store.actions.setCardItemBlocked(guid, cardId);
|
|
|
|
updateState({ cards: cards.current });
|
|
|
|
},
|
|
|
|
clearCardBlocked: async (cardId) => {
|
|
|
|
const { guid } = session.current;
|
|
|
|
setCardBlocked(cardId, false);
|
|
|
|
await store.actions.clearCardItemBlocked(guid, cardId);
|
|
|
|
updateState({ cards: cards.current });
|
2022-09-29 06:30:22 +00:00
|
|
|
},
|
|
|
|
setSyncRevision: async (cardId, channelId, revision) => {
|
|
|
|
const { guid } = session.current;
|
2022-09-29 18:31:55 +00:00
|
|
|
await store.actions.setCardChannelItemSyncRevision(guid, cardId, channelId, revision);
|
|
|
|
setCardChannelSyncRevision(cardId, channelId, revision);
|
|
|
|
updateState({ cards: cards.current });
|
2022-09-29 06:30:22 +00:00
|
|
|
},
|
2022-10-11 19:18:08 +00:00
|
|
|
setChannelBlocked: async (cardId, channelId) => {
|
|
|
|
const { guid } = session.current;
|
|
|
|
await store.actions.setCardChannelItemBlocked(guid, cardId, channelId);
|
|
|
|
setCardChannelBlocked(cardId, channelId, true);
|
|
|
|
updateState({ cards: cards.current });
|
|
|
|
},
|
|
|
|
clearChannelBlocked: async (cardId, channelId) => {
|
|
|
|
const { guid } = session.current;
|
|
|
|
await store.actions.clearCardChannelItemBlocked(guid, cardId, channelId);
|
|
|
|
setCardChannelBlocked(cardId, channelId, false);
|
|
|
|
updateState({ cards: cards.current });
|
|
|
|
},
|
2022-10-18 20:40:14 +00:00
|
|
|
setChannelTopicBlocked: async (cardId, channelId, topicId) => {
|
|
|
|
const { guid } = session.current;
|
|
|
|
await store.actions.setCardChannelTopicBlocked(guid, cardId, channelId, topicId, true);
|
|
|
|
},
|
2022-10-18 21:52:13 +00:00
|
|
|
clearChannelTopicBlocked: async (cardId, channelId, topicId) => {
|
|
|
|
const { guid } = session.current;
|
|
|
|
await store.actions.setCardChannelTopicBlocked(guid, cardId, channelId, topicId, false);
|
|
|
|
},
|
|
|
|
getChannelTopicBlocked: async () => {
|
|
|
|
const { guid } = session.current;
|
|
|
|
return await store.actions.getCardChannelTopicBlocked(guid);
|
|
|
|
},
|
2022-09-29 18:31:55 +00:00
|
|
|
getChannelTopicItems: async (cardId, channelId) => {
|
2022-09-29 06:30:22 +00:00
|
|
|
const { guid } = session.current;
|
|
|
|
return await store.actions.getCardChannelTopicItems(guid, cardId, channelId);
|
|
|
|
},
|
2022-09-29 23:00:48 +00:00
|
|
|
setChannelTopicItem: async (cardId, channelId, topicId, topic) => {
|
2022-09-29 06:30:22 +00:00
|
|
|
const { guid } = session.current;
|
2022-09-29 23:00:48 +00:00
|
|
|
return await store.actions.setCardChannelTopicItem(guid, cardId, channelId, topicId, topic);
|
2022-09-29 06:30:22 +00:00
|
|
|
},
|
2022-09-29 18:31:55 +00:00
|
|
|
clearChannelTopicItem: async (cardId, channelId, topicId) => {
|
2022-09-29 06:30:22 +00:00
|
|
|
const { guid } = session.current;
|
|
|
|
return await store.actions.clearCardChannelTopicItem(guid, cardId, channelId, topicId);
|
|
|
|
},
|
2022-09-29 18:31:55 +00:00
|
|
|
clearChannelTopicItems: async (cardId, channelId) => {
|
2022-09-29 06:30:22 +00:00
|
|
|
const { guid } = session.current;
|
|
|
|
return await store.actions.clearCardChannelTopicItems(guid, cardId, channelId);
|
|
|
|
},
|
2022-09-29 18:31:55 +00:00
|
|
|
getChannelTopic: async (cardId, channelId, topicId) => {
|
2022-10-25 07:06:39 +00:00
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
2022-09-29 23:00:48 +00:00
|
|
|
return await getContactChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId);
|
2022-09-29 18:31:55 +00:00
|
|
|
},
|
2022-09-29 22:21:18 +00:00
|
|
|
getChannelTopics: async (cardId, channelId, revision) => {
|
2022-10-25 07:06:39 +00:00
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
2022-09-29 23:00:48 +00:00
|
|
|
return await getContactChannelTopics(profile.node, `${profile.guid}.${detail.token}`, channelId, revision);
|
2022-09-29 18:31:55 +00:00
|
|
|
},
|
2022-09-29 22:21:18 +00:00
|
|
|
getChannelTopicAssetUrl: (cardId, channelId, topicId, assetId) => {
|
2022-10-25 07:06:39 +00:00
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
2022-09-29 23:00:48 +00:00
|
|
|
return getContactChannelTopicAssetUrl(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId, assetId);
|
2022-09-29 18:31:55 +00:00
|
|
|
},
|
2022-10-07 05:52:28 +00:00
|
|
|
addChannelTopic: async (cardId, channelId, message, files) => {
|
2022-10-25 07:06:39 +00:00
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
2022-10-07 05:52:28 +00:00
|
|
|
const node = profile.node;
|
|
|
|
const token = `${profile.guid}.${detail.token}`;
|
|
|
|
if (files?.length > 0) {
|
|
|
|
const topicId = await addContactChannelTopic(node, token, channelId, null, null);
|
|
|
|
upload.actions.addContactTopic(node, token, cardId, channelId, topicId, files, async (assets) => {
|
|
|
|
message.assets = assets;
|
|
|
|
await setContactChannelTopicSubject(node, token, channelId, topicId, message);
|
|
|
|
}, async () => {
|
|
|
|
try {
|
|
|
|
await removeContactChannelTopic(node, token, channelId, topicId);
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
}
|
|
|
|
});
|
2022-10-06 22:34:29 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-10-07 05:52:28 +00:00
|
|
|
await addContactChannelTopic(node, token, channelId, message, []);
|
2022-10-06 22:34:29 +00:00
|
|
|
}
|
2022-09-29 18:31:55 +00:00
|
|
|
},
|
|
|
|
setChannelTopicSubject: async (cardId, channelId, topicId, data) => {
|
2022-10-25 07:06:39 +00:00
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
2022-09-29 23:00:48 +00:00
|
|
|
return await setContactChannelTopicSubject(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId, data);
|
2022-09-29 18:31:55 +00:00
|
|
|
},
|
|
|
|
removeChannel: async (cardId, channelId) => {
|
2022-10-25 07:06:39 +00:00
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
2022-10-10 06:55:08 +00:00
|
|
|
return await removeContactChannel(profile.node, `${profile.guid}.${detail.token}`, channelId);
|
2022-09-29 18:31:55 +00:00
|
|
|
},
|
|
|
|
removeChannelTopic: async (cardId, channelId, topicId) => {
|
2022-10-25 07:06:39 +00:00
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
2022-10-18 16:56:00 +00:00
|
|
|
return await removeContactChannelTopic(profile.node, `${profile.guid}.${detail.token}`, channelId, topicId);
|
2022-09-29 18:31:55 +00:00
|
|
|
},
|
2022-10-31 07:32:27 +00:00
|
|
|
addChannelReport: async (cardId, channelId) => {
|
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
|
|
|
return await addFlag(profile.node, profile.guid, channelId);
|
|
|
|
},
|
|
|
|
addChannelTopicReport: async (cardId, channelId, topicId) => {
|
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
|
|
|
return await addFlag(profile.node, profile.guid, channelId, topicId);
|
|
|
|
},
|
2022-11-14 21:30:58 +00:00
|
|
|
getChannelNotifications: async (cardId, channelId) => {
|
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
|
|
|
return await getContactChannelNotifications(profile.node, `${profile.guid}.${detail.token}`, channelId);
|
|
|
|
},
|
|
|
|
setChannelNotifications: async (cardId, channelId, notify) => {
|
|
|
|
const { detail, profile } = getCardEntry(cardId);
|
|
|
|
return await setContactChannelNotifications(profile.node, `${profile.guid}.${detail.token}`, channelId, notify);
|
|
|
|
},
|
2022-10-25 07:06:39 +00:00
|
|
|
resync: (cardId) => {
|
|
|
|
resync.current.push(cardId);
|
|
|
|
sync();
|
|
|
|
},
|
2022-09-15 08:03:20 +00:00
|
|
|
}
|
2022-11-14 21:30:58 +00:00
|
|
|
|
2022-09-15 08:03:20 +00:00
|
|
|
return { state, actions }
|
|
|
|
}
|
|
|
|
|