From 6a76d9cd6fedda0d92be7d0ea98178ee21792097 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 27 Mar 2018 18:21:02 +0200 Subject: [PATCH] [BIGUPDATE] interface API propagation --- webpack/lib/channel-controller.js | 98 ++++++++++++++++++++-------- webpack/lib/content-controller.js | 6 +- webpack/lib/popup-controller.js | 7 +- webpack/lib/room-controller.js | 57 ++++++++++++++-- webpack/mockup/api-channel-init.json | 8 +-- webpack/mockup/api.list | 14 ++-- webpack/page/auth/channel.js | 26 +------- webpack/vue/auth/wrapper.vue | 2 + 8 files changed, 143 insertions(+), 75 deletions(-) diff --git a/webpack/lib/channel-controller.js b/webpack/lib/channel-controller.js index 0d97e87..94e18ec 100644 --- a/webpack/lib/channel-controller.js +++ b/webpack/lib/channel-controller.js @@ -14,9 +14,6 @@ export default class ChannelController{ { id: -2, link: null, label: 'add', sub: null, icon: 'add', room: [], add: 1 } ]; - /* (3) Initialize channel data buffer */ - this.buffer = {}; - } @@ -37,23 +34,16 @@ export default class ChannelController{ /* (1) Get channel data */ var channel = this.get(channel_id); - /* (2) Try to load channel data */ - if( !this.load(channel.id) ) - return false; - - /* (3) Navigate vue-router */ + /* (2) Navigate vue-router */ gs.get.router.push(`/channel/${channel.link}`); - /* (4) Load rooms */ - gs.get.room.dump(channel.room); + /* (3) Load rooms */ + gs.get.room.fetch(); - /* (5) Default open first 'text' room */ - gs.get.room.nav('text', gs.get.room.text.list[0].id); - - /* (6) Update active element */ + /* (4) Update active element */ this.current = channel.id; - /* (7) Log channel */ + /* (5) Log channel */ console.log(`[channel.current] ${channel.link} (${channel.label})`); return true; @@ -81,12 +71,14 @@ export default class ChannelController{ this.list.splice(1); /* (4) Apply new channels */ + this.buffer = {}; for(let c of channels) - this.list.push(c); + ( this.list.push(c) === 2 ) && ( this.buffer = c ); /* (5) Restore LAST */ this.list.push(last_item); + return true; } @@ -160,26 +152,74 @@ export default class ChannelController{ } - /* (5) Fetch channel data - * - * @channel_id Channel ID - * - * @return fetched Whether channel data have been fetched + + + /* (6) Fetch channel data * ---------------------------------------------------------*/ - load(channel_id=null){ - console.log(`channel.load(${channel_id})`); + fetch(){ - /* (1) Exit if invalid @channel_id */ - if( isNaN(channel_id) ) + + 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 */ + console.log(`[restore.channel] ${redirect_id}`); + this.nav(redirect_id); + + }.bind(this), auth.token); + } + + + /* (7) Create a new channel + * + * @name channel name + * @link channel URI link + * + * @return created Whether the channel has been created + * + ---------------------------------------------------------*/ + create(name=null, link=null){ + + /* (1) Manage invalid @link */ + if( typeof link !== 'string' || /^[a-z0-9\._-]$/i.test(link) ) return false; - /* (2) Call API to get data */ - setTimeout(() => { + /* (2) Manage invalid @name */ + if( typeof name !== 'string' || /^[a-z0-9\._-]$/i.test(name) ) + return false; - this.buffer = require('../mockup/api-channel-init.json'); + /* (3) Try to create room in API */ + api.call('POST /channel', { link: link, name: name }, function(rs){ - }, 500); + /* (1) Manage error */ + if( rs.error !== 0 || rs.cid == null ) + return false; + + /* (2) Reload channel list */ + this.fetch(); + + /* (3) Hide popup */ + gs.get.popup.hide(); + + }, auth.token); return true; diff --git a/webpack/lib/content-controller.js b/webpack/lib/content-controller.js index fcea67f..fb204af 100644 --- a/webpack/lib/content-controller.js +++ b/webpack/lib/content-controller.js @@ -25,14 +25,14 @@ export default class ContentController{ get rbuf(){ /* (1) Ignore: if no rooms empty */ - if( this.cbuf.room == null || this.cbuf.room.length === 0 ) + if( gs.get.room.text.list == null || gs.get.room.text.list.length === 0 ) return {}; /* (2) Search for current room */ - for( let r of this.cbuf.room ){ + for( let r of gs.get.room.text.list ){ // Return if room found // - if( r.rid === this.rid ) + if( r.id === this.rid ) return r; } diff --git a/webpack/lib/popup-controller.js b/webpack/lib/popup-controller.js index 2fe95a3..8ffd439 100644 --- a/webpack/lib/popup-controller.js +++ b/webpack/lib/popup-controller.js @@ -32,10 +32,11 @@ export default class PopupController{ /* (2) Create a new Channel */ this.register('channel.create', { data: { - name: '' + name: '', + link: '' }, - reset(){ this.data.name = ''; }, - submit(){ gs.get.channel.create(this.data.name) && this.parent.hide(); } + reset(){ this.data.link = '', this.data.name = ''; }, + submit(){ gs.get.channel.create(this.data.name, this.data.link) && this.parent.hide(); } }); } diff --git a/webpack/lib/room-controller.js b/webpack/lib/room-controller.js index 62b4f3e..c5d43ac 100644 --- a/webpack/lib/room-controller.js +++ b/webpack/lib/room-controller.js @@ -61,7 +61,7 @@ export default class RoomController{ ---------------------------------------------------------*/ dump(rooms){ - console.log(`room.dump([${rooms instanceof Array?rooms.length:0}])`); + console.log(`room.dump([${rooms instanceof Array?rooms.length:-1}])`); /* (1) Check @rooms type */ if( !(rooms instanceof Array) ) @@ -73,6 +73,9 @@ export default class RoomController{ this[type].current = null; } + // redirection + let redirected = false; + /* (3) Browse each room */ for(let r of rooms){ @@ -81,15 +84,37 @@ export default class RoomController{ continue; // {2} Ignore: if missing field // - if( isNaN(r.rid) || typeof r.type !== 'string' || typeof r.name !== 'string' ) + if( isNaN(r.rid) || typeof r.type !== 'string' ) continue; - // {3} Ignore: not available type // + // {3} Ignore if cannot find name in buffer // + var name = null; + gs.get.content.cbuf.room.map( (v) => { ( v.rid === r.rid ) && ( name = v.name ); }); + + if( name === null ) + continue; + + // {4} Default: missing messages // + if( !( r.messages instanceof Array) ) + r.messages = []; + + // {5} Default: missing members // + if( !( r.members instanceof Array) ) + r.members = []; + + + // {6} Ignore: not available type // if( this[r.type] == null ) continue; + // {7} store data + this[r.type].list.push({ id: r.rid, name: name, type: r.type, messages: r.messages }); - this[r.type].list.push({ id: r.rid, name: r.name, type: r.type }); + // {8} redirect if first element + if( !redirected && r.type == 'text' ){ + redirected = true; + this.nav('text', r.rid); + } } return true; @@ -147,7 +172,29 @@ export default class RoomController{ } - /* (5) Create a new room + + + /* (5) Fetch channel data + * + ---------------------------------------------------------*/ + fetch(){ + + api.call(`GET /channel/${gs.get.channel.current}`, {}, function(rs){ + + /* (1) Check errors */ + if( rs.error !== 0 || rs.channel == null ) + return; + + /* (2) Dump data */ + console.log(rs.channel.room); + this.dump(rs.channel.room); + + }.bind(this), auth.token); + + } + + + /* (6) Create a new room * * @type room type * @name room name diff --git a/webpack/mockup/api-channel-init.json b/webpack/mockup/api-channel-init.json index a786fe5..414531d 100644 --- a/webpack/mockup/api-channel-init.json +++ b/webpack/mockup/api-channel-init.json @@ -4,10 +4,10 @@ "uid": 1, "users": [ - { "uid": 1, "online": true, "name": "First user YaY!" }, - { "uid": 3, "online": false, "name": "Another channel's user" }, - { "uid": 4, "online": true, "name": "And another one" }, - { "uid": 5, "online": true, "name": "And another" } + { "uid": 1, "name": "First user YaY!" }, + { "uid": 3, "name": "Another channel's user" }, + { "uid": 4, "name": "And another one" }, + { "uid": 5, "name": "And another" } ], "room": [ diff --git a/webpack/mockup/api.list b/webpack/mockup/api.list index 3b68046..f37350c 100644 --- a/webpack/mockup/api.list +++ b/webpack/mockup/api.list @@ -55,27 +55,27 @@ === CHANNEL <> USER === - >>> POST @token:/channel/:cid/subscribe + >>> POST @token:/channel/subscribe/:cid {} - >>> DELETE @token:/channel/:cid/subscribe + >>> DELETE @token:/channel/subscribe/:cid {} === ROOM === - >>> POST @token:/channel/:cid/room/:type/ + >>> POST @token:/channel/room/:cid { name: } - >>> GET @token:/channel/:cid/room/:type/ --> all channel's rooms of this type + >>> GET @token:/channel/room/:cid --> all channel's rooms of this type - >>> GET @token:/channel/:cid/room/:type/:rid --> room information of this type + >>> GET @token:/channel/room/:cid/:rid --> room information of this type - >>> PUT @token:/channel/:cid/room/:type/:rid + >>> PUT @token:/channel/room/:cid/:rid { name: } - >>> DELETE @token:/channel/:cid/room/:type/:rid --> remove room + >>> DELETE @token:/channel/room/:cid/:rid --> remove room diff --git a/webpack/page/auth/channel.js b/webpack/page/auth/channel.js index f8a5526..ff0034a 100644 --- a/webpack/page/auth/channel.js +++ b/webpack/page/auth/channel.js @@ -10,29 +10,6 @@ import ChannelController from '../../lib/channel-controller' window.initial_link = gs.get.router.history.current.params.link; console.log(`[channel.URL] ${initial_link}`); -/* (2) Fetch channel data */ -setTimeout(() => { - - /* (3) Fetch data */ - gs.get.channel.dump( require('../../mockup/api-channels.json') ); - - /* (4) Find if @link matches */ - var redirect_id = null; - for( let c of gs.get.channel.list ){ - - if( c.link === window.initial_link ){ - redirect_id = c.id; - break; - } - - } - - /* (4) Emulate navigatation from URL */ - console.log(`[restore.channel] ${redirect_id}`); - gs.get.channel.nav(redirect_id); - -}, 500); - /* (2) Main components @@ -47,4 +24,5 @@ gs.set('content', new ContentController()); gs.set('room', new RoomController()); /* (4) Initialize channels & channel menu */ -gs.set('channel', new ChannelController()); \ No newline at end of file +gs.set('channel', new ChannelController()); +gs.get.channel.fetch(); // fetch at load time \ No newline at end of file diff --git a/webpack/vue/auth/wrapper.vue b/webpack/vue/auth/wrapper.vue index 1dd4b4c..91897db 100644 --- a/webpack/vue/auth/wrapper.vue +++ b/webpack/vue/auth/wrapper.vue @@ -42,6 +42,8 @@ + +