86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
|
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) {
|
||
|
resultResolve('Töötti: ' + toot);
|
||
|
}, function(error) {
|
||
|
resultReject('Töötti: Jokin virhe tapahtui');
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
return resultPromise;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Mastodon;
|