From 6249b674dc3d36a0cbd58a48ced399f46611e41c Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 25 Mar 2020 16:42:53 +0000 Subject: [PATCH 1/3] Issue 7892: Improve "plink" generation for Diaspora posts --- src/Model/Contact.php | 6 ++-- src/Protocol/Diaspora.php | 58 ++++++++++++++++++++++++++++----------- 2 files changed, 45 insertions(+), 19 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 9ae27994c..2b739278a 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1142,7 +1142,7 @@ class Contact // Fetch contact data from the contact table for the given user $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`, - `keywords`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`, `rel`, `pending` + `keywords`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, `self`, `rel`, `pending`,`baseurl` FROM `contact` WHERE `addr` = '%s' AND `uid` = %d AND NOT `deleted`", DBA::escape($addr), intval($uid) @@ -1150,7 +1150,7 @@ class Contact // Fetch the data from the contact table with "uid=0" (which is filled automatically) if (!DBA::isResult($r)) { $r = q("SELECT `id`, 0 AS `cid`, `id` AS `zid`, 0 AS `gid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`, - `keywords`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`, `rel`, `pending` + `keywords`, `photo`, `thumb`, `micro`, `forum`, `prv`, (`forum` | `prv`) AS `community`, `contact-type`, `bd` AS `birthday`, 0 AS `self`, `rel`, `pending`, `baseurl` FROM `contact` WHERE `addr` = '%s' AND `uid` = 0 AND NOT `deleted`", DBA::escape($addr) ); @@ -1159,7 +1159,7 @@ class Contact // Fetch the data from the gcontact table if (!DBA::isResult($r)) { $r = q("SELECT 0 AS `id`, 0 AS `cid`, `id` AS `gid`, 0 AS `zid`, 0 AS `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, '' AS `xmpp`, - `keywords`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`, 2 AS `rel`, 0 AS `pending` + `keywords`, `photo`, `photo` AS `thumb`, `photo` AS `micro`, `community` AS `forum`, 0 AS `prv`, `community`, `contact-type`, `birthday`, 0 AS `self`, 2 AS `rel`, 0 AS `pending`, `server_url` AS `baseurl` FROM `gcontact` WHERE `addr` = '%s'", DBA::escape($addr) ); diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 5c90f88f7..08a4c7bdd 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -1581,9 +1581,9 @@ class Diaspora * * @return bool is it a hubzilla server? */ - public static function isRedmatrix($url) + private static function isHubzilla($url) { - return(strstr($url, "/channel/")); + return(strstr($url, '/channel/')); } /** @@ -1600,28 +1600,55 @@ class Diaspora private static function plink($addr, $guid, $parent_guid = '') { $contact = Contact::getDetailsByAddr($addr); + if (empty($contact)) { + Logger::info('No contact data for address', ['addr' => $addr]); + return ''; + } - // Fallback - if (!$contact) { - if ($parent_guid != '') { - return "https://" . substr($addr, strpos($addr, "@") + 1) . "/posts/" . $parent_guid . "#" . $guid; - } else { - return "https://" . substr($addr, strpos($addr, "@") + 1) . "/posts/" . $guid; + if (empty($contact['baseurl'])) { + $contact['baseurl'] = 'https://' . substr($addr, strpos($addr, '@') + 1); + Logger::info('Create baseurl from address', ['baseurl' => $contact['baseurl'], 'url' => $contact['url']]); + } + + $platform = ''; + $gserver = DBA::selectFirst('gserver', ['platform'], ['nurl' => Strings::normaliseLink($contact['baseurl'])]); + if (!empty($gserver['platform'])) { + $platform = strtolower($gserver['platform']); + Logger::info('Detected platform', ['platform' => $platform, 'url' => $contact['url']]); + } + if ($platform == 'socialhome') { + // Socialhome doesn't offer an item endpoint that we could use + Logger::info('Ignoring Socialhome', ['platform' => $platform, 'url' => $contact['url']]); + return ''; + } + + if (!in_array($platform, ['diaspora', 'friendica', 'hubzilla'])) { + if (self::isHubzilla($contact['url'])) { + Logger::info('Detected unknown platform as Hubzilla', ['platform' => $platform, 'url' => $contact['url']]); + $platform = 'hubzilla'; + } elseif ($contact['network'] == Protocol::DFRN) { + Logger::info('Detected unknown platform as Friendica', ['platform' => $platform, 'url' => $contact['url']]); + $platform = 'friendica'; } } - if ($contact["network"] == Protocol::DFRN) { - return str_replace("/profile/" . $contact["nick"] . "/", "/display/" . $guid, $contact["url"] . "/"); + if ($platform == 'friendica') { + return str_replace('/profile/' . $contact['nick'] . '/', '/display/' . $guid, $contact['url'] . '/'); } - if (self::isRedmatrix($contact["url"])) { - return $contact["url"] . "/?mid=" . $guid; + if ($platform == 'hubzilla') { + return $contact['baseurl'] . '/item/' . $guid; + } + + if ($platform != 'diaspora') { + Logger::info('Unknown platform', ['platform' => $platform, 'url' => $contact['url']]); + return ''; } if ($parent_guid != '') { - return "https://" . substr($addr, strpos($addr, "@") + 1) . "/posts/" . $parent_guid . "#" . $guid; + return $contact['baseurl'] . '/posts/' . $parent_guid . '#' . $guid; } else { - return "https://" . substr($addr, strpos($addr, "@") + 1) . "/posts/" . $guid; + return $contact['baseurl'] . '/posts/' . $guid; } } @@ -1869,7 +1896,6 @@ class Diaspora $datarray["changed"] = $datarray["created"] = $datarray["edited"] = $created_at; $datarray["plink"] = self::plink($author, $guid, $parent_item['guid']); - $body = Markdown::toBBCode($text); $datarray["body"] = self::replacePeopleGuid($body, $person["url"]); @@ -2907,7 +2933,7 @@ class Diaspora $datarray["object-type"] = Activity\ObjectType::NOTE; // Add OEmbed and other information to the body - if (!self::isRedmatrix($contact["url"])) { + if (!self::isHubzilla($contact["url"])) { $body = add_page_info_to_body($body, false, true); } } From 1e9d793d74a1b654ef08915e2396d52317c51e74 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 25 Mar 2020 17:25:23 +0000 Subject: [PATCH 2/3] Socialhome added --- src/Protocol/Diaspora.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 08a4c7bdd..734c1c3c6 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -1616,13 +1616,8 @@ class Diaspora $platform = strtolower($gserver['platform']); Logger::info('Detected platform', ['platform' => $platform, 'url' => $contact['url']]); } - if ($platform == 'socialhome') { - // Socialhome doesn't offer an item endpoint that we could use - Logger::info('Ignoring Socialhome', ['platform' => $platform, 'url' => $contact['url']]); - return ''; - } - if (!in_array($platform, ['diaspora', 'friendica', 'hubzilla'])) { + if (!in_array($platform, ['diaspora', 'friendica', 'hubzilla', 'socialhome'])) { if (self::isHubzilla($contact['url'])) { Logger::info('Detected unknown platform as Hubzilla', ['platform' => $platform, 'url' => $contact['url']]); $platform = 'hubzilla'; @@ -1640,6 +1635,11 @@ class Diaspora return $contact['baseurl'] . '/item/' . $guid; } + if ($platform == 'socialhome') { + // This doesn't really seem to work on Socialhome + return $contact['baseurl'] . '/content/' . $guid; + } + if ($platform != 'diaspora') { Logger::info('Unknown platform', ['platform' => $platform, 'url' => $contact['url']]); return ''; From 52e6f97b612cac19d31cb8af8983a52a00dc6348 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 25 Mar 2020 18:27:35 +0000 Subject: [PATCH 3/3] Removed wrong comment --- src/Protocol/Diaspora.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 734c1c3c6..828333b41 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -1636,7 +1636,6 @@ class Diaspora } if ($platform == 'socialhome') { - // This doesn't really seem to work on Socialhome return $contact['baseurl'] . '/content/' . $guid; }