docbrown/app/javascript/chat/util.js
Mac Siri b4292ade33 Implement chatroom (#308)
* Fix multiple chat loading bug

* Open profile links in chat via new tab

* Add unmount handling for chat

* Add overscroll property to chat

* Add channel_name to ChatChannel model

* Create route and page for chatroom WIP

* Expose ChatChannel id to live page

* Implement multichannel support WIP

* Implement chatroom page WIP

* Add css for chatroom

* Update chat's moderation

* Refactor chat's message type

* Swap the use of :message_markdown & :message_html

* Add channel permissions WIP

* Fix failing specs

* Adjust json reponses

* Add empty message prevention

* Update participant error message

* Change Workshop channel to General
2018-05-14 12:50:32 -04:00

56 lines
1.7 KiB
JavaScript

export function getUserDataAndCsrfToken() {
const promise = new Promise((resolve, reject) => {
let i = 0;
const waitingOnUserData = setInterval(() => {
let userData = null;
const dataUserAttribute = document.body.getAttribute('data-user');
const meta = document.querySelector("meta[name='csrf-token']");
if (
dataUserAttribute &&
dataUserAttribute !== 'undefined' &&
dataUserAttribute !== undefined &&
meta &&
meta.content !== 'undefined' &&
meta.content !== undefined
) {
userData = JSON.parse(dataUserAttribute);
}
i += 1;
if (userData) {
clearInterval(waitingOnUserData);
resolve(userData);
} else if (i === 3000) {
clearInterval(waitingOnUserData);
reject(new Error("Couldn't find user data on page."));
}
}, 5);
});
return promise;
}
export function scrollToBottom() {
const element = document.getElementById('messagelist');
element.scrollTop = element.scrollHeight;
}
export function setupObserver(callback) {
const sentinel = document.querySelector('#messagelist__sentinel');
const somethingObserver = new IntersectionObserver(callback);
somethingObserver.observe(sentinel);
}
export function hideMessages(messages, userId) {
const cleanedMessages = Object.keys(messages).reduce(
(accumulator, channelId) => {
const newMessages = messages[channelId].map(message => {
if (message.user_id === userId) {
return Object.assign({ type: 'hidden' }, message);
}
return message;
});
return { ...accumulator, [channelId]: newMessages };
},
{},
);
return cleanedMessages;
}