2018-03-24 15:49:00 +00:00
|
|
|
export default class ChannelController{
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
/* (1) Construct default attributes
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
constructor(){
|
|
|
|
|
|
|
|
/* (1) Set default active channel */
|
|
|
|
this.current = null;
|
|
|
|
|
|
|
|
/* (2) Initialize channel list */
|
2018-03-28 19:04:49 +00:00
|
|
|
this.list = [];
|
|
|
|
|
|
|
|
/* (3) Current channel buffer */
|
2018-03-28 19:51:04 +00:00
|
|
|
this._buffer = {};
|
2018-03-28 19:04:49 +00:00
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Channel navigation
|
|
|
|
*
|
|
|
|
* @channel_id<int> Channel id (NULL uses get())
|
|
|
|
*
|
|
|
|
* @return status<boolean> Whether the navigation has been successful
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
nav(channel_id=null){
|
2018-03-27 14:41:36 +00:00
|
|
|
|
2018-04-03 18:34:16 +00:00
|
|
|
// console.log(`channel.nav(${channel_id})`);
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
/* (1) Get channel data */
|
|
|
|
var channel = this.get(channel_id);
|
|
|
|
|
2018-03-27 16:21:02 +00:00
|
|
|
/* (2) Navigate vue-router */
|
2018-03-22 13:57:03 +00:00
|
|
|
gs.get.router.push(`/channel/${channel.link}`);
|
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
/* (3) Update current id */
|
2018-03-22 13:57:03 +00:00
|
|
|
this.current = channel.id;
|
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
/* (4) Update buffer */
|
2018-03-28 19:51:04 +00:00
|
|
|
this._buffer = {};
|
|
|
|
for( let c of this.list )
|
2018-03-28 23:26:18 +00:00
|
|
|
if( c.id === this.current )
|
2018-03-28 19:51:04 +00:00
|
|
|
this._buffer = c;
|
2018-03-28 19:04:49 +00:00
|
|
|
|
|
|
|
/* (5) Load rooms */
|
2018-03-27 16:48:26 +00:00
|
|
|
gs.get.room.fetch();
|
|
|
|
|
2018-04-03 13:37:18 +00:00
|
|
|
/* (6) Open channel websocket */
|
2018-04-03 18:34:16 +00:00
|
|
|
( window.csock != null ) && csock.close();
|
2018-04-03 13:37:18 +00:00
|
|
|
window.csock = new wscd(`wss://ws.douscord.xdrm.io/channel/${channel.id}`, { token: auth.token });
|
|
|
|
csock.onreceive = (_dat) => {
|
|
|
|
|
|
|
|
if( _dat.error !== 0 )
|
|
|
|
return console.log('[WS] auth failed');
|
|
|
|
|
|
|
|
csock.send({ buffer: { rid: gs.get.content.rid } });
|
|
|
|
|
|
|
|
csock.onreceive = gs.get.content.manage_update;
|
|
|
|
};
|
2018-04-03 18:26:55 +00:00
|
|
|
csock.bind();
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
/* (6) Log channel */
|
2018-04-03 18:34:16 +00:00
|
|
|
// console.log(`[channel.current] ${channel.link} (${channel.label})`);
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Dump/Update channel data
|
|
|
|
*
|
|
|
|
* @channels<array> Channels data
|
2018-04-03 19:59:39 +00:00
|
|
|
* @append<bool> Whether to keep old data and only push new
|
2018-03-22 13:57:03 +00:00
|
|
|
*
|
|
|
|
* @return udpated<boolean> Whether channels have been updated
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-03 19:59:39 +00:00
|
|
|
dump(channels, append=false){
|
2018-04-03 18:34:16 +00:00
|
|
|
// console.log(`channel.dump([${channels instanceof Array?channels.length:0}])`);
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
/* (1) Check @channels type */
|
|
|
|
if( !(channels instanceof Array) )
|
|
|
|
return false;
|
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
/* (2) Clear list () */
|
2018-04-03 19:59:39 +00:00
|
|
|
if( append !== true )
|
|
|
|
this.list.splice(0);
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
/* (3) Apply new channels */
|
|
|
|
for(let c of channels){
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
// add 'users' field (will be filled by GET channel/cid)
|
2018-04-03 20:12:01 +00:00
|
|
|
!( c.users instanceof Array ) && ( c.users = [] );
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
this.list.push(c);
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
}
|
2018-03-27 16:21:02 +00:00
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Get channel data
|
|
|
|
*
|
|
|
|
* @channel_id<int> Channel ID
|
|
|
|
* NULL: current channel || from URL (and set current)
|
|
|
|
*
|
|
|
|
* @return channel<array> Channel data
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
get(channel_id=null){
|
|
|
|
// console.log(`channel.get(${channel_id})`);
|
|
|
|
|
|
|
|
if( channel_id === null ){
|
|
|
|
|
|
|
|
/* (1) Get @current channel
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) If @current is set */
|
|
|
|
if( !isNaN(this.current) ){
|
|
|
|
|
|
|
|
/* (2) Return matching id in list */
|
|
|
|
for( let c of this.list ){
|
|
|
|
|
|
|
|
if( c.id === this.current )
|
|
|
|
return c; // exit point
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (2) Get from URL
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) If vue-router has @link param */
|
|
|
|
if( gs.get.router.history.current.params.link ){
|
|
|
|
|
|
|
|
/* (2) Extract @link */
|
|
|
|
let link = gs.get.router.history.current.params.link;
|
|
|
|
|
|
|
|
/* (3) Return matching link in list */
|
|
|
|
for( let c of this.list ){
|
|
|
|
|
|
|
|
if( c.link === link ){
|
|
|
|
|
|
|
|
this.current = c.id; // set @current
|
|
|
|
return c; // exit point
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Get channel data
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Return channel matching id */
|
|
|
|
for( let c of this.list )
|
|
|
|
if( c.id === channel_id )
|
|
|
|
return c; // exit point
|
|
|
|
|
|
|
|
/* (2) Return default: if ID not found */
|
|
|
|
this.current = this.list[0].id;
|
|
|
|
return this.list[0];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-27 16:21:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (6) Fetch channel data
|
2018-03-22 13:57:03 +00:00
|
|
|
*
|
2018-03-27 16:21:02 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
fetch(){
|
|
|
|
|
|
|
|
|
|
|
|
api.call('GET /channel', {}, function(rs){
|
|
|
|
|
|
|
|
/* (1) Check errors */
|
|
|
|
if( rs.error !== 0 || rs.channels == null )
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* (2) Dump data */
|
|
|
|
this.dump(rs.channels);
|
|
|
|
|
|
|
|
/* (3) Find if @link matches */
|
|
|
|
var redirect_id = null;
|
|
|
|
for( let c of this.list ){
|
|
|
|
|
|
|
|
if( c.link === window.initial_link ){
|
|
|
|
redirect_id = c.id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* (4) Emulate navigatation from URL */
|
2018-04-03 18:34:16 +00:00
|
|
|
// console.log(`[restore.channel] ${redirect_id}`);
|
2018-03-27 16:21:02 +00:00
|
|
|
this.nav(redirect_id);
|
|
|
|
|
|
|
|
}.bind(this), auth.token);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (7) Create a new channel
|
2018-03-22 13:57:03 +00:00
|
|
|
*
|
2018-03-27 16:21:02 +00:00
|
|
|
* @name<String> channel name
|
|
|
|
* @link<String> channel URI link
|
|
|
|
*
|
|
|
|
* @return created<bool> Whether the channel has been created
|
2018-03-22 13:57:03 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-03-27 16:21:02 +00:00
|
|
|
create(name=null, link=null){
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-27 16:21:02 +00:00
|
|
|
/* (1) Manage invalid @link */
|
2018-03-28 23:30:47 +00:00
|
|
|
if( typeof link !== 'string' )
|
2018-03-22 13:57:03 +00:00
|
|
|
return false;
|
|
|
|
|
2018-03-27 16:21:02 +00:00
|
|
|
/* (2) Manage invalid @name */
|
2018-03-28 23:30:47 +00:00
|
|
|
if( typeof name !== 'string' )
|
2018-03-27 16:21:02 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (3) Try to create room in API */
|
|
|
|
api.call('POST /channel', { link: link, name: name }, function(rs){
|
|
|
|
|
2018-03-29 10:56:17 +00:00
|
|
|
/* (1) Hide popup */
|
|
|
|
gs.get.popup.hide();
|
|
|
|
|
|
|
|
/* (2) Manage error */
|
2018-03-29 20:59:00 +00:00
|
|
|
if( rs.error !== 0 )
|
2018-03-27 16:21:02 +00:00
|
|
|
return false;
|
|
|
|
|
2018-03-29 10:56:17 +00:00
|
|
|
/* (3) Reload channel list */
|
2018-04-03 20:12:01 +00:00
|
|
|
// this.fetch();
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-28 23:37:15 +00:00
|
|
|
}.bind(this), auth.token);
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-29 20:59:00 +00:00
|
|
|
|
|
|
|
/* (8) Removes the current channel
|
|
|
|
*
|
|
|
|
* @return removed<bool> Whether the channel has been removed
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
remove(){
|
|
|
|
|
|
|
|
/* (2) Try to remove channel in API */
|
|
|
|
api.call(`DELETE /channel/${gs.get.content.cid}`, {}, function(rs){
|
|
|
|
|
|
|
|
/* (1) Hide popup */
|
|
|
|
gs.get.popup.hide();
|
|
|
|
|
|
|
|
/* (2) Reload channel list */
|
2018-04-03 20:12:01 +00:00
|
|
|
// this.fetch();
|
2018-03-29 20:59:00 +00:00
|
|
|
|
|
|
|
}.bind(this), auth.token);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (9) Invite a friend to the current channel
|
|
|
|
*
|
|
|
|
* @username<String> Username
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
invite(username=null) {
|
|
|
|
|
|
|
|
/* (1) Manage invalid @username */
|
|
|
|
if( typeof username !== 'string' )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Try to remove channel in API */
|
|
|
|
api.call(`POST /channel/subscribe/${gs.get.content.cid}/${username}`, {}, function(rs){
|
|
|
|
|
|
|
|
/* (1) Hide popup */
|
|
|
|
gs.get.popup.hide();
|
|
|
|
|
|
|
|
}.bind(this), auth.token);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (10) Leave the current channel
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
leave(){
|
|
|
|
|
|
|
|
/* (1) Try to leave channel in API */
|
|
|
|
api.call(`DELETE /channel/subscribe/${gs.get.content.cid}`, {}, function(rs){
|
|
|
|
|
|
|
|
/* (1) Hide popup */
|
|
|
|
gs.get.popup.hide();
|
|
|
|
|
|
|
|
/* (2) Reload channels */
|
|
|
|
this.fetch();
|
|
|
|
|
|
|
|
|
|
|
|
}.bind(this), auth.token);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
}
|