Skip to content
Snippets Groups Projects
Commit 0bad6d9b authored by Xavier Ferrer de la Penyita's avatar Xavier Ferrer de la Penyita
Browse files

clear room messages when leaving a room if there are more than 30

parent 3c55d578
No related branches found
No related tags found
2 merge requests!69Beta,!67Clear room messages
...@@ -116,6 +116,31 @@ export const SolidXMPPChat = { ...@@ -116,6 +116,31 @@ export const SolidXMPPChat = {
// Change chat plugin // Change chat plugin
converse_sib.service.plugins.sibChat = new(class { converse_sib.service.plugins.sibChat = new(class {
changeChat(jid, is_groupchat, root) { changeChat(jid, is_groupchat, root) {
function isEmptyMessage (attrs) {
if (attrs && attrs.attributes) {
attrs = attrs.attributes;
}
return !attrs['oob_url'] &&
!attrs['file'] &&
!(attrs['is_encrypted'] && attrs['plaintext']) &&
!attrs['message'];
};
function removeUnnecessaryDayIndicators(view) {
const pred = (el) =>
el.matches('.date-separator') && el.nextElementSibling.matches('.date-separator');
const container = view.el.querySelector('.chat-content__messages');
const to_remove = Array.from(container.children).filter(pred);
to_remove.forEach((el) => el.parentElement.removeChild(el));
};
function isHidden(classList){
for(let i = 0; i < classList.length; i++){
if(classList[i] === 'hidden'){
return true;
}
}
return false;
}
if (!jid) { if (!jid) {
return; return;
} }
...@@ -130,6 +155,29 @@ export const SolidXMPPChat = { ...@@ -130,6 +155,29 @@ export const SolidXMPPChat = {
if (converse_el) { if (converse_el) {
root.appendChild(converse_el); root.appendChild(converse_el);
if (is_groupchat) {
const jid_to_clear = converse_el.getElementsByClassName('converse-chatboxes');
let room_to_clear_view = '';
if (jid_to_clear.length && jid_to_clear[0].children.length){
for (let i = 0; i < jid_to_clear[0].children.length; i++) {
if(!isHidden(jid_to_clear[0].children[i].classList)){
room_to_clear_view = this._converse.chatboxviews.views[jid_to_clear[0].children[i].id.split('-')[1]];
break;
}
}
}
if (room_to_clear_view) {
if (room_to_clear_view.model.messages.length > 30) {
const non_empty_messages = room_to_clear_view.model.messages.filter((m) => !isEmptyMessage(m));
if (non_empty_messages.length > 30) {
while (non_empty_messages.length > 30) {
non_empty_messages.shift().destroy();
}
removeUnnecessaryDayIndicators(room_to_clear_view);
}
}
}
}
} }
if (is_groupchat) { if (is_groupchat) {
this._converse.api.rooms.open(jid, {}, true); this._converse.api.rooms.open(jid, {}, true);
...@@ -138,6 +186,7 @@ export const SolidXMPPChat = { ...@@ -138,6 +186,7 @@ export const SolidXMPPChat = {
} }
} }
}); });
// Initialize deferred resolution plugin
setTimeout(async () => { setTimeout(async () => {
// Initialize change change plugin // Initialize change change plugin
converse.plugins.add('conversejs-changechat', converse_sib.service.plugins.sibChat); converse.plugins.add('conversejs-changechat', converse_sib.service.plugins.sibChat);
...@@ -292,7 +341,7 @@ export const SolidXMPPChat = { ...@@ -292,7 +341,7 @@ export const SolidXMPPChat = {
'root': this.element.shadowRoot, 'root': this.element.shadowRoot,
'show_client_info': false, 'show_client_info': false,
'show_desktop_notifications': false, 'show_desktop_notifications': false,
'persistent_store': 'IndexedDB', 'persistent_store': 'localStorage',
'sounds_path': ComponentPath + '/dist/conversejs/', 'sounds_path': ComponentPath + '/dist/conversejs/',
'show_send_button': false, 'show_send_button': false,
'view_mode': 'fullscreen', 'view_mode': 'fullscreen',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment