From cf3a46b1265f52c56c013ae27900cf1a378f659b Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 10 Feb 2019 11:28:17 +0000 Subject: [PATCH 1/3] Don't store multiple follow request from a single person --- src/Model/Item.php | 17 +++++++++++++++++ src/Model/User.php | 14 ++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Model/Item.php b/src/Model/Item.php index 6a5ff01d59..ead907f8b4 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1451,6 +1451,23 @@ class Item extends BaseObject Logger::log("Set network to " . $item["network"] . " for " . $item["uri"], Logger::DEBUG); } + if ($item['verb'] == ACTIVITY_FOLLOW) { + Logger::log('Blubb: '.$item['uid'].' - '.$item['parent-uri']); + if (!$item['origin'] && ($item['author-id'] == User::getPublicContactById($uid))) { + // Our own follow request can be relayed to us. We don't store them to avoid notification chaos. + Logger::log("Blubb: Don't store not origin follow request from us for " . $item['parent-uri'], Logger::DEBUG); + return 0; + } + + $condition = ['verb' => ACTIVITY_FOLLOW, 'uid' => $item['uid'], + 'parent-uri' => $item['parent-uri'], 'author-id' => $item['author-id']]; + if (self::exists($condition)) { + // It happens that we receive multiple follow requests by the same author - we only store one. + Logger::log('Blubb: Found existing follow request from author ' . $item['author-id'] . ' for ' . $item['parent-uri'], Logger::DEBUG); + return 0; + } + } + // Checking if there is already an item with the same guid Logger::log('Checking for an item for user '.$item['uid'].' on network '.$item['network'].' with the guid '.$item['guid'], Logger::DEBUG); $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']]; diff --git a/src/Model/User.php b/src/Model/User.php index 59ca4c90a4..841c81f741 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -99,6 +99,20 @@ class User return DBA::selectFirst('user', [], ['uid' => $uid]); } + /** + * @param integer $uid + * @return array|boolean User record if it exists, false otherwise + * @throws Exception + */ + public static function getPublicContactById($uid) + { + $self = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]); + if (!DBA::isResult($self)) { + return false; + } + return Contact::getIdForURL($self['url'], 0, true); + } + /** * @brief Returns the user id of a given profile URL * From 758eabd2f6bab475b1345e77cb15b199d91f99ff Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 10 Feb 2019 12:21:16 +0000 Subject: [PATCH 2/3] Removed test output --- src/Model/Item.php | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Model/Item.php b/src/Model/Item.php index ead907f8b4..3f2f3c1752 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1315,6 +1315,8 @@ class Item extends BaseObject $item['gravity'] = GRAVITY_PARENT; } elseif (activity_match($item['verb'], ACTIVITY_POST)) { $item['gravity'] = GRAVITY_COMMENT; + } elseif (activity_match($item['verb'], ACTIVITY_FOLLOW)) { + $item['gravity'] = GRAVITY_ACTIVITY; } else { $item['gravity'] = GRAVITY_UNKNOWN; // Should not happen Logger::log('Unknown gravity for verb: ' . $item['verb'], Logger::DEBUG); @@ -1451,11 +1453,18 @@ class Item extends BaseObject Logger::log("Set network to " . $item["network"] . " for " . $item["uri"], Logger::DEBUG); } + // Checking if there is already an item with the same guid + Logger::log('Checking for an item for user '.$item['uid'].' on network '.$item['network'].' with the guid '.$item['guid'], Logger::DEBUG); + $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']]; + if (self::exists($condition)) { + Logger::log('found item with guid '.$item['guid'].' for user '.$item['uid'].' on network '.$item['network'], Logger::DEBUG); + return 0; + } + if ($item['verb'] == ACTIVITY_FOLLOW) { - Logger::log('Blubb: '.$item['uid'].' - '.$item['parent-uri']); if (!$item['origin'] && ($item['author-id'] == User::getPublicContactById($uid))) { // Our own follow request can be relayed to us. We don't store them to avoid notification chaos. - Logger::log("Blubb: Don't store not origin follow request from us for " . $item['parent-uri'], Logger::DEBUG); + Logger::log("Follow: Don't store not origin follow request from us for " . $item['parent-uri'], Logger::DEBUG); return 0; } @@ -1463,19 +1472,11 @@ class Item extends BaseObject 'parent-uri' => $item['parent-uri'], 'author-id' => $item['author-id']]; if (self::exists($condition)) { // It happens that we receive multiple follow requests by the same author - we only store one. - Logger::log('Blubb: Found existing follow request from author ' . $item['author-id'] . ' for ' . $item['parent-uri'], Logger::DEBUG); + Logger::log('Follow: Found existing follow request from author ' . $item['author-id'] . ' for ' . $item['parent-uri'], Logger::DEBUG); return 0; } } - // Checking if there is already an item with the same guid - Logger::log('Checking for an item for user '.$item['uid'].' on network '.$item['network'].' with the guid '.$item['guid'], Logger::DEBUG); - $condition = ['guid' => $item['guid'], 'network' => $item['network'], 'uid' => $item['uid']]; - if (self::exists($condition)) { - Logger::log('found item with guid '.$item['guid'].' for user '.$item['uid'].' on network '.$item['network'], Logger::DEBUG); - return 0; - } - // Check for hashtags in the body and repair or add hashtag links self::setHashtags($item); From 7939cacc44099f10fc3ee8ef47bbb812eb83ebd9 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 10 Feb 2019 17:19:10 +0000 Subject: [PATCH 3/3] The function moved from the user to the contact class --- src/Model/Contact.php | 17 +++++++++++++++++ src/Model/Item.php | 4 ++-- src/Model/User.php | 14 -------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index cf90406e24..2d6bc716bb 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -155,6 +155,23 @@ class Contact extends BaseObject return PortableContact::detectServer($url); } + /** + * Returns the public contact id of the given user id + * + * @param integer $uid User ID + * + * @return integer|boolean Public contact id for given user id + * @throws Exception + */ + public static function getPublicIdByUserId($uid) + { + $self = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]); + if (!DBA::isResult($self)) { + return false; + } + return self::getIdForURL($self['url'], 0, true); + } + /** * @brief Returns the contact id for the user and the public contact id for a given contact id * diff --git a/src/Model/Item.php b/src/Model/Item.php index 3f2f3c1752..54bd4a0ef7 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -1462,8 +1462,8 @@ class Item extends BaseObject } if ($item['verb'] == ACTIVITY_FOLLOW) { - if (!$item['origin'] && ($item['author-id'] == User::getPublicContactById($uid))) { - // Our own follow request can be relayed to us. We don't store them to avoid notification chaos. + if (!$item['origin'] && ($item['author-id'] == Contact::getPublicIdByUserId($uid))) { + // Our own follow request can be relayed to us. We don't store it to avoid notification chaos. Logger::log("Follow: Don't store not origin follow request from us for " . $item['parent-uri'], Logger::DEBUG); return 0; } diff --git a/src/Model/User.php b/src/Model/User.php index 841c81f741..59ca4c90a4 100644 --- a/src/Model/User.php +++ b/src/Model/User.php @@ -99,20 +99,6 @@ class User return DBA::selectFirst('user', [], ['uid' => $uid]); } - /** - * @param integer $uid - * @return array|boolean User record if it exists, false otherwise - * @throws Exception - */ - public static function getPublicContactById($uid) - { - $self = DBA::selectFirst('contact', ['url'], ['self' => true, 'uid' => $uid]); - if (!DBA::isResult($self)) { - return false; - } - return Contact::getIdForURL($self['url'], 0, true); - } - /** * @brief Returns the user id of a given profile URL *