Skip to content

Commit

Permalink
stop chat going cross-room
Browse files Browse the repository at this point in the history
  • Loading branch information
TodePond committed Mar 1, 2025
1 parent aabb4bc commit b84b9b6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
49 changes: 49 additions & 0 deletions src/chat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { pastamirror } from './main.js';
import { getRoomName, getSession } from './session.js';

export function getChatSessionName() {
return `session:${getRoomName()}:chat`;
}

export function sendChatMessage({ docId, message, from, user, color }) {
const session = getSession();
session._pubSubClient.publish(getChatSessionName(), {
docId,
message,
user,
from,
color,
});
}

export function handleChatMessage(message) {
const view = pastamirror.editorViews.get(message.docId);
let from = message.from;
const pos = view.coordsAtPos(from);
const chatContainer = document.querySelector('.chat-container');
if (pos) {
const messageContainer = document.createElement('div');
messageContainer.innerText = message.message;
const pointer_color = message.color;
messageContainer.style = `position:fixed;top:${pos.top}px;left:${pos.left}px`;
messageContainer.style.color = pointer_color;
messageContainer.classList.add('rising-animation');
messageContainer.classList.add('message-container');
chatContainer?.appendChild(messageContainer);
setTimeout(() => {
messageContainer.remove();
}, 7000);
} else {
console.warn('could not get line position');
}
}

export function subscribeToChat() {
const session = getSession();
session._pubSubClient.subscribe(getChatSessionName(), (args) => handleChatMessage(args.message));
}

export function unsubscribeFromChat() {
const session = getSession();
session._pubSubClient.unsubscribe(getChatSessionName());
}
35 changes: 1 addition & 34 deletions src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { getColorFromUserHue, getSettings } from './settings.js';
import { insertNewline } from '@codemirror/commands';
import { nudelAlert } from './alert.js';
import { strudelAutocomplete } from './strudel-autocomplete.js';
import { pastamirror } from './main.js';
import { handleChatMessage, sendChatMessage } from './chat.js';
import { getSession } from './session.js';

// we need to access these variables from the strudel iframe:
Expand Down Expand Up @@ -394,37 +394,4 @@ export class PastaMirror {
}
}
}

chat(message) {
const view = this.editorViews.get(message.docId);
let from = message.from;
const pos = view.coordsAtPos(from);
const chatContainer = document.querySelector('.chat-container');
if (pos) {
const messageContainer = document.createElement('div');
messageContainer.innerText = message.message;
const pointer_color = message.color;
messageContainer.style = `position:fixed;top:${pos.top}px;left:${pos.left}px`;
messageContainer.style.color = pointer_color;
messageContainer.classList.add('rising-animation');
messageContainer.classList.add('message-container');
chatContainer?.appendChild(messageContainer);
setTimeout(() => {
messageContainer.remove();
}, 7000);
} else {
console.warn('could not get line position');
}
}
}

export function sendChatMessage({ docId, message, from, user, color }) {
const session = getSession();
session._pubSubClient.publish(`session:pastagang:chat`, {
docId,
message,
user,
from,
color,
});
}
5 changes: 4 additions & 1 deletion src/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getStdSource } from './export.js';
import { pastamirror, Frame } from './main.js';
import { clearGlobalError, setError, clearLocalError } from './error.js';
import { getSettings } from './settings.js';
import { getChatSessionName, handleChatMessage, subscribeToChat, unsubscribeFromChat } from './chat.js';

const PASTAGANG_ROOM_NAME = 'pastagang3';

Expand Down Expand Up @@ -74,12 +75,14 @@ function makeSession() {
// session._pubSubClient seems to take a while to be defined..
// this might or might not be a good place to make sure its ready
// the event might call multiple times so... do i need to unsub???
session._pubSubClient.subscribe(`session:pastagang:chat`, (args) => pastamirror.chat(args.message));
subscribeToChat();
});
session.on('pubsub:close', () => {
// untested
setError('Disconnected from Server...');
// unsub session:pastagang:chat here?
// lets try (?)
unsubscribeFromChat();
});

// hydra
Expand Down

0 comments on commit b84b9b6

Please sign in to comment.