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\Module\Settings;
25 use Friendica\Content\Conversation\Factory\Timeline;
26 use Friendica\Content\Conversation\Repository\Channel;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session\Capability\IHandleUserSessions;
30 use Friendica\Module\BaseSettings;
31 use Friendica\Module\Response;
32 use Friendica\Network\HTTPException;
33 use Friendica\Util\Profiler;
34 use Psr\Log\LoggerInterface;
36 class Channels extends BaseSettings
43 public function __construct(Timeline $timeline, Channel $channel, App\Page $page, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
45 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
47 $this->timeline = $timeline;
48 $this->channel = $channel;
51 protected function post(array $request = [])
53 $uid = $this->session->getLocalUserId();
55 throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
58 if (empty($request['edit_channel']) && empty($request['add_channel'])) {
62 self::checkFormSecurityTokenRedirectOnError('/settings/channels', 'settings_channels');
64 if (!empty($request['add_channel'])) {
65 $channel = $this->timeline->createFromTableRow([
66 'label' => $request['new_label'],
67 'description' => $request['new_description'],
68 'access-key' => substr(mb_strtolower($request['new_access_key']), 0, 1),
70 'include-tags' => $this->cleanTags($request['new_include_tags']),
71 'exclude-tags' => $this->cleanTags($request['new_exclude_tags']),
72 'full-text-search' => $this->cleanTags($request['new_text_search']),
73 'media-type' => ($request['new_image'] ? 1 : 0) | ($request['new_video'] ? 2 : 0) | ($request['new_audio'] ? 4 : 0),
75 $saved = $this->channel->save($channel);
76 $this->logger->debug('New channel added', ['saved' => $saved]);
80 foreach (array_keys($request['label']) as $id) {
81 if ($request['delete'][$id]) {
82 $success = $this->channel->deleteById($id, $uid);
83 $this->logger->debug('Channel deleted', ['id' => $id, 'success' => $success]);
87 $channel = $this->timeline->createFromTableRow([
89 'label' => $request['label'][$id],
90 'description' => $request['description'][$id],
91 'access-key' => substr(mb_strtolower($request['access_key'][$id]), 0, 1),
93 'include-tags' => $this->cleanTags($request['include_tags'][$id]),
94 'exclude-tags' => $this->cleanTags($request['exclude_tags'][$id]),
95 'full-text-search' => $this->cleanTags($request['text_search'][$id]),
96 'media-type' => ($request['image'][$id] ? 1 : 0) | ($request['video'][$id] ? 2 : 0) | ($request['audio'][$id] ? 4 : 0),
98 $saved = $this->channel->save($channel);
99 $this->logger->debug('Save channel', ['id' => $id, 'saved' => $saved]);
102 $this->baseUrl->redirect('/settings/channels');
105 protected function content(array $request = []): string
109 $uid = $this->session->getLocalUserId();
111 throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
115 foreach ($this->channel->selectByUid($uid) as $channel) {
117 'label' => ["label[$channel->code]", $this->t('Label'), $channel->label, '', $this->t('Required')],
118 'description' => ["description[$channel->code]", $this->t("Description"), $channel->description],
119 'access_key' => ["access_key[$channel->code]", $this->t("Access Key"), $channel->accessKey],
120 'include_tags' => ["include_tags[$channel->code]", $this->t("Include Tags"), $channel->includeTags],
121 'exclude_tags' => ["exclude_tags[$channel->code]", $this->t("Exclude Tags"), $channel->excludeTags],
122 'text_search' => ["text_search[$channel->code]", $this->t("Full Text Search"), $channel->fullTextSearch],
123 'image' => ["image[$channel->code]", $this->t("Images"), $channel->mediaType & 1],
124 'video' => ["video[$channel->code]", $this->t("Videos"), $channel->mediaType & 2],
125 'audio' => ["audio[$channel->code]", $this->t("Audio"), $channel->mediaType & 4],
126 'delete' => ["delete[$channel->code]", $this->t("Delete channel") . ' (' . $channel->label . ')', false, $this->t("Check to delete this entry from the channel list")]
130 $t = Renderer::getMarkupTemplate('settings/channels.tpl');
131 return Renderer::replaceMacros($t, [
132 'label' => ["new_label", $this->t('Label'), '', $this->t('Short name for the channel. It is displayed on the channels widget.'), $this->t('Required')],
133 'description' => ["new_description", $this->t("Description"), '', $this->t('This should describe the content of the channel in a few word.')],
134 'access_key' => ["new_access_key", $this->t("Access Key"), '', $this->t('When you want to access this channel via an access key, you can define it here. Pay attentioon to not use an already used one.')],
135 'include_tags' => ["new_include_tags", $this->t("Include Tags"), '', $this->t('Comma separated list of tags. A post will be used when it contains any of the listed tags.')],
136 'exclude_tags' => ["new_exclude_tags", $this->t("Exclude Tags"), '', $this->t('Comma separated list of tags. If a post contain any of these tags, then it will not be part of nthis channel.')],
137 'text_search' => ["new_text_search", $this->t("Full Text Search"), '', $this->t('Search terms for the body.')],
138 'image' => ['new_image', $this->t("Images"), false, $this->t("Check to display images in the channel.")],
139 'video' => ["new_video", $this->t("Videos"), false, $this->t("Check to display videos in the channel.")],
140 'audio' => ["new_audio", $this->t("Audio"), false, $this->t("Check to display audio in the channel.")],
142 'title' => $this->t('Channels'),
143 'intro' => $this->t('This page can be used to define your own channels.'),
144 'addtitle' => $this->t('Add new entry to the channel list'),
145 'addsubmit' => $this->t('Add'),
146 'savechanges' => $this->t('Save'),
147 'currenttitle' => $this->t('Current Entries in the channel list'),
148 'thurl' => $this->t('Blocked server domain pattern'),
149 'threason' => $this->t('Reason for the block'),
150 'delentry' => $this->t('Delete entry from the channel list'),
151 'confirm_delete' => $this->t('Delete entry from the channel list?'),
153 '$entries' => $blocklistform,
154 '$baseurl' => $this->baseUrl,
156 '$form_security_token' => self::getFormSecurityToken('settings_channels'),
160 private function cleanTags(string $tag_list): string
164 $tagitems = explode(',', mb_strtolower($tag_list));
165 foreach ($tagitems as $tag) {
166 $tag = trim($tag, '# ');
171 return implode(',', $tags);