]> git.mxchange.org Git - friendica.git/commitdiff
Use channels for non public content
authorMichael <heluecht@pirati.ca>
Sun, 17 Sep 2023 19:28:38 +0000 (19:28 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 17 Sep 2023 19:28:38 +0000 (19:28 +0000)
database.sql
doc/database/db_post-engagement.md
src/Model/Item.php
src/Model/Post.php
src/Model/Post/Engagement.php
src/Module/Contact/Profile.php
src/Module/Conversation/Timeline.php
src/Protocol/Relay.php
static/dbstructure.config.php

index 2eec6fb80932c537a4784aced45f10dfa4dbbb52..329381b95b3dfc4e7efba5e50381646fe083941e 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2023.09-dev (Giant Rhubarb)
--- DB_UPDATE_VERSION 1533
+-- DB_UPDATE_VERSION 1534
 -- ------------------------------------------
 
 
@@ -1310,6 +1310,7 @@ CREATE TABLE IF NOT EXISTS `post-engagement` (
        `media-type` tinyint NOT NULL DEFAULT 0 COMMENT 'Type of media in a bit array (1 = image, 2 = video, 4 = audio',
        `language` varbinary(128) COMMENT 'Language information about this post',
        `created` datetime COMMENT '',
+       `restricted` boolean NOT NULL DEFAULT '0' COMMENT 'If true, this post is either unlisted or not from a federated network',
        `comments` mediumint unsigned COMMENT 'Number of comments',
        `activities` mediumint unsigned COMMENT 'Number of activities (like, dislike, ...)',
         PRIMARY KEY(`uri-id`),
index 3409680e2891778758bc1b725474371bbff30418..19cb64d54fb58f7a3e155447d2bff7eebafdb27b 100644 (file)
@@ -6,16 +6,17 @@ Engagement data per post
 Fields
 ------
 
-| Field        | Description                                                   | Type               | Null | Key | Default | Extra |
-| ------------ | ------------------------------------------------------------- | ------------------ | ---- | --- | ------- | ----- |
-| uri-id       | Id of the item-uri table entry that contains the item uri     | int unsigned       | NO   | PRI | NULL    |       |
-| owner-id     | Item owner                                                    | int unsigned       | NO   |     | 0       |       |
-| contact-type | Person, organisation, news, community, relay                  | tinyint            | NO   |     | 0       |       |
-| media-type   | Type of media in a bit array (1 = image, 2 = video, 4 = audio | tinyint            | NO   |     | 0       |       |
-| language     | Language information about this post                          | varbinary(128)     | YES  |     | NULL    |       |
-| created      |                                                               | datetime           | YES  |     | NULL    |       |
-| comments     | Number of comments                                            | mediumint unsigned | YES  |     | NULL    |       |
-| activities   | Number of activities (like, dislike, ...)                     | mediumint unsigned | YES  |     | NULL    |       |
+| Field        | Description                                                           | Type               | Null | Key | Default | Extra |
+| ------------ | --------------------------------------------------------------------- | ------------------ | ---- | --- | ------- | ----- |
+| uri-id       | Id of the item-uri table entry that contains the item uri             | int unsigned       | NO   | PRI | NULL    |       |
+| owner-id     | Item owner                                                            | int unsigned       | NO   |     | 0       |       |
+| contact-type | Person, organisation, news, community, relay                          | tinyint            | NO   |     | 0       |       |
+| media-type   | Type of media in a bit array (1 = image, 2 = video, 4 = audio         | tinyint            | NO   |     | 0       |       |
+| language     | Language information about this post                                  | varbinary(128)     | YES  |     | NULL    |       |
+| created      |                                                                       | datetime           | YES  |     | NULL    |       |
+| restricted   | If true, this post is either unlisted or not from a federated network | boolean            | NO   |     | 0       |       |
+| comments     | Number of comments                                                    | mediumint unsigned | YES  |     | NULL    |       |
+| activities   | Number of activities (like, dislike, ...)                             | mediumint unsigned | YES  |     | NULL    |       |
 
 Indexes
 ------------
index e48bae77a619b2bced6a68f311e5b5f4d8bee861..8972d3b75e1870214e2da3ecbc8670ac3c9c8b00 100644 (file)
@@ -1250,7 +1250,7 @@ class Item
                        }
                }
 
-               Post::insert($item['uri-id'], $item);
+               $inserted = Post::insert($item['uri-id'], $item);
 
                if ($item['gravity'] == self::GRAVITY_PARENT) {
                        Post\Thread::insert($item['uri-id'], $item);
@@ -1405,7 +1405,9 @@ class Item
                        self::updateDisplayCache($posted_item['uri-id']);
                }
 
-               Post\Engagement::storeFromItem($posted_item);
+               if ($inserted) {
+                       Post\Engagement::storeFromItem($posted_item);
+               }
 
                return $post_user_id;
        }
index 6f855867f6ea18bd59c1b0101dda71ad5474ce5a..fe484502212ae67895d4cadc888f3555c4163ba6 100644 (file)
@@ -36,10 +36,10 @@ class Post
         *
         * @param integer $uri_id
         * @param array   $fields
-        * @return int    ID of inserted post
+        * @return bool   Success of the insert process
         * @throws \Exception
         */
-       public static function insert(int $uri_id, array $data = []): int
+       public static function insert(int $uri_id, array $data = []): bool
        {
                if (empty($uri_id)) {
                        throw new BadMethodCallException('Empty URI_id');
@@ -50,11 +50,7 @@ class Post
                // Additionally assign the key fields
                $fields['uri-id'] = $uri_id;
 
-               if (!DBA::insert('post', $fields, Database::INSERT_IGNORE)) {
-                       return 0;
-               }
-
-               return DBA::lastInsertId();
+               return DBA::insert('post', $fields, Database::INSERT_IGNORE);
        }
 
        /**
index 0e284bd41d292921112f3b226c4131dc21fc4366..fad2b1080a44f0dc1fe72bc8c0d51abbebed6d91 100644 (file)
@@ -29,8 +29,10 @@ use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Item;
 use Friendica\Model\Post;
+use Friendica\Model\Tag;
 use Friendica\Model\Verb;
 use Friendica\Protocol\Activity;
+use Friendica\Protocol\Relay;
 use Friendica\Util\DateTimeFormat;
 
 // Channel
@@ -45,26 +47,12 @@ class Engagement
         */
        public static function storeFromItem(array $item)
        {
-               if (!in_array($item['network'], Protocol::FEDERATED)) {
-                       Logger::debug('No federated network', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'network' => $item['network']]);
-                       return;
-               }
-
-               if (($item['uid'] != 0) && ($item['gravity'] == Item::GRAVITY_COMMENT)) {
-                       Logger::debug('Non public comments are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $item['uid']]);
-                       return;
-               }
-
                if (in_array($item['verb'], [Activity::FOLLOW, Activity::VIEW, Activity::READ])) {
                        Logger::debug('Technical activities are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'verb' => $item['verb']]);
                        return;
                }
 
                $parent = Post::selectFirst(['created', 'owner-id', 'uid', 'private', 'contact-contact-type', 'language'], ['uri-id' => $item['parent-uri-id']]);
-               if ($parent['private'] != Item::PUBLIC) {
-                       Logger::debug('Non public posts are not stored', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'uid' => $parent['uid'], 'private' => $parent['private']]);
-                       return;
-               }
 
                if ($parent['created'] < DateTimeFormat::utc('now - ' . DI::config()->get('channel', 'engagement_hours') . ' hour')) {
                        Logger::debug('Post is too old', ['uri-id' => $item['uri-id'], 'parent-uri-id' => $item['parent-uri-id'], 'created' => $parent['created']]);
@@ -77,6 +65,15 @@ class Engagement
                        $store = Contact::hasFollowers($parent['owner-id']);
                }
 
+               if (!$store) {
+                       $tagList = Relay::getSubscribedTags();
+                       foreach (array_column(Tag::getByURIId($item['parent-uri-id'], [Tag::HASHTAG]), 'name') as $tag) {
+                               if (in_array($tag, $tagList)) {
+                                       $store = true;
+                               }
+                       }
+               }
+
                $mediatype = self::getMediaType($item['parent-uri-id']);
 
                if (!$store) {
@@ -90,6 +87,7 @@ class Engagement
                        'media-type'   => $mediatype,
                        'language'     => $parent['language'],
                        'created'      => $parent['created'],
+                       'restricted'   => !in_array($item['network'], Protocol::FEDERATED) || ($parent['private'] != Item::PUBLIC),
                        'comments'     => DBA::count('post', ['parent-uri-id' => $item['parent-uri-id'], 'gravity' => Item::GRAVITY_COMMENT]),
                        'activities'   => DBA::count('post', [
                                "`parent-uri-id` = ? AND `gravity` = ? AND NOT `vid` IN (?, ?, ?)",
@@ -98,7 +96,7 @@ class Engagement
                        ])
                ];
                if (!$store && ($engagement['comments'] == 0) && ($engagement['activities'] == 0)) {
-                       Logger::debug('No media, follower, comments or activities. Engagement not stored', ['fields' => $engagement]);
+                       Logger::debug('No media, follower, subscribed tags, comments or activities. Engagement not stored', ['fields' => $engagement]);
                        return;
                }
                $ret = DBA::insert('post-engagement', $engagement, Database::INSERT_UPDATE);
index d9faed7d9712b1095dead2d04fb4b9af98ca00fa..6584d8fe6d43fb40a5594dd3bcadffcc18f9735f 100644 (file)
@@ -340,13 +340,8 @@ class Profile extends BaseModule
                        ];
                }
 
-               if (in_array($contact['network'], Protocol::FEDERATED)) {
-                       $channel_settings_label = $this->t('Channel Settings');
-                       $channel_frequency     = Contact\User::getChannelFrequency($contact['id'], $this->session->getLocalUserId());
-               } else {
-                       $channel_settings_label = null;
-                       $channel_frequency     = null;
-               }
+               $channel_settings_label = $this->t('Channel Settings');
+               $channel_frequency     = Contact\User::getChannelFrequency($contact['id'], $this->session->getLocalUserId());
 
                $poll_interval = null;
                if ((($contact['network'] == Protocol::FEED) && !$this->config->get('system', 'adjust_poll_frequency')) || ($contact['network'] == Protocol::MAIL)) {
index d685013ba4abb67e8a673b02e843cbe6ddc95eb6..1a4d98e61c152d29b2a7d79c4a914ecbcadc2e4e 100644 (file)
@@ -306,6 +306,8 @@ class Timeline extends BaseModule
                        $condition = $this->addLanguageCondition($uid, $condition);
                }
 
+               $condition = DBA::mergeConditions($condition, ["(NOT `restricted` OR EXISTS(SELECT `id` FROM `post-user` WHERE `uid` = ? AND `uri-id` = `post-engagement`.`uri-id`))", $uid]);
+
                $condition = DBA::mergeConditions($condition, ["NOT EXISTS(SELECT `cid` FROM `user-contact` WHERE `uid` = ? AND `cid` = `post-engagement`.`owner-id` AND (`ignored` OR `blocked` OR `collapsed` OR `is-blocked` OR `channel-frequency` = ?))", $uid, Contact\User::FREQUENCY_NEVER]);
 
                if (($this->selectedTab != TimelineEntity::WHATSHOT) && !is_null($this->accountType)) {
@@ -381,7 +383,7 @@ class Timeline extends BaseModule
                        return $comments;
                }
 
-               $condition = ["`contact-type` != ? AND `comments` > ?", Contact::TYPE_COMMUNITY, 0];
+               $condition = ["`contact-type` != ? AND `comments` > ? AND NOT `restricted`", Contact::TYPE_COMMUNITY, 0];
                $condition = $this->addLanguageCondition($uid, $condition);
 
                $limit    = $this->database->count('post-engagement', $condition) / $divider;
@@ -405,7 +407,7 @@ class Timeline extends BaseModule
                        return $activities;
                }
 
