summaryrefslogtreecommitdiffstats
path: root/lib/Db
diff options
context:
space:
mode:
authorMaxence Lange <maxence@artificial-owl.com>2018-11-29 17:28:37 -0100
committerJulius Härtl <jus@bitgrid.net>2018-11-30 11:06:13 +0100
commit06ad4230eb5343cb5895a6803a6e54acee0b8284 (patch)
tree295caed48d7a0eb683739db2dc0a99b8eaa73c82 /lib/Db
parent2a7d07cdaa74bc36e5e9dd8c229fb2c92f2e37a1 (diff)
count followers/following/posts on cache update
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
Diffstat (limited to 'lib/Db')
-rw-r--r--lib/Db/CoreRequestBuilder.php23
-rw-r--r--lib/Db/FollowsRequest.php40
-rw-r--r--lib/Db/FollowsRequestBuilder.php16
-rw-r--r--lib/Db/NotesRequest.php17
-rw-r--r--lib/Db/NotesRequestBuilder.php16
5 files changed, 112 insertions, 0 deletions
diff --git a/lib/Db/CoreRequestBuilder.php b/lib/Db/CoreRequestBuilder.php
index f6622803..130dedd7 100644
--- a/lib/Db/CoreRequestBuilder.php
+++ b/lib/Db/CoreRequestBuilder.php
@@ -196,6 +196,18 @@ class CoreRequestBuilder {
/**
+ * Limit the request to the FollowId
+ *
+ * @param IQueryBuilder $qb
+ * @param bool $accepted
+ */
+ protected function limitToAccepted(IQueryBuilder &$qb, bool $accepted) {
+ $this->limitToDBField($qb, 'accepted', ($accepted) ? '1' : '0');
+
+ }
+
+
+ /**
* Limit the request to the ServiceId
*
* @param IQueryBuilder $qb
@@ -273,6 +285,17 @@ class CoreRequestBuilder {
/**
+ * Limit the request to the url
+ *
+ * @param IQueryBuilder $qb
+ * @param string $actorId
+ */
+ protected function limitToAttributedTo(IQueryBuilder &$qb, string $actorId) {
+ $this->limitToDBField($qb, 'attributed_to', $actorId, false);
+ }
+
+
+ /**
* Limit the request to the status
*
* @param IQueryBuilder $qb
diff --git a/lib/Db/FollowsRequest.php b/lib/Db/FollowsRequest.php
index b4197b17..5b024414 100644
--- a/lib/Db/FollowsRequest.php
+++ b/lib/Db/FollowsRequest.php
@@ -31,6 +31,7 @@ declare(strict_types=1);
namespace OCA\Social\Db;
+use daita\MySmallPhpTools\Traits\TArrayTools;
use OCA\Social\Exceptions\FollowDoesNotExistException;
use OCA\Social\Model\ActivityPub\Follow;
@@ -43,6 +44,9 @@ use OCA\Social\Model\ActivityPub\Follow;
class FollowsRequest extends FollowsRequestBuilder {
+ use TArrayTools;
+
+
/**
* Insert a new Note in the database.
*
@@ -97,6 +101,42 @@ class FollowsRequest extends FollowsRequestBuilder {
/**
+ * @param string $actorId
+ *
+ * @return int
+ */
+ public function countFollowers(string $actorId): int {
+ $qb = $this->countFollowsSelectSql();
+ $this->limitToObjectId($qb, $actorId);
+ $this->limitToAccepted($qb, true);
+
+ $cursor = $qb->execute();
+ $data = $cursor->fetch();
+ $cursor->closeCursor();
+
+ return $this->getInt('count', $data, 0);
+ }
+
+
+ /**
+ * @param string $actorId
+ *
+ * @return int
+ */
+ public function countFollowing(string $actorId): int {
+ $qb = $this->countFollowsSelectSql();
+ $this->limitToActorId($qb, $actorId);
+ $this->limitToAccepted($qb, true);
+
+ $cursor = $qb->execute();
+ $data = $cursor->fetch();
+ $cursor->closeCursor();
+
+ return $this->getInt('count', $data, 0);
+ }
+
+
+ /**
* @param string $followId
*
* @return Follow[]
diff --git a/lib/Db/FollowsRequestBuilder.php b/lib/Db/FollowsRequestBuilder.php
index 1947dd4c..7a6882c4 100644
--- a/lib/Db/FollowsRequestBuilder.php
+++ b/lib/Db/FollowsRequestBuilder.php
@@ -93,6 +93,22 @@ class FollowsRequestBuilder extends CoreRequestBuilder {
/**
+ * Base of the Sql Select request for Shares
+ *
+ * @return IQueryBuilder
+ */
+ protected function countFollowsSelectSql(): IQueryBuilder {
+ $qb = $this->dbConnection->getQueryBuilder();
+ $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
+ ->from(self::TABLE_SERVER_FOLLOWS, 'f');
+
+ $this->defaultSelectAlias = 'f';
+
+ return $qb;
+ }
+
+
+ /**
* Base of the Sql Delete request
*
* @return IQueryBuilder
diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php
index e2c7d9c8..f4bf6842 100644
--- a/lib/Db/NotesRequest.php
+++ b/lib/Db/NotesRequest.php
@@ -139,6 +139,23 @@ class NotesRequest extends NotesRequestBuilder {
/**
* @param string $actorId
+ *
+ * @return int
+ */
+ public function countNotesFromActorId(string $actorId): int {
+ $qb = $this->countNotesSelectSql();
+ $this->limitToAttributedTo($qb, $actorId);
+
+ $cursor = $qb->execute();
+ $data = $cursor->fetch();
+ $cursor->closeCursor();
+
+ return $this->getInt('count', $data, 0);
+ }
+
+
+ /**
+ * @param string $actorId
* @param int $since
* @param int $limit
*
diff --git a/lib/Db/NotesRequestBuilder.php b/lib/Db/NotesRequestBuilder.php
index 90ff24e1..e6f2cb18 100644
--- a/lib/Db/NotesRequestBuilder.php
+++ b/lib/Db/NotesRequestBuilder.php
@@ -93,6 +93,22 @@ class NotesRequestBuilder extends CoreRequestBuilder {
/**
+ * Base of the Sql Select request for Shares
+ *
+ * @return IQueryBuilder
+ */
+ protected function countNotesSelectSql(): IQueryBuilder {
+ $qb = $this->dbConnection->getQueryBuilder();
+ $qb->selectAlias($qb->createFunction('COUNT(*)'), 'count')
+ ->from(self::TABLE_SERVER_NOTES, 'sn');
+
+ $this->defaultSelectAlias = 'sn';
+
+ return $qb;
+ }
+
+
+ /**
* Base of the Sql Delete request
*
* @return IQueryBuilder