[lib.content-controller] manage_update() with DELETE/UPDATE/NEW_MESSAGES/CREATE rooms + notification API when messages on another room
This commit is contained in:
parent
e8ce193cc0
commit
6dfa2b12e1
|
@ -147,29 +147,74 @@ export default class ContentController{
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
manage_update(_dat){
|
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;
|
continue;
|
||||||
|
|
||||||
|
// 3. Check whether room already exists in interface
|
||||||
|
let existing_index = -1;
|
||||||
|
|
||||||
// create new room
|
for( let r in current_list )
|
||||||
for( let r2 in gs.get.room.text.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([{
|
||||||
uid: m.uid,
|
rid: ri,
|
||||||
mid: m.mid,
|
name: room.name,
|
||||||
msg: m.content,
|
messages: room.messages,
|
||||||
ts: m.ts
|
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}`);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,6 +62,7 @@ export default class RoomController{
|
||||||
/* (3) Dump/Update room data (raw format -> tree structure)
|
/* (3) Dump/Update room data (raw format -> tree structure)
|
||||||
*
|
*
|
||||||
* @rooms<array> rooms data (raw format)
|
* @rooms<array> rooms data (raw format)
|
||||||
|
* @append<bool> Whether to keep old data and only push new
|
||||||
*
|
*
|
||||||
* {raw_format}
|
* {raw_format}
|
||||||
* [
|
* [
|
||||||
|
@ -73,7 +74,7 @@ export default class RoomController{
|
||||||
* @return udpated<boolean> Whether rooms have been updated
|
* @return udpated<boolean> Whether rooms have been updated
|
||||||
*
|
*
|
||||||
---------------------------------------------------------*/
|
---------------------------------------------------------*/
|
||||||
dump(rooms){
|
dump(rooms, append=false){
|
||||||
|
|
||||||
console.log(`room.dump([${rooms instanceof Array?rooms.length:-1}])`);
|
console.log(`room.dump([${rooms instanceof Array?rooms.length:-1}])`);
|
||||||
|
|
||||||
|
@ -82,9 +83,13 @@ export default class RoomController{
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* (2) Clear data */
|
/* (2) Clear data */
|
||||||
for( let type in this ){
|
if( append !== true ){
|
||||||
this[type].list = [];
|
|
||||||
this[type].current = null;
|
for( let type in this ){
|
||||||
|
this[type].list = [];
|
||||||
|
this[type].current = null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// redirection
|
// redirection
|
||||||
|
@ -236,9 +241,6 @@ export default class RoomController{
|
||||||
if( rs.error !== 0 || rs.rid == null )
|
if( rs.error !== 0 || rs.rid == null )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* (2) Reload room list */
|
|
||||||
this.fetch();
|
|
||||||
|
|
||||||
}.bind(this), auth.token);
|
}.bind(this), auth.token);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
@ -273,9 +275,6 @@ export default class RoomController{
|
||||||
if( rs.error !== 0 )
|
if( rs.error !== 0 )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
/* (2) Reload room list */
|
|
||||||
this.fetch();
|
|
||||||
|
|
||||||
}.bind(this), auth.token);
|
}.bind(this), auth.token);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -52,9 +52,8 @@ gs.set('router', new VueRouter({
|
||||||
/* (5) refresh page */
|
/* (5) refresh page */
|
||||||
gs.set('refresh', () => ( document.location = '' ) );
|
gs.set('refresh', () => ( document.location = '' ) );
|
||||||
|
|
||||||
|
/* (6) Ask for permission API */
|
||||||
|
Notification.requestPermission();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue