From a1dd299eb3b84d0521386ff928d19e7334aeeaf1 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 6 Apr 2022 13:34:11 +0000 Subject: [PATCH 1/3] Preparations for "featured" posts --- database.sql | 17 ++++++- doc/database.md | 1 + doc/database/db_post-collection.md | 29 ++++++++++++ src/Model/APContact.php | 1 - src/Model/Post/Collection.php | 61 ++++++++++++++++++++++++++ src/Protocol/ActivityPub/Processor.php | 4 +- static/dbstructure.config.php | 13 +++++- static/dbview.config.php | 4 ++ 8 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 doc/database/db_post-collection.md create mode 100644 src/Model/Post/Collection.php diff --git a/database.sql b/database.sql index 1f35cd0573..d52abf1ccb 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2022.05-dev (Siberian Iris) --- DB_UPDATE_VERSION 1455 +-- DB_UPDATE_VERSION 1456 -- ------------------------------------------ @@ -1062,6 +1062,17 @@ CREATE TABLE IF NOT EXISTS `post-category` ( FOREIGN KEY (`tid`) REFERENCES `tag` (`id`) ON UPDATE RESTRICT ON DELETE RESTRICT ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='post relation to categories'; +-- +-- TABLE post-collection +-- +CREATE TABLE IF NOT EXISTS `post-collection` ( + `uri-id` int unsigned NOT NULL COMMENT 'Id of the item-uri table entry that contains the item uri', + `type` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '', + PRIMARY KEY(`uri-id`,`type`), + INDEX `type` (`type`), + FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE +) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Collection of posts'; + -- -- TABLE post-content -- @@ -1617,6 +1628,7 @@ CREATE VIEW `post-user-view` AS SELECT `post-thread-user`.`origin` AS `parent-origin`, `post-thread-user`.`mention` AS `mention`, `post-user`.`global` AS `global`, + EXISTS(SELECT `type` FROM `post-collection` WHERE `type` = 0 AND `uri-id` = `post-user`.`uri-id`) AS `featured`, `post-user`.`network` AS `network`, `post-user`.`vid` AS `vid`, `post-user`.`psid` AS `psid`, @@ -1777,6 +1789,7 @@ CREATE VIEW `post-thread-user-view` AS SELECT `post-thread-user`.`origin` AS `origin`, `post-thread-user`.`mention` AS `mention`, `post-user`.`global` AS `global`, + EXISTS(SELECT `type` FROM `post-collection` WHERE `type` = 0 AND `uri-id` = `post-thread-user`.`uri-id`) AS `featured`, `post-thread-user`.`network` AS `network`, `post-user`.`vid` AS `vid`, `post-thread-user`.`psid` AS `psid`, @@ -1923,6 +1936,7 @@ CREATE VIEW `post-view` AS SELECT `post`.`visible` AS `visible`, `post`.`deleted` AS `deleted`, `post`.`global` AS `global`, + EXISTS(SELECT `type` FROM `post-collection` WHERE `type` = 0 AND `uri-id` = `post`.`uri-id`) AS `featured`, `post`.`network` AS `network`, `post`.`vid` AS `vid`, IF (`post`.`vid` IS NULL, '', `verb`.`name`) AS `verb`, @@ -2044,6 +2058,7 @@ CREATE VIEW `post-thread-view` AS SELECT `post`.`visible` AS `visible`, `post`.`deleted` AS `deleted`, `post`.`global` AS `global`, + EXISTS(SELECT `type` FROM `post-collection` WHERE `type` = 0 AND `uri-id` = `post-thread`.`uri-id`) AS `featured`, `post-thread`.`network` AS `network`, `post`.`vid` AS `vid`, IF (`post`.`vid` IS NULL, '', `verb`.`name`) AS `verb`, diff --git a/doc/database.md b/doc/database.md index f64434b886..d436dcc32b 100644 --- a/doc/database.md +++ b/doc/database.md @@ -47,6 +47,7 @@ Database Tables | [photo](help/database/db_photo) | photo storage | | [post](help/database/db_post) | Structure for all posts | | [post-category](help/database/db_post-category) | post relation to categories | +| [post-collection](help/database/db_post-collection) | Collection of posts | | [post-content](help/database/db_post-content) | Content for all posts | | [post-delivery-data](help/database/db_post-delivery-data) | Delivery data for items | | [post-link](help/database/db_post-link) | Post related external links | diff --git a/doc/database/db_post-collection.md b/doc/database/db_post-collection.md new file mode 100644 index 0000000000..1e8e368efd --- /dev/null +++ b/doc/database/db_post-collection.md @@ -0,0 +1,29 @@ +Table post-collection +=========== + +Collection of posts + +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 | | +| type | | tinyint unsigned | NO | PRI | 0 | | + +Indexes +------------ + +| Name | Fields | +| ------- | ------------ | +| PRIMARY | uri-id, type | +| type | type | + +Foreign Keys +------------ + +| Field | Target Table | Target Field | +|-------|--------------|--------------| +| uri-id | [item-uri](help/database/db_item-uri) | id | + +Return to [database documentation](help/database) diff --git a/src/Model/APContact.php b/src/Model/APContact.php index deed6299de..e068ecefe2 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -39,7 +39,6 @@ use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPSignature; use Friendica\Util\JsonLD; use Friendica\Util\Network; -use Friendica\Util\Strings; class APContact { diff --git a/src/Model/Post/Collection.php b/src/Model/Post/Collection.php new file mode 100644 index 0000000000..cf968d6174 --- /dev/null +++ b/src/Model/Post/Collection.php @@ -0,0 +1,61 @@ +. + * + */ + +namespace Friendica\Model\Post; + +use Friendica\Database\DBA; +use \BadMethodCallException; +use Friendica\Database\Database; + +class Collection +{ + const FEATURED = 0; + + /** + * Add a post to a collection + * + * @param integer $uri_id + * @param integer $type + */ + public static function add(int $uri_id, int $type) + { + if (empty($uri_id)) { + throw new BadMethodCallException('Empty URI_id'); + } + + DBA::insert('post-collection', ['uri-id' => $uri_id, 'type' => $type], Database::INSERT_IGNORE); + } + + /** + * Remove a post from a collection + * + * @param integer $uri_id + * @param integer $type + */ + public static function remove(int $uri_id, int $type) + { + if (empty($uri_id)) { + throw new BadMethodCallException('Empty URI_id'); + } + + DBA::delete('post-collection', ['uri-id' => $uri_id, 'type' => $type]); + } +} diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 5604043fa4..b8758a0c82 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -492,7 +492,7 @@ class Processor Logger::debug('Add post to featured collection', ['uri-id' => $uriid]); - // @todo Add functionality + Post\Collection::add($uriid, Post\Collection::FEATURED); } /** @@ -509,7 +509,7 @@ class Processor Logger::debug('Remove post from featured collection', ['uri-id' => $uriid]); - // @todo Add functionality + Post\Collection::remove($uriid, Post\Collection::FEATURED); } /** diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 95daab4649..7d3904444b 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1455); + define('DB_UPDATE_VERSION', 1456); } return [ @@ -1103,6 +1103,17 @@ return [ "uid" => ["uid"], ] ], + "post-collection" => [ + "comment" => "Collection of posts", + "fields" => [ + "uri-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"], + "type" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "primary" => "1", "comment" => ""], + ], + "indexes" => [ + "PRIMARY" => ["uri-id", "type"], + "type" => ["type"], + ] + ], "post-content" => [ "comment" => "Content for all posts", "fields" => [ diff --git a/static/dbview.config.php b/static/dbview.config.php index a5c52c9b13..f0a421484c 100644 --- a/static/dbview.config.php +++ b/static/dbview.config.php @@ -93,6 +93,7 @@ "parent-origin" => ["post-thread-user", "origin"], "mention" => ["post-thread-user", "mention"], "global" => ["post-user", "global"], + "featured" => "EXISTS(SELECT `type` FROM `post-collection` WHERE `type` = 0 AND `uri-id` = `post-user`.`uri-id`)", "network" => ["post-user", "network"], "vid" => ["post-user", "vid"], "psid" => ["post-user", "psid"], @@ -251,6 +252,7 @@ "origin" => ["post-thread-user", "origin"], "mention" => ["post-thread-user", "mention"], "global" => ["post-user", "global"], + "featured" => "EXISTS(SELECT `type` FROM `post-collection` WHERE `type` = 0 AND `uri-id` = `post-thread-user`.`uri-id`)", "network" => ["post-thread-user", "network"], "vid" => ["post-user", "vid"], "psid" => ["post-thread-user", "psid"], @@ -395,6 +397,7 @@ "visible" => ["post", "visible"], "deleted" => ["post", "deleted"], "global" => ["post", "global"], + "featured" => "EXISTS(SELECT `type` FROM `post-collection` WHERE `type` = 0 AND `uri-id` = `post`.`uri-id`)", "network" => ["post", "network"], "vid" => ["post", "vid"], "verb" => "IF (`post`.`vid` IS NULL, '', `verb`.`name`)", @@ -514,6 +517,7 @@ "visible" => ["post", "visible"], "deleted" => ["post", "deleted"], "global" => ["post", "global"], + "featured" => "EXISTS(SELECT `type` FROM `post-collection` WHERE `type` = 0 AND `uri-id` = `post-thread`.`uri-id`)", "network" => ["post-thread", "network"], "vid" => ["post", "vid"], "verb" => "IF (`post`.`vid` IS NULL, '', `verb`.`name`)", From 612e91b603a5a5817839d04d120e1cf672b8a80e Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 6 Apr 2022 15:02:21 +0000 Subject: [PATCH 2/3] Added description --- database.sql | 2 +- doc/database/db_post-collection.md | 2 +- static/dbstructure.config.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/database.sql b/database.sql index d52abf1ccb..2971c0af15 100644 --- a/database.sql +++ b/database.sql @@ -1067,7 +1067,7 @@ CREATE TABLE IF NOT EXISTS `post-category` ( -- CREATE TABLE IF NOT EXISTS `post-collection` ( `uri-id` int unsigned NOT NULL COMMENT 'Id of the item-uri table entry that contains the item uri', - `type` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '', + `type` tinyint unsigned NOT NULL DEFAULT 0 COMMENT '0 - Featured', PRIMARY KEY(`uri-id`,`type`), INDEX `type` (`type`), FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE diff --git a/doc/database/db_post-collection.md b/doc/database/db_post-collection.md index 1e8e368efd..3018067c8d 100644 --- a/doc/database/db_post-collection.md +++ b/doc/database/db_post-collection.md @@ -9,7 +9,7 @@ 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 | | -| type | | tinyint unsigned | NO | PRI | 0 | | +| type | 0 - Featured | tinyint unsigned | NO | PRI | 0 | | Indexes ------------ diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 7d3904444b..d7f2c1d00c 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -1107,7 +1107,7 @@ return [ "comment" => "Collection of posts", "fields" => [ "uri-id" => ["type" => "int unsigned", "not null" => "1", "primary" => "1", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the item uri"], - "type" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "primary" => "1", "comment" => ""], + "type" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "primary" => "1", "comment" => "0 - Featured"], ], "indexes" => [ "PRIMARY" => ["uri-id", "type"], From 5a3fe900f054b8557b53fcc047699785e2ec6d23 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 6 Apr 2022 15:32:14 +0000 Subject: [PATCH 3/3] Fix test --- src/Model/Post/Collection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Post/Collection.php b/src/Model/Post/Collection.php index cf968d6174..f7a1a996bb 100644 --- a/src/Model/Post/Collection.php +++ b/src/Model/Post/Collection.php @@ -22,7 +22,7 @@ namespace Friendica\Model\Post; use Friendica\Database\DBA; -use \BadMethodCallException; +use BadMethodCallException; use Friendica\Database\Database; class Collection