From 178bc543e3c9da9092115a85664ae87570894cce Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 8 Jul 2021 17:32:41 +0000 Subject: [PATCH] New founction to count threads --- database.sql | 1 + doc/database/db_post-thread-user.md | 35 +++++++++++++++-------------- src/Model/Post.php | 21 +++++++++++++++++ src/Module/BaseApi.php | 6 ++--- static/dbstructure.config.php | 1 + 5 files changed, 44 insertions(+), 20 deletions(-) diff --git a/database.sql b/database.sql index 69ae84696e..160d59aae1 100644 --- a/database.sql +++ b/database.sql @@ -1294,6 +1294,7 @@ CREATE TABLE IF NOT EXISTS `post-thread-user` ( INDEX `post-user-id` (`post-user-id`), INDEX `commented` (`commented`), INDEX `uid_received` (`uid`,`received`), + INDEX `uid_wall_received` (`uid`,`wall`,`received`), INDEX `uid_pinned` (`uid`,`pinned`), INDEX `uid_commented` (`uid`,`commented`), INDEX `uid_starred` (`uid`,`starred`), diff --git a/doc/database/db_post-thread-user.md b/doc/database/db_post-thread-user.md index 3efb5a77af..7307dc78d6 100644 --- a/doc/database/db_post-thread-user.md +++ b/doc/database/db_post-thread-user.md @@ -35,23 +35,24 @@ Fields Indexes ------------ -| Name | Fields | -| ------------- | -------------- | -| PRIMARY | uid, uri-id | -| uri-id | uri-id | -| owner-id | owner-id | -| author-id | author-id | -| causer-id | causer-id | -| uid | uid | -| contact-id | contact-id | -| psid | psid | -| post-user-id | post-user-id | -| commented | commented | -| uid_received | uid, received | -| uid_pinned | uid, pinned | -| uid_commented | uid, commented | -| uid_starred | uid, starred | -| uid_mention | uid, mention | +| Name | Fields | +| ----------------- | ------------------- | +| PRIMARY | uid, uri-id | +| uri-id | uri-id | +| owner-id | owner-id | +| author-id | author-id | +| causer-id | causer-id | +| uid | uid | +| contact-id | contact-id | +| psid | psid | +| post-user-id | post-user-id | +| commented | commented | +| uid_received | uid, received | +| uid_wall_received | uid, wall, received | +| uid_pinned | uid, pinned | +| uid_commented | uid, commented | +| uid_starred | uid, starred | +| uid_mention | uid, mention | Foreign Keys ------------ diff --git a/src/Model/Post.php b/src/Model/Post.php index 902525f992..7b77276de8 100644 --- a/src/Model/Post.php +++ b/src/Model/Post.php @@ -156,6 +156,27 @@ class Post return DBA::count('post-user-view', $condition, $params); } + /** + * Counts the post-thread-user-view records satisfying the provided condition + * + * @param array $condition array of fields for condition + * @param array $params Array of several parameters + * + * @return int + * + * Example: + * $condition = ["uid" => 1, "network" => 'dspr']; + * or: + * $condition = ["`uid` = ? AND `network` IN (?, ?)", 1, 'dfrn', 'dspr']; + * + * $count = Post::count($condition); + * @throws \Exception + */ + public static function countThread(array $condition = [], array $params = []) + { + return DBA::count('post-thread-user-view', $condition, $params); + } + /** * Counts the post-view records satisfying the provided condition * diff --git a/src/Module/BaseApi.php b/src/Module/BaseApi.php index d2aa9662c1..9d5c36f423 100644 --- a/src/Module/BaseApi.php +++ b/src/Module/BaseApi.php @@ -294,7 +294,7 @@ class BaseApi extends BaseModule $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60); $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom]; - $posts_day = Post::count($condition); + $posts_day = Post::countThread($condition); if ($posts_day > $throttle_day) { Logger::info('Daily posting limit reached', ['uid' => $uid, 'posts' => $posts_day, 'limit' => $throttle_day]); @@ -310,7 +310,7 @@ class BaseApi extends BaseModule $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7); $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom]; - $posts_week = Post::count($condition); + $posts_week = Post::countThread($condition); if ($posts_week > $throttle_week) { Logger::info('Weekly posting limit reached', ['uid' => $uid, 'posts' => $posts_week, 'limit' => $throttle_week]); @@ -326,7 +326,7 @@ class BaseApi extends BaseModule $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30); $condition = ["`gravity` = ? AND `uid` = ? AND `wall` AND `received` > ?", GRAVITY_PARENT, $uid, $datefrom]; - $posts_month = Post::count($condition); + $posts_month = Post::countThread($condition); if ($posts_month > $throttle_month) { Logger::info('Monthly posting limit reached', ['uid' => $uid, 'posts' => $posts_month, 'limit' => $throttle_month]); diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index f302f1ecc4..7821774e75 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -1326,6 +1326,7 @@ return [ "post-user-id" => ["post-user-id"], "commented" => ["commented"], "uid_received" => ["uid", "received"], + "uid_wall_received" => ["uid", "wall", "received"], "uid_pinned" => ["uid", "pinned"], "uid_commented" => ["uid", "commented"], "uid_starred" => ["uid", "starred"],