231 lines
4.7 KiB
JavaScript
231 lines
4.7 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) Change username
|
|
*
|
|
* @username<String> New username
|
|
---------------------------------------------------------*/
|
|
change_username(username=null){
|
|
|
|
/* (1) Error: if invalid user_id */
|
|
if( typeof username !== 'string' )
|
|
return false;
|
|
|
|
/* (2) Error: unknown user */
|
|
if( this.uid == null )
|
|
return false;
|
|
|
|
/* (3) Call api UPDATE */
|
|
api.call(`PUT /user/${this.uid}`, { username: username }, function(rs){
|
|
|
|
gs.get.popup.hide();
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
/* (7) Change password
|
|
*
|
|
* @password<String> New password
|
|
*
|
|
---------------------------------------------------------*/
|
|
change_password(password=null){
|
|
|
|
/* (1) Error: if invalid user_id */
|
|
if( typeof password !== 'string' )
|
|
return false;
|
|
|
|
/* (2) Error: unknown user */
|
|
if( this.uid == null )
|
|
return false;
|
|
|
|
/* (3) Call api UPDATE */
|
|
api.call(`PUT /user/${this.uid}`, { password: password }, () => gs.get.popup.hide(), auth.token);
|
|
|
|
/* (4) Error */
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
/* (8) Send message
|
|
*
|
|
---------------------------------------------------------*/
|
|
send_message(_msg=null){
|
|
|
|
/* (1) Manage invalid _msg */
|
|
if( typeof _msg !== 'string' || _msg.length <= 0 )
|
|
return true;
|
|
|
|
/* (2) Send message */
|
|
window.csock.send({ buffer: {
|
|
rid: this.rid,
|
|
mid: null,
|
|
message: _msg
|
|
}});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
/* (9) MAIN UPDATER
|
|
*
|
|
---------------------------------------------------------*/
|
|
manage_update(_dat){
|
|
|
|
/* (1) Manage rooms
|
|
---------------------------------------------------------*/
|
|
/* (1) Extract ids */
|
|
let room_ids = Object.keys(_dat.room).map( (v) => parseInt(v) );
|
|
let current_list = gs.get.room.text.list;
|
|
|
|
/* (2) Manage DELETED rooms */
|
|
for( let ri in current_list ){
|
|
|
|
// if existing room is not in received keys -> has been deleted
|
|
let to_remove = room_ids.indexOf(current_list[ri].id) < 0;
|
|
|
|
// delete room from interface
|
|
( to_remove ) && current_list.splice(ri,1);
|
|
|
|
}
|
|
|
|
|
|
/* (3) Manage UPDATE + CREATE rooms */
|
|
for( let ri of room_ids ){
|
|
|
|
// 1. Extract room data
|
|
let room = _dat.room[ri];
|
|
|
|
// 2. if room data is null -> ignore
|
|
if( room === null )
|
|
continue;
|
|
|
|
// 3. Check whether room already exists in interface
|
|
let existing_index = -1;
|
|
|
|
for( let r in current_list )
|
|
if( current_list[r].id === ri ){ existing_index = r; break; }
|
|
|
|
|
|
// 4. Create room
|
|
if( existing_index < 0 ){
|
|
|
|
gs.get.room.dump([{
|
|
rid: ri,
|
|
name: room.name,
|
|
messages: room.messages,
|
|
members: room.members,
|
|
type: 'text'
|
|
}], true);
|
|
|
|
continue;
|
|
}
|
|
|
|
// 5. Update room
|
|
current_list[existing_index].name = room.name;
|
|
current_list[existing_index].members = room.members;
|
|
|
|
// 6. Push new messages
|
|
for( let m of room.messages ){
|
|
|
|
current_list[existing_index].messages.push({
|
|
uid: m.uid,
|
|
mid: m.mid,
|
|
msg: m.content,
|
|
ts: m.ts
|
|
});
|
|
|
|
}
|
|
|
|
/* (7) Notification API -> if not current channel */
|
|
if( room.messages.length > 0 && ri !== gs.get.content.rid ){
|
|
|
|
let title = `Room #${room.name}`;
|
|
let body = `${room.messages.length} new messages`;
|
|
new Notification(title, { body: body });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} |