X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=discourse%2Fdiscourse.php;h=f52edb9fb74597294cc1aa9127aead42e850e6ba;hb=629c1cca9f330db4e57c8ed3f505acf9f29b734e;hp=d8bb95f231565354305f828d239a5e0afb432cc2;hpb=0235126589e0ad23c3e72a34203402b73bf3dea0;p=friendica-addons.git diff --git a/discourse/discourse.php b/discourse/discourse.php index d8bb95f2..f52edb9f 100644 --- a/discourse/discourse.php +++ b/discourse/discourse.php @@ -7,22 +7,27 @@ * Author: Michael Vogel * */ -//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\Renderer; 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() { @@ -31,39 +36,20 @@ function discourse_install() Hook::register('connector_settings_post', __FILE__, 'discourse_settings_post'); } -function discourse_uninstall() -{ - Hook::unregister('email_getmessage', __FILE__, 'discourse_email_getmessage'); - Hook::unregister('connector_settings', __FILE__, 'discourse_settings'); - Hook::unregister('connector_settings_post', __FILE__, 'discourse_settings_post'); -} - function discourse_settings(App $a, &$s) { if (!local_user()) { return; } - $enabled = intval(PConfig::get(local_user(), 'discourse', 'enabled')); - $css = ($enabled ? '' : '-disabled'); - - $s .= ''; - $s .= '

' . L10n::t('Discourse') . '

'; - $s .= '
'; - $s .= ''; } function discourse_settings_post(App $a) @@ -72,7 +58,7 @@ function discourse_settings_post(App $a) return; } - PConfig::set(local_user(), 'discourse', 'enabled', intval($_POST['enabled'])); + DI::pConfig()->set(local_user(), 'discourse', 'enabled', intval($_POST['enabled'])); } function discourse_email_getmessage(App $a, &$message) @@ -81,7 +67,7 @@ function discourse_email_getmessage(App $a, &$message) return; } - if (!PConfig::get($message['item']['uid'], 'discourse', 'enabled')) { + if (!DI::pConfig()->get($message['item']['uid'], 'discourse', 'enabled')) { return; } @@ -94,7 +80,6 @@ function discourse_email_getmessage(App $a, &$message) if (preg_match('=topic/(.*\d)@(.*)=', $message['item']['uri'], $matches) && discourse_fetch_topic_from_api($message, 'https://' . $matches[2], $matches[1], 1)) { - discourse_fetch_post_from_api($message, $matches[2], $matches[3]); Logger::info('Fetched starting post via API (message-id mode)', ['host' => $matches[2], 'topic' => $matches[1]]); return; } @@ -114,14 +99,14 @@ function discourse_email_getmessage(App $a, &$message) return; } - Logger::info('Fallback mode'); + 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']); } } @@ -129,7 +114,7 @@ function discourse_email_getmessage(App $a, &$message) function discourse_fetch_post($host, $topic, $pid) { $url = $host . '/t/' . $topic . '/' . $pid . '.json'; - $curlResult = Network::curl($url); + $curlResult = DI::httpRequest()->get($url); if (!$curlResult->isSuccess()) { Logger::info('No success', ['url' => $url]); return false; @@ -166,7 +151,7 @@ function discourse_fetch_post_from_api(&$message, $post, $host) { $hostaddr = 'https://' . $host; $url = $hostaddr . '/posts/' . $post . '.json'; - $curlResult = Network::curl($url); + $curlResult = DI::httpRequest()->get($url); if (!$curlResult->isSuccess()) { return false; } @@ -213,12 +198,12 @@ function discourse_get_user($post, $hostaddr) $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; } @@ -241,6 +226,12 @@ function discourse_process_post($message, $post, $hostaddr) if ($post['post_number'] == 1) { $message['item']['parent-uri'] = $message['item']['uri'] = 'topic/' . $post['topic_id'] . '@' . $host; + + // 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; @@ -277,7 +268,7 @@ function discourse_get_html($message) $profile = discourse_get_profile($xpath); 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'];