Commit 60df56ad authored by Iván Sánchez Ortega's avatar Iván Sánchez Ortega

Improved account creation/storage

parent 392313b3
......@@ -18,6 +18,7 @@
"build": "rollup -c rollup-config.js"
},
"devDependencies": {
"rollup": "^0.41.6",
"rollup-plugin-buble": "^0.15.0"
}
}
......@@ -5,6 +5,7 @@
* Every platform is a ES2015 module which must expose:
* - Account (a subclass of 'Account'; a bit confusing, I know)
* - name (a String)
* - defaultBadge (a short String)
* - signUp (a function that must return a Promise to a set of config options
* for a new configured account)
*/
......@@ -25,7 +26,6 @@ class ClockAccount extends Account {
this._timers = {};
this._opts = Object.assign({
badge: '⏰',
colour: 'red'
}, opts);
......@@ -92,6 +92,8 @@ class ClockAccount extends Account {
// will be instantiated.
export function signUp() {
console.log('Performing sign-up procedure for a new "clock" account');
// There is nothing asynchronous here, so...
/// TODO: Extend this with a prompt for the default channel to listen to, or a prompt for
......@@ -108,6 +110,8 @@ export function signUp() {
// The clock platform does not need any parameters to initialize
// an account. In a normal case, this response would include
// auth tokens and so on.
foo: 'bar',
qux: { something: 'quux' }
};
} else {
throw new Error('Not creating the new clock account then.');
......@@ -121,7 +125,7 @@ export function signUp() {
}
export const defaultBadge = 'cl'; // or '⏰'
export const name = 'clock';
export { ClockAccount as Account };
......@@ -16,7 +16,6 @@ class MastodonAccount extends Account {
// this._timers = {};
this._opts = Object.assign({
badge: '🐘',
colour: 'grey'
}, opts);
......@@ -116,41 +115,9 @@ export function signUp() {
});
/*
// There is nothing asynchronous here, so...
/// TODO: Extend this with a prompt for the default channel to listen to, or a prompt for
/// the badge.
return inquirer.prompt([{
type: 'list',
name: 'confirm',
message: 'You are going to add a "clock" fake platform. Is this right?',
choices: ['yes', 'no']
}]).then((answer)=>{
if (answer.confirm === 'yes') {
return {
// The clock platform does not need any parameters to initialize
// an account. In a normal case, this response would include
// auth tokens and so on.
};
} else {
throw new Error('Not creating the new clock account then.');
}
});*/
// return Promise.resolve({
// // No options needed to initialize this platform, really.
// // Normally this would include auth tokens and such.
// });
}
export const defaultBadge = 'ma'; // or '🐘'
export const name = 'mastodon';
export { MastodonAccount as Account };
......@@ -4,12 +4,14 @@
import * as platforms from './platforms/index'
import Vorpal from 'vorpal';
import inquirer from 'inquirer';
import Configstore from 'configstore';
import chalk from 'chalk256';
import Flatten from 'flatten-obj';
const flatten = Flatten();
const vorpal = Vorpal();
vorpal.history('soclial');
vorpal.log(chalk.purple('>>> Initializing soCLIal...'));
// create a Configstore instance with an unique ID e.g.
......@@ -80,38 +82,48 @@ function handle(platform, data, metadata) {
vorpal.log(chalk.purple('>>> Initializing social accounts...'));
let allConf = conf.all;
// These define the state of the application in this global scope.
let userPlatforms = {};
let activeAccounts = {};
function activateAccount(accId) {
console.log(accId, conf.all.accounts[accId]);
let accConf = conf.all.accounts[accId];
let pl = accConf.platform;
let badge = accConf.badge;
vorpal.log(
chalk.purple('>>> Activating account ') +
chalk.cyan(accId) +
chalk.purple(', represented by badge ') +
chalk.cyan(badge) +
chalk.purple(', platform ') +
chalk.cyan(pl) +
chalk.purple('...')
);
if (!((accConf.platform) in platforms)) {
vorpal.log(chalk.red.bold('>>> Unknown platform: ') + chalk.cyan(pl));
throw new Error('Unknown platform in a configured account. Please edit your configuration manually to remove the offending account.');
}
for (let pl in allConf.accounts) {
userPlatforms[pl] = {};
for (let acc in allConf.accounts[pl]) {
activeAccounts[accId] = new platforms[pl].Account(handle, accConf);
for (let ch in accConf.channels) {
vorpal.log(
chalk.purple('>>> Platform ') +
chalk.cyan(pl) +
chalk.purple(', account ') +
chalk.cyan(acc) +
chalk.purple('>>> Listening to channel ') +
chalk.cyan(ch) +
chalk.purple('...')
);
activeAccounts[accId].listen(ch);
}
}
if (!(pl in platforms)) {
vorpal.error(chalk.red('>>> Unknown platform: ') + chalk.cyan(pl));
continue;
}
userPlatforms[pl][acc] = new platforms[pl].Account(handle, allConf.accounts[pl][acc]);
for (let ch in allConf.accounts[pl][acc].channels) {
vorpal.log(
chalk.purple('>>> Listening to channel ') +
chalk.cyan(ch) +
chalk.purple('...')
);
userPlatforms[pl][acc].listen(ch);
}
}
for (let accId in conf.all.accounts) {
activateAccount(accId);
}
......@@ -184,18 +196,18 @@ vorpal
.action(function(args, callback) {
this.log( chalk.purple('>>> Listing registered accounts:'));
let allConf = conf.all;
for (let pl in allConf.accounts) {
userPlatforms[pl] = {};
for (let acc in allConf.accounts[pl]) {
vorpal.log(
chalk.purple('>>> Platform ') +
chalk.cyan(pl) +
chalk.purple(', account ') +
chalk.cyan(acc) +
chalk.purple('.')
);
}
for (let accId in conf.all.accounts) {
let accConf = conf.all.accounts[accId];
let pl = accConf.platform;
let badge = accConf.badge;
vorpal.log(
chalk.cyan(accId) +
chalk.purple(', represented by badge ') +
chalk.cyan(badge) +
chalk.purple(', platform ') +
chalk.cyan(pl)
);
}
callback();
......@@ -205,10 +217,10 @@ vorpal
// Command: accounts delete
vorpal
.command('accounts delete [platform] [alias]', 'Removes an account given its platform and alias.')
.command('accounts delete [id]', 'Removes an account given its platform and alias.')
.action(function(args, callback) {
if (args.platform === undefined || args.alias === undefined ) {
if (args.id === undefined) {
this.log( chalk.red('>>> Specify platform and alias of account to be deleted.'));
} else {
this.log( chalk.red.bold('>>> NOT IMPLEMENTED YET.'));
......@@ -221,46 +233,82 @@ vorpal
// Command: accounts delete
// Command: accounts add
vorpal
.command('accounts add [platform] [alias]', 'Adds an account given its platform and alias. Most likely you want the alias to be your username in that platform.')
.command('accounts add', 'Adds a new account. You will be asked questions about it.')
.action(function(args, callback) {
if (args.platform === undefined || args.alias === undefined ) {
this.log( chalk.red('>>> Specify platform and alias of the account.'));
callback();
// } else { /// TODO: check if that alias already exists.
} else {
if (!(args.platform in platforms)) {
this.log( chalk.red('>>> Specified platform does not exist or is not supported. Maybe you mistyped it?'));
callback();
} else {
vorpal.ui.cancel();
// vorpal.ui.redraw();
platforms[args.platform].signUp().then((opts)=>{
// this.log( chalk.red.bold('>>> NOT IMPLEMENTED YET.'));
vorpal.ui.cancel();
inquirer.prompt([{
type: 'list',
name: 'platform',
message: 'You are adding a new account to soCLIal. From which social platform?',
choices: Object.keys(platforms)
}]).then(answer=>{
// vorpal.ui.redraw();
this.log('Asking the platform code to perform the sign-up procedure');
return platforms[answer.platform].signUp().then((opts)=>{
this.log('Platform code says we can create a new account with data: ', opts);
// vorpal.ui.cancel();
return inquirer.prompt([{
type: 'input',
name: 'badge',
message: 'What will be the badge of this account? The badge is a string of text that will show next to all messages from this account, and it\'s better to keep it short.',
choices: Object.keys(platforms),
default: platforms[answer.platform].badge
}]).then(answer2=>{
opts.badge = answer2.badge;
opts.platform = answer.platform;
// Find the lowest unused account ID
let accId = 1;
while( accId in conf.all.accounts ) {
accId++;
}
// this.log( chalk.red.bold('>>> NOT IMPLEMENTED YET.'));
this.log( 'Should store config for a new account: ', opts);
// ui.redraw.done()
if (!args.platform in conf.all.accounts) {
conf.all.accounts[args.platform] = {};
// ui.redraw.done()
let flat = flatten(opts);
this.log(conf.all.accounts);
this.log('flat account config: ', flat);
for (let i in flat) {
conf.set('accounts.' + accId + '.' + i , flat[i]);
this.log('accounts.' + accId + '.' + i + ' = ' + flat[i]);
}
conf.all.accounts[args.platform][args.alias] = opts;
this.log( 'New account added to config. Enabling now.');
activateAccount(accId);
callback();
}).catch((err)=>{
this.log( chalk.red.bold('>>> Creation of new account cancelled.'));
// ui.redraw.done()
callback();
});
}
}
// }).catch((err)=>{
// this.log( chalk.red.bold('>>> Creation of new account cancelled (no badge).', err));
// // ui.redraw.done()
// callback();
});
// }).catch((err)=>{
//
// this.log( chalk.red.bold('>>> Creation of new account cancelled (no data from sign-up procedure).', err));
// // ui.redraw.done()
// callback();
});
}).catch((err)=>{
this.log( chalk.red.bold('>>> Creation of new account cancelled ', err));
// ui.redraw.done()
callback();
});
});
......
......@@ -391,6 +391,12 @@ rollup-pluginutils@^1.5.0:
estree-walker "^0.2.1"
minimatch "^3.0.2"
rollup@^0.41.6:
version "0.41.6"
resolved "https://registry.yarnpkg.com/rollup/-/rollup-0.41.6.tgz#e0d05497877a398c104d816d2733a718a7a94e2a"
dependencies:
source-map-support "^0.4.0"
run-async@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/run-async/-/run-async-0.1.0.tgz#c8ad4a5e110661e402a7d21b530e009f25f8e389"
......@@ -419,6 +425,16 @@ slide@^1.1.5:
version "1.1.6"
resolved "https://registry.yarnpkg.com/slide/-/slide-1.1.6.tgz#56eb027d65b4d2dce6cb2e2d32c4d4afc9e1d707"
source-map-support@^0.4.0:
version "0.4.14"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.14.tgz#9d4463772598b86271b4f523f6c1f4e02a7d6aef"
dependencies:
source-map "^0.5.6"
source-map@^0.5.6:
version "0.5.6"
resolved "https://registry.yarnpkg.com/source-map/-/source-map-0.5.6.tgz#75ce38f52bf0733c5a7f0c118d81334a2bb5f412"
string-width@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/string-width/-/string-width-1.0.2.tgz#118bdf5b8cdc51a2a7e70d211e07e2b0b9b107d3"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment