[lib.content-controller] added channel deletion + fixed VOICE rooms that kept adding over and over

This commit is contained in:
xdrm-brackets 2018-04-03 21:59:39 +02:00
parent 7467a0012e
commit b76d2f8499
2 changed files with 82 additions and 21 deletions

View File

@ -71,11 +71,12 @@ export default class ChannelController{
/* (3) Dump/Update channel data
*
* @channels<array> Channels data
* @append<bool> Whether to keep old data and only push new
*
* @return udpated<boolean> Whether channels have been updated
*
---------------------------------------------------------*/
dump(channels){
dump(channels, append=false){
// console.log(`channel.dump([${channels instanceof Array?channels.length:0}])`);
/* (1) Check @channels type */
@ -83,6 +84,7 @@ export default class ChannelController{
return false;
/* (2) Clear list () */
if( append !== true )
this.list.splice(0);
/* (3) Apply new channels */

View File

@ -147,25 +147,30 @@ export default class ContentController{
---------------------------------------------------------*/
manage_update(_dat){
/* (1) Manage rooms
/* (1) Manage rooms DELETE
---------------------------------------------------------*/
/* (1) Extract ids */
let room_ids = Object.keys(_dat.room).map( (v) => parseInt(v) );
let current_list = gs.get.room.text.list;
let current_list = gs.get.room;
/* (2) Manage DELETED rooms */
for( let ri in current_list ){
for( let t in current_list ){
for( let ri in current_list[t].list ){
// if existing room is not in received keys -> has been deleted
let to_remove = room_ids.indexOf(current_list[ri].id) < 0;
let to_remove = room_ids.indexOf(current_list[t].list[ri].id) < 0;
// delete room from interface
( to_remove ) && current_list.splice(ri,1);
( to_remove ) && current_list[t].list.splice(ri,1);
}
}
/* (3) Manage UPDATE + CREATE rooms */
/* (2) Manage rooms CREATE + UPDATE
---------------------------------------------------------*/
for( let ri of room_ids ){
// 1. Extract room data
@ -175,35 +180,55 @@ export default class ContentController{
if( room === null )
continue;
// 3. Check whether room already exists in interface
// 3. Manage room 'type'
room.type = (room.type === 0) ? 'text' : 'voice';
// 4. Check whether room already exists in interface
let existing_index = -1;
for( let r in current_list )
if( current_list[r].id === ri ){ existing_index = r; break; }
for( let t in current_list ){
for( let r in current_list[t].list ){
if( current_list[t].list[r].id === ri ){
existing_index = r;
break;
}
}
}
// 4. Create room
// 5. Create room
if( existing_index < 0 ){
console.log(`create room#${ri} of type ${room.type}`);
gs.get.room.dump([{
rid: ri,
name: room.name,
messages: room.messages,
members: room.members,
type: 'text'
type: room.type
}], true);
continue;
}
// 5. Update room
current_list[existing_index].name = room.name;
current_list[existing_index].members = room.members;
current_list[room.type].list[existing_index].name = room.name;
current_list[room.type].list[existing_index].members = room.members;
// 6. Push new messages
// 6. We are done if VOICE room
if( room.type === 'voice' )
continue;
// 7. Push new messages
for( let m of room.messages ){
current_list[existing_index].messages.push({
current_list[room.type].list[existing_index].messages.push({
uid: m.uid,
mid: m.mid,
msg: m.content,
@ -212,7 +237,7 @@ export default class ContentController{
}
/* (7) Notification API -> if not current channel */
// 8. Notification API -> if not current channel
if( room.messages.length > 0 && ri !== gs.get.content.rid ){
let title = `Room #${room.name}`;
@ -223,9 +248,43 @@ export default class ContentController{
}
/* (3) Manage channels DELETE
---------------------------------------------------------*/
for( let c of _dat.channels.rem ){
console.log(c);
// gs.get.channel.dump({
// id: parseInt(c.id),
// label: c.name,
// link: c.link
// }, true);
}
/* (4) Manage channels CREATE
---------------------------------------------------------*/
for( let c of _dat.channels.add ){
gs.get.channel.dump({
id: parseInt(c.id),
label: c.name,
link: c.link
}, true);
}
}
}