3 * @copyright Copyright (C) 2010-2024, 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\Content\Conversation\Repository;
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;
34 class UserDefinedChannel extends \Friendica\BaseRepository
36 protected static $table_name = 'channel';
38 /** @var IManagePersonalConfigValues */
41 public function __construct(Database $database, LoggerInterface $logger, Factory\UserDefinedChannel $factory, IManagePersonalConfigValues $pConfig)
43 parent::__construct($database, $logger, $factory);
45 $this->pConfig = $pConfig;
49 * @param array $condition
50 * @param array $params
51 * @return UserDefinedChannels
54 protected function _select(array $condition, array $params = []): BaseCollection
56 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
58 $Entities = new UserDefinedChannels();
59 foreach ($rows as $fields) {
60 $Entities[] = $this->factory->createFromTableRow($fields);
67 * Fetch a single user channel
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
74 public function selectById(int $id, int $uid): Entity\UserDefinedChannel
76 return $this->_selectOne(['id' => $id, 'uid' => $uid]);
80 * Checks if the provided channel id exists for this user
86 public function existsById(int $id, int $uid): bool
88 return $this->exists(['id' => $id, 'uid' => $uid]);
92 * Delete the given channel
98 public function deleteById(int $id, int $uid): bool
100 return $this->db->delete(self::$table_name, ['id' => $id, 'uid' => $uid]);
104 * Fetch all user channels
106 * @param integer $uid
107 * @return UserDefinedChannels
110 public function selectByUid(int $uid): UserDefinedChannels
112 return $this->_select(['uid' => $uid]);
115 public function save(Entity\UserDefinedChannel $Channel): Entity\UserDefinedChannel
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 'languages' => serialize($Channel->languages),
130 if ($Channel->code) {
131 $this->db->update(self::$table_name, $fields, ['uid' => $Channel->uid, 'id' => $Channel->code]);
133 $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
135 $newChannelId = $this->db->lastInsertId();
137 $Channel = $this->selectById($newChannelId, $Channel->uid);
144 * Checks, if one of the user defined channels matches with the given search text
145 * @todo To increase the performance, this functionality should be replaced with a single SQL call.
147 * @param string $searchtext
148 * @param string $language
151 public function match(string $searchtext, string $language): bool
153 if (!in_array($language, User::getLanguages())) {
154 $this->logger->debug('Unwanted language found. No matched channel found.', ['language' => $language, 'searchtext' => $searchtext]);
159 $this->db->insert('check-full-text-search', ['pid' => getmypid(), 'searchtext' => $searchtext], Database::INSERT_UPDATE);
160 $channels = $this->db->select(self::$table_name, ['full-text-search', 'uid', 'label'], ["`full-text-search` != ? AND `circle` = ?", '', 0]);
161 while ($channel = $this->db->fetch($channels)) {
162 $channelsearchtext = $channel['full-text-search'];
163 foreach (Engagement::KEYWORDS as $keyword) {
164 $channelsearchtext = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $channelsearchtext);
166 if ($this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $channelsearchtext])) {
167 if (in_array($language, $this->pConfig->get($channel['uid'], 'channel', 'languages', [User::getLanguageCode($channel['uid'])]))) {
169 $this->logger->debug('Matching channel found.', ['uid' => $channel['uid'], 'label' => $channel['label'], 'language' => $language, 'channelsearchtext' => $channelsearchtext, 'searchtext' => $searchtext]);
174 $this->db->close($channels);
176 $this->db->delete('check-full-text-search', ['pid' => getmypid()]);