discord-client/parcel/lib/room-controller.js

306 lines
6.4 KiB
JavaScript

export default class RoomController{
/* (1) Construct default attributes
*
---------------------------------------------------------*/
constructor(){
/* (1) Init available @room sets */
this.text = { list: [], current: 0, visible: true };
this.voice = { list: [], current: null, visible: true };
/* (2) Current room buffer */
this._buffer = {
text: {},
voice: {}
};
}
/* (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 */
if( Object.keys(room).length == 0 )
return false;
/* (3) VOICE room : toggle @active */
if( type === 'voice' )
this[type].current = (this[type].current === room.id) ? null : room.id;
/* (4) Update @active room */
if( type === 'text' )
this[type].current = room.id;
/* (5) Tell websocket: new text room */
if( type === 'text' && window.csock instanceof wscd )
csock.send({ buffer: { rid: room.id } });
/* (6) If 'voice' room -> launch audio */
if( type === 'voice' ){
if( typeof this[type].current === 'number' )
AudioManager.launch();
else
AudioManager.kill();
}
/* (6) Update buffer */
this._buffer[type] = {};
for( let r of this[type].list )
if( r.id === this[type].current )
this._buffer[type] = r;
// 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)
* @append<bool> Whether to keep old data and only push new
*
* {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, append=false){
// console.log(`room.dump([${rooms instanceof Array?rooms.length:-1}])`);
/* (1) Check @rooms type */
if( !(rooms instanceof Array) )
return false;
/* (2) Clear data */
if( append !== true ){
for( let type in this ){
this[type].list = [];
this[type].current = null;
}
}
// redirection
let redirected = false;
/* (3) Browse each room */
for(let r of rooms){
// {1} Ignore: if not object //
if( typeof r !== 'object' )
continue;
// {2} Ignore: if missing field //
if( isNaN(r.rid) || typeof r.type !== 'string' || typeof r.name !== 'string' )
continue;
// {3} Default: missing messages //
if( !( r.messages instanceof Array) )
r.messages = [];
// {4} Default: missing members //
if( !( r.members instanceof Array) )
r.members = [];
// {5} Ignore: not available type //
if( this[r.type] == null )
continue;
// {6} store data
this[r.type].list.push({
id: r.rid,
name: r.name,
type: r.type,
messages: r.messages,
members: r.members
});
// {7} redirect if first element
if( !redirected && r.type == 'text' ){
redirected = true;
this.nav('text', r.rid);
}
}
/* (3) If full dump (append: false) -> create websocket */
if( append !== true )
gs.get.content.ws_connect();
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 )
return {};
/* (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;
return {};
}
/* (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;
/* (2) Dump rooms data */
this.dump(rs.channel.room);
/* (3) Store channel users */
gs.get.content.cbuf.users = rs.channel.users;
}.bind(this), auth.token);
}
/* (6) Create a new room
*
* @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 */
if( typeof name !== 'string' )
return false;
/* (3) Try to create room in API */
api.call(`POST /channel/room/${gs.get.content.cid}`, { type: type, name: name }, function(rs){
gs.get.popup.hide();
/* (1) Manage error */
if( rs.error !== 0 || rs.rid == null )
return false;
}.bind(this), auth.token);
return true;
}
/* (7) Remove an existing room
*
* @type<int> room type
* @rid<String> room id
*
* @return removed<bool> Whether the room has been removed
*
---------------------------------------------------------*/
remove(type=null, rid=null){
/* (1) Manage invalid @type */
if( typeof type !== 'string' || this[type] == null )
return false;
/* (2) Manage invalid @rid */
if( rid === null || isNaN(rid) )
return false;
/* (3) Try to create room in API */
api.call(`DELETE /channel/room/${gs.get.content.cid}/${rid}`, {}, function(rs){
gs.get.popup.hide();
/* (1) Manage error */
if( rs.error !== 0 )
return false;
}.bind(this), auth.token);
return true;
}
}