[lib.content-controller] added channel deletion + fixed VOICE rooms that kept adding over and over
This commit is contained in:
parent
7467a0012e
commit
b76d2f8499
|
@ -71,11 +71,12 @@ export default class ChannelController{
|
||||||
/* (3) Dump/Update channel data
|
/* (3) Dump/Update channel data
|
||||||
*
|
*
|
||||||
* @channels<array> Channels data
|
* @channels<array> Channels data
|
||||||
|
* @append<bool> Whether to keep old data and only push new
|
||||||
*
|
*
|
||||||
* @return udpated<boolean> Whether channels have been updated
|
* @return udpated<boolean> Whether channels have been updated
|
||||||
*
|
*
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
dump(channels){
|
dump(channels, append=false){
|
||||||
// console.log(`channel.dump([${channels instanceof Array?channels.length:0}])`);
|
// console.log(`channel.dump([${channels instanceof Array?channels.length:0}])`);
|
||||||
|
|
||||||
/* (1) Check @channels type */
|
/* (1) Check @channels type */
|
||||||
|
@ -83,7 +84,8 @@ export default class ChannelController{
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* (2) Clear list () */
|
/* (2) Clear list () */
|
||||||
this.list.splice(0);
|
if( append !== true )
|
||||||
|
this.list.splice(0);
|
||||||
|
|
||||||
/* (3) Apply new channels */
|
/* (3) Apply new channels */
|
||||||
for(let c of channels){
|
for(let c of channels){
|
||||||
|
|
|
@ -147,25 +147,30 @@ export default class ContentController{
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
manage_update(_dat){
|
manage_update(_dat){
|
||||||
|
|
||||||
/* (1) Manage rooms
|
/* (1) Manage rooms DELETE
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
/* (1) Extract ids */
|
/* (1) Extract ids */
|
||||||
let room_ids = Object.keys(_dat.room).map( (v) => parseInt(v) );
|
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 */
|
/* (2) Manage DELETED rooms */
|
||||||
for( let ri in current_list ){
|
for( let t in current_list ){
|
||||||
|
|
||||||
// if existing room is not in received keys -> has been deleted
|
for( let ri in current_list[t].list ){
|
||||||
let to_remove = room_ids.indexOf(current_list[ri].id) < 0;
|
|
||||||
|
|
||||||
// delete room from interface
|
// if existing room is not in received keys -> has been deleted
|
||||||
( to_remove ) && current_list.splice(ri,1);
|
let to_remove = room_ids.indexOf(current_list[t].list[ri].id) < 0;
|
||||||
|
|
||||||
|
// delete room from interface
|
||||||
|
( 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 ){
|
for( let ri of room_ids ){
|
||||||
|
|
||||||
// 1. Extract room data
|
// 1. Extract room data
|
||||||
|
@ -175,35 +180,55 @@ export default class ContentController{
|
||||||
if( room === null )
|
if( room === null )
|
||||||
continue;
|
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;
|
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 ){
|
if( existing_index < 0 ){
|
||||||
|
|
||||||
|
console.log(`create room#${ri} of type ${room.type}`);
|
||||||
gs.get.room.dump([{
|
gs.get.room.dump([{
|
||||||
rid: ri,
|
rid: ri,
|
||||||
name: room.name,
|
name: room.name,
|
||||||
messages: room.messages,
|
messages: room.messages,
|
||||||
members: room.members,
|
members: room.members,
|
||||||
type: 'text'
|
type: room.type
|
||||||
}], true);
|
}], true);
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Update room
|
// 5. Update room
|
||||||
current_list[existing_index].name = room.name;
|
current_list[room.type].list[existing_index].name = room.name;
|
||||||
current_list[existing_index].members = room.members;
|
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 ){
|
for( let m of room.messages ){
|
||||||
|
|
||||||
current_list[existing_index].messages.push({
|
current_list[room.type].list[existing_index].messages.push({
|
||||||
uid: m.uid,
|
uid: m.uid,
|
||||||
mid: m.mid,
|
mid: m.mid,
|
||||||
msg: m.content,
|
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 ){
|
if( room.messages.length > 0 && ri !== gs.get.content.rid ){
|
||||||
|
|
||||||
let title = `Room #${room.name}`;
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue