Privatri is now responsive
This commit is contained in:
@@ -3,6 +3,9 @@ var apx = apx || {};
|
||||
apx.privatri = {};
|
||||
apx.privatri.templates = {};
|
||||
|
||||
const apiUrl = "http://admin.apxtri.farm.test/api/apxtri/privatri"
|
||||
const tribe = "apxtri";
|
||||
|
||||
apx.privatri.loadwco = async (id, dataObj = {}, ctx = null) => {
|
||||
// check if not authenticate, do nothing cause by default screensignin and wait authentification
|
||||
// if authenticate, if url xhash then redirect if no url then change wco-link=screenmyworld
|
||||
@@ -22,18 +25,65 @@ apx.privatri.loadwco = async (id, dataObj = {}, ctx = null) => {
|
||||
return Mustache.render(template, dataObj);
|
||||
};
|
||||
|
||||
apx.privatri.syncronizeBackend = async (alias, lastConnection) => {
|
||||
// const response = await fetch
|
||||
const response = [];
|
||||
async function getOldestPrivatriids(db, storeName) {
|
||||
const keysArray = await apx.indexedDB.getAllKeys(db, storeName);
|
||||
|
||||
const oldestPerUuid = {};
|
||||
|
||||
for (const message of response) {
|
||||
await apx.indexedDB.set("privatri", "messages", message);
|
||||
for (const key of keysArray) {
|
||||
const [uuid, timestampStr] = key.split("_");
|
||||
const timestamp = Number(timestampStr);
|
||||
|
||||
if (!oldestPerUuid[uuid] || timestamp < oldestPerUuid[uuid].timestamp) {
|
||||
oldestPerUuid[uuid] = { key, timestamp };
|
||||
};
|
||||
};
|
||||
|
||||
const oldestKeysArray = Object.values(oldestPerUuid).map(obj => obj.key)
|
||||
|
||||
const threadsArray = [];
|
||||
|
||||
for (key of oldestKeysArray) {
|
||||
threadsArray.push(await apx.indexedDB.get(db, storeName, key));
|
||||
};
|
||||
|
||||
return threadsArray;
|
||||
};
|
||||
|
||||
apx.privatri.syncronizeBackend = async (alias, lastConnection) => {
|
||||
const threadsArray = await getOldestPrivatriids("privatri", "messages");
|
||||
|
||||
for (const threadObj of threadsArray) {
|
||||
try {
|
||||
const response = await fetch(`${apiUrl}/${tribe}/${threadObj.thread}?since=${lastConnection}`);
|
||||
|
||||
if (response.ok === false) {
|
||||
throw new Error("HTTP error");
|
||||
};
|
||||
|
||||
console.log(response);
|
||||
|
||||
// for (const message of response) {
|
||||
// await apx.indexedDB.set("privatri", "messages", message);
|
||||
// };
|
||||
} catch (error) {
|
||||
const displayToastAlert = async (message) => {
|
||||
return await apx.privatri.loadwco("toastAlert", { message });
|
||||
};
|
||||
|
||||
document.querySelector("body").insertAdjacentHTML("beforeend", await displayToastAlert("An error occurred while synchronizing messages. Please try again later."));
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
apx.privatri.templates.scripts = {
|
||||
createThread: async () => {
|
||||
const bodyEl = document.querySelector("body");
|
||||
const autoDeletionBtnElArray = document.querySelectorAll("li.autoDeletionBtn");
|
||||
|
||||
const displayToastAlert = async (message) => {
|
||||
return await apx.privatri.loadwco("toastAlert", { message });
|
||||
};
|
||||
|
||||
autoDeletionBtnElArray.forEach(btn => {
|
||||
btn.addEventListener("click", () => {
|
||||
@@ -70,10 +120,20 @@ apx.privatri.templates.scripts = {
|
||||
};
|
||||
})(publicKey);
|
||||
|
||||
// Faire un post sur l'endpoint /privatri
|
||||
|
||||
await apx.indexedDB.set("privatri", "threads", { uuid: messageObj.thread, privateKey: privateKey });
|
||||
await apx.indexedDB.set("privatri", "messages", messageObj);
|
||||
try {
|
||||
const response = await fetch(`${apiUrl}/`);
|
||||
|
||||
if (response.ok === false) {
|
||||
throw new Error("HTTP error");
|
||||
};
|
||||
|
||||
await apx.indexedDB.set("privatri", "threads", { uuid: messageObj.thread, privateKey: privateKey });
|
||||
await apx.indexedDB.set("privatri", "messages", messageObj);
|
||||
|
||||
bodyEl.insertAdjacentHTML("beforeend", await displayToastAlert("Thread created successfully."));
|
||||
} catch (error) {
|
||||
bodyEl.insertAdjacentHTML("beforeend", await displayToastAlert("An error occurred while creating the thread. Please try again later."));
|
||||
};
|
||||
});
|
||||
},
|
||||
inviteAlias: async () => {
|
||||
@@ -92,7 +152,7 @@ apx.privatri.templates.scripts = {
|
||||
height: 425,
|
||||
type: "svg",
|
||||
data: url,
|
||||
image: "./assets/icon.png",
|
||||
image: "static/img/icons/privatri.png",
|
||||
dotsOptions: {
|
||||
color: "#ffffff",
|
||||
type: "rounded"
|
||||
@@ -251,61 +311,6 @@ apx.privatri.templates.scripts = {
|
||||
}
|
||||
};
|
||||
|
||||
async function getOldestPrivatriids(dbName, storeName) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open(dbName, 1);
|
||||
|
||||
request.onupgradeneeded = (event) => {
|
||||
const db = event.target.result;
|
||||
|
||||
if (!db.objectStoreNames.contains("threads")) {
|
||||
db.createObjectStore("threads", { keyPath: "uuid" });
|
||||
};
|
||||
|
||||
if (!db.objectStoreNames.contains("messages")) {
|
||||
db.createObjectStore("messages", { keyPath: "privatriid" });
|
||||
};
|
||||
};
|
||||
|
||||
request.onsuccess = (event) => {
|
||||
const db = event.target.result;
|
||||
|
||||
if (!db.objectStoreNames.contains(storeName)) {
|
||||
resolve([]);
|
||||
return;
|
||||
};
|
||||
|
||||
const transaction = db.transaction(storeName, "readonly");
|
||||
const store = transaction.objectStore(storeName);
|
||||
const cursorRequest = store.openCursor();
|
||||
const uuidMap = {};
|
||||
|
||||
cursorRequest.onsuccess = (event) => {
|
||||
const cursor = event.target.result;
|
||||
if (cursor) {
|
||||
const obj = cursor.value;
|
||||
|
||||
const [uuid, timestamp] = obj.privatriid.split("_");
|
||||
|
||||
if (!uuidMap[uuid] || Number(timestamp) < uuidMap[uuid].timestamp) {
|
||||
uuidMap[uuid] = { privatriid: obj.privatriid, timestamp: Number(timestamp) };
|
||||
};
|
||||
|
||||
cursor.continue();
|
||||
} else {
|
||||
const result = Object.values(uuidMap).map(event => event.privatriid);
|
||||
|
||||
resolve(result);
|
||||
};
|
||||
};
|
||||
|
||||
cursorRequest.onerror = (event) => reject(event);
|
||||
};
|
||||
|
||||
request.onerror = (event) => reject(event);
|
||||
});
|
||||
};
|
||||
|
||||
apx.ready(async () => {
|
||||
document.querySelector("body").innerHTML = await apx.privatri.loadwco("main", JSON.parse(localStorage.getItem("admin")).tpldata.privatri_main_privatri);
|
||||
|
||||
@@ -321,16 +326,17 @@ apx.ready(async () => {
|
||||
await (async () => {
|
||||
const lastConnection = JSON.parse(localStorage.getItem("lastConnection")) || Date.now();
|
||||
|
||||
await apx.privatri.syncronizeBackend(apx.data.headers.xalias, lastConnection);
|
||||
// await apx.privatri.syncronizeBackend(apx.data.headers.xalias, lastConnection);
|
||||
|
||||
const privatriidArray = await getOldestPrivatriids("privatri", "messages");
|
||||
console.log(privatriidArray);
|
||||
|
||||
const thread = async (name, uuid) => {
|
||||
return await apx.privatri.loadwco("thread", { uuid, name });
|
||||
};
|
||||
|
||||
for (const privatriid of privatriidArray) {
|
||||
const obj = await apx.indexedDB.get("privatri", "messages", privatriid)
|
||||
for (const privatriidObj of privatriidArray) {
|
||||
const obj = await apx.indexedDB.get("privatri", "messages", privatriidObj.privatriid)
|
||||
|
||||
const privateKey = (await apx.indexedDB.get("privatri", "threads", obj.thread)).privateKey;
|
||||
const name = (await apx.crypto.decryptMessage(obj.title, privateKey)).data;
|
||||
@@ -542,14 +548,21 @@ apx.ready(async () => {
|
||||
|
||||
messageInputEl.value = "";
|
||||
|
||||
// Faire un post sur l'endpoint /privatri
|
||||
|
||||
await apx.indexedDB.set("privatri", "messages", messageObj);
|
||||
try {
|
||||
const response = await fetch(`${apiUrl}/`);
|
||||
|
||||
if (response.ok === false) {
|
||||
throw new Error("HTTP error");
|
||||
};
|
||||
|
||||
await apx.indexedDB.set("privatri", "messages", messageObj);
|
||||
} catch (error) {
|
||||
bodyEl.insertAdjacentHTML("beforeend", await displayToastAlert("An error occurred while sending the message. Please try again later."));
|
||||
};
|
||||
};
|
||||
});
|
||||
|
||||
window.addEventListener("beforeunload", () => {
|
||||
// if (apx)
|
||||
localStorage.setItem("lastConnection", JSON.stringify(Date.now()));
|
||||
});
|
||||
|
||||
|
Reference in New Issue
Block a user