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\Config\Capability\IManageConfigValues;
29 use Friendica\Database\Database;
30 use Friendica\Database\DBA;
31 use Friendica\Model\Contact;
32 use Friendica\Model\Post\Engagement;
33 use Friendica\Model\User;
34 use Friendica\Util\DateTimeFormat;
35 use Psr\Log\LoggerInterface;
37 class UserDefinedChannel extends \Friendica\BaseRepository
39 protected static $table_name = 'channel';
41 /** @var IManageConfigValues */
44 public function __construct(Database $database, LoggerInterface $logger, Factory\UserDefinedChannel $factory, IManageConfigValues $config)
46 parent::__construct($database, $logger, $factory);
48 $this->config = $config;
52 * @param array $condition
53 * @param array $params
54 * @return UserDefinedChannels
57 protected function _select(array $condition, array $params = []): BaseCollection
59 $rows = $this->db->selectToArray(static::$table_name, [], $condition, $params);
61 $Entities = new UserDefinedChannels();
62 foreach ($rows as $fields) {
63 $Entities[] = $this->factory->createFromTableRow($fields);
69 public function select(array $condition, array $params = []): BaseCollection
71 return $this->_select($condition, $params);
75 * Fetch a single user channel
77 * @param int $id The id of the user defined channel
78 * @param int $uid The user that this channel belongs to. (Not part of the primary key)
79 * @return Entity\UserDefinedChannel
80 * @throws \Friendica\Network\HTTPException\NotFoundException
82 public function selectById(int $id, int $uid): Entity\UserDefinedChannel
84 return $this->_selectOne(['id' => $id, 'uid' => $uid]);
88 * Checks if the provided channel id exists for this user
94 public function existsById(int $id, int $uid): bool
96 return $this->exists(['id' => $id, 'uid' => $uid]);
100 * Delete the given channel
103 * @param integer $uid
106 public function deleteById(int $id, int $uid): bool
108 return $this->db->delete(self::$table_name, ['id' => $id, 'uid' => $uid]);
112 * Fetch all user channels
114 * @param integer $uid
115 * @return UserDefinedChannels
118 public function selectByUid(int $uid): UserDefinedChannels
120 return $this->_select(['uid' => $uid]);
123 public function save(Entity\UserDefinedChannel $Channel): Entity\UserDefinedChannel
126 'label' => $Channel->label,
127 'description' => $Channel->description,
128 'access-key' => $Channel->accessKey,
129 'uid' => $Channel->uid,
130 'circle' => $Channel->circle,
131 'include-tags' => $Channel->includeTags,
132 'exclude-tags' => $Channel->excludeTags,
133 'full-text-search' => $Channel->fullTextSearch,
134 'media-type' => $Channel->mediaType,
135 'languages' => serialize($Channel->languages),
138 if ($Channel->code) {
139 $this->db->update(self::$table_name, $fields, ['uid' => $Channel->uid, 'id' => $Channel->code]);
141 $this->db->insert(self::$table_name, $fields, Database::INSERT_IGNORE);
143 $newChannelId = $this->db->lastInsertId();
145 $Channel = $this->selectById($newChannelId, $Channel->uid);
152 * Checks, if one of the user defined channels matches with the given search text
153 * @todo To increase the performance, this functionality should be replaced with a single SQL call.
155 * @param string $searchtext
156 * @param string $language
158 * @param int $media_type
161 public function match(string $searchtext, string $language, array $tags, int $media_type): bool
163 $condition = ["`verified` AND NOT `blocked` AND NOT `account_removed` AND NOT `account_expired` AND `user`.`uid` > ?", 0];
165 $abandon_days = intval($this->config->get('system', 'account_abandon_days'));
166 if (!empty($abandon_days)) {
167 $condition = DBA::mergeConditions($condition, ["`last-activity` > ?", DateTimeFormat::utc('now - ' . $abandon_days . ' days')]);
170 $users = $this->db->selectToArray('user', ['uid'], $condition);
175 return !empty($this->getMatches($searchtext, $language, $tags, $media_type, 0, array_column($users, 'uid'), false));
179 * Fetch the channel users that have got matching channels
181 * @param string $searchtext
182 * @param string $language
184 * @param int $media_type
185 * @param int $owner_id
188 public function getMatchingChannelUsers(string $searchtext, string $language, array $tags, int $media_type, int $owner_id): array
190 $users = $this->db->selectToArray('user', ['uid'], ["`account-type` = ? AND `uid` != ?", User::ACCOUNT_TYPE_RELAY, 0]);
194 return $this->getMatches($searchtext, $language, $tags, $media_type, $owner_id, array_column($users, 'uid'), true);
197 private function getMatches(string $searchtext, string $language, array $tags, int $media_type, int $owner_id, array $channelUids, bool $relayMode): array
199 if (!in_array($language, User::getLanguages())) {
200 $this->logger->debug('Unwanted language found. No matched channel found.', ['language' => $language, 'searchtext' => $searchtext]);
204 $this->db->insert('check-full-text-search', ['pid' => getmypid(), 'searchtext' => $searchtext], Database::INSERT_UPDATE);
208 $condition = ['uid' => $channelUids];
210 $condition = DBA::mergeConditions($condition, ["`full-text-search` != ?", '']);
213 foreach ($this->select($condition) as $channel) {
214 if (in_array($channel->uid, $uids)) {
217 if (!empty($channel->circle) && ($channel->circle > 0) && !in_array($channel->uid, $uids)) {
218 $account = Contact::selectFirstAccountUser(['id'], ['pid' => $owner_id, 'uid' => $channel->uid]);
219 if (empty($account['id']) || !$this->db->exists('group_member', ['gid' => $channel->circle, 'contact-id' => $account['id']])) {
223 if (!empty($channel->languages) && !in_array($channel->uid, $uids)) {
224 if (!in_array($language, $channel->languages)) {
227 } elseif (!in_array($language, User::getWantedLanguages($channel->uid))) {
230 if (!empty($channel->includeTags) && !in_array($channel->uid, $uids)) {
235 foreach (explode(',', $channel->includeTags) as $tag) {
236 if (in_array($tag, $tags)) {
245 if (!empty($tags) && !empty($channel->excludeTags) && !in_array($channel->uid, $uids)) {
247 foreach (explode(',', $channel->excludeTags) as $tag) {
248 if (in_array($tag, $tags)) {
257 if (!empty($channel->mediaType) && !in_array($channel->uid, $uids)) {
258 if (!($channel->mediaType & $media_type)) {
262 if (!empty($channel->fullTextSearch) && !in_array($channel->uid, $uids)) {
263 $channelsearchtext = $channel->fullTextSearch;
264 foreach (Engagement::KEYWORDS as $keyword) {
265 $channelsearchtext = preg_replace('~(' . $keyword . ':.[\w@\.-]+)~', '"$1"', $channelsearchtext);
267 if (!$this->db->exists('check-full-text-search', ["`pid` = ? AND MATCH (`searchtext`) AGAINST (? IN BOOLEAN MODE)", getmypid(), $channelsearchtext])) {
271 $uids[] = $channel->uid;
272 $this->logger->debug('Matching channel found.', ['uid' => $channel->uid, 'label' => $channel->label, 'language' => $language, 'tags' => $tags, 'media_type' => $media_type, 'searchtext' => $searchtext]);
278 $this->db->delete('check-full-text-search', ['pid' => getmypid()]);