add deno run script

This commit is contained in:
bobbydigitales
2024-11-08 09:46:19 -08:00
parent 6ce693cefc
commit 6c692c4b9f
13 changed files with 383 additions and 283 deletions

View File

@@ -300,8 +300,12 @@ export async function getData(userID: string, lowerID: Date, upperID: Date): Pro
const getAllRequest = index.getAll(keyRangeValue);
getAllRequest.onsuccess = () => {
const records = getAllRequest.result.map((item: any) => item.data);
resolve(records);
// let records = [];
// for (let record of getAllRequest.result) {
// records.push(record);
// }
// // const records = getAllRequest.result.map((item: any) => item.data);
resolve(getAllRequest.result);
};
getAllRequest.onerror = () => {

View File

@@ -145,6 +145,8 @@ function log(message: string) {
if (logLines.length > 10) {
logLines = logLines.slice(logLines.length - logLength);
}
renderLog();
}
function generateID() {
@@ -155,6 +157,10 @@ function generateID() {
return uuidv4();
}
interface StoragePost {
data: Post;
}
class Post {
post_timestamp: Date;
post_id: string;
@@ -288,6 +294,7 @@ class wsConnection {
websocket: WebSocket | null = null;
userID = "";
peerID = "";
UserIDsToSync: Set<string>;
websocketPingInterval: number = 0;
helloRefreshInterval: number = 0;
retry = 10;
@@ -299,6 +306,26 @@ class wsConnection {
seenPeers: Map<string, any> = new Map();
constructor(userID: string, peerID: string, IDsToSync: Set<string>) {
this.userID = userID;
this.peerID = peerID;
this.UserIDsToSync = new Set(IDsToSync);
this.messageHandlers.set('hello', this.helloResponseHandler.bind(this));
this.messageHandlers.set('pong', this.pongHandler);
this.messageHandlers.set('peer_message', this.peerMessageHandler.bind(this));
this.peerMessageHandlers.set('get_post_ids_for_user', this.getPostIdsForUserHandler.bind(this));
this.peerMessageHandlers.set('get_post_ids_for_user_response', this.getPostIdsForUserResponseHandler.bind(this));
this.peerMessageHandlers.set('get_posts_for_user', this.getPostsForUserHandler.bind(this));
this.peerMessageHandlers.set('get_posts_for_user_response', this.getPostsForUserReponseHandler.bind(this));
window.addEventListener('beforeunload', () => this.disconnect());
this.connect();
}
async send(message: any) {
let json = ""
try {
@@ -312,54 +339,6 @@ class wsConnection {
}
helloResponseHandler(data: any) {
let users = [];
let receivedUsers = Object.entries(data.userPeers);
log(`Net: got ${receivedUsers.length} users from bootstrap peer.`)
try {
let preferentialID = app.getPreferentialID();
let currentUserPeers = data.userPeers[preferentialID];
users.push([preferentialID, currentUserPeers]);
delete data.userPeers[preferentialID];
} catch (e) {
console.log('helloResponseHandler', e);
}
let getAllUsers = app.router.route !== App.Route.USER
if (getAllUsers) {
users = [...users, ...Object.entries(data.userPeers)];
}
// log(`Net: got ${users.length} users from bootstrap peer. \n${users.map((user)=>user[0]).join('\n')}`)
for (let [userID, peerIDs] of users) {
if (this.userBlockList.has(userID)) {
console.log("Skipping user on blocklist:", userID)
continue;
}
// this.peers.set(userID, [...peerIDs]);
for (let peerID of [...peerIDs]) {
if (peerID === this.peerID) {
continue;
}
log(`Net: Req post IDs for user ${logID(userID)} from peer ${logID(peerID)}`);
this.send({
type: "peer_message",
from: this.peerID,
from_username: app.username,
from_peername: app.peername,
to: peerID,
message: { type: "get_post_ids_for_user", user_id: userID }
})
}
}
}
pongHandler(data: any) {
}
@@ -504,7 +483,7 @@ class wsConnection {
for (let post of message.posts) {
// HACK: Some posts have insanely large images, so I'm gonna skip them.
// If we supported delete then we we could delete these posts in a sensible way.
// Once we support delete then we we could delete these posts in a sensible way.
if (this.postBlockList.has(post.post_id)) {
log(`Skipping blocked post: ${post.post_id}`);
continue;
@@ -528,7 +507,7 @@ class wsConnection {
log(`getPostsForUserReponseHandler receive took: ${receiveTime.toFixed(2)}ms`);
if (message.user_id === app.getPreferentialID()) {
if (message.user_id === app.getPreferentialUserID() || app.following.has(message.user_id)) {
app.render();
}
}
@@ -556,17 +535,69 @@ class wsConnection {
userBlockList = new Set([
'5d63f0b2-a842-41bf-bf06-e0e4f6369271',
'5f1b85c4-b14c-454c-8df1-2cacc93f8a77',
'bba3ad24-9181-4e22-90c8-c265c80873ea'
// 'bba3ad24-9181-4e22-90c8-c265c80873ea'
])
async sendHello() {
let knownUsers = [...(await indexedDB.databases())].map((db) => db.name?.replace('user_', ''));
knownUsers = knownUsers.filter((userID) => userID && !this.userBlockList.has(userID));
knownUsers = knownUsers.filter(async (userID) => userID && (await getAllIds(userID)).length > 0);
// TODO only get users you're following here. ✅
let knownUsers = [...(await indexedDB.databases())].map(db => db.name?.replace('user_', '')).filter(userID => userID !== undefined);
knownUsers = knownUsers
.filter(userID => this.UserIDsToSync.has(userID))
.filter(userID => !this.userBlockList.has(userID))
.filter(async userID => (await getAllIds(userID)).length > 0); // TODO getting all the IDs is unecessary, replace it with a test to get a single ID.
console.log('Net: Sending known users', knownUsers.map(userID => logID(userID ?? "")));
return await this.send({ type: "hello", user_id: this.userID, user_name: app.username, peer_id: this.peerID, peer_name: app.peername, known_users: knownUsers });
}
helloResponseHandler(data: any) {
let users = [];
let receivedUsers = Object.entries(data.userPeers);
log(`Net: got ${receivedUsers.length} users from bootstrap peer.`)
try {
let preferentialUserID = app.getPreferentialUserID();
let currentUserPeers = data.userPeers[preferentialUserID];
users.push([preferentialUserID, currentUserPeers]);
delete data.userPeers[preferentialUserID];
} catch (e) {
console.log('helloResponseHandler', e);
}
let getAllUsers = app.router.route !== App.Route.USER
if (getAllUsers) {
users = [...users, ...Object.entries(data.userPeers).filter(userID => this.UserIDsToSync.has(userID[0]))];
}
// log(`Net: got ${users.length} users from bootstrap peer. \n${users.map((user)=>user[0]).join('\n')}`)
for (let [userID, peerIDs] of users) {
if (this.userBlockList.has(userID)) {
console.log("Skipping user on blocklist:", userID)
continue;
}
// this.peers.set(userID, [...peerIDs]);
for (let peerID of [...peerIDs]) {
if (peerID === this.peerID) {
continue;
}
log(`Net: Req post IDs for user ${logID(userID)} from peer ${logID(peerID)}`);
this.send({
type: "peer_message",
from: this.peerID,
from_username: app.username,
from_peername: app.peername,
to: peerID,
message: { type: "get_post_ids_for_user", user_id: userID }
})
}
}
}
connect(): void {
if (this.websocket?.readyState === WebSocket.OPEN) {
return;
@@ -639,30 +670,6 @@ class wsConnection {
disconnect() {
this.websocket?.close();
}
constructor(userID: string, peerID: string) {
this.userID = userID;
this.peerID = peerID;
this.messageHandlers.set('hello', this.helloResponseHandler.bind(this));
this.messageHandlers.set('pong', this.pongHandler);
this.messageHandlers.set('peer_message', this.peerMessageHandler.bind(this));
this.peerMessageHandlers.set('get_post_ids_for_user', this.getPostIdsForUserHandler.bind(this));
this.peerMessageHandlers.set('get_post_ids_for_user_response', this.getPostIdsForUserResponseHandler.bind(this));
this.peerMessageHandlers.set('get_posts_for_user', this.getPostsForUserHandler.bind(this));
this.peerMessageHandlers.set('get_posts_for_user_response', this.getPostsForUserReponseHandler.bind(this));
this.connect();
if (!this.websocket) {
// set a timer and retry?
}
}
}
class App {
@@ -671,7 +678,7 @@ class App {
userID: string = '';
peerID: string = '';
following: Set<string> = new Set();
posts: Post[] = [];
posts: StoragePost[] = [];
isHeadless: boolean = false;
showLog: boolean = false;
markedAvailable = false;
@@ -681,7 +688,7 @@ class App {
qrcode: any = null;
connectURL: string = "";
getPreferentialID() {
getPreferentialUserID() {
return this.router.userID.length !== 0 ? this.router.userID : this.userID;
}
@@ -752,6 +759,19 @@ class App {
return fullText
}
downloadJson(data: any, filename = 'data.json') {
const jsonString = JSON.stringify(data);
const blob = new Blob([jsonString], { type: 'application/json' });
const url = window.URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
}
async exportPostsForUser(userID: string) {
let posts = await getAllData(userID);
@@ -769,10 +789,7 @@ class App {
output.push(newPost);
}
let json = JSON.stringify(output);
console.log(json);
this.downloadJson(output, `ddln_${this.username}_export`);
}
async importTweetArchive(userID: string, tweetArchive: any[]) {
@@ -1116,10 +1133,43 @@ class App {
});
}
initButtons(userID: string, posts: Post[], registration: ServiceWorkerRegistration | undefined) {
async lazyCreateQRCode() {
if (this.qrcode != null) {
return;
}
this.qrcode = await new QRCode(document.getElementById('qrcode'), {
text: this.connectURL,
width: 150,
height: 150,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
}
showInfo() {
let infoElement = document.getElementById('info');
if (infoElement === null) {
return;
}
infoElement.style.display == 'none' ? infoElement.style.display = 'block' : infoElement.style.display = 'none';
logVisible = infoElement.style.display == 'block';
renderLog();
this.lazyCreateQRCode();
(document.querySelector('#qrcode > img') as HTMLImageElement).classList.add('qrcode_image');
(document.querySelector('#qrcode > canvas') as HTMLImageElement).classList.add('qrcode_image');
this.showLog = true;
}
initButtons(userID: string, posts: StoragePost[], registration: ServiceWorkerRegistration | undefined) {
// let font1Button = document.getElementById("button_font1") as HTMLButtonElement;
// let font2Button = document.getElementById("button_font2") as HTMLButtonElement;
let importTweetsButton = document.getElementById("import_tweets") as HTMLButtonElement;
let exportButton = document.getElementById("export_button") as HTMLButtonElement;
let clearPostsButton = document.getElementById("clear_posts") as HTMLButtonElement;
let updateApp = document.getElementById("update_app") as HTMLButtonElement;
let ddlnLogoButton = document.getElementById('ddln_logo_button') as HTMLDivElement;
@@ -1128,6 +1178,8 @@ class App {
let filePicker = document.getElementById('file_input') as HTMLInputElement;
let toggleDark = document.getElementById('toggle_dark') as HTMLButtonElement;
exportButton.addEventListener('click', async e => await this.exportPostsForUser(this.userID));
toggleDark.addEventListener('click', () => {
document.documentElement.style.setProperty('--main-bg-color', 'white');
document.documentElement.style.setProperty('--main-fg-color', 'black');
@@ -1201,31 +1253,9 @@ class App {
registration?.active?.postMessage({ type: "update_app" });
});
let infoElement = document.getElementById('info');
if (infoElement === null) {
return;
}
ddlnLogoButton.addEventListener('click', async () => {
infoElement.style.display == 'none' ? infoElement.style.display = 'block' : infoElement.style.display = 'none';
logVisible = infoElement.style.display == 'block';
renderLog();
if (this.qrcode != null) {
return;
}
this.qrcode = await new QRCode(document.getElementById('qrcode'), {
text: this.connectURL,
width: 150,
height: 150,
colorDark: "#000000",
colorLight: "#ffffff",
correctLevel: QRCode.CorrectLevel.H
});
(document.querySelector('#qrcode > img') as HTMLImageElement).classList.add('qrcode_image');
(document.querySelector('#qrcode > canvas') as HTMLImageElement).classList.add('qrcode_image');
this.showInfo()
});
}
@@ -1235,7 +1265,7 @@ class App {
// This isn't really going to work very well.
// Eventually we'll need a db that only has followed user posts so we can get them chronologically
//
let posts: Post[] = [];
let posts: StoragePost[] = [];
for (let followedID of this.following.keys()) {
posts = posts.concat(await getData(followedID, new Date(2022, 8), new Date()));
// console.log(followedID);
@@ -1260,13 +1290,20 @@ class App {
]
}
if (userID === '05a495a0-0dd8-4186-94c3-b8309ba6fc4c') {
return [
'b38b623c-c3fa-4351-9cab-50233c99fa4e',
'a0e42390-08b5-4b07-bc2b-787f8e5f1297', // BMO
]
}
return ['a0e42390-08b5-4b07-bc2b-787f8e5f1297']; // Follow BMO by default :)
}
async loadPostsFromStorage(userID: string, postID?: string) {
this.timerStart();
let posts: Post[] = [];
let posts: StoragePost[] = [];
// if (postID) {
// posts = await gePostForUser(userID, postID);
@@ -1292,7 +1329,7 @@ class App {
return;
}
let preferredId = app.getPreferentialID()
let preferredId = app.getPreferentialUserID()
for (let userID of knownUsers as string[]) {
if (userID === preferredId) {
continue;
@@ -1421,7 +1458,7 @@ class App {
async main() {
// await this.exportPostsForUser('bba3ad24-9181-4e22-90c8-c265c80873ea');
// await this.exportPostsForUser('b38b623c-c3fa-4351-9cab-50233c99fa4e');
// Get initial state and route from URL and user agent etc
@@ -1452,10 +1489,13 @@ class App {
this.userID = this.getUserID();
this.username = this.getUsername();
this.connectURL = `https://${document.location.hostname}/connect/${this.userID}`;
document.getElementById('connectURL')!.innerHTML = `<a href="${this.connectURL}">connect</a>`;
let urlParams = (new URL(window.location.href)).searchParams;
if (urlParams.has('log')) {
document.getElementById('info')!.style.display = "block";
this.showLog = true;
this.showInfo();
}
if (urlParams.has('headless')) {
@@ -1510,16 +1550,18 @@ class App {
this.initButtons(this.userID, this.posts, registration);
this.connectURL = `https://${document.location.hostname}/connect/${this.userID}`;
document.getElementById('connectURL')!.innerHTML = `<a href="${this.connectURL}">connect</a>`;
log(`username:${this.username} user:${this.userID} peername:${this.peername} peer:${this.peerID}`);
await this.purgeEmptyUsers();
this.websocket = new wsConnection(this.userID, this.peerID);
window.addEventListener('beforeunload', () => { this.websocket?.disconnect() })
let IDsToSync = this.following;
if (this.router.route === App.Route.USER) {
IDsToSync = new Set([this.router.userID]);
}
this.websocket = new wsConnection(this.userID, this.peerID, IDsToSync);
this.initOffline(this.websocket);
// this.createNetworkViz();
@@ -1541,6 +1583,10 @@ class App {
// })
}
renderWelcome(contentDiv: HTMLDivElement) {
contentDiv.innerHTML = `<div style="font-size:32px">Doing complicated shennanigans to load posts for you so just hang on a minute, ok!?</div>`;
}
// keep a map of posts to dom nodes.
// on re-render
@@ -1607,7 +1653,7 @@ class App {
throw new Error();
}
if (this.posts.length === 0) {
contentDiv.innerHTML = `<div style="font-size:32px">Doing complicated shennanigans to load posts for you so just hang on a minute, ok!?</div>`;
this.renderWelcome(contentDiv);
return;
}
@@ -1631,8 +1677,6 @@ class App {
// console.log("added:", addedPosts, "removed:", deletedPosts);
const fragment = document.createDocumentFragment();
contentDiv.innerHTML = "";
@@ -1643,10 +1687,9 @@ class App {
let postData = this.posts[i];
// this.postsSet.add(postData);
// return promises for all image loads and await those.
let post = this.renderPost(postData);
this.renderedPosts.set(postData.post_id, post);
let post = this.renderPost(postData.data);
// this.renderedPosts.set(postData.post_id, post);
if (post) {
fragment.appendChild(post);
count++;
@@ -1671,9 +1714,9 @@ class App {
if ((performance as any)?.memory) {
log(`memory used: ${((performance as any).memory.usedJSHeapSize / 1024 / 1024).toFixed(2)}Mb`)
}
// if ((performance as any)?.memory) {
// log(`memory used: ${((performance as any).memory.usedJSHeapSize / 1024 / 1024).toFixed(2)}Mb`)
// }
}
@@ -1745,6 +1788,7 @@ class App {
containerDiv.querySelector('#shareButton')?.appendChild(shareButton);
if (!("image_data" in post && post.image_data)) {
// containerDiv.appendChild(timestampDiv);
return containerDiv;

View File

@@ -8,7 +8,6 @@ const contentToCache = [
'/static/main.js',
'/static/lib/marked.min.js',
'/static/lib/qrcode.min.js',
'/static/lib/d3.js',
'/static/db.js',
'/static/favicon.ico'
];