-               $condition = ["`contact-type` != ? AND `activities` > ?", Contact::TYPE_COMMUNITY, 0];
+               $condition = ["`contact-type` != ? AND `activities` > ? AND NOT `restricted`", Contact::TYPE_COMMUNITY, 0];
                $condition = $this->addLanguageCondition($uid, $condition);
 
                $limit      = $this->database->count('post-engagement', $condition) / $divider;
index 2002aa9bb8cb3426fdc51e5b642a20d05eeed749..57bd7125570d7145ef3f796ca3c189b78c369913 100644 (file)
@@ -86,24 +86,14 @@ class Relay
 
                $body = ActivityPub\Processor::normalizeMentionLinks($body);
 
-               $systemTags = [];
-               $userTags = [];
                $denyTags = [];
 
                if ($scope == self::SCOPE_TAGS) {
-                       $server_tags = $config->get('system', 'relay_server_tags');
-                       $tagitems = explode(',', mb_strtolower($server_tags));
-                       foreach ($tagitems as $tag) {
-                               $systemTags[] = trim($tag, '# ');
-                       }
-
-                       if ($config->get('system', 'relay_user_tags')) {
-                               $userTags = Search::getUserTags();
-                       }
+                       $tagList = self::getSubscribedTags();
+               } else {
+                       $tagList  = [];
                }
 
