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