2018-03-24 15:49:00 +00:00
|
|
|
export default class ContentController{
|
2018-03-22 13:57:03 +00:00
|
|
|
|
|
|
|
/* (1) Construct default attributes
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
|
2018-03-22 14:28:34 +00:00
|
|
|
constructor(){}
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-22 14:28:34 +00:00
|
|
|
/* (2) Channel bindings
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
get cid(){ return gs.get.channel.current; }
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-28 19:51:04 +00:00
|
|
|
get cbuf(){ return gs.get.channel._buffer; }
|
2018-03-28 13:54:19 +00:00
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
|
2018-03-22 14:28:34 +00:00
|
|
|
/* (3) Room ID binding
|
2018-03-22 13:57:03 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
get rid(){ return gs.get.room.text.current; }
|
2018-03-28 19:51:04 +00:00
|
|
|
get rbuf(){ return gs.get.room._buffer.text; }
|
2018-03-22 14:28:34 +00:00
|
|
|
|
|
|
|
get messages(){ return this.rbuf.messages; }
|
|
|
|
get members(){ return this.rbuf.members; }
|
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
// current user data
|
|
|
|
get uid(){ return gs.get.auth.user.uid; }
|
|
|
|
get ubuf(){ return gs.get.auth.user; }
|
|
|
|
|
2018-03-22 15:03:09 +00:00
|
|
|
|
|
|
|
/* (5) User getter
|
|
|
|
*
|
|
|
|
* @user_id<int> User id
|
|
|
|
*
|
|
|
|
* @return user<array> User data
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
user(user_id=null){
|
|
|
|
|
|
|
|
/* (1) Error: if invalid user_id */
|
|
|
|
if( isNaN(user_id) )
|
|
|
|
return {};
|
|
|
|
|
|
|
|
/* (2) Error: unknown user */
|
|
|
|
if( this.cbuf.users == null || this.cbuf.users.length < 1 )
|
|
|
|
return {};
|
|
|
|
|
|
|
|
/* (3) return user data */
|
|
|
|
for( let u of this.cbuf.users )
|
|
|
|
if( u.uid === user_id )
|
|
|
|
return u;
|
|
|
|
|
|
|
|
/* (4) Error */
|
|
|
|
return {};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-23 07:41:17 +00:00
|
|
|
/* (6) Textarea auto_grow
|
|
|
|
*
|
|
|
|
* @e<Event> Textarea event
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
auto_grow(e){
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
e.target.style.height = '0';
|
|
|
|
e.target.style.height = `calc( ${e.target.scrollHeight}px )`;
|
|
|
|
}, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
/* (7) Change username
|
|
|
|
*
|
|
|
|
* @username<String> New username
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
change_username(username=null){
|
|
|
|
|
|
|
|
/* (1) Error: if invalid user_id */
|
2018-03-28 23:37:15 +00:00
|
|
|
if( typeof username !== 'string' )
|
2018-03-28 19:04:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Error: unknown user */
|
|
|
|
if( this.uid == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (3) Call api UPDATE */
|
2018-03-28 20:54:49 +00:00
|
|
|
api.call(`PUT /user/${this.uid}`, { username: username }, function(rs){
|
2018-03-28 19:04:49 +00:00
|
|
|
|
2018-03-29 10:56:17 +00:00
|
|
|
gs.get.popup.hide();
|
|
|
|
|
2018-03-28 19:04:49 +00:00
|
|
|
// manage error
|
|
|
|
if( rs.error !== 0 )
|
|
|
|
return;
|
|
|
|
|
|
|
|
// update global username
|
|
|
|
let tmp_user = auth.user;
|
|
|
|
tmp_user.username = username;
|
|
|
|
auth.user = tmp_user;
|
|
|
|
|
|
|
|
// update username in channel
|
|
|
|
for( let u in this.cbuf.users )
|
|
|
|
if( this.cbuf.users[u].uid == this.uid )
|
|
|
|
this.cbuf.users[u].username = username;
|
|
|
|
|
|
|
|
}.bind(this), auth.token);
|
|
|
|
|
|
|
|
/* (4) Error */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-28 20:54:49 +00:00
|
|
|
/* (7) Change password
|
|
|
|
*
|
|
|
|
* @password<String> New password
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
change_password(password=null){
|
|
|
|
|
|
|
|
/* (1) Error: if invalid user_id */
|
2018-03-28 23:45:18 +00:00
|
|
|
if( typeof password !== 'string' )
|
2018-03-28 20:54:49 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (2) Error: unknown user */
|
|
|
|
if( this.uid == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* (3) Call api UPDATE */
|
2018-03-29 10:56:17 +00:00
|
|
|
api.call(`PUT /user/${this.uid}`, { password: password }, () => gs.get.popup.hide(), auth.token);
|
2018-03-28 20:54:49 +00:00
|
|
|
|
|
|
|
/* (4) Error */
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-03-23 07:41:17 +00:00
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
}
|