var Promise = require('promise') var requestPromise = require('./helpers/requestPromise.js') var BasePlugin = require('./base.js') var htmlToText = require('html-to-text') class Mastodon extends BasePlugin { constructor (config) { super(config) this.name = 'Mastodon' } test (input) { if (input.match(/http(s)?:\/\/(www\.)?((.*\..*\/users\/([a-zA-Z0-9_-]*)\/statuses\/([0-9_-]*)))/)) { return true } if (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 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(new Error('')) } }) }) return promise } result (input) { var that = this var resultPromise = new Promise(function (resolve, reject) { var host = null var tootId = null var res = input.match(/http(s)?:\/\/(www\.)?(((.*\..*)\/users\/([a-zA-Z0-9_-]*)\/statuses\/([0-9_-]*)))/) if (res !== null) { host = res[5] tootId = res[7] } res = input.match(/http(s)?:\/\/(www\.)?(((.*\..*)\/@([a-zA-Z0-9_-]*)\/([0-9_-]*)))/) if (res !== null) { host = res[5] tootId = res[7] } if (host !== null && tootId !== null) { that.getToot(host, tootId).then(function (toot) { resolve('Tuuttaus: ' + toot) }, function () { reject(new Error('Tuuttaus: Jokin virhe tapahtui')) }) } }) return resultPromise } } module.exports = Mastodon