diff --git a/webpack/lib/content-controller.js b/webpack/lib/content-controller.js index 02e1545..c00592a 100644 --- a/webpack/lib/content-controller.js +++ b/webpack/lib/content-controller.js @@ -147,29 +147,74 @@ export default class ContentController{ ---------------------------------------------------------*/ manage_update(_dat){ - for( let r in _dat.room ){ + /* (1) Manage rooms + ---------------------------------------------------------*/ + /* (1) Extract ids */ + let room_ids = Object.keys(_dat.room).map( (v) => parseInt(v) ); + let current_list = gs.get.room.text.list; - if( _dat.room[r] === null ) + /* (2) Manage DELETED rooms */ + for( let ri in current_list ){ + + // if existing room is not in received keys -> has been deleted + let to_remove = room_ids.indexOf(current_list[ri].id) < 0; + + // delete room from interface + ( to_remove ) && current_list.splice(ri,1); + + } + + + /* (3) Manage UPDATE + CREATE rooms */ + for( let ri of room_ids ){ + + // 1. Extract room data + let room = _dat.room[ri]; + + // 2. if room data is null -> ignore + if( room === null ) continue; + // 3. Check whether room already exists in interface + let existing_index = -1; - // create new room - for( let r2 in gs.get.room.text.list ){ + for( let r in current_list ) + if( current_list[r].id === ri ){ existing_index = r; break; } - if( gs.get.room.text.list[r2].id === r ) - return console.log('CREATE ROOM'); - for( let m of _dat.room[r].messages ) + // 4. Create room + if( existing_index < 0 ){ - gs.get.room.text.list[r2].messages.push({ - uid: m.uid, - mid: m.mid, - msg: m.content, - ts: m.ts - }) + gs.get.room.dump([{ + rid: ri, + name: room.name, + messages: room.messages, + members: room.members, + type: 'text' + }], true); + + continue; + } + + // 5. Update room + current_list[existing_index].name = room.name; + current_list[existing_index].members = room.members; + + // 6. Push new messages + for( let m of room.messages ){ + + current_list[existing_index].messages.push({ + uid: m.uid, + mid: m.mid, + msg: m.content, + ts: m.ts + }); } + /* (7) Notification API -> if not current channel */ + if( room.messages.length > 0 && ri !== gs.get.content.rid ) + new Notification(`${room.messages.length} new messages in #${room.name}`); } diff --git a/webpack/lib/room-controller.js b/webpack/lib/room-controller.js index 1d97654..c1adf63 100644 --- a/webpack/lib/room-controller.js +++ b/webpack/lib/room-controller.js @@ -62,6 +62,7 @@ export default class RoomController{ /* (3) Dump/Update room data (raw format -> tree structure) * * @rooms rooms data (raw format) + * @append Whether to keep old data and only push new * * {raw_format} * [ @@ -73,7 +74,7 @@ export default class RoomController{ * @return udpated Whether rooms have been updated * ---------------------------------------------------------*/ - dump(rooms){ + dump(rooms, append=false){ console.log(`room.dump([${rooms instanceof Array?rooms.length:-1}])`); @@ -82,9 +83,13 @@ export default class RoomController{ return false; /* (2) Clear data */ - for( let type in this ){ - this[type].list = []; - this[type].current = null; + if( append !== true ){ + + for( let type in this ){ + this[type].list = []; + this[type].current = null; + } + } // redirection @@ -236,9 +241,6 @@ export default class RoomController{ if( rs.error !== 0 || rs.rid == null ) return false; - /* (2) Reload room list */ - this.fetch(); - }.bind(this), auth.token); return true; @@ -273,9 +275,6 @@ export default class RoomController{ if( rs.error !== 0 ) return false; - /* (2) Reload room list */ - this.fetch(); - }.bind(this), auth.token); return true; diff --git a/webpack/setup.js b/webpack/setup.js index 3be9e39..09dd4e9 100644 --- a/webpack/setup.js +++ b/webpack/setup.js @@ -52,9 +52,8 @@ gs.set('router', new VueRouter({ /* (5) refresh page */ gs.set('refresh', () => ( document.location = '' ) ); - - - +/* (6) Ask for permission API */ +Notification.requestPermission();