Skip to content

Commit

Permalink
Migrate cMessages to map & add chat history messages to cmessages
Browse files Browse the repository at this point in the history
  • Loading branch information
Zekiah-A committed Nov 30, 2024
1 parent 3febb5e commit 6731549
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
42 changes: 24 additions & 18 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,8 @@
const newMessage = createLiveChatMessage(messageId, txt, intId, name, sendDate, repliesTo, reactions)
if (before) {
chatMessages.prepend(newMessage)
const channelMessages = chatMessages.get(currentChannel)
channelMessages.unshift(newMessage)
}
messageRenderPromises.push(
newMessage.updateComplete.then(() => {
Expand Down Expand Up @@ -494,7 +496,7 @@
if (data.byteLength - offset >= 4) {
repliesTo = data.getUint32(offset)
}
if (!(channel in cMessages)) {
if (!cMessages.has(channel)) {
break
}

Expand All @@ -510,9 +512,10 @@
const atScrollBottom = chatMessages.scrollTop + chatMessages.offsetHeight + 64 >= chatMessages.scrollHeight

// Insert the message into the channel
cMessages[channel].push(newMessage)
if (cMessages[channel].length > MAX_CHANNEL_MESSAGES) {
cMessages[channel].shift()
const channelMessages = cMessages.get(channel)
channelMessages.push(newMessage)
if (channelMessages.length > MAX_CHANNEL_MESSAGES) {
channelMessages.shift()
}
if (channel == currentChannel) {
if (chatMessages.children.length > MAX_CHANNEL_MESSAGES) {
Expand Down Expand Up @@ -551,7 +554,7 @@
}
case 17: {// Live chat delete
const messageId = data.getUint32(1)
for (const channel of Object.values(cMessages)) {
for (const channel of cMessages.values()) {
for (const messageEl of channel) {
if (messageEl.messageId !== messageId) continue
channel.splice(channel.indexOf(messageEl), 1)
Expand All @@ -564,7 +567,7 @@
const messageId = data.getUint32(1)
const reactorId = data.getUint32(5)
const reactionKey = decoder.decode(data.buffer.slice(9))
for (const channel of Object.values(cMessages)) {
for (const channel of cMessages.values()) {
for (const messageEl of channel) {
if (messageEl.messageId !== messageId) {
continue
Expand Down Expand Up @@ -1268,9 +1271,11 @@ <h4>Manage:</h4>
<div id="modMessageIdForm" class="mod-form">
<input type="number" id="modMessageId" title="Message ID" placeholder="Message ID" oninput="
let found = null
for (let message of cMessages[currentChannel]) { if (message.messageId == this.value) {
found = message
}}
for (let message of cMessages.get(currentChannel)) {
if (message.messageId == this.value) {
found = message
}
}
modMessagePreview.innerHTML = found?.innerHTML ?? 'Message not found'">
<div id="modMessagePreview">Message not found</div>
</div>
Expand Down Expand Up @@ -2346,7 +2351,10 @@ <h4>Quest complete! 🥳</h4>
const hash = (text) => text.split("").reduce((hash, char) => (hash * 31 + char.charCodeAt()) >>> 0, 0)
let online = 1
let extraLanguage = (lang == "en" ? "tr" : lang)
let cMessages = { [extraLanguage]: [], en: [] }
/** @type {Map<string, LiveChatMessage[]>} */ const cMessages = new Map([
[extraLanguage, []],
["en", []]
])
let chatPreviousLoadDebounce = false
let chatPreviousAutoLoad = false
let currentChannel = lang
Expand Down Expand Up @@ -2437,14 +2445,12 @@ <h4>Quest complete! 🥳</h4>
}

function extraChannel(code) {
// TODO: Implement if persisting all channels becomes too heavy
// delete cMessages[extraLanguage]
let info = LANG_INFOS.get(code)
channelMineName.innerText = code.toUpperCase()
channelMineImg.src = info?.flag
channelMineImg.style.display = ((info?.flag) ? "inline" : "none")
extraLanguage = code
cMessages[code] ||= []
cMessages.set(code, cMessages.get(code) || [])
}

function switchLanguageChannel(selected) {
Expand All @@ -2463,8 +2469,8 @@ <h4>Quest complete! 🥳</h4>
// User must ask to load previous at least once for each channel before site
// will start auto loading previous chat messages
chatPreviousAutoLoad = false
if (cMessages[selected]?.length) {
for (const el of cMessages[selected]) {
if (cMessages.get(selected)?.length) {
for (const el of cMessages.get(selected)) {
chatMessages.appendChild(el)
}
}
Expand Down Expand Up @@ -2497,7 +2503,7 @@ <h4>Quest complete! 🥳</h4>
}

function chatReply(messageId, senderId) {
for (let m of cMessages[currentChannel]) {
for (let m of cMessages.get(currentChannel)) {
m.removeAttribute("reply")
}
currentReply = messageId
Expand All @@ -2507,7 +2513,7 @@ <h4>Quest complete! 🥳</h4>
messageInput.focus()
messageReplyPanel.removeAttribute("closed")
messageReplyLabel.innerText = translate("replyTo") + ": " + (intIdNames.get(senderId) || ("#" + senderId))
for (let m of cMessages[currentChannel]) {
for (let m of cMessages.get(currentChannel)) {
if (m["messageId"] == messageId) {
m.setAttribute("reply", "true")
break
Expand All @@ -2516,7 +2522,7 @@ <h4>Quest complete! 🥳</h4>
}

function chatCancelReplies() {
for (let m of cMessages[currentChannel]) {
for (let m of cMessages.get(currentChannel)) {
m.removeAttribute("reply")
}
currentReply = null
Expand Down
2 changes: 1 addition & 1 deletion live-chat-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class LiveChatMessage extends LitElement {
}

#findReplyingMessage() {
if (!cMessages[currentChannel]) {
if (!cMessages.has(currentChannel)) {
return { name: "[ERROR]", content: "Channel not found", fake: true }
}

Expand Down

0 comments on commit 6731549

Please sign in to comment.