3 * @copyright Copyright (C) 2010-2023, 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\User;
31 use Psr\Log\LoggerInterface;
33 class UserDefinedChannel extends \Friendica\BaseRepository
35 protected static $table_name = 'channel';
37 /** @var IManagePersonalConfigValues */
40 public function __construct(Database $database, LoggerInterface $logger, Factory\UserDefinedChannel $factory, IManagePersonalConfigValues $pConfig)
42 parent::__construct($database, $logger, $factory);
44 $this->pConfig = $pConfig;
48 * @param array $condition
49 * @param array $params
50 * @return UserDefinedChannels
53 protected function _select(array $condition, array $params = []): BaseCollection
55 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
57 $Entities = new UserDefinedChannels();
58 foreach ($rows as $fields) {
59 $Entities[] = $this->factory->createFromTableRow($fields);
66 * Fetch a single user channel
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
73 public function selectById(int $id, int $uid): Entity\UserDefinedChannel
75 return $this->_selectOne(['id' => $id, 'uid' => $uid]);
79 * Checks if the provided channel id exists for this user
85 public function existsById(int $id, int $uid): bool
87 return $this->exists(['id' => $id, 'uid' => $uid]);
91 * Delete the given channel
97 public function deleteById(int $id, int $uid): bool
99 return $this->db->delete(self::$table_name, ['id' => $id, 'uid' => $uid]);
103 * Fetch all user channels
105 * @param integer $uid
106 * @return UserDefinedChannels
109 public function selectByUid(int $uid): UserDefinedChannels
111 return $this->_select(['uid' => $uid]);
114 public function save(Entity\UserDefinedChannel $Channel): Entity\UserDefinedChannel
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,
128 if ($Channel->code) {
129 $this->db->update(self::$table_name, $fields, ['uid' => $Channel->uid, 'id' => $Channel->code]);
131 $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
133 $newChannelId = $this->db->lastInsertId();
135 $Channel = $this->selectById($newChannelId, $Channel->uid);
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.
145 * @param string $searchtext
146 * @param string $language
149 public function match(string $searchtext, string $language): bool
151 if (!in_array($language, User::getLanguages())) {
152 $this->logger->debug('Unwanted language found. No matched channel found.', ['language' => $language, 'searchtext' => $searchtext]);
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);
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'])]))) {
167 $this->logger->debug('Matching channel found.', ['uid' => $channel['uid'], 'label' => $channel['label'], 'language' => $language, 'channelsearchtext' => $channelsearchtext, 'searchtext' => $searchtext]);
172 $this->db->close($channels);
174 $this->db->delete('check-full-text-search', ['pid' => getmypid()]);