* Author: Michael Vogel <http://pirati.ca/profile/heluecht>
*
*/
-//use DOMDocument;
-//use DOMXPath;
+
use Friendica\App;
+use Friendica\Content\Text\Markdown;
use Friendica\Core\Hook;
-use Friendica\Core\L10n;
use Friendica\Core\Logger;
-use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
+use Friendica\Core\Renderer;
use Friendica\Database\DBA;
+use Friendica\DI;
use Friendica\Model\Contact;
-use Friendica\Util\XML;
-use Friendica\Content\Text\Markdown;
-use Friendica\Util\Network;
+use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
-Use Friendica\Util\DateTimeFormat;
+
+/* Todo:
+ * - Obtaining API tokens to be able to read non public posts as well
+ * - Handling duplicates (possibly using some non visible marker)
+ * - Fetching missing posts
+ * - Fetch topic information
+ * - Support mail free mode when write tokens are available
+ * - Fix incomplete (relative) links (hosts are missing)
+*/
function discourse_install()
{
- Hook::register('email_getmessage', __FILE__, 'discourse_email_getmessage');
- Hook::register('email_getmessage_end', __FILE__, 'discourse_email_getmessage_end');
- Hook::register('addon_settings', __FILE__, 'discourse_addon_settings');
- Hook::register('addon_settings_post', __FILE__, 'discourse_addon_settings_post');
+ Hook::register('email_getmessage', __FILE__, 'discourse_email_getmessage');
+ Hook::register('connector_settings', __FILE__, 'discourse_settings');
+ Hook::register('connector_settings_post', __FILE__, 'discourse_settings_post');
}
-function discourse_uninstall()
+function discourse_settings(array &$data)
{
- Hook::unregister('email_getmessage', __FILE__, 'discourse_email_getmessage');
- Hook::unregister('email_getmessage_end', __FILE__, 'discourse_email_getmessage_end');
- Hook::unregister('addon_settings', __FILE__, 'discourse_addon_settings');
- Hook::unregister('addon_settings_post', __FILE__, 'discourse_addon_settings_post');
-}
+ if (!DI::userSession()->getLocalUserId()) {
+ return;
+ }
-function discourse_addon_settings(App $a, &$s)
-{
+ $enabled = intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'discourse', 'enabled'));
+
+ $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/discourse/');
+ $html = Renderer::replaceMacros($t, [
+ '$enabled' => ['enabled', DI::l10n()->t('Enable processing of Discourse mailing list mails'), $enabled, DI::l10n()->t('If enabled, incoming mails from Discourse will be improved so they look much better. To make it work, you have to configure the e-mail settings in Friendica. You also have to enable the mailing list mode in Discourse. Then you have to add the Discourse mail account as contact.')],
+ ]);
+
+ $data = [
+ 'connector' => 'discourse',
+ 'title' => DI::l10n()->t('Discourse'),
+ 'image' => 'images/discourse.png',
+ 'enabled' => $enabled,
+ 'html' => $html,
+ ];
}
-function discourse_addon_settings_post(App $a)
+function discourse_settings_post()
{
+ if (!DI::userSession()->getLocalUserId() || empty($_POST['discourse-submit'])) {
+ return;
+ }
+
+ DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'discourse', 'enabled', intval($_POST['enabled']));
}
-function discourse_email_getmessage(App $a, &$message)
+function discourse_email_getmessage(&$message)
{
-// Logger::info('Got raw message', $message);
+ if (empty($message['item']['uid'])) {
+ return;
+ }
-/* if (preg_match('=topic/(.*)/(.*)@(.*)=', $message['item']['uri'], $matches)) {
- Logger::info('Got post data', ['topic' => $matches[1], 'post' => $matches[2], 'host' => $matches[3]]);
- if (discourse_fetch_post_from_api($message, $matches[2], $matches[3])) {
- return;
- }
+ if (!DI::pConfig()->get($message['item']['uid'], 'discourse', 'enabled')) {
+ return;
}
-*/
+
+ // We do assume that all Discourse servers are running with SSL
+ if (preg_match('=topic/(.*\d)/(.*\d)@(.*)=', $message['item']['uri'], $matches) &&
+ discourse_fetch_post_from_api($message, $matches[2], $matches[3])) {
+ Logger::info('Fetched comment via API (message-id mode)', ['host' => $matches[3], 'topic' => $matches[1], 'post' => $matches[2]]);
+ return;
+ }
+
+ if (preg_match('=topic/(.*\d)@(.*)=', $message['item']['uri'], $matches) &&
+ discourse_fetch_topic_from_api($message, 'https://' . $matches[2], $matches[1], 1)) {
+ Logger::info('Fetched starting post via API (message-id mode)', ['host' => $matches[2], 'topic' => $matches[1]]);
+ return;
+ }
+
// Search in the text part for the link to the discourse entry and the text body
- // The text body is used as alternative, if the fetched HTML isn't working
if (!empty($message['text'])) {
$message = discourse_get_text($message);
}
- if (!empty($message['item']['plink'])) {
- if (preg_match('=(http.*)/t/.*/(.*\d)/(.*\d)=', $message['item']['plink'], $matches)) {
- if (discourse_fetch_topic_from_api($message, $matches[1], $matches[2], $matches[3])) {
- return;
- }
- }
+ if (empty($message['item']['plink']) || !preg_match('=(http.*)/t/.*/(.*\d)/(.*\d)=', $message['item']['plink'], $matches)) {
+ Logger::info('This is no Discourse post');
+ return;
+ }
+
+ if (discourse_fetch_topic_from_api($message, $matches[1], $matches[2], $matches[3])) {
+ Logger::info('Fetched post via API (plink mode)', ['host' => $matches[1], 'topic' => $matches[2], 'id' => $matches[3]]);
+ return;
}
- Logger::info('Stop');
-die('Test');
+
+ Logger::info('Fallback mode', ['plink' => $message['item']['plink']]);
// Search in the HTML part for the discourse entry and the author profile
if (!empty($message['html'])) {
$message = discourse_get_html($message);
}
// Remove the title on comments, they don't serve any purpose there
- if ($message['item']['parent-uri'] != $message['item']['uri']) {
+ if ($message['item']['thr-parent'] != $message['item']['uri']) {
unset($message['item']['title']);
}
}
function discourse_fetch_post($host, $topic, $pid)
{
$url = $host . '/t/' . $topic . '/' . $pid . '.json';
- $curlResult = Network::curl($url);
+ $curlResult = DI::httpClient()->get($url);
if (!$curlResult->isSuccess()) {
Logger::info('No success', ['url' => $url]);
return false;
$posts = $data['post_stream']['posts'];
foreach($posts as $post) {
if ($post['post_number'] != $pid) {
- // Test
- discourse_get_user($post, $host);
+ /// @todo Possibly fetch missing posts here
continue;
}
Logger::info('Got post data from topic', $post);
{
$hostaddr = 'https://' . $host;
$url = $hostaddr . '/posts/' . $post . '.json';
- $curlResult = Network::curl($url);
+ $curlResult = DI::httpClient()->get($url);
if (!$curlResult->isSuccess()) {
return false;
}
{
$host = parse_url($hostaddr, PHP_URL_HOST);
+ // Currently unused contact fields:
+ // - display_username
+ // - user_id
+
$contact = [];
- // display_username
- // user_id
$contact['uid'] = 0;
$contact['network'] = Protocol::DISCOURSE;
$contact['name'] = $contact['nick'] = $post['username'];
$contact['nurl'] = Strings::normaliseLink($contact['url']);
$contact['baseurl'] = $hostaddr;
Logger::info('Contact', $contact);
- $contact['id'] = Contact::getIdForURL($contact['url'], 0, true, $contact);
+ $contact['id'] = Contact::getIdForURL($contact['url'], 0, false, $contact);
if (!empty($contact['id'])) {
$avatar = $contact['photo'];
unset($contact['photo']);
DBA::update('contact', $contact, ['id' => $contact['id']]);
- Contact::updateAvatar($avatar, 0, $contact['id']);
+ Contact::updateAvatar($contact['id'], $avatar);
$contact['photo'] = $avatar;
}
if ($post['post_number'] == 1) {
$message['item']['parent-uri'] = $message['item']['uri'] = 'topic/' . $post['topic_id'] . '@' . $host;
- // To-Do: Thread information
+
+ // Remove the Discourse forum name from the subject
+ $pattern = '=\[.*\].*\s(\[.*\].*)=';
+ if (preg_match($pattern, $message['item']['title'])) {
+ $message['item']['title'] = preg_replace($pattern, '$1', $message['item']['title']);
+ }
+ /// @ToDo Fetch thread information
} else {
$message['item']['uri'] = 'topic/' . $post['topic_id'] . '/' . $post['id'] . '@' . $host;
unset($message['item']['title']);
$xpath = new DomXPath($doc);
- // Fetch the first 'div' before the 'hr' -hopefully this fits for all systems
+ // Fetch the first 'div' before the 'hr' - hopefully this fits for all systems
$result = $xpath->query("//hr//preceding::div[1]");
$div = $doc2->importNode($result->item(0), true);
$doc2->appendChild($div);
Logger::info('Found html body', ['html' => $message['html']]);
$profile = discourse_get_profile($xpath);
- if (!empty($profile)) {
+ if (!empty($profile['url'])) {
Logger::info('Found profile', $profile);
- $message['item']['author-id'] = Contact::getIdForURL($profile['url'], 0, true, $profile);
+ $message['item']['author-id'] = Contact::getIdForURL($profile['url'], 0, false, $profile);
$message['item']['author-link'] = $profile['url'];
$message['item']['author-name'] = $profile['name'];
$message['item']['author-avatar'] = $profile['photo'];
}
return $profile;
}
-
-function discourse_email_getmessage_end(App $a, &$message)
-{
-// Logger::info('Got converted message', $message);
-}