-               $tagList = array_unique(array_merge($systemTags, $userTags));
-
                $deny_tags = $config->get('system', 'relay_deny_tags');
                $tagitems = explode(',', mb_strtolower($deny_tags));
                foreach ($tagitems as $tag) {
@@ -149,6 +139,29 @@ class Relay
                return false;
        }
 
+       /**
+        * Get a list of subscribed tags by both the users and the tags that are defined by the admin
+        *
+        * @return array
+        */
+       public static function getSubscribedTags(): array
+       {
+               $systemTags  = [];
+               $server_tags = DI::config()->get('system', 'relay_server_tags');
+
+               foreach (explode(',', mb_strtolower($server_tags)) as $tag) {
+                       $systemTags[] = trim($tag, '# ');
+               }
+
+               if (DI::config()->get('system', 'relay_user_tags')) {
+                       $userTags = Search::getUserTags();
+               } else {
+                       $userTags = [];
+               }
+
+               return array_unique(array_merge($systemTags, $userTags));
+       }
+
        /**
         * Detect the language of a post and decide if the post should be accepted
         *
index 89e234e7e9e6f39eebadce0c0087dc5cb39ba006..606173b817b15a15c6103c99e44951c321691d93 100644 (file)
@@ -56,7 +56,7 @@ use Friendica\Database\DBA;
 
 // This file is required several times during the test in DbaDefinition which justifies this condition
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1533);
+       define('DB_UPDATE_VERSION', 1534);
 }
 
 return [
@@ -1333,6 +1333,7 @@ return [
                        "media-type" => ["type" => "tinyint", "not null" => "1", "default" => "0", "comment" => "Type of media in a bit array (1 = image, 2 = video, 4 = audio"],
                        "language" => ["type" => "varbinary(128)", "comment" => "Language information about this post"],
                        "created" => ["type" => "datetime", "comment" => ""],
+                       "restricted" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "If true, this post is either unlisted or not from a federated network"],
                        "comments" => ["type" => "mediumint unsigned", "comment" => "Number of comments"],
                        "activities" => ["type" => "mediumint unsigned", "comment" => "Number of activities (like, dislike, ...)"],
                ],