2017-10-18 23:17:20 +03:00
|
|
|
var Promise = require('promise');
|
|
|
|
var requestPromise = require('./helpers/requestPromise.js');
|
|
|
|
var BasePlugin = require('./base.js');
|
|
|
|
var logger = require('winston');
|
|
|
|
var htmlToText = require('html-to-text');
|
|
|
|
class Mastodon extends BasePlugin {
|
|
|
|
constructor(config) {
|
|
|
|
super(config);
|
|
|
|
this.name = 'Mastodon';
|
|
|
|
}
|
|
|
|
|
|
|
|
test(input) {
|
|
|
|
var res = null;
|
|
|
|
if (res = input.match(/http(s)?:\/\/(www\.)?((.*\..*\/users\/([a-zA-Z0-9_-]*)\/statuses\/([0-9_-]*)))/)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (res = input.match(/http(s)?:\/\/(www\.)?(((.*\..*)\/@([a-zA-Z0-9_-]*)\/([0-9_-]*)))/)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
getToot(host, statusId) {
|
|
|
|
var promise = new Promise(function(resolve, reject) {
|
|
|
|
var url = 'https://' + host + '/api/v1/statuses/' + statusId;
|
|
|
|
requestPromise(url, {}, {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
}).then(function(toot) {
|
|
|
|
if(!toot.error) {
|
|
|
|
var user = toot.account.username;
|
|
|
|
var time = toot.created_at;
|
|
|
|
var dateTime = new Date(time);
|
|
|
|
var dateTimeLocale = dateTime.toLocaleDateString('fi-FI') + ' ' + dateTime.getHours() + '.' + dateTime.getMinutes();
|
|
|
|
|
|
|
|
var tootContent = toot.content;
|
|
|
|
var tootContent = htmlToText.fromString(tootContent, {
|
|
|
|
wordwrap: false,
|
|
|
|
ignoreHref: true,
|
|
|
|
preserveNewlines: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
var str = '@' + user + ': "' + tootContent + '" ('+ dateTimeLocale +')';
|
|
|
|
str = str.replace(/(?:\r\n|\r|\n)/g, ' ');
|
|
|
|
|
|
|
|
resolve(str);
|
|
|
|
} else {
|
|
|
|
reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
result(input) {
|
|
|
|
var that = this;
|
|
|
|
var resultPromise = new Promise(function(resultResolve, resultReject) {
|
|
|
|
var host = null;
|
|
|
|
var tootId = null;
|
|
|
|
|
|
|
|
var res = null;
|
|
|
|
if (res = input.match(/http(s)?:\/\/(www\.)?(((.*\..*)\/users\/([a-zA-Z0-9_-]*)\/statuses\/([0-9_-]*)))/)) {
|
|
|
|
host = res[5];
|
|
|
|
tootId = res[7];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res = input.match(/http(s)?:\/\/(www\.)?(((.*\..*)\/@([a-zA-Z0-9_-]*)\/([0-9_-]*)))/)) {
|
|
|
|
host = res[5];
|
|
|
|
tootId = res[7];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (host !== null && tootId !== null) {
|
|
|
|
that.getToot(host, tootId).then(function(toot) {
|
2019-03-12 21:29:01 +02:00
|
|
|
resultResolve('Tuuttaus: ' + toot);
|
2017-10-18 23:17:20 +03:00
|
|
|
}, function(error) {
|
2019-03-12 21:29:01 +02:00
|
|
|
resultReject('Tuuttaus: Jokin virhe tapahtui');
|
2017-10-18 23:17:20 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return resultPromise;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Mastodon;
|