113 lines
2.3 KiB
JavaScript
113 lines
2.3 KiB
JavaScript
export default class ContentController{
|
|
|
|
/* (1) Construct default attributes
|
|
*
|
|
---------------------------------------------------------*/
|
|
|
|
constructor(){}
|
|
|
|
/* (2) Channel bindings
|
|
*
|
|
---------------------------------------------------------*/
|
|
get cid(){ return gs.get.channel.current; }
|
|
|
|
get cbuf(){ return gs.get.channel.buffer; }
|
|
|
|
|
|
/* (3) Room ID binding
|
|
*
|
|
---------------------------------------------------------*/
|
|
get rid(){ return gs.get.room.text.current; }
|
|
get rbuf(){ return gs.get.room.buffer.text; }
|
|
|
|
get messages(){ return this.rbuf.messages; }
|
|
get members(){ return this.rbuf.members; }
|
|
|
|
// current user data
|
|
get uid(){ return gs.get.auth.user.uid; }
|
|
get ubuf(){ return gs.get.auth.user; }
|
|
|
|
|
|
/* (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 {};
|
|
|
|
}
|
|
|
|
|
|
/* (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);
|
|
}
|
|
|
|
|
|
/* (7) Change username
|
|
*
|
|
* @username<String> New username
|
|
---------------------------------------------------------*/
|
|
change_username(username=null){
|
|
|
|
/* (1) Error: if invalid user_id */
|
|
if( typeof username !== 'string' || username.length < 3 )
|
|
return false;
|
|
|
|
/* (2) Error: unknown user */
|
|
if( this.uid == null )
|
|
return false;
|
|
|
|
/* (3) Call api UPDATE */
|
|
api.call(`PUT /user/${this.uid}`, { username: username, password: null }, function(rs){
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
} |