3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Contact\LocalRelationship\Repository;
24 use Friendica\Contact\LocalRelationship\Entity;
25 use Friendica\Contact\LocalRelationship\Exception;
26 use Friendica\Contact\LocalRelationship\Factory;
27 use Friendica\Database\Database;
28 use Friendica\Network\HTTPException;
29 use Psr\Log\LoggerInterface;
31 class LocalRelationship extends \Friendica\BaseRepository
33 protected static $table_name = 'user-contact';
35 /** @var Factory\LocalRelationship */
38 public function __construct(Database $database, LoggerInterface $logger, Factory\LocalRelationship $factory)
40 parent::__construct($database, $logger, $factory);
46 * @return Entity\LocalRelationship
47 * @throws HTTPException\NotFoundException
49 public function selectForUserContact(int $uid, int $cid): Entity\LocalRelationship
51 return $this->_selectOne(['uid' => $uid, 'cid' => $cid]);
55 * Returns the existing local relationship between a user and a public contact or a default
56 * relationship if it doesn't.
60 * @return Entity\LocalRelationship
61 * @throws HTTPException\NotFoundException
63 public function getForUserContact(int $uid, int $cid): Entity\LocalRelationship
66 return $this->selectForUserContact($uid, $cid);
67 } catch (HTTPException\NotFoundException $e) {
68 return $this->factory->createFromTableRow(['uid' => $uid, 'cid' => $cid]);
78 public function existsForUserContact(int $uid, int $cid): bool
80 return $this->exists(['uid' => $uid, 'cid' => $cid]);
84 * Converts a given local relationship into a DB compatible row array
86 * @param Entity\LocalRelationship $localRelationship
90 protected function convertToTableRow(Entity\LocalRelationship $localRelationship): array
93 'uid' => $localRelationship->userId,
94 'cid' => $localRelationship->contactId,
95 'uri-id' => $localRelationship->uriId,
96 'blocked' => $localRelationship->blocked,
97 'ignored' => $localRelationship->ignored,
98 'collapsed' => $localRelationship->collapsed,
99 'pending' => $localRelationship->pending,
100 'rel' => $localRelationship->rel,
101 'info' => $localRelationship->info,
102 'notify_new_posts' => $localRelationship->notifyNewPosts,
103 'remote_self' => $localRelationship->isRemoteSelf,
104 'fetch_further_information' => $localRelationship->fetchFurtherInformation,
105 'ffi_keyword_denylist' => $localRelationship->ffiKeywordDenylist,
106 'subhub' => $localRelationship->subhub,
107 'hub-verify' => $localRelationship->hubVerify,
108 'protocol' => $localRelationship->protocol,
109 'rating' => $localRelationship->rating,
110 'priority' => $localRelationship->priority,
115 * @param Entity\LocalRelationship $localRelationship
117 * @return Entity\LocalRelationship
119 * @throws Exception\LocalRelationshipPersistenceException In case the underlying storage cannot save the LocalRelationship
121 public function save(Entity\LocalRelationship $localRelationship): Entity\LocalRelationship
124 $fields = $this->convertToTableRow($localRelationship);
126 $this->db->insert(self::$table_name, $fields, Database::INSERT_UPDATE);
128 return $localRelationship;
129 } catch (\Exception $exception) {
130 throw new Exception\LocalRelationshipPersistenceException(sprintf('Cannot insert/update the local relationship %d for user %d', $localRelationship->contactId, $localRelationship->userId), $exception);