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-04-02 19:15:28 +00:00
|
|
|
/* (6) Change username
|
2018-03-28 19:04:49 +00:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
}.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-04-02 19:15:28 +00:00
|
|
|
/* (8) Send message
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
send_message(_msg=null){
|
|
|
|
|
|
|
|
/* (1) Manage invalid _msg */
|
|
|
|
if( typeof _msg !== 'string' || _msg.length <= 0 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* (2) Send message */
|
2018-04-03 13:37:18 +00:00
|
|
|
window.csock.send({ buffer: {
|
|
|
|
rid: this.rid,
|
|
|
|
mid: null,
|
|
|
|
message: _msg
|
|
|
|
}});
|
2018-04-02 19:15:28 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
/* (9) Websocket connection / reconnection
|
2018-04-03 13:37:18 +00:00
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
2018-04-04 09:19:31 +00:00
|
|
|
ws_connect(){
|
|
|
|
|
|
|
|
console.warn(`new ws(/channel/${this.cid})`);
|
|
|
|
|
|
|
|
// 1. Close websocket if exists
|
2018-04-04 09:34:54 +00:00
|
|
|
if( window.csock instanceof wscd ){
|
|
|
|
csock.onclose = function(){}; // stop propagating recursive ws_connect()
|
2018-04-04 09:19:31 +00:00
|
|
|
csock.close();
|
2018-04-04 09:34:54 +00:00
|
|
|
}
|
2018-04-04 09:19:31 +00:00
|
|
|
|
|
|
|
// 2. Create new connection
|
|
|
|
window.csock = new wscd(`wss://ws.douscord.xdrm.io/channel/${this.cid}`, { token: auth.token });
|
|
|
|
|
|
|
|
// 3. Bind events
|
2018-04-04 09:34:54 +00:00
|
|
|
csock.onreceive = gs.get.content.ws_handler.bind({ event: 'receive' });
|
|
|
|
csock.onclose = gs.get.content.ws_handler.bind({ event: 'close' });
|
2018-04-04 09:19:31 +00:00
|
|
|
|
|
|
|
// 4. Start communication
|
|
|
|
csock.bind();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (10) Websocket connection manager
|
|
|
|
*
|
|
|
|
* @this.event<String> Event type :
|
|
|
|
* 'close' -> socket closed
|
|
|
|
* 'receive' -> received message
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
ws_handler(_response){
|
|
|
|
|
|
|
|
console.warn('ws(', this.event, _response || '', ')');
|
|
|
|
|
2018-04-04 09:34:54 +00:00
|
|
|
/* (1) Manage error */
|
2018-04-04 09:19:31 +00:00
|
|
|
if( this.event === null )
|
|
|
|
return;
|
|
|
|
|
2018-04-04 09:34:54 +00:00
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
/* (2) CLOSE event -> reconnect in 500ms
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( this.event === 'close' )
|
|
|
|
return setTimeout(gs.get.content.ws_connect.bind(gs.get.content), 500);
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) RECEIVE event
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
if( this.event === 'receive' ){
|
|
|
|
|
|
|
|
/* (1) Communication error -> reconnect in 500ms */
|
|
|
|
if( typeof _response !== 'object' )
|
|
|
|
return setTimeout(gs.get.content.ws_connect.bind(gs.get.content), 500);
|
|
|
|
|
|
|
|
/* (2) If message update -> update interface model */
|
|
|
|
gs.get.content.ws_to_model(_response);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (11) MAIN UPDATER
|
|
|
|
*
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
ws_to_model(_dat){
|
|
|
|
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
/* (1) Manage rooms DELETE
|
2018-04-03 16:56:10 +00:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
/* (1) Extract ids */
|
|
|
|
let room_ids = Object.keys(_dat.room).map( (v) => parseInt(v) );
|
2018-04-03 19:59:39 +00:00
|
|
|
let current_list = gs.get.room;
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-04-03 16:56:10 +00:00
|
|
|
/* (2) Manage DELETED rooms */
|
2018-04-03 19:59:39 +00:00
|
|
|
for( let t in current_list ){
|
2018-04-03 16:56:10 +00:00
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
for( let ri in current_list[t].list ){
|
2018-04-03 16:56:10 +00:00
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
// if existing room is not in received keys -> has been deleted
|
|
|
|
let to_remove = room_ids.indexOf(current_list[t].list[ri].id) < 0;
|
|
|
|
|
|
|
|
// delete room from interface
|
|
|
|
( to_remove ) && current_list[t].list.splice(ri,1);
|
|
|
|
|
|
|
|
}
|
2018-04-03 16:56:10 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
/* (2) Manage rooms CREATE + UPDATE
|
|
|
|
---------------------------------------------------------*/
|
2018-04-03 16:56:10 +00:00
|
|
|
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 )
|
2018-04-03 13:37:18 +00:00
|
|
|
continue;
|
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
// 3. Manage room 'type'
|
|
|
|
room.type = (room.type === 0) ? 'text' : 'voice';
|
|
|
|
|
|
|
|
// 4. Check whether room already exists in interface
|
2018-04-03 16:56:10 +00:00
|
|
|
let existing_index = -1;
|
|
|
|
|
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
main_loop: for( let t in current_list ){
|
2018-04-03 16:56:10 +00:00
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
for( let r in current_list[t].list ){
|
|
|
|
|
|
|
|
if( current_list[t].list[r].id === ri ){
|
|
|
|
existing_index = r;
|
2018-04-04 09:19:31 +00:00
|
|
|
break main_loop;
|
2018-04-03 19:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
|
|
|
|
// 5. Create room
|
|
|
|
if( existing_index < 0 ){
|
|
|
|
|
|
|
|
console.log(`create room#${ri} of type ${room.type}`);
|
2018-04-03 16:56:10 +00:00
|
|
|
gs.get.room.dump([{
|
|
|
|
rid: ri,
|
|
|
|
name: room.name,
|
|
|
|
messages: room.messages,
|
|
|
|
members: room.members,
|
2018-04-03 19:59:39 +00:00
|
|
|
type: room.type
|
2018-04-03 16:56:10 +00:00
|
|
|
}], true);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-04-03 16:56:10 +00:00
|
|
|
// 5. Update room
|
2018-04-03 19:59:39 +00:00
|
|
|
current_list[room.type].list[existing_index].name = room.name;
|
|
|
|
current_list[room.type].list[existing_index].members = room.members;
|
|
|
|
|
|
|
|
// 6. We are done if VOICE room
|
|
|
|
if( room.type === 'voice' )
|
|
|
|
continue;
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
|
|
|
|
// 7. Push new messages
|
2018-04-03 16:56:10 +00:00
|
|
|
for( let m of room.messages ){
|
2018-04-03 13:37:18 +00:00
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
current_list[room.type].list[existing_index].messages.push({
|
2018-04-03 16:56:10 +00:00
|
|
|
uid: m.uid,
|
|
|
|
mid: m.mid,
|
|
|
|
msg: m.content,
|
|
|
|
ts: m.ts
|
|
|
|
});
|
2018-04-03 13:37:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
// 8. Notification API -> if not current channel
|
2018-04-03 17:24:48 +00:00
|
|
|
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 });
|
|
|
|
|
|
|
|
}
|
2018-04-03 13:37:18 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* (3) Manage channels DELETE
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
for( let c of _dat.channels.rem ){
|
|
|
|
|
2018-04-03 20:12:01 +00:00
|
|
|
for( let ci in gs.get.channel.list ){
|
2018-04-03 19:59:39 +00:00
|
|
|
|
2018-04-03 20:12:01 +00:00
|
|
|
// 1. Local copy channel data
|
|
|
|
let channel = gs.get.channel.list[ci];
|
|
|
|
|
|
|
|
// 2. If id matches -> REMOVE
|
|
|
|
if( channel.id === c.id )
|
|
|
|
gs.get.channel.list.splice(ci, 1);
|
|
|
|
|
|
|
|
}
|
2018-04-03 19:59:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (4) Manage channels CREATE
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
for( let c of _dat.channels.add ){
|
|
|
|
|
2018-04-04 09:19:31 +00:00
|
|
|
gs.get.channel.dump([{
|
2018-04-03 19:59:39 +00:00
|
|
|
id: parseInt(c.id),
|
|
|
|
label: c.name,
|
2018-04-03 20:19:57 +00:00
|
|
|
link: c.link
|
2018-04-04 09:19:31 +00:00
|
|
|
}], true);
|
2018-04-03 19:59:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-03 20:12:01 +00:00
|
|
|
|
|
|
|
/* (5) Manage channels UPDATE
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
for( let c of _dat.channels.upd ){
|
|
|
|
|
|
|
|
for( let ci in gs.get.channel.list ){
|
|
|
|
|
|
|
|
// 1. Local copy channel data
|
|
|
|
let channel = gs.get.channel.list[ci];
|
|
|
|
|
|
|
|
// 2. If id matches -> UPDATE
|
|
|
|
if( channel.id === c.id ){
|
|
|
|
|
|
|
|
gs.get.channel.list[ci].label = c.name;
|
|
|
|
gs.get.channel.list[ci].link = c.link;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-04 09:59:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let userset = gs.get.content.cbuf.users;
|
|
|
|
|
|
|
|
|
|
|
|
/* (6) Manage users DELETE
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
for( let u of _dat.users.rem ){
|
|
|
|
|
|
|
|
for( let ui in userset ){
|
|
|
|
|
|
|
|
// 1. Local copy user data
|
|
|
|
let user = userset[ui];
|
|
|
|
|
|
|
|
// 2. If id matches -> REMOVE
|
|
|
|
if( user.uid === u.id )
|
|
|
|
userset.splice(ui, 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* (7) Manage users CREATE
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
for( let u of _dat.users.add ){
|
|
|
|
|
|
|
|
userset.push({
|
|
|
|
uid: parseInt(u.id),
|
|
|
|
username: u.name
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* (8) Manage users UPDATE
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
for( let u of _dat.users.upd ){
|
|
|
|
|
|
|
|
for( let ui in userset ){
|
|
|
|
|
|
|
|
// 1. Local copy user data
|
|
|
|
let user = userset[ui];
|
|
|
|
|
|
|
|
// 2. If id matches -> UPDATE
|
|
|
|
if( user.uid === u.id )
|
|
|
|
userset[ui].username = u.name;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-03 13:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-02 19:15:28 +00:00
|
|
|
|
2018-03-23 07:41:17 +00:00
|
|
|
|
2018-04-03 19:59:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-03-22 13:57:03 +00:00
|
|
|
}
|