Complete move to IndexedDB
This commit is contained in:
200
src/main.ts
200
src/main.ts
@@ -1,7 +1,6 @@
|
||||
import { openDatabase, getData, addData, addDataArray } from "./db.js"
|
||||
|
||||
declare let marked: any;
|
||||
declare let DOMPurify: any;
|
||||
// let posts:any;
|
||||
// let keyBase = "dandelion_posts_v1_"
|
||||
// let key:string = "";
|
||||
@@ -52,28 +51,6 @@ function waitMs(durationMs: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, durationMs));
|
||||
}
|
||||
|
||||
|
||||
// function getTimestampFromDate(date: Date) {
|
||||
// return {
|
||||
// year: date.getFullYear(),
|
||||
// month: date.getMonth() + 1,
|
||||
// day: date.getDate(),
|
||||
// hour: date.getHours(),
|
||||
// minute: date.getMinutes(),
|
||||
// second: date.getSeconds(),
|
||||
// };
|
||||
// }
|
||||
|
||||
// function getTimestampFromString(dateString: string) {
|
||||
// let date = new Date(dateString);
|
||||
// return getTimestampFromDate(date);
|
||||
// }
|
||||
|
||||
// function getCurrentTimestamp() {
|
||||
// let date = new Date();
|
||||
// return getTimestampFromDate(date);
|
||||
// }
|
||||
|
||||
function uuidv4() {
|
||||
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, (c: any) =>
|
||||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||
@@ -83,7 +60,7 @@ function uuidv4() {
|
||||
let logLines: string[] = [];
|
||||
let logLength = 10;
|
||||
function log(message: string) {
|
||||
logLines.push(`${new Date().toLocaleTimeString()}:${message}`);
|
||||
logLines.push(`${new Date().toLocaleTimeString()}: ${message}`);
|
||||
if (logLines.length > 10) {
|
||||
logLines = logLines.slice(logLines.length - logLength);
|
||||
}
|
||||
@@ -145,50 +122,6 @@ async function getFixedTweetText(entry: any) {
|
||||
}
|
||||
|
||||
return fullText
|
||||
|
||||
// for (let url of )
|
||||
// const regex = /https:\/\/t\.co\/[\w-]+/g;
|
||||
|
||||
// let returnText = tweetText;
|
||||
|
||||
// if (tweetText.includes("t.co")) {
|
||||
// // console.log(tweetText);
|
||||
// }
|
||||
|
||||
// let matches = returnText.match(regex);
|
||||
// if (!matches) {
|
||||
// return returnText;
|
||||
// }
|
||||
|
||||
// for (const match of matches) {
|
||||
// // console.log(match);
|
||||
// let fetchedData = "";
|
||||
// try {
|
||||
// fetchedData = await (await fetch(match)).text();
|
||||
// await waitMs(100);
|
||||
// } catch (e) {
|
||||
// console.log(e);
|
||||
// }
|
||||
|
||||
// if (fetchedData.includes('http-equiv="refresh"')) {
|
||||
// // console.log(fetchedData);
|
||||
|
||||
// // let urlRegex = /URL=(http.:\/\/.+)\"/;
|
||||
// let urlRegex = new RegExp(`URL=(http[s]?://.+?)"`);
|
||||
// let urlMatch = fetchedData.match(urlRegex);
|
||||
// if (urlMatch?.length == 2) {
|
||||
// // console.log(urlMatch[1]);
|
||||
// returnText = returnText.replace(match, urlMatch[1]);
|
||||
// // URL=https://youtu.be/yGci-Lb87zs"
|
||||
// } else {
|
||||
// throw new Error(fetchedData);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// console.log(returnText);
|
||||
|
||||
// return returnText;
|
||||
}
|
||||
|
||||
async function createTestData2(userID: string) {
|
||||
@@ -217,36 +150,26 @@ async function createTestData2(userID: string) {
|
||||
}
|
||||
|
||||
let imageData = null;
|
||||
// if (isImage) {
|
||||
// try {
|
||||
// let response = await fetch(mediaURL);
|
||||
// await waitMs(100);
|
||||
// if (response.status === 200) {
|
||||
// imageData = await response.arrayBuffer();
|
||||
// }
|
||||
// console.log(imageData);
|
||||
// } catch (e) {
|
||||
// console.log(e);
|
||||
// }
|
||||
if (isImage) {
|
||||
try {
|
||||
let response = await fetch(mediaURL);
|
||||
await waitMs(100);
|
||||
if (response.status === 200) {
|
||||
imageData = await response.arrayBuffer();
|
||||
}
|
||||
console.log(imageData);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
|
||||
// }
|
||||
}
|
||||
|
||||
let timeStamp = new Date(entry.tweet.created_at);
|
||||
|
||||
// let tweetText = entry.tweet.full_text;
|
||||
let tweetText = await getFixedTweetText(entry);
|
||||
|
||||
let newPost = new Post('bobbydigitales', userID, tweetText, timeStamp, imageData, 'twitter', entry);
|
||||
|
||||
postsTestData.push(newPost);
|
||||
|
||||
// postsTestData.push({
|
||||
// post_timestamp: timeStamp,
|
||||
// author: `bobbydigitales`,
|
||||
// text: tweetText,
|
||||
// image: imageData
|
||||
// });
|
||||
|
||||
count++;
|
||||
if (count % 100 === 0) {
|
||||
log(`Imported ${count} posts...`);
|
||||
@@ -258,22 +181,6 @@ async function createTestData2(userID: string) {
|
||||
// }
|
||||
|
||||
}
|
||||
|
||||
// let rant = await (await fetch('ranting.md')).text();;
|
||||
|
||||
// postsTestData.unshift({
|
||||
// post_timestamp: {
|
||||
// year: 2023,
|
||||
// month: 10,
|
||||
// day: 19,
|
||||
// hour: 14,
|
||||
// minute: 53,
|
||||
// second: 0,
|
||||
// },
|
||||
// author: `bobbydigitales`,
|
||||
// text: rant
|
||||
// })
|
||||
|
||||
return postsTestData;
|
||||
}
|
||||
|
||||
@@ -283,11 +190,6 @@ async function createTestData3(userID: string) {
|
||||
return posts;
|
||||
}
|
||||
|
||||
// function post(key: string, posts: { author: any; text: any; image: any; }[], author: any, text: any, image: any) {
|
||||
// posts.push({ author: author, text: text, image: image });
|
||||
// localStorage.setItem(key, JSON.stringify(posts));
|
||||
// }
|
||||
|
||||
async function registerServiceWorker() {
|
||||
if (!("serviceWorker" in navigator)) {
|
||||
return;
|
||||
@@ -348,20 +250,20 @@ function connectWebsocket(userID: string) {
|
||||
let websocket = new WebSocket(`ws://${window.location.hostname}:${window.location.port}/ws`);
|
||||
|
||||
websocket.onopen = function (evt) {
|
||||
console.log("CONNECTED");
|
||||
log("Websocket: CONNECTED");
|
||||
websocket.send(`{"messageType":"connect", "id": "${userID}"}`);
|
||||
};
|
||||
|
||||
websocket.onclose = function (evt) {
|
||||
console.log("DISCONNECTED");
|
||||
log("Websocket: DISCONNECTED");
|
||||
};
|
||||
|
||||
websocket.onmessage = function (evt) {
|
||||
console.log('RESPONSE: ' + evt.data);
|
||||
log('Websocket: RESPONSE: ' + evt.data);
|
||||
};
|
||||
|
||||
websocket.onerror = function (evt) {
|
||||
console.log('ERROR: ' + evt);
|
||||
log('Websocket: ERROR: ' + evt);
|
||||
};
|
||||
|
||||
return websocket;
|
||||
@@ -385,7 +287,7 @@ function initOffline() {
|
||||
// Event listener for going online
|
||||
window.addEventListener('online', () => { log("online") });
|
||||
|
||||
log(`${navigator.onLine ? "online" : "offline"}`)
|
||||
log(`Online status: ${navigator.onLine ? "online" : "offline"}`)
|
||||
|
||||
}
|
||||
|
||||
@@ -414,7 +316,7 @@ function initButtons(userID: string, posts: Post[]) {
|
||||
async function loadPosts(userID: string) {
|
||||
|
||||
timerStart();
|
||||
let posts: any = await getData(userID, new Date(2021, 1), new Date());
|
||||
let posts: any = await getData(userID, new Date(2022, 8), new Date());
|
||||
|
||||
if (posts.length > 0) {
|
||||
log(`Loaded ${posts.length} posts in ${timerDelta().toFixed(2)}ms`);
|
||||
@@ -425,56 +327,7 @@ async function loadPosts(userID: string) {
|
||||
|
||||
log("Adding test data...");
|
||||
addDataArray(userID, posts);
|
||||
|
||||
// let count = 0;
|
||||
// for (let post of posts) {
|
||||
// debugger;
|
||||
// await addData(userID, post);
|
||||
// count++;
|
||||
// if (count % 100 === 0) {
|
||||
// log(`Added ${count} posts...`);
|
||||
// }
|
||||
// }
|
||||
// log("Finished!");
|
||||
|
||||
|
||||
return await getData(userID, new Date(2022, 1), new Date());
|
||||
|
||||
|
||||
// debugger;
|
||||
|
||||
// let postsJSON = await getData(userID)
|
||||
|
||||
// if (!postsJSON) {
|
||||
// let testPosts = await createTestData3(userID);
|
||||
// for (let post of testPosts) {
|
||||
// await addData(userID, post);
|
||||
// }
|
||||
// // localStorage.setItem(key, postsJSON);
|
||||
// }
|
||||
|
||||
// let delta = timerDelta();
|
||||
// if (postsJSON) {
|
||||
// log(`read ${(postsJSON.length / 1024 / 1024).toFixed(2)}Mb from indexedDB in ${delta.toFixed(2)}ms`);
|
||||
// }
|
||||
|
||||
// let posts = [];
|
||||
// try {
|
||||
// timerStart();
|
||||
// posts = JSON.parse(postsJSON);
|
||||
// delta = timerDelta();
|
||||
// log(`parsed ${posts.length} posts from indexedDB in ${delta.toFixed(2)}ms`);
|
||||
// } catch (e) {
|
||||
// log("Couldn't read posts from local storage, resetting...");
|
||||
// }
|
||||
|
||||
// if (!posts) {
|
||||
// await createTestData3();
|
||||
// // localStorage.setItem(key, JSON.stringify(testData));
|
||||
// posts = testData;
|
||||
// }
|
||||
|
||||
// return posts;
|
||||
return await getData(userID, new Date(2022, 8), new Date());
|
||||
}
|
||||
|
||||
async function main() {
|
||||
@@ -493,9 +346,11 @@ async function main() {
|
||||
|
||||
if (navigator.storage && navigator.storage.persist && !navigator.storage.persisted) {
|
||||
const isPersisted = await navigator.storage.persist();
|
||||
console.log(`Persisted storage granted: ${isPersisted}`);
|
||||
log(`Persisted storage granted: ${isPersisted}`);
|
||||
}
|
||||
|
||||
log(`Persisted: ${(await navigator.storage.persisted()).toString()}`);
|
||||
|
||||
initMarkdown();
|
||||
|
||||
// let main = await fetch("/main.js");
|
||||
@@ -538,7 +393,7 @@ function render(posts: Post[]) {
|
||||
fragment.appendChild(post);
|
||||
count++;
|
||||
}
|
||||
if (count > 500) {
|
||||
if (count > 100) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -565,18 +420,19 @@ function renderPost(post: Post) {
|
||||
textDiv.innerHTML = marked.parse(post.text);
|
||||
// textDiv.innerHTML = DOMPurify.sanitize(marked.parse(post.text));
|
||||
|
||||
timestampDiv.innerText = post.post_timestamp.toLocaleDateString();
|
||||
timestampDiv.innerText = `${post.post_timestamp.toDateString()}`;
|
||||
timestampDiv.title = `${post.post_timestamp.toLocaleTimeString()} · ${post.post_timestamp.toDateString()}`
|
||||
containerDiv.appendChild(hr);
|
||||
containerDiv.appendChild(textDiv);
|
||||
|
||||
if (!("image" in post && post.image)) {
|
||||
if (!("image_data" in post && post.image_data)) {
|
||||
containerDiv.appendChild(timestampDiv);
|
||||
return containerDiv;
|
||||
// return null;
|
||||
}
|
||||
|
||||
let image = document.createElement("img");
|
||||
const blob = new Blob([post.image as ArrayBuffer], { type: 'image/jpg' });
|
||||
const blob = new Blob([post.image_data as ArrayBuffer], { type: 'image/jpg' });
|
||||
const url = URL.createObjectURL(blob);
|
||||
image.onload = () => {
|
||||
URL.revokeObjectURL(url);
|
||||
|
||||
Reference in New Issue
Block a user