]> git.mxchange.org Git - friendica.git/commitdiff
Store the tag type for the post targets
authorMichael <heluecht@pirati.ca>
Sat, 23 Apr 2022 11:39:19 +0000 (11:39 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 23 Apr 2022 11:39:19 +0000 (11:39 +0000)
database.sql
doc/database/db_tag.md
src/Model/Tag.php
src/Module/PermissionTooltip.php
src/Protocol/ActivityPub/Processor.php
static/dbstructure.config.php
static/dbview.config.php
view/lang/C/messages.po

index 48a75377fe94b99fb5430644fbed98f82a5f547e..97d9912bac90eb2005241c798aa9b2077f46168f 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2022.05-dev (Siberian Iris)
--- DB_UPDATE_VERSION 1458
+-- DB_UPDATE_VERSION 1459
 -- ------------------------------------------
 
 
@@ -232,6 +232,7 @@ CREATE TABLE IF NOT EXISTS `tag` (
        `id` int unsigned NOT NULL auto_increment COMMENT '',
        `name` varchar(96) NOT NULL DEFAULT '' COMMENT '',
        `url` varbinary(255) NOT NULL DEFAULT '' COMMENT '',
+       `type` tinyint unsigned COMMENT 'Type of the tag (Unknown, Collection or Account)',
         PRIMARY KEY(`id`),
         UNIQUE INDEX `type_name_url` (`name`,`url`),
         INDEX `url` (`url`)
@@ -2235,7 +2236,8 @@ CREATE VIEW `tag-view` AS SELECT
        `post-tag`.`tid` AS `tid`,
        `post-tag`.`cid` AS `cid`,
        CASE `cid` WHEN 0 THEN `tag`.`name` ELSE `contact`.`name` END AS `name`,
-       CASE `cid` WHEN 0 THEN `tag`.`url` ELSE `contact`.`url` END AS `url`
+       CASE `cid` WHEN 0 THEN `tag`.`url` ELSE `contact`.`url` END AS `url`,
+       CASE `cid` WHEN 0 THEN `tag`.`type` ELSE 1 END AS `tag-type`
        FROM `post-tag`
                        LEFT JOIN `tag` ON `post-tag`.`tid` = `tag`.`id`
                        LEFT JOIN `contact` ON `post-tag`.`cid` = `contact`.`id`;
index 8c742917a748061e171a9f6ae3a7e6d5ad3aff37..acde7bb2658de5bd47c7f0056cb9774b599f6a76 100644 (file)
@@ -6,11 +6,12 @@ tags and mentions
 Fields
 ------
 
-| Field | Description | Type           | Null | Key | Default | Extra          |
-| ----- | ----------- | -------------- | ---- | --- | ------- | -------------- |
-| id    |             | int unsigned   | NO   | PRI | NULL    | auto_increment |
-| name  |             | varchar(96)    | NO   |     |         |                |
-| url   |             | varbinary(255) | NO   |     |         |                |
+| Field | Description                                      | Type             | Null | Key | Default | Extra          |
+| ----- | ------------------------------------------------ | ---------------- | ---- | --- | ------- | -------------- |
+| id    |                                                  | int unsigned     | NO   | PRI | NULL    | auto_increment |
+| name  |                                                  | varchar(96)      | NO   |     |         |                |
+| url   |                                                  | varbinary(255)   | NO   |     |         |                |
+| type  | Type of the tag (Unknown, Collection or Account) | tinyint unsigned | YES  |     | NULL    |                |
 
 Indexes
 ------------
index 1cc48bd2f7dca5572e2cafa9517d42c335e60ae2..b86c5e1cdc162328fe78518f5facbbe88adc1dc1 100644 (file)
@@ -29,6 +29,7 @@ use Friendica\Core\System;
 use Friendica\Database\Database;
 use Friendica\Database\DBA;
 use Friendica\DI;
+use Friendica\Protocol\ActivityPub;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Strings;
 
@@ -57,6 +58,9 @@ class Tag
        const BTO = 12;
        const BCC = 13;
 
+       const ACCOUNT    = 1;
+       const COLLECTION = 2;
+
        const TAG_CHARACTER = [
                self::HASHTAG           => '#',
                self::MENTION           => '@',
@@ -71,8 +75,9 @@ class Tag
         * @param integer $type
         * @param string  $name
         * @param string  $url
+        * @param integer $target
         */
-       public static function store(int $uriid, int $type, string $name, string $url = '')
+       public static function store(int $uriid, int $type, string $name, string $url = '', int $target = null)
        {
                if ($type == self::HASHTAG) {
                        // Trim Unicode non-word characters
@@ -108,11 +113,14 @@ class Tag
                        Logger::debug('Got id for contact', ['cid' => $cid, 'url' => $url]);
 
                        if (empty($cid)) {
-                               // The contact wasn't found in the system (most likely some dead account)
-                               // We ensure that we only store a single entry by overwriting the previous name
-                               Logger::info('URL is not a known contact, updating tag', ['url' => $url, 'name' => $name]);
-                               if (!DBA::exists('tag', ['name' => substr($name, 0, 96), 'url' => $url])) {
-                                       DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
+                               $tag = DBA::selectFirst('tag', ['name', 'type'], ['url' => $url]);
+                               if (!empty($tag)) {
+                                       if ($tag['name'] != substr($name, 0, 96)) {
+                                               DBA::update('tag', ['name' => substr($name, 0, 96)], ['url' => $url]);
+                                       }
+                                       if (!empty($target) && ($tag['type'] != $target)) {
+                                               DBA::update('tag', ['type' => $target], ['url' => $url]);
+                                       }
                                }
                        }
                }
@@ -126,7 +134,7 @@ class Tag
                                }
                        }
 
-                       $tagid = self::getID($name, $url);
+                       $tagid = self::getID($name, $url, $target);
                        if (empty($tagid)) {
                                return;
                        }
@@ -148,17 +156,66 @@ class Tag
                Logger::info('Stored tag/mention', ['uri-id' => $uriid, 'tag-id' => $tagid, 'contact-id' => $cid, 'name' => $name, 'type' => $type, 'callstack' => System::callstack(8)]);
        }
 
+       public static function getTargetType(string $url)
+       {
+               if (empty($url)) {
+                       return null;
+               }
+
+               $tag = DBA::selectFirst('tag', ['url', 'type'], ['url' => $url]);
+               if (!empty($tag['type'])) {
+                       Logger::debug('Found existing type', ['type' => $tag['type'], 'url' => $url]);
+                       return $tag['type'];
+               }
+
+               $target = null;
+
+               if ($url == ActivityPub::PUBLIC_COLLECTION) {
+                       $target = Tag::COLLECTION;
+                       Logger::debug('Public collection', ['url' => $url]);
+               } else {
+                       if (DBA::exists('apcontact', ['followers' => $url])) {
+                               $target = Tag::COLLECTION;
+                               Logger::debug('Found collection via existing apcontact', ['url' => $url]);
+                       } elseif (Contact::getIdForURL($url, 0)) {
+                               $target = Tag::ACCOUNT;
+                               Logger::debug('URL is an account', ['url' => $url]);
+                       } else {
+                               $content = ActivityPub::fetchContent($url);
+                               if (!empty($content['type']) && ($content['type'] == 'OrderedCollection')) {
+                                       $target = Tag::COLLECTION;
+                                       Logger::debug('URL is an ordered collection', ['url' => $url]);
+                               }
+                       }
+               }
+
+               if (!empty($target) && !empty($tag['url']) && empty($tag['type'])) {
+                       DBA::update('tag', ['type' => $target], ['url' => $url]);
+               }
+
+               if (empty($target)) {
+                       Logger::debug('No type could be detected', ['url' => $url]);
+               }
+
+               return $target;
+       }
+
        /**
         * Get a tag id for a given tag name and url
         *
         * @param string $name
         * @param string $url
+        * @param int    $type
         * @return void
         */
-       public static function getID(string $name, string $url = '')
+       public static function getID(string $name, string $url = '', int $type = null)
        {
                $fields = ['name' => substr($name, 0, 96), 'url' => $url];
 
+               if (!empty($type)) {
+                       $fields['type'] = $type;
+               }
+
                $tag = DBA::selectFirst('tag', ['id'], $fields);
                if (DBA::isResult($tag)) {
                        return $tag['id'];
@@ -373,7 +430,7 @@ class Tag
        public static function getByURIId(int $uri_id, array $type = [self::HASHTAG, self::MENTION, self::EXCLUSIVE_MENTION, self::IMPLICIT_MENTION])
        {
                $condition = ['uri-id' => $uri_id, 'type' => $type];
-               return DBA::selectToArray('tag-view', ['type', 'name', 'url'], $condition);
+               return DBA::selectToArray('tag-view', ['type', 'name', 'url', 'tag-type'], $condition);
        }
 
        /**
index 58b6df086d1f6ad01fcb753a3c0b2c0879dfd39a..a58465b40ecc2fdd0684eb964920d8958a2c684d 100644 (file)
@@ -200,6 +200,8 @@ class PermissionTooltip extends \Friendica\BaseModule
                                        $receivers[$receiver['type']][] = DI::l10n()->t('Followers (%s)', $apcontact['name']);
                                } elseif ($apcontact = APContact::getByURL($receiver['url'], false)) {
                                        $receivers[$receiver['type']][] = $apcontact['name'];
+                               } elseif ($receiver['tag-type'] == Tag::COLLECTION) {
+                                       $receivers[$receiver['type']][] = DI::l10n()->t('Collection (%s)', $receiver['name']);
                                } else {
                                        $receivers[$receiver['type']][] = $receiver['name'];
                                }
index be17ea469c55c9959e2eaa2868f522aeaa4a0861..007144265dcec0dadbf96d7941fd0ddadb6b3311 100644 (file)
@@ -941,7 +941,10 @@ class Processor
                                        } else {
                                                $name = trim(parse_url($receiver, PHP_URL_PATH), '/');
                                        }
-                                       Tag::store($uriid, $type, $name, $receiver);
+
+                                       $target = Tag::getTargetType($receiver);
+                                       Logger::debug('Got target type', ['type' => $target, 'url' => $receiver]);
+                                       Tag::store($uriid, $type, $name, $receiver, $target);
                                }
                        }
                }
index 8c2f4dcf907d45456e0ab2223aac5589cbb12264..833d7da9ca498739c5c0bf52bb923bbc65feeb73 100644 (file)
@@ -55,7 +55,7 @@
 use Friendica\Database\DBA;
 
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1458);
+       define('DB_UPDATE_VERSION', 1459);
 }
 
 return [
@@ -289,7 +289,8 @@ return [
                "fields" => [
                        "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => ""],
                        "name" => ["type" => "varchar(96)", "not null" => "1", "default" => "", "comment" => ""],
-                       "url" => ["type" => "varbinary(255)", "not null" => "1", "default" => "", "comment" => ""]
+                       "url" => ["type" => "varbinary(255)", "not null" => "1", "default" => "", "comment" => ""],
+                       "type" => ["type" => "tinyint unsigned", "comment" => "Type of the tag (Unknown, Collection or Account)"],
                ],
                "indexes" => [
                        "PRIMARY" => ["id"],
index 82ccaa9d1259c12eaf92647c9bf1ee232b3ed0d7..04fc406b437a97560b4b46fa1977025395934d2c 100644 (file)
                        "cid" => ["post-tag", "cid"],
                        "name" => "CASE `cid` WHEN 0 THEN `tag`.`name` ELSE `contact`.`name` END",
                        "url" => "CASE `cid` WHEN 0 THEN `tag`.`url` ELSE `contact`.`url` END",
+                       "tag-type" => "CASE `cid` WHEN 0 THEN `tag`.`type` ELSE 1 END",
                ],
                "query" => "FROM `post-tag`
                        LEFT JOIN `tag` ON `post-tag`.`tid` = `tag`.`id`
index 8057f6b2e1bc551b468109cc40c2993c02666636..2182a7c75bb223466fa14bb6672580ec0a9ed640 100644 (file)
@@ -8,7 +8,7 @@ msgid ""
 msgstr ""
 "Project-Id-Version: 2022.05-dev\n"
 "Report-Msgid-Bugs-To: \n"
-"POT-Creation-Date: 2022-04-20 20:58+0000\n"
+"POT-Creation-Date: 2022-04-23 08:49+0000\n"
 "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
 "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
 "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -8588,22 +8588,27 @@ msgstr ""
 msgid "Followers (%s)"
 msgstr ""
 
-#: src/Module/PermissionTooltip.php:216
+#: src/Module/PermissionTooltip.php:204
+#, php-format
+msgid "Collection (%s)"
+msgstr ""
+
+#: src/Module/PermissionTooltip.php:218
 #, php-format
 msgid "%d more"
 msgstr ""
 
-#: src/Module/PermissionTooltip.php:220
+#: src/Module/PermissionTooltip.php:222
 #, php-format
 msgid "<b>To:</b> %s<br>"
 msgstr ""
 
-#: src/Module/PermissionTooltip.php:223
+#: src/Module/PermissionTooltip.php:225
 #, php-format
 msgid "<b>CC:</b> %s<br>"
 msgstr ""
 
-#: src/Module/PermissionTooltip.php:226
+#: src/Module/PermissionTooltip.php:228
 #, php-format
 msgid "<b>BCC:</b> %s<br>"
 msgstr ""