Commit 392313b3 authored by Iván Sánchez Ortega's avatar Iván Sánchez Ortega

Implemented a basic, probably buggy Mastodon auth logic

parent e5333522
......@@ -11,6 +11,7 @@
"configstore": "^3.0.0",
"flatten-obj": "^3.1.0",
"inquirer": "^3.0.6",
"oauth": "^0.9.15",
"vorpal": "^1.11.4"
},
"scripts": {
......
......@@ -41,6 +41,7 @@ class ClockAccount extends Account {
// 🍂channel hour; Ticks once every hour
// 🍂channel quarter; Ticks once every 15 minutes
// 🍂channel minute; Ticks once every minute
// 🍂channel second; Ticks once every second
listen(channel) {
let delay = Number(channel) * 1000;
......@@ -48,8 +49,8 @@ class ClockAccount extends Account {
if (channel === 'day') { delay = 1000 * 3600 * 24; }
if (channel === 'hour') { delay = 1000 * 3600; }
if (channel === 'quarter') { delay = 1000 * 60 * 15; }
// if (channel === 'minute') { delay = 1000 * 60; }
if (channel === 'minute') { delay = 1000 * 5; }
if (channel === 'minute') { delay = 1000 * 60; }
if (channel === 'second') { delay = 1000; }
if (delay <= 0) { return; }
......@@ -103,20 +104,16 @@ export function signUp() {
choices: ['yes', 'no']
}]).then((answer)=>{
if (answer.confirm === 'yes') {
return {};
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 new Promise((resolve, reject)=>{
// });
// return Promise.resolve({
// // No options needed to initialize this platform, really.
// // Normally this would include auth tokens and such.
......@@ -124,23 +121,6 @@ export function signUp() {
}
// 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 Promise.resolve({
// // No options needed to initialize this platform, really.
// // Normally this would include auth tokens and such.
// });
//
// }
export const name = 'clock';
export { ClockAccount as Account };
......
......@@ -2,5 +2,8 @@
import * as clock from './clock';
export { clock };
import * as mastodon from './mastodon';
export { mastodon };
import Account from '../abstracts/account';
import inquirer from 'inquirer';
import { OAuth2 } from 'oauth';
import chalk from 'chalk256';
// 🍂class MastodonAccount
// 🍂inherits Account
// Support for the Mastodon microblogging federated network.
class MastodonAccount extends Account {
constructor(callback, opts) {
// this._timers = {};
this._opts = Object.assign({
badge: '🐘',
colour: 'grey'
}, opts);
return super.constructor(callback, opts);
}
listen(channel) {
/*
let delay = Number(channel) * 1000;
if (channel === 'day') { delay = 1000 * 3600 * 24; }
if (channel === 'hour') { delay = 1000 * 3600; }
if (channel === 'quarter') { delay = 1000 * 60 * 15; }
if (channel === 'minute') { delay = 1000 * 60; }
if (channel === 'second') { delay = 1000; }
if (delay <= 0) { return; }
let now = Date.now();
let msSinceLastTick = now % delay;
this._timers[ channel ] = setTimeout(()=>{
this.emit({str: 'Tick.', channel: channel, realtime: true});
this._timers[ channel ] = setInterval(()=>{
this.emit({str: 'Tick.', channel: channel, realtime: true});
}, delay);
}, delay - msSinceLastTick);
return this;*/
}
unlisten(channel) {
// channel = Number(channel);
// if (channel <= 0) return;
//
// delete this._timers[ channel ];
//
// return this;
}
listChannels() {
// return ['day', 'hour', 'quarter', 'minute'];
}
getLastUpdateTimestamp() {
// The clock runs updates real-time, so
// return Date.now();
}
}
// signUp returns a Promise for a set of options. If the promise resolves, these options
// will be saved into the user preferences, then a new instance of this platform's `Account`
// will be instantiated.
export function signUp() {
/// TODO: Prompt the user for a server.
let server = 'mastodon.social';
/// TODO: Do some automatic client ID/secret fetching, depending on the server.
let clientId = 'aa0b5f7df451a9f9cfeff58e103ee5de4b6f724f4a576f71f7e81a87fb255722';
let clientSecret = '3736a19af63f85ed75d72caa7ec09661ba8ed38303c178316cbe927d38233f7e';
let oauth = new OAuth2(clientId, clientSecret, 'https://' + server, null, '/oauth/token');
let url = oauth.getAuthorizeUrl({ redirect_uri: 'urn:ietf:wg:oauth:2.0:oob', response_type: 'code', scope: 'read write follow' });
// Get the user to open up the url in their browser and get the code
console.log( chalk.bold('In order to add a new Mastodon account, visit ', chalk.underline(url), ' to get a verification code.') );
return inquirer.prompt([{
type: 'input',
name: 'code',
message: 'Enter verification code: '
}]).then((answer)=>{
if (!answer.code) {
throw new Error('No verification code was introduced.');
}
return new Promise((resolve, reject) => {
oauth.getOAuthAccessToken(
answer.code,
{ grant_type: 'authorization_code', redirect_uri: 'urn:ietf:wg:oauth:2.0:oob' },
function(err, accessToken, refreshToken, res) {
if (err) { return reject(err); }
return resolve({
// server: server,
// clientId: clientId,
// clientSecret: clientSecret,
api_url: 'https://' + server + '/api/v1/',
access_token: accessToken
});
}
);
});
});
/*
// 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 name = 'mastodon';
export { MastodonAccount as Account };
......@@ -230,6 +230,8 @@ vorpal
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?'));
......@@ -240,9 +242,16 @@ vorpal
platforms[args.platform].signUp().then((opts)=>{
this.log( chalk.red.bold('>>> NOT IMPLEMENTED YET.'));
// 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] = {};
}
conf.all.accounts[args.platform][args.alias] = opts;
callback();
}).catch((err)=>{
this.log( chalk.red.bold('>>> Creation of new account cancelled.'));
......
......@@ -319,6 +319,10 @@ number-is-nan@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/number-is-nan/-/number-is-nan-1.0.1.tgz#097b602b53422a522c1afb8790318336941a011d"
oauth@^0.9.15:
version "0.9.15"
resolved "https://registry.yarnpkg.com/oauth/-/oauth-0.9.15.tgz#bd1fefaf686c96b75475aed5196412ff60cfb9c1"
object-assign@^4.1.0:
version "4.1.1"
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
......
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