]> git.mxchange.org Git - friendica.git/blob - src/Contact/LocalRelationship/Repository/LocalRelationship.php
Only attach images
[friendica.git] / src / Contact / LocalRelationship / Repository / LocalRelationship.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Contact\LocalRelationship\Repository;
23
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;
30
31 class LocalRelationship extends \Friendica\BaseRepository
32 {
33         protected static $table_name = 'user-contact';
34
35         /** @var Factory\LocalRelationship */
36         protected $factory;
37
38         public function __construct(Database $database, LoggerInterface $logger, Factory\LocalRelationship $factory)
39         {
40                 parent::__construct($database, $logger, $factory);
41         }
42
43         /**
44          * @param int $uid
45          * @param int $cid
46          * @return Entity\LocalRelationship
47          * @throws HTTPException\NotFoundException
48          */
49         public function selectForUserContact(int $uid, int $cid): Entity\LocalRelationship
50         {
51                 return $this->_selectOne(['uid' => $uid, 'cid' => $cid]);
52         }
53
54         /**
55          * Returns the existing local relationship between a user and a public contact or a default
56          * relationship if it doesn't.
57          *
58          * @param int $uid
59          * @param int $cid
60          * @return Entity\LocalRelationship
61          * @throws HTTPException\NotFoundException
62          */
63         public function getForUserContact(int $uid, int $cid): Entity\LocalRelationship
64         {
65                 try {
66                         return $this->selectForUserContact($uid, $cid);
67                 } catch (HTTPException\NotFoundException $e) {
68                         return $this->factory->createFromTableRow(['uid' => $uid, 'cid' => $cid]);
69                 }
70         }
71
72         /**
73          * @param int $uid
74          * @param int $cid
75          * @return bool
76          * @throws \Exception
77          */
78         public function existsForUserContact(int $uid, int $cid): bool
79         {
80                 return $this->exists(['uid' => $uid, 'cid' => $cid]);
81         }
82
83         /**
84          * Converts a given local relationship into a DB compatible row array
85          *
86          * @param Entity\LocalRelationship $localRelationship
87          *
88          * @return array
89          */
90         protected function convertToTableRow(Entity\LocalRelationship $localRelationship): array
91         {
92                 return [
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,
111                 ];
112         }
113
114         /**
115          * @param Entity\LocalRelationship $localRelationship
116          *
117          * @return Entity\LocalRelationship
118          *
119          * @throws Exception\LocalRelationshipPersistenceException In case the underlying storage cannot save the LocalRelationship
120          */
121         public function save(Entity\LocalRelationship $localRelationship): Entity\LocalRelationship
122         {
123                 try {
124                         $fields = $this->convertToTableRow($localRelationship);
125
126                         $this->db->insert(self::$table_name, $fields, Database::INSERT_UPDATE);
127
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);
131                 }
132         }
133 }