From 019a9d44c54f26fdfe6b8e8690dbb131c2211ce9 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 26 Jun 2020 05:28:25 +0000 Subject: [PATCH 1/2] New function to fetch contact data by url --- src/Model/Contact.php | 59 +++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 04cece5359..e3fc2d334d 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -190,6 +190,44 @@ class Contact return DBA::selectFirst('contact', $fields, ['id' => $id]); } + /** + * Fetches a contact by a given url + * + * @param string $url profile url + * @param integer $uid User ID of the contact + * @param array $fields Field list + * @param boolean $update true = always update, false = never update, null = update when not found or outdated + * @return array contact array + */ + public static function getByURL(string $url, int $uid = 0, array $fields = [], $update = null) + { + if ($update || is_null($update)) { + $cid = self::getIdForURL($url, $uid, !($update ?? false)); + if (empty($cid)) { + return []; + } + return self::getById($cid, $fields); + } + + // We first try the nurl (http://server.tld/nick), most common case + $options = ['order' => ['id']]; + $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false], $options); + + // Then the addr (nick@server.tld) + if (!DBA::isResult($contact)) { + $contact = DBA::selectFirst('contact', $fields, ['addr' => str_replace('acct:', '', $url), 'uid' => $uid, 'deleted' => false], $options); + } + + // Then the alias (which could be anything) + if (!DBA::isResult($contact)) { + // The link could be provided as http although we stored it as https + $ssl_url = str_replace('http://', 'https://', $url); + $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid]; + $contact = DBA::selectFirst('contact', $fields, $condition, $options); + } + return $contact; + } + /** * Tests if the given contact is a follower * @@ -1459,26 +1497,9 @@ class Contact return 0; } - /// @todo Verify if we can't use Contact::getDetailsByUrl instead of the following - // We first try the nurl (http://server.tld/nick), most common case - $fields = ['id', 'avatar', 'updated', 'network']; - $options = ['order' => ['id']]; - $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false], $options); + $contact = self::getByURL($url, $uid, ['id', 'avatar', 'updated', 'network'], false); - // Then the addr (nick@server.tld) - if (!DBA::isResult($contact)) { - $contact = DBA::selectFirst('contact', $fields, ['addr' => str_replace('acct:', '', $url), 'uid' => $uid, 'deleted' => false], $options); - } - - // Then the alias (which could be anything) - if (!DBA::isResult($contact)) { - // The link could be provided as http although we stored it as https - $ssl_url = str_replace('http://', 'https://', $url); - $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid]; - $contact = DBA::selectFirst('contact', $fields, $condition, $options); - } - - if (DBA::isResult($contact)) { + if (!empty($contact)) { $contact_id = $contact["id"]; $update_contact = false; From a0ee12aade4d60166e2e7bf87a71adb9dd51a863 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 26 Jun 2020 05:29:00 +0000 Subject: [PATCH 2/2] Fix notice "Undefined index: forum" --- src/Protocol/OStatus.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index a2181beeb3..2720d6050f 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -2087,9 +2087,8 @@ class OStatus $mentioned = $newmentions; foreach ($mentioned as $mention) { - $contact = Contact::getDetailsByURL($mention, $owner['uid']); - if (!empty($contact) && ($contact["forum"] || $contact["prv"] || ($owner['contact-type'] == Contact::TYPE_COMMUNITY) || - ($contact['self'] && ($owner['account-type'] == User::ACCOUNT_TYPE_COMMUNITY)))) { + $contact = Contact::getByURL($mention, 0, ['contact-type']); + if (!empty($contact) && ($contact['contact-type'] == Contact::TYPE_COMMUNITY)) { XML::addElement($doc, $entry, "link", "", [ "rel" => "mentioned",