[lib.content-controller] manage_update() with DELETE/UPDATE/NEW_MESSAGES/CREATE rooms + notification API when messages on another room

This commit is contained in:
xdrm-brackets 2018-04-03 18:56:10 +02:00
parent e8ce193cc0
commit 6dfa2b12e1
3 changed files with 69 additions and 26 deletions

View File

@ -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({
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}`);
}

View File

@ -62,6 +62,7 @@ export default class RoomController{
/* (3) Dump/Update room data (raw format -> tree structure)
*
* @rooms<array> rooms data (raw format)
* @append<bool> Whether to keep old data and only push new
*
* {raw_format}
* [
@ -73,7 +74,7 @@ export default class RoomController{
* @return udpated<boolean> Whether rooms have been updated
*
---------------------------------------------------------*/
dump(rooms){
dump(rooms, append=false){
console.log(`room.dump([${rooms instanceof Array?rooms.length:-1}])`);
@ -82,11 +83,15 @@ export default class RoomController{
return false;
/* (2) Clear data */
if( append !== true ){
for( let type in this ){
this[type].list = [];
this[type].current = null;
}
}
// redirection
let redirected = false;
@ -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;

View File

@ -52,9 +52,8 @@ gs.set('router', new VueRouter({
/* (5) refresh page */
gs.set('refresh', () => ( document.location = '' ) );
/* (6) Ask for permission API */
Notification.requestPermission();