This repository has been archived by the owner on Feb 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A big update for 11. Migrate to WWEBJS and SurrealDB with kysely, res…
…tart command, graceful shutdown and restart, upgrade packages, added live-reloading when message is created, custom @everyone body message, basic messaging, and pretty much most of what is needed for basic Chatting apps. THIS IS PARTIAL WORK AND BUGS MAY BE PRESENT.
- Loading branch information
1 parent
9164824
commit 0a51471
Showing
57 changed files
with
3,549 additions
and
2,274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,6 @@ pnpm-debug.log* | |
|
||
# macOS-specific files | ||
.DS_Store | ||
|
||
# tmp files | ||
tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "ts-node", | ||
"command": "pnpm ts-node index.ts", | ||
"request": "launch", | ||
"type": "node-terminal", | ||
"cwd": "${fileDirname}", | ||
"internalConsoleOptions": "openOnSessionStart" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { toggleSideBarComponentMobile } from "./sideBarLogic" | ||
|
||
export function attachChatsLogic() { | ||
const listIcon = document.getElementById("listIcon"); | ||
listIcon?.addEventListener("click", toggleSideBarComponentMobile); | ||
} | ||
attachChatsLogic(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useSocketio } from "../shared-clients"; | ||
|
||
const socket = useSocketio(); | ||
|
||
export function attachInputBarLogic() { | ||
const iconSendComponent = document.getElementById("icon-send"); | ||
|
||
iconSendComponent?.addEventListener("click", sendMessage); | ||
} | ||
attachInputBarLogic(); | ||
|
||
export async function sendMessage() { | ||
const textInput = document.getElementById("input-text") as HTMLInputElement; | ||
const mainComponent = document.getElementById("main") as HTMLDivElement; | ||
let chatPhone = mainComponent.dataset.chatPhone; | ||
if (textInput?.value.indexOf("@everyone") > -1) { | ||
try { | ||
await socket.timeout(10000).emitWithAck("sendMessageMentionAll", chatPhone, textInput.value); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
if (textInput?.value) { | ||
try { | ||
await socket.timeout(10000).emitWithAck("sendMessage", chatPhone, textInput.value); | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,33 @@ | ||
import { toggleChatsComponentMobile } from "./chatLogic"; | ||
import { toggleMainHeaderBackButtonComponentMobile } from "./headerLogic" | ||
import { toggleSideBarComponentMobile } from "./sideBarLogic" | ||
import { toggleChatsComponentMobile, hideChatsComponentMobile } from "./chatLogic"; | ||
import { toggleMainHeaderBackButtonComponentMobile } from "./headerLogic"; | ||
import { hideSideBarComponentMobile } from "./sideBarLogic"; | ||
|
||
export function toggleMainComponentMobile() { | ||
const divMain = document.getElementById("main"); | ||
if (!divMain?.offsetParent || divMain?.classList.contains("forceFlex")) { | ||
toggleChatsComponentMobile(); | ||
toggleMainHeaderBackButtonComponentMobile(); | ||
toggleSideBarComponentMobile(); | ||
toggleMainHeaderBackButtonComponentMobile(); | ||
hideSideBarComponentMobile(); | ||
divMain?.classList.toggle("forceFlex"); | ||
} | ||
} | ||
|
||
export function showMainComponentMobile() { | ||
const divMain = document.getElementById("main"); | ||
if (!divMain?.offsetParent && !divMain?.classList.contains("forceFlex")) { | ||
hideChatsComponentMobile(); | ||
toggleMainHeaderBackButtonComponentMobile(); | ||
hideSideBarComponentMobile(); | ||
} | ||
divMain?.classList.add("forceFlex"); | ||
} | ||
|
||
export function hideMainComponentMobile() { | ||
const divMain = document.getElementById("main"); | ||
if (!divMain?.offsetParent && divMain?.classList.contains("forceFlex")) { | ||
toggleChatsComponentMobile(); | ||
toggleMainHeaderBackButtonComponentMobile(); | ||
hideSideBarComponentMobile(); | ||
} | ||
divMain?.classList.remove("forceFlex"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { useSocketio } from "../shared-clients"; | ||
|
||
const socket = useSocketio(); | ||
|
||
export function attachMessageLogic() { | ||
const messagesComponent = document.getElementById("messages"); | ||
const messagesChildren = messagesComponent?.children; | ||
scrollToBottom(); | ||
|
||
let promises: Promise<void>[] = []; | ||
Array.from(messagesChildren || []).forEach(async (messageElement) => { | ||
promises.push(fetchMedia(messageElement, true)); | ||
// TODO: Would be nice to make this configurable, especially for lower hardware. | ||
promises.push(new Promise(r => setTimeout(r, 100))) | ||
messageElement.addEventListener("click", async () => { | ||
await fetchMedia(messageElement); | ||
}); | ||
}); | ||
Promise.all(promises).then(scrollToBottom); | ||
} | ||
attachMessageLogic(); | ||
|
||
export function scrollToBottom() { | ||
const messagesComponent = document.getElementById("messages"); | ||
if (messagesComponent) { | ||
messagesComponent.scrollTop = messagesComponent.scrollHeight; | ||
} | ||
} | ||
|
||
async function fetchMedia(messageElement: Element, firstLoad: boolean = false) { | ||
if (messageElement.getAttribute("has-media") === "true" && messageElement.getAttribute("resolved") !== "true") { | ||
const response = await socket.emitWithAck("fetchMedia", messageElement.id.substring(16)); | ||
|
||
const jsonData = JSON.parse(response); | ||
const media: string | undefined = decodeURIComponent(jsonData.messageBody); | ||
const mediaType: string | undefined = jsonData.messageType; | ||
const mediaElement = messageElement.querySelector("#media"); | ||
if (!mediaElement) return console.error(`${mediaElement} is undefined.`); | ||
if (!media) { | ||
const errorElement = document.createElement("p"); | ||
errorElement.innerText = "[Failed to fetch: media is undefined]"; | ||
mediaElement.replaceChildren(errorElement); | ||
return; | ||
} | ||
|
||
switch (mediaType) { | ||
case "sticker": | ||
const stickerElement = document.createElement("img"); | ||
stickerElement.src = media; | ||
stickerElement.classList.add("max-h-[10rem]"); | ||
stickerElement.classList.add("py-2"); | ||
mediaElement.replaceChildren(stickerElement); | ||
messageElement.setAttribute("resolved", "true"); | ||
break; | ||
case "image": | ||
const imageElement = document.createElement("img"); | ||
imageElement.src = media; | ||
imageElement.classList.add("max-h-[20rem]"); | ||
imageElement.classList.add("py-2"); | ||
mediaElement.replaceChildren(imageElement); | ||
messageElement.setAttribute("resolved", "true"); | ||
break; | ||
case "ptt": | ||
const audioElement = document.createElement("audio"); | ||
audioElement.src = media; | ||
audioElement.classList.add("max-h-[20rem]"); | ||
audioElement.classList.add("py-2"); | ||
audioElement.setAttribute("controls", ""); | ||
mediaElement.replaceChildren(audioElement); | ||
messageElement.setAttribute("resolved", "true"); | ||
break; | ||
case "video": | ||
if (firstLoad) break; | ||
const videoElement = document.createElement("video"); | ||
videoElement.src = media; | ||
videoElement.classList.add("max-h-[20rem]"); | ||
videoElement.classList.add("py-2"); | ||
videoElement.setAttribute("controls", ""); | ||
mediaElement.replaceChildren(videoElement); | ||
messageElement.setAttribute("resolved", "true"); | ||
break; | ||
default: | ||
if (firstLoad) return; | ||
mediaElement.innerHTML = media; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { useSocketio } from "./shared-clients"; | ||
import { attachChatLogic } from "./logic/chatLogic"; | ||
import { attachChatsLogic } from "./logic/chatsLogic"; | ||
import { attachHeaderLogic } from "./logic/headerLogic"; | ||
import { attachMessageLogic } from "./logic/messageLogic"; | ||
import { showMainComponentMobile, hideMainComponentMobile } from "./logic/mainLogic"; | ||
import { attachInputBarLogic } from "./logic/inputBarLogic"; | ||
|
||
function socketHandler() { | ||
const socket = useSocketio(); | ||
|
||
socket.on("connect", () => { | ||
console.log(socket.id); | ||
socket.emit("fetchInit"); | ||
}); | ||
|
||
socket.on("reloadChat", (prerender) => { | ||
let chatsComponent = document.getElementById("chats"); | ||
chatsComponent ? (chatsComponent.outerHTML = prerender) : void 0; | ||
hideMainComponentMobile(); | ||
attachChatLogic(); | ||
attachChatsLogic(); | ||
}); | ||
|
||
socket.on("reloadMain", (prerender) => { | ||
let mainComponent = document.getElementById("main"); | ||
mainComponent ? (mainComponent.outerHTML = prerender) : void 0; | ||
showMainComponentMobile(); | ||
attachHeaderLogic(); | ||
attachMessageLogic(); | ||
attachInputBarLogic(); | ||
}); | ||
} | ||
socketHandler(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export type chatsData = chatData[] | ||
|
||
export type chatData = { | ||
chatName: string | ||
chatProfile: string | undefined | null | ||
chatPhone: string | ||
chatIsPinned: boolean | ||
chatIsArchived: boolean | ||
chatMessageLatest: messageLatest | undefined | null | ||
} | ||
|
||
export type messageLatest = { | ||
messageTimestamp: 1683537000, | ||
messageBody: string | ||
messageName: string | ||
messageAuthor: string | ||
messageFromMe: boolean | ||
messageHasMedia: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.