Commit 9159fc3d authored by Iván Sánchez Ortega's avatar Iván Sánchez Ortega

Added "url" and "post" commands; display replied use in twitter, prevent callback-less fs.write()s

parent 36d8ff9f
......@@ -36,7 +36,7 @@ export function push(data) {
// fs.writeFile('debug/' + str + '.json', JSON.stringify(data, ' '));
// console.log(data);
fs.writeFile('debug/' + str + '.json', JSON.stringify(data, null, ' '));
fs.writeFile('debug/' + str + '.json', JSON.stringify(data, null, ' '), (err)=>{});
return str;
}
......@@ -109,6 +109,16 @@ class MastodonAccount extends Account {
// return Date.now();
}
post(str) {
this._masto.post('statuses', {status: str});
// .then((error, toot, response)=>{
// if(error) throw error;
// // console.log(toot); // Tootbody.
// // console.log(response); // Raw response object.
// });
}
_cleanString(str) {
return striptags( he.decode( str.replace(/<\/p><p>/g, ' // ') ) );
}
......@@ -142,6 +152,7 @@ class MastodonAccount extends Account {
accountId: this._id,
sender: msg.account.acct,
str: msg.reblog ? '' : this._cleanString(msg.content),
url: msg.url,
channel: channel,
// realtime: true,
media: msg.media_attachments.map(i=>i.text_url || i.remote_url),
......
......@@ -149,7 +149,15 @@ class TwitterAccount extends Account {
// return Date.now();
}
_cleanString(str, entities) {
post(str) {
this._tw.post('statuses/update', {status: str}, function(error, tweet, response) {
if(error) throw error;
// console.log(tweet); // Tweet body.
// console.log(response); // Raw response object.
});
}
_cleanString(str, entities, inReplyTo) {
if (entities.urls) {
entities.urls.forEach(el=>str = str.replace(el.url, el.expanded_url));
......@@ -159,6 +167,10 @@ class TwitterAccount extends Account {
entities.media.forEach(el=>str = str.replace(el.url, ''));
}
if (inReplyTo) {
str = "[re: @" + inReplyTo + "] " + str;
}
// return striptags( he.decode( str.replace('</p><p>', ' // ') ) );
return str.
replace(/\n/g, ' // ').
......@@ -282,6 +294,7 @@ class TwitterAccount extends Account {
raw: channel && msg,
sender: msg.user.screen_name,
str: '',
url: 'https://www.twitter.com/' + msg.user.screen_name + '/status/' + msg.id_str,
channel: channel,
media: [],
timestamp: Date.parse(msg.created_at),
......@@ -308,7 +321,7 @@ class TwitterAccount extends Account {
// i.media_url
// })) || [];
let str = this._cleanString(msg.text, allEntities);
let str = this._cleanString(msg.text, allEntities, msg.in_reply_to_screen_name);
// Wait for normalization of quoted, return a promise
if (msg.quoted_status) {
......@@ -320,6 +333,7 @@ class TwitterAccount extends Account {
accountId: this._id,
sender: msg.user.screen_name,
str: str,
url: 'https://www.twitter.com/' + msg.user.screen_name + '/status/' + msg.id_str,
channel: channel,
raw: channel && msg,
// realtime: true,
......@@ -340,6 +354,7 @@ class TwitterAccount extends Account {
accountId: this._id,
sender: msg.user.screen_name,
str: str,
url: 'https://www.twitter.com/' + msg.user.screen_name + '/status/' + msg.id_str,
channel: channel,
raw: channel && msg,
// realtime: true,
......
......@@ -223,7 +223,7 @@ vorpal
// Command: config set
vorpal
.command('config set [key] [value]', 'Sets a configuration value.')
.command('config set <key> <value>', 'Sets a configuration value.')
.action(function(args, callback) {
if (args.key === undefined || args.value === undefined ) {
this.log( chalk.red('>>> Specify configuration key and value to be set.'));
......@@ -245,7 +245,7 @@ vorpal
// Command: config delete
vorpal
.command('config delete [key]', 'Deletes a configuration value.')
.command('config delete <key>', 'Deletes a configuration value.')
.action(function(args, callback) {
if (args.key === undefined ) {
......@@ -294,16 +294,21 @@ vorpal
// Command: accounts delete
vorpal
.command('accounts delete [id]', 'Removes an account given its platform and alias.')
.command('accounts delete <id>', 'Removes an account given its soCLIal numerical ID.')
.action(function(args, callback) {
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.'));
/// TODO: Delete config values
let flat = flatten(conf.all.accounts[args.id]);
for (let i in flat) {
conf.delete('accounts.' + args.id + '.' + i);
}
/// TODO: Unlisten to all channels
/// TODO: Destroy instance
this.log( chalk.red.bold('>>> Account deleted. Restart soCLIal to apply changes.'));
}
callback();
});
......@@ -391,6 +396,50 @@ vorpal
// Command: item url
vorpal
.command('url <id>', 'Displays the unequivocal URL for an item, given its soCLIal letter-letter-number ID.')
.action(function(args, callback) {
if (args.id === undefined) {
this.log( chalk.red('>>> Specify item ID, e.g. "url ac7".'));
} else {
let item = circularBuffer.get(args.id);
if (!item) {
this.log( chalk.red('>>> Item ID doesn\'t exist.'));
} else {
this.log(
chalk.purple('>>> URL for ') +
chalk.cyan(args.id) +
chalk.purple(' is ') +
chalk.cyan(item.url)
);
}
}
callback();
});
// Command: post something
vorpal
.command('post [str...]')
.description('Post something to all platforms at once.')
.action((args, callback)=>{
let str = args.str.join(' ');
vorpal.log(chalk.green('Should post: ' + args.str.join(' ')));
// this.cancel();
for (let i in activeAccounts) {
activeAccounts[i].post(str);
}
callback();
});
vorpal.log( chalk.white.bold('>>> Welcome to soCLIal !!!') );
vorpal.log( chalk.white.bold('>>> Type "help" to see available commands.') );
......
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