]> git.mxchange.org Git - friendica.git/blob - src/Content/Conversation/Repository/UserDefinedChannel.php
Merge pull request #13785 from foss-/patch-11
[friendica.git] / src / Content / Conversation / Repository / UserDefinedChannel.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2024, 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\Content\Conversation\Repository;
23
24 use Friendica\BaseCollection;
25 use Friendica\Content\Conversation\Collection\UserDefinedChannels;
26 use Friendica\Content\Conversation\Entity;
27 use Friendica\Content\Conversation\Factory;
28 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
29 use Friendica\Database\Database;
30 use Friendica\Model\Post\Engagement;
31 use Friendica\Model\User;
32 use Psr\Log\LoggerInterface;
33
34 class UserDefinedChannel extends \Friendica\BaseRepository
35 {
36         protected static $table_name = 'channel';
37
38         /** @var IManagePersonalConfigValues */
39         private $pConfig;
40
41         public function __construct(Database $database, LoggerInterface $logger, Factory\UserDefinedChannel $factory, IManagePersonalConfigValues $pConfig)
42         {
43                 parent::__construct($database, $logger, $factory);
44
45                 $this->pConfig = $pConfig;
46         }
47
48         /**
49          * @param array $condition
50          * @param array $params
51          * @return UserDefinedChannels
52          * @throws \Exception
53          */
54         protected function _select(array $condition, array $params = []): BaseCollection
55         {
56                 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
57
58                 $Entities = new UserDefinedChannels();
59                 foreach ($rows as $fields) {
60                         $Entities[] = $this->factory->createFromTableRow($fields);
61                 }
62
63                 return $Entities;
64         }
65
66         /**
67          * Fetch a single user channel
68          *
69          * @param int $id  The id of the user defined channel
70          * @param int $uid The user that this channel belongs to. (Not part of the primary key)
71          * @return Entity\UserDefinedChannel
72          * @throws \Friendica\Network\HTTPException\NotFoundException
73          */
74         public function selectById(int $id, int $uid): Entity\UserDefinedChannel
75         {
76                 return $this->_selectOne(['id' => $id, 'uid' => $uid]);
77         }
78
79         /**
80          * Checks if the provided channel id exists for this user
81          *
82          * @param integer $id
83          * @param integer $uid
84          * @return boolean
85          */
86         public function existsById(int $id, int $uid): bool
87         {
88                 return $this->exists(['id' => $id, 'uid' => $uid]);
89         }
90
91         /**
92          * Delete the given channel
93          *
94          * @param integer $id
95          * @param integer $uid
96          * @return boolean
97          */
98         public function deleteById(int $id, int $uid): bool
99         {
100                 return $this->db->delete(self::$table_name, ['id' => $id, 'uid' => $uid]);
101         }
102
103         /**
104          * Fetch all user channels
105          *
106          * @param integer $uid
107          * @return UserDefinedChannels
108          * @throws \Exception
109          */
110         public function selectByUid(int $uid): UserDefinedChannels
111         {
112                 return $this->_select(['uid' => $uid]);
113         }
114
115         public function save(Entity\UserDefinedChannel $Channel): Entity\UserDefinedChannel
116         {
117                 $fields = [
118                         'label'            => $Channel->label,
119                         'description'      => $Channel->description,
120                         'access-key'       => $Channel->accessKey,
121                         'uid'              => $Channel->uid,
122                         'circle'           => $Channel->circle,
123                         'include-tags'     => $Channel->includeTags,
124                         'exclude-tags'     => $Channel->excludeTags,
125                         'full-text-search' => $Channel->fullTextSearch,
126                         'media-type'       => $Channel->mediaType,
127                 ];
128
129                 if ($Channel->code) {
130                         $this->db->update(self::$table_name, $fields, ['uid' => $Channel->uid, 'id' => $Channel->code]);
131                 } else {
132                         $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
133
134                         $newChannelId = $this->db->lastInsertId();
135
136                         $Channel = $this->selectById($newChannelId, $Channel->uid);
137                 }
138
139                 return $Channel;
140         }
141
142         /**
143          * Checks, if one of the user defined channels matches with the given search text
144          * @todo To increase the performance, this functionality should be replaced with a single SQL call.
145          *
146          * @param string $searchtext
147          * @param string $language
148          * @return boolean
149          */
150         public function match(string $searchtext, string $language): bool
151         {
152                 if (!in_array($language, User::getLanguages())) {
153                         $this->logger->debug('Unwanted language found. No matched channel found.', ['language' => $language, 'searchtext' => $searchtext]);
154                         return false;
155                 }
156
157                 $store = false;
158                 $this->db->insert('check-full-text-search', ['pid' => getmypid(), 'searchtext' => $searchtext], Database::INSERT_UPDATE);
159                 $channels = $this->db->select(self::$table_name, ['full-text-search', 'uid', 'label'], ["`full-text-search` != ? AND `circle` = ?", '', 0]);
160                 while ($channel = $this->db->fetch($channels)) {
161                         $channelsearchtext = $channel['full-text-search'];
162                         foreach (Engagement::KEYWORDS as $keyword) {
163                                 $channelsearchtext = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $channelsearchtext);
164                         }
165                         if ($this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $channelsearchtext])) {
166                                 if (in_array($language, $this->pConfig->get($channel['uid'], 'channel', 'languages', [User::getLanguageCode($channel['uid'])]))) {
167                                         $store = true;
168                                         $this->logger->debug('Matching channel found.', ['uid' => $channel['uid'], 'label' => $channel['label'], 'language' => $language, 'channelsearchtext' => $channelsearchtext, 'searchtext' => $searchtext]);
169                                         break;
170                                 }
171                         }
172                 }
173                 $this->db->close($channels);
174
175                 $this->db->delete('check-full-text-search', ['pid' => getmypid()]);
176                 return $store;
177         }
178 }