aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/KnownUser
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/KnownUser')
-rw-r--r--lib/private/KnownUser/KnownUser.php30
-rw-r--r--lib/private/KnownUser/KnownUserMapper.php73
-rw-r--r--lib/private/KnownUser/KnownUserService.php76
3 files changed, 179 insertions, 0 deletions
diff --git a/lib/private/KnownUser/KnownUser.php b/lib/private/KnownUser/KnownUser.php
new file mode 100644
index 00000000000..808c0400c6e
--- /dev/null
+++ b/lib/private/KnownUser/KnownUser.php
@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\KnownUser;
+
+use OCP\AppFramework\Db\Entity;
+
+/**
+ * @method void setKnownTo(string $knownTo)
+ * @method string getKnownTo()
+ * @method void setKnownUser(string $knownUser)
+ * @method string getKnownUser()
+ */
+class KnownUser extends Entity {
+ /** @var string */
+ protected $knownTo;
+
+ /** @var string */
+ protected $knownUser;
+
+ public function __construct() {
+ $this->addType('knownTo', 'string');
+ $this->addType('knownUser', 'string');
+ }
+}
diff --git a/lib/private/KnownUser/KnownUserMapper.php b/lib/private/KnownUser/KnownUserMapper.php
new file mode 100644
index 00000000000..4c3bbb0b436
--- /dev/null
+++ b/lib/private/KnownUser/KnownUserMapper.php
@@ -0,0 +1,73 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\KnownUser;
+
+use OCP\AppFramework\Db\QBMapper;
+use OCP\IDBConnection;
+
+/**
+ * @method KnownUser mapRowToEntity(array $row)
+ *
+ * @template-extends QBMapper<KnownUser>
+ */
+class KnownUserMapper extends QBMapper {
+ /**
+ * @param IDBConnection $db
+ */
+ public function __construct(IDBConnection $db) {
+ parent::__construct($db, 'known_users', KnownUser::class);
+ }
+
+ /**
+ * @param string $knownTo
+ * @return int Number of deleted entities
+ */
+ public function deleteKnownTo(string $knownTo): int {
+ $query = $this->db->getQueryBuilder();
+ $query->delete($this->getTableName())
+ ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
+
+ return $query->executeStatement();
+ }
+
+ /**
+ * @param string $knownUser
+ * @return int Number of deleted entities
+ */
+ public function deleteKnownUser(string $knownUser): int {
+ $query = $this->db->getQueryBuilder();
+ $query->delete($this->getTableName())
+ ->where($query->expr()->eq('known_user', $query->createNamedParameter($knownUser)));
+
+ return $query->executeStatement();
+ }
+
+ /**
+ * Returns all "known users" for the given "known to" user
+ *
+ * @param string $knownTo
+ * @return KnownUser[]
+ */
+ public function getKnownUsers(string $knownTo): array {
+ $query = $this->db->getQueryBuilder();
+ $query->select('*')
+ ->from($this->getTableName())
+ ->where($query->expr()->eq('known_to', $query->createNamedParameter($knownTo)));
+
+ return $this->findEntities($query);
+ }
+
+ public function createKnownUserFromRow(array $row): KnownUser {
+ return $this->mapRowToEntity([
+ 'id' => $row['s_id'],
+ 'known_to' => $row['known_to'],
+ 'known_user' => $row['known_user'],
+ ]);
+ }
+}
diff --git a/lib/private/KnownUser/KnownUserService.php b/lib/private/KnownUser/KnownUserService.php
new file mode 100644
index 00000000000..cd257cf1b12
--- /dev/null
+++ b/lib/private/KnownUser/KnownUserService.php
@@ -0,0 +1,76 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\KnownUser;
+
+class KnownUserService {
+ /** @var KnownUserMapper */
+ protected $mapper;
+ /** @var array */
+ protected $knownUsers = [];
+
+ public function __construct(KnownUserMapper $mapper) {
+ $this->mapper = $mapper;
+ }
+
+ /**
+ * Delete all matches where the given user is the owner of the phonebook
+ *
+ * @param string $knownTo
+ * @return int Number of deleted matches
+ */
+ public function deleteKnownTo(string $knownTo): int {
+ return $this->mapper->deleteKnownTo($knownTo);
+ }
+
+ /**
+ * Delete all matches where the given user is the one in the phonebook
+ *
+ * @param string $contactUserId
+ * @return int Number of deleted matches
+ */
+ public function deleteByContactUserId(string $contactUserId): int {
+ return $this->mapper->deleteKnownUser($contactUserId);
+ }
+
+ /**
+ * Store a match because $knownTo has $contactUserId in their phonebook
+ *
+ * @param string $knownTo User id of the owner of the phonebook
+ * @param string $contactUserId User id of the contact in the phonebook
+ */
+ public function storeIsKnownToUser(string $knownTo, string $contactUserId): void {
+ $entity = new KnownUser();
+ $entity->setKnownTo($knownTo);
+ $entity->setKnownUser($contactUserId);
+ $this->mapper->insert($entity);
+ }
+
+ /**
+ * Check if $contactUserId is in the phonebook of $knownTo
+ *
+ * @param string $knownTo User id of the owner of the phonebook
+ * @param string $contactUserId User id of the contact in the phonebook
+ * @return bool
+ */
+ public function isKnownToUser(string $knownTo, string $contactUserId): bool {
+ if ($knownTo === $contactUserId) {
+ return true;
+ }
+
+ if (!isset($this->knownUsers[$knownTo])) {
+ $entities = $this->mapper->getKnownUsers($knownTo);
+ $this->knownUsers[$knownTo] = [];
+ foreach ($entities as $entity) {
+ $this->knownUsers[$knownTo][$entity->getKnownUser()] = true;
+ }
+ }
+
+ return isset($this->knownUsers[$knownTo][$contactUserId]);
+ }
+}