2018-03-24 15:49:00 +00:00
|
|
|
export default class RoomController{
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (1) Construct default attributes
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
constructor(){
|
|
|
|
|
|
|
|
/* (1) Init available @room sets */
|
|
|
|
this.text = { list: [], current: 0, visible: true };
|
|
|
|
this.voice = { list: [], current: null, visible: true };
|
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
/* (2) Current room buffer */
|
2018-03-28 19:51:04 +00:00
|
|
|
this._buffer = {
|
2018-03-28 19:04:49 +00:00
|
|
|
text: {},
|
|
|
|
voice: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) room navigation
|
|
|
|
*
|
|
|
|
* @type<int> room type
|
|
|
|
* @id<int> room ID (NULL uses get())
|
|
|
|
*
|
|
|
|
* @return status<boolean> Whether the navigation has been successful
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
nav(type=null, id=null){
|
|
|
|
console.log(`room.nav(${type}, ${id})`);
|
|
|
|
|
|
|
|
/* (1) Try to get current room from id */
|
|
|
|
let room = this.get(type, id);
|
|
|
|
|
|
|
|
/* (2) Manage invalid room */
|
2018-03-23 07:41:17 +00:00
|
|
|
if( Object.keys(room).length == 0 )
|
2018-03-22 13:57:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (3) Close last room */
|
|
|
|
|
|
|
|
/* (4) Open new room */
|
|
|
|
|
|
|
|
/* (5) Update @active room */
|
|
|
|
this[type].current = room.id;
|
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
/* (6) Update buffer */
|
2018-03-28 19:51:04 +00:00
|
|
|
this._buffer[type] = {};
|
2018-03-28 19:04:49 +00:00
|
|
|
for( let r of this[type].list )
|
|
|
|
if( r.id === this[type].current )
|
2018-03-28 19:51:04 +00:00
|
|
|
this._buffer[type] = r;
|
2018-03-28 19:04:49 +00:00
|
|
|
|
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
console.log(`[room.${type}.opened] ${room.name} (${room.id})`, room.id);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Dump/Update room data (raw format -> tree structure)
|
|
|
|
*
|
|
|
|
* @rooms<array> rooms data (raw format)
|
|
|
|
*
|
|
|
|
* {raw_format}
|
|
|
|
* [
|
|
|
|
* { rid: <int>, type: 'text', name: <string> }
|
|
|
|
* { rid: <int>, type: 'voice', name: <string> }
|
|
|
|
* { rid: <int>, type: 'video', name: <string> }
|
|
|
|
* ]
|
|
|
|
*
|
|
|
|
* @return udpated<boolean> Whether rooms have been updated
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
dump(rooms){
|
|
|
|
|
2018-03-27 16:21:02 +00:00
|
|
|
console.log(`room.dump([${rooms instanceof Array?rooms.length:-1}])`);
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
/* (1) Check @rooms type */
|
|
|
|
if( !(rooms instanceof Array) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Clear data */
|
|
|
|
for( let type in this ){
|
|
|
|
this[type].list = [];
|
|
|
|
this[type].current = null;
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:21:02 +00:00
|
|
|
// redirection
|
|
|
|
let redirected = false;
|
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
/* (3) Browse each room */
|
|
|
|
for(let r of rooms){
|
|
|
|
|
|
|
|
// {1} Ignore: if not object //
|
|
|
|
if( typeof r !== 'object' )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// {2} Ignore: if missing field //
|
2018-03-27 16:33:39 +00:00
|
|
|
if( isNaN(r.rid) || typeof r.type !== 'string' || typeof r.name !== 'string' )
|
2018-03-22 13:57:03 +00:00
|
|
|
continue;
|
|
|
|
|
2018-03-27 16:33:39 +00:00
|
|
|
// {3} Default: missing messages //
|
2018-03-27 16:21:02 +00:00
|
|
|
if( !( r.messages instanceof Array) )
|
|
|
|
r.messages = [];
|
|
|
|
|
2018-03-27 16:33:39 +00:00
|
|
|
// {4} Default: missing members //
|
2018-03-27 16:21:02 +00:00
|
|
|
if( !( r.members instanceof Array) )
|
|
|
|
r.members = [];
|
|
|
|
|
2018-03-27 16:33:39 +00:00
|
|
|
// {5} Ignore: not available type //
|
2018-03-22 13:57:03 +00:00
|
|
|
if( this[r.type] == null )
|
|
|
|
continue;
|
|
|
|
|
2018-03-27 16:33:39 +00:00
|
|
|
// {6} store data
|
|
|
|
this[r.type].list.push({
|
|
|
|
id: r.rid,
|
|
|
|
name: r.name,
|
|
|
|
type: r.type,
|
|
|
|
messages: r.messages
|
|
|
|
});
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-27 16:33:39 +00:00
|
|
|
// {7} redirect if first element
|
2018-03-27 16:21:02 +00:00
|
|
|
if( !redirected && r.type == 'text' ){
|
|
|
|
redirected = true;
|
|
|
|
this.nav('text', r.rid);
|
|
|
|
}
|
2018-03-22 13:57:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Get room data
|
|
|
|
*
|
|
|
|
* @type<int> room type
|
|
|
|
* @id<int> room ID
|
|
|
|
* NULL: current room || from URL (and set current)
|
|
|
|
*
|
|
|
|
* @return room<array> room data
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
get(type=null, id=null){
|
|
|
|
console.log(`room.get(${type}, ${id})`);
|
|
|
|
|
|
|
|
/* (1) Manage invalid @type */
|
|
|
|
if( typeof type !== 'string' || this[type] == null )
|
2018-03-23 07:41:17 +00:00
|
|
|
return {};
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
/* (1) Get @current room: if id is null
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( id === null ){
|
|
|
|
|
|
|
|
/* (1) If @current is set */
|
|
|
|
if( !isNaN(this[type].current) ){
|
|
|
|
|
|
|
|
/* (2) Return matching id in list */
|
|
|
|
for( let r of this[type].list ){
|
|
|
|
|
|
|
|
if( r.id === this[type].current )
|
|
|
|
return r; // exit point
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Get room data
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Return room matching id */
|
|
|
|
for( let r of this[type].list )
|
|
|
|
if( r.id === id )
|
|
|
|
return r; // exit point
|
|
|
|
|
|
|
|
/* (2) Return default: if ID not found */
|
|
|
|
this[type].current = null;
|
2018-03-23 07:41:17 +00:00
|
|
|
return {};
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-27 16:21:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (5) Fetch channel data
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
fetch(){
|
|
|
|
|
|
|
|
api.call(`GET /channel/${gs.get.channel.current}`, {}, function(rs){
|
|
|
|
|
|
|
|
/* (1) Check errors */
|
|
|
|
if( rs.error !== 0 || rs.channel == null )
|
|
|
|
return;
|
|
|
|
|
2018-03-28 13:54:19 +00:00
|
|
|
/* (2) Dump rooms data */
|
2018-03-27 16:21:02 +00:00
|
|
|
this.dump(rs.channel.room);
|
|
|
|
|
2018-03-28 13:54:19 +00:00
|
|
|
/* (3) Store channel users */
|
|
|
|
gs.get.content.cbuf.users = rs.channel.users;
|
|
|
|
|
|
|
|
|
2018-03-27 16:21:02 +00:00
|
|
|
}.bind(this), auth.token);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (6) Create a new room
|
2018-03-22 18:13:52 +00:00
|
|
|
*
|
|
|
|
* @type<int> room type
|
|
|
|
* @name<String> room name
|
|
|
|
*
|
|
|
|
* @return created<bool> Whether the room has been created
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
create(type=null, name=null){
|
|
|
|
|
|
|
|
/* (1) Manage invalid @type */
|
|
|
|
if( typeof type !== 'string' || this[type] == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Manage invalid @name */
|
2018-03-28 23:30:47 +00:00
|
|
|
if( typeof name !== 'string' )
|
2018-03-22 18:13:52 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (3) Try to create room in API */
|
2018-03-27 16:33:39 +00:00
|
|
|
api.call(`POST /channel/room/${gs.get.content.cid}`, { type: type, name: name }, function(rs){
|
2018-03-22 18:13:52 +00:00
|
|
|
|
2018-03-27 16:33:39 +00:00
|
|
|
/* (1) Manage error */
|
|
|
|
if( rs.error !== 0 || rs.rid == null )
|
|
|
|
return false;
|
2018-03-22 18:13:52 +00:00
|
|
|
|
2018-03-27 16:33:39 +00:00
|
|
|
/* (2) Reload room list */
|
|
|
|
this.fetch();
|
|
|
|
|
|
|
|
/* (3) Hide popup */
|
|
|
|
gs.get.popup.hide();
|
|
|
|
|
|
|
|
}.bind(this), auth.token);
|
2018-03-22 18:13:52 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-22 14:28:34 +00:00
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
}
|