From e247a14d2bc75a83abc04329fd8b9a2af2e13d5b Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 17 Jul 2019 21:37:13 +0000 Subject: [PATCH] Added the option to only receive top level posts from people you follow --- mod/settings.php | 3 + src/Model/Contact.php | 67 +++++++++++++++++++ src/Module/Admin/Site.php | 2 +- src/Protocol/ActivityPub/Processor.php | 19 ++++-- view/templates/settings/connectors.tpl | 1 + .../frio/templates/settings/connectors.tpl | 2 + 6 files changed, 89 insertions(+), 5 deletions(-) diff --git a/mod/settings.php b/mod/settings.php index d744dbff17..2f61a6e079 100644 --- a/mod/settings.php +++ b/mod/settings.php @@ -234,6 +234,7 @@ function settings_post(App $a) BaseModule::checkFormSecurityTokenRedirectOnError('/settings/connectors', 'settings_connectors'); if (!empty($_POST['general-submit'])) { + PConfig::set(local_user(), 'system', 'accept_only_sharer', intval($_POST['accept_only_sharer'])); PConfig::set(local_user(), 'system', 'disable_cw', intval($_POST['disable_cw'])); PConfig::set(local_user(), 'system', 'no_intelligent_shortening', intval($_POST['no_intelligent_shortening'])); PConfig::set(local_user(), 'system', 'ostatus_autofriend', intval($_POST['snautofollow'])); @@ -786,6 +787,7 @@ function settings_content(App $a) } if (($a->argc > 1) && ($a->argv[1] === 'connectors')) { + $accept_only_sharer = intval(PConfig::get(local_user(), 'system', 'accept_only_sharer')); $disable_cw = intval(PConfig::get(local_user(), 'system', 'disable_cw')); $no_intelligent_shortening = intval(PConfig::get(local_user(), 'system', 'no_intelligent_shortening')); $ostatus_autofriend = intval(PConfig::get(local_user(), 'system', 'ostatus_autofriend')); @@ -844,6 +846,7 @@ function settings_content(App $a) '$ostat_enabled' => $ostat_enabled, '$general_settings' => L10n::t('General Social Media Settings'), + '$accept_only_sharer' => ['accept_only_sharer', L10n::t('Accept only top level posts by contacts you follow'), $accept_only_sharer, L10n::t('The system does an auto completion of threads when a comment arrives. This has got the side effect that can you receive posts that had been started by a non-follower but had been commented by someone you follow. This setting deactivates this behaviour. When activated, you strictly only will receive posts from people you really do follow.')], '$disable_cw' => ['disable_cw', L10n::t('Disable Content Warning'), $disable_cw, L10n::t('Users on networks like Mastodon or Pleroma are able to set a content warning field which collapse their post by default. This disables the automatic collapsing and sets the content warning as the post title. Doesn\'t affect any other content filtering you eventually set up.')], '$no_intelligent_shortening' => ['no_intelligent_shortening', L10n::t('Disable intelligent shortening'), $no_intelligent_shortening, L10n::t('Normally the system tries to find the best link to add to shortened posts. If this option is enabled then every shortened post will always point to the original friendica post.')], '$ostatus_autofriend' => ['snautofollow', L10n::t("Automatically follow any GNU Social \x28OStatus\x29 followers/mentioners"), $ostatus_autofriend, L10n::t('If you receive a message from an unknown OStatus user, this option decides what to do. If it is checked, a new contact will be created for every unknown user.')], diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 9f6fa6264b..559236001e 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -174,6 +174,73 @@ class Contact extends BaseObject return DBA::exists('contact', $condition); } + /** + * @brief Tests if the given contact url is a follower + * + * @param string $url Contact URL + * @param int $uid User ID + * + * @return boolean is the contact id a follower? + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function isFollowerByURL($url, $uid) + { + $cid = self::getIdForURL($url, $uid, true); + + if (empty($cid)) { + return false; + } + + return self::isFollower($cid, $uid); + } + + /** + * @brief Tests if the given user follow the given contact + * + * @param int $cid Either public contact id or user's contact id + * @param int $uid User ID + * + * @return boolean is the contact url being followed? + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function isSharing($cid, $uid) + { + if (self::isBlockedByUser($cid, $uid)) { + return false; + } + + $cdata = self::getPublicAndUserContacID($cid, $uid); + if (empty($cdata['user'])) { + return false; + } + + $condition = ['id' => $cdata['user'], 'rel' => [self::SHARING, self::FRIEND]]; + return DBA::exists('contact', $condition); + } + + /** + * @brief Tests if the given user follow the given contact url + * + * @param string $url Contact URL + * @param int $uid User ID + * + * @return boolean is the contact url being followed? + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function isSharingByURL($url, $uid) + { + $cid = self::getIdForURL($url, $uid, true); + + if (empty($cid)) { + return false; + } + + return self::isSharing($cid, $uid); + } + /** * @brief Get the basepath for a given contact link * diff --git a/src/Module/Admin/Site.php b/src/Module/Admin/Site.php index 78576efe3b..8e36d86505 100644 --- a/src/Module/Admin/Site.php +++ b/src/Module/Admin/Site.php @@ -627,7 +627,7 @@ class Site extends BaseAdminModule '$community_page_style' => ['community_page_style', L10n::t('Community pages for visitors'), Config::get('system', 'community_page_style'), L10n::t('Which community pages should be available for visitors. Local users always see both pages.'), $community_page_style_choices], '$max_author_posts_community_page' => ['max_author_posts_community_page', L10n::t('Posts per user on community page'), Config::get('system', 'max_author_posts_community_page'), L10n::t('The maximum number of posts per user on the community page. (Not valid for "Global Community")')], '$ostatus_disabled' => ['ostatus_disabled', L10n::t('Disable OStatus support'), Config::get('system', 'ostatus_disabled'), L10n::t('Disable built-in OStatus (StatusNet, GNU Social etc.) compatibility. All communications in OStatus are public, so privacy warnings will be occasionally displayed.')], - '$ostatus_full_threads' => ['ostatus_full_threads', L10n::t('Only import OStatus/ActivityPub threads from our contacts'), Config::get('system', 'ostatus_full_threads'), L10n::t('Normally we import every content from our OStatus and ActivityPub contacts. With this option we only store threads that are started by a contact that is known on our system.')], + '$ostatus_full_threads' => ['ostatus_full_threads', L10n::t('Only import OStatus threads from our contacts'), Config::get('system', 'ostatus_full_threads'), L10n::t('Normally we import every content from our OStatus contacts. With this option we only store threads that are started by a contact that is known on our system.')], '$ostatus_not_able' => L10n::t('OStatus support can only be enabled if threading is enabled.'), '$diaspora_able' => $diaspora_able, '$diaspora_not_able' => L10n::t('Diaspora support can\'t be enabled because Friendica was installed into a sub directory.'), diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index bdf2e63f2e..aaf5a25185 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -425,6 +425,21 @@ class Processor continue; } + if (PConfig::get($receiver, 'system', 'accept_only_sharer', false) && ($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT)) { + $skip = !Contact::isSharingByURL($activity['author'], $receiver); + + if ($skip && (($activity['type'] == 'as:Announce') || $isForum)) { + $skip = !Contact::isSharingByURL($activity['actor'], $receiver); + } + + if ($skip) { + Logger::info('Skipping post', ['uid' => $receiver, 'url' => $item['uri']]); + continue; + } + + Logger::info('Accepting post', ['uid' => $receiver, 'url' => $item['uri']]); + } + if ($activity['object_type'] == 'as:Event') { self::createEvent($activity, $item); } @@ -524,10 +539,6 @@ class Processor */ public static function fetchMissingActivity($url, $child = []) { - if (Config::get('system', 'ostatus_full_threads')) { - return; - } - if (!empty($child['receiver'])) { $uid = ActivityPub\Receiver::getFirstUserFromReceivers($child['receiver']); } else { diff --git a/view/templates/settings/connectors.tpl b/view/templates/settings/connectors.tpl index e275e68ce3..28f9ffab19 100644 --- a/view/templates/settings/connectors.tpl +++ b/view/templates/settings/connectors.tpl @@ -11,6 +11,7 @@