2023-09-20 21:39:05 +00:00
< ? php
/**
2024-01-02 20:57:26 +00:00
* @ copyright Copyright ( C ) 2010 - 2024 , the Friendica project
2023-09-20 21:39:05 +00:00
*
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License
* along with this program . If not , see < https :// www . gnu . org / licenses />.
*
*/
namespace Friendica\Module\Settings ;
use Friendica\App ;
2023-10-07 05:44:24 -04:00
use Friendica\Content\Conversation\Factory ;
use Friendica\Content\Conversation\Repository\UserDefinedChannel ;
2024-01-07 19:22:56 +00:00
use Friendica\Core\Config\Capability\IManageConfigValues ;
2023-09-20 21:39:05 +00:00
use Friendica\Core\L10n ;
use Friendica\Core\Renderer ;
use Friendica\Core\Session\Capability\IHandleUserSessions ;
2023-09-24 00:45:07 +00:00
use Friendica\Model\Circle ;
2024-01-03 19:17:14 +00:00
use Friendica\Model\User ;
2023-09-20 21:39:05 +00:00
use Friendica\Module\BaseSettings ;
use Friendica\Module\Response ;
use Friendica\Network\HTTPException ;
use Friendica\Util\Profiler ;
2024-01-28 06:32:55 +00:00
use Friendica\Util\Strings ;
2023-09-20 21:39:05 +00:00
use Psr\Log\LoggerInterface ;
class Channels extends BaseSettings
{
2023-10-07 05:44:24 -04:00
/** @var UserDefinedChannel */
2023-09-20 21:39:05 +00:00
private $channel ;
2023-10-07 05:44:24 -04:00
/** @var Factory\UserDefinedChannel */
private $userDefinedChannel ;
2024-01-07 19:22:56 +00:00
/** @var IManageConfigValues */
private $config ;
2023-09-20 21:39:05 +00:00
2024-01-07 19:22:56 +00:00
public function __construct ( Factory\UserDefinedChannel $userDefinedChannel , UserDefinedChannel $channel , App\Page $page , IHandleUserSessions $session , L10n $l10n , App\BaseURL $baseUrl , App\Arguments $args , LoggerInterface $logger , Profiler $profiler , Response $response , IManageConfigValues $config , array $server , array $parameters = [])
2023-09-20 21:39:05 +00:00
{
parent :: __construct ( $session , $page , $l10n , $baseUrl , $args , $logger , $profiler , $response , $server , $parameters );
2023-10-07 05:44:24 -04:00
$this -> userDefinedChannel = $userDefinedChannel ;
$this -> channel = $channel ;
2024-01-07 19:22:56 +00:00
$this -> config = $config ;
2023-09-20 21:39:05 +00:00
}
protected function post ( array $request = [])
{
$uid = $this -> session -> getLocalUserId ();
if ( ! $uid ) {
throw new HTTPException\ForbiddenException ( $this -> t ( 'Permission denied.' ));
}
if ( empty ( $request [ 'edit_channel' ]) && empty ( $request [ 'add_channel' ])) {
return ;
}
self :: checkFormSecurityTokenRedirectOnError ( '/settings/channels' , 'settings_channels' );
2024-01-03 19:17:14 +00:00
$channel_languages = User :: getWantedLanguages ( $uid );
2023-09-20 21:39:05 +00:00
if ( ! empty ( $request [ 'add_channel' ])) {
2024-01-03 19:17:14 +00:00
if ( ! array_diff (( array ) $request [ 'new_languages' ], $channel_languages )) {
$request [ 'new_languages' ] = null ;
}
2023-10-07 05:44:24 -04:00
$channel = $this -> userDefinedChannel -> createFromTableRow ([
2023-09-20 21:39:05 +00:00
'label' => $request [ 'new_label' ],
'description' => $request [ 'new_description' ],
'access-key' => substr ( mb_strtolower ( $request [ 'new_access_key' ]), 0 , 1 ),
'uid' => $uid ,
2023-09-24 00:45:07 +00:00
'circle' => ( int ) $request [ 'new_circle' ],
2024-01-28 06:32:55 +00:00
'include-tags' => Strings :: cleanTags ( $request [ 'new_include_tags' ]),
'exclude-tags' => Strings :: cleanTags ( $request [ 'new_exclude_tags' ]),
2024-01-30 10:05:05 +00:00
'min-size' => $request [ 'new_min_size' ] != '' ? ( int ) $request [ 'new_min_size' ] : null ,
'max-size' => $request [ 'new_max_size' ] != '' ? ( int ) $request [ 'new_max_size' ] : null ,
2023-10-14 18:39:35 +00:00
'full-text-search' => $request [ 'new_text_search' ],
2023-09-20 21:39:05 +00:00
'media-type' => ( $request [ 'new_image' ] ? 1 : 0 ) | ( $request [ 'new_video' ] ? 2 : 0 ) | ( $request [ 'new_audio' ] ? 4 : 0 ),
2024-01-03 19:17:14 +00:00
'languages' => $request [ 'new_languages' ],
2023-09-20 21:39:05 +00:00
]);
$saved = $this -> channel -> save ( $channel );
$this -> logger -> debug ( 'New channel added' , [ 'saved' => $saved ]);
return ;
}
2023-11-21 23:13:26 +00:00
foreach ( array_keys (( array ) $request [ 'label' ]) as $id ) {
2023-09-20 21:39:05 +00:00
if ( $request [ 'delete' ][ $id ]) {
$success = $this -> channel -> deleteById ( $id , $uid );
$this -> logger -> debug ( 'Channel deleted' , [ 'id' => $id , 'success' => $success ]);
continue ;
}
2024-02-03 11:01:17 +00:00
if ( ! array_diff (( array ) $request [ 'languages' ][ $id ], $channel_languages ) && ( count (( array ) $request [ 'languages' ][ $id ]) == count ( $channel_languages ))) {
2024-01-03 19:17:14 +00:00
$request [ 'languages' ][ $id ] = null ;
}
2023-10-07 05:44:24 -04:00
$channel = $this -> userDefinedChannel -> createFromTableRow ([
2023-09-20 21:39:05 +00:00
'id' => $id ,
'label' => $request [ 'label' ][ $id ],
'description' => $request [ 'description' ][ $id ],
'access-key' => substr ( mb_strtolower ( $request [ 'access_key' ][ $id ]), 0 , 1 ),
'uid' => $uid ,
2023-09-24 00:45:07 +00:00
'circle' => ( int ) $request [ 'circle' ][ $id ],
2024-01-28 06:32:55 +00:00
'include-tags' => Strings :: cleanTags ( $request [ 'include_tags' ][ $id ]),
'exclude-tags' => Strings :: cleanTags ( $request [ 'exclude_tags' ][ $id ]),
2024-01-30 10:05:05 +00:00
'min-size' => $request [ 'min_size' ][ $id ] != '' ? ( int ) $request [ 'min_size' ][ $id ] : null ,
'max-size' => $request [ 'max_size' ][ $id ] != '' ? ( int ) $request [ 'max_size' ][ $id ] : null ,
2023-10-14 18:39:35 +00:00
'full-text-search' => $request [ 'text_search' ][ $id ],
2023-09-20 21:39:05 +00:00
'media-type' => ( $request [ 'image' ][ $id ] ? 1 : 0 ) | ( $request [ 'video' ][ $id ] ? 2 : 0 ) | ( $request [ 'audio' ][ $id ] ? 4 : 0 ),
2024-01-03 19:17:14 +00:00
'languages' => $request [ 'languages' ][ $id ],
2024-01-10 22:04:02 +00:00
'publish' => $request [ 'publish' ][ $id ] ? ? false ,
2023-09-20 21:39:05 +00:00
]);
$saved = $this -> channel -> save ( $channel );
$this -> logger -> debug ( 'Save channel' , [ 'id' => $id , 'saved' => $saved ]);
}
}
protected function content ( array $request = []) : string
{
parent :: content ();
$uid = $this -> session -> getLocalUserId ();
if ( ! $uid ) {
throw new HTTPException\ForbiddenException ( $this -> t ( 'Permission denied.' ));
}
2024-01-06 21:52:28 +00:00
$user = User :: getById ( $uid , [ 'account-type' ]);
$account_type = $user [ 'account-type' ];
2024-01-07 18:36:47 +00:00
if ( in_array ( $account_type , [ User :: ACCOUNT_TYPE_COMMUNITY , User :: ACCOUNT_TYPE_RELAY ])) {
2024-01-06 22:04:30 +00:00
$intro = $this -> t ( 'This page can be used to define the channels that will automatically be reshared by your account.' );
$circles = [
0 => $this -> l10n -> t ( 'Global Community' )
];
2024-01-06 21:52:28 +00:00
} else {
2024-01-06 22:04:30 +00:00
$intro = $this -> t ( 'This page can be used to define your own channels.' );
2024-01-06 21:52:28 +00:00
$circles = [
0 => $this -> l10n -> t ( 'Global Community' ),
2024-02-04 07:14:57 +00:00
- 5 => $this -> l10n -> t ( 'Latest Activity' ),
- 4 => $this -> l10n -> t ( 'Latest Posts' ),
- 3 => $this -> l10n -> t ( 'Latest Creation' ),
2024-01-06 21:52:28 +00:00
- 1 => $this -> l10n -> t ( 'Following' ),
- 2 => $this -> l10n -> t ( 'Followers' ),
];
}
2023-09-24 00:45:07 +00:00
2023-09-26 05:05:51 +00:00
foreach ( Circle :: getByUserId ( $uid ) as $circle ) {
2023-09-24 00:45:07 +00:00
$circles [ $circle [ 'id' ]] = $circle [ 'name' ];
}
2024-01-03 19:17:14 +00:00
$languages = $this -> l10n -> getLanguageCodes ( true );
$channel_languages = User :: getWantedLanguages ( $uid );
2023-12-16 21:43:01 +00:00
$channels = [];
2023-09-20 21:39:05 +00:00
foreach ( $this -> channel -> selectByUid ( $uid ) as $channel ) {
2023-12-17 06:16:26 +00:00
if ( ! empty ( $request [ 'id' ])) {
$open = $channel -> code == $request [ 'id' ];
} elseif ( ! empty ( $request [ 'new_label' ])) {
$open = $channel -> label == $request [ 'new_label' ];
} else {
$open = false ;
}
2024-01-07 19:22:56 +00:00
if ( $this -> config -> get ( 'system' , 'allow_relay_channels' ) && in_array ( $account_type , [ User :: ACCOUNT_TYPE_COMMUNITY , User :: ACCOUNT_TYPE_RELAY ])) {
2024-01-07 18:36:47 +00:00
$publish = [ " publish[ $channel->code ] " , $this -> t ( " Publish " ), $channel -> publish , $this -> t ( " When selected, the channel results are reshared. This only works for public ActivityPub posts from the public timeline or the user defined circles. " )];
} else {
$publish = null ;
}
2023-12-16 21:43:01 +00:00
$channels [] = [
2023-12-17 06:16:26 +00:00
'id' => $channel -> code ,
'open' => $open ,
2023-09-20 21:39:05 +00:00
'label' => [ " label[ $channel->code ] " , $this -> t ( 'Label' ), $channel -> label , '' , $this -> t ( 'Required' )],
'description' => [ " description[ $channel->code ] " , $this -> t ( " Description " ), $channel -> description ],
'access_key' => [ " access_key[ $channel->code ] " , $this -> t ( " Access Key " ), $channel -> accessKey ],
2023-09-24 00:45:07 +00:00
'circle' => [ " circle[ $channel->code ] " , $this -> t ( 'Circle/Channel' ), $channel -> circle , '' , $circles ],
2023-10-14 18:39:35 +00:00
'include_tags' => [ " include_tags[ $channel->code ] " , $this -> t ( " Include Tags " ), str_replace ( ',' , ', ' , $channel -> includeTags )],
'exclude_tags' => [ " exclude_tags[ $channel->code ] " , $this -> t ( " Exclude Tags " ), str_replace ( ',' , ', ' , $channel -> excludeTags )],
2024-01-30 10:05:05 +00:00
'min_size' => [ " min_size[ $channel->code ] " , $this -> t ( " Minimum Size " ), $channel -> minSize ],
'max_size' => [ " max_size[ $channel->code ] " , $this -> t ( " Maximum Size " ), $channel -> maxSize ],
2023-09-21 23:27:05 +00:00
'text_search' => [ " text_search[ $channel->code ] " , $this -> t ( " Full Text Search " ), $channel -> fullTextSearch ],
2023-09-20 21:39:05 +00:00
'image' => [ " image[ $channel->code ] " , $this -> t ( " Images " ), $channel -> mediaType & 1 ],
'video' => [ " video[ $channel->code ] " , $this -> t ( " Videos " ), $channel -> mediaType & 2 ],
'audio' => [ " audio[ $channel->code ] " , $this -> t ( " Audio " ), $channel -> mediaType & 4 ],
2024-01-03 19:17:14 +00:00
'languages' => [ " languages[ $channel->code ][] " , $this -> t ( 'Languages' ), $channel -> languages ? ? $channel_languages , $this -> t ( 'Select all languages that you want to see in this channel.' ), $languages , 'multiple' ],
2024-01-07 18:36:47 +00:00
'publish' => $publish ,
2023-09-20 21:39:05 +00:00
'delete' => [ " delete[ $channel->code ] " , $this -> t ( " Delete channel " ) . ' (' . $channel -> label . ')' , false , $this -> t ( " Check to delete this entry from the channel list " )]
];
}
$t = Renderer :: getMarkupTemplate ( 'settings/channels.tpl' );
return Renderer :: replaceMacros ( $t , [
2023-12-17 06:41:19 +00:00
'open' => count ( $channels ) == 0 ,
2023-09-20 21:39:05 +00:00
'label' => [ " new_label " , $this -> t ( 'Label' ), '' , $this -> t ( 'Short name for the channel. It is displayed on the channels widget.' ), $this -> t ( 'Required' )],
'description' => [ " new_description " , $this -> t ( " Description " ), '' , $this -> t ( 'This should describe the content of the channel in a few word.' )],
2023-09-23 14:46:15 +00:00
'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 attention to not use an already used one.' )],
2023-09-24 00:45:07 +00:00
'circle' => [ 'new_circle' , $this -> t ( 'Circle/Channel' ), 0 , $this -> t ( 'Select a circle or channel, that your channel should be based on.' ), $circles ],
2023-09-20 21:39:05 +00:00
'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.' )],
'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.' )],
2024-01-30 10:05:05 +00:00
'min_size' => [ " new_min_size " , $this -> t ( " Minimum Size " ), '' , $this -> t ( 'Minimum post size. Leave empty for no minimum size. The size is calculated without links, attached posts, mentions or hashtags.' )],
'max_size' => [ " new_max_size " , $this -> t ( " Maximum Size " ), '' , $this -> t ( 'Maximum post size. Leave empty for no maximum size. The size is calculated without links, attached posts, mentions or hashtags.' )],
2023-09-23 14:46:15 +00:00
'text_search' => [ " new_text_search " , $this -> t ( " Full Text Search " ), '' , $this -> t ( 'Search terms for the body, supports the "boolean mode" operators from MariaDB. See the help for a complete list of operators and additional keywords: %s' , '<a href="help/Channels">help/Channels</a>' )],
2023-09-20 21:39:05 +00:00
'image' => [ 'new_image' , $this -> t ( " Images " ), false , $this -> t ( " Check to display images in the channel. " )],
'video' => [ " new_video " , $this -> t ( " Videos " ), false , $this -> t ( " Check to display videos in the channel. " )],
'audio' => [ " new_audio " , $this -> t ( " Audio " ), false , $this -> t ( " Check to display audio in the channel. " )],
2024-01-03 19:17:14 +00:00
'languages' => [ " new_languages[] " , $this -> t ( 'Languages' ), $channel_languages , $this -> t ( 'Select all languages that you want to see in this channel.' ), $languages , 'multiple' ],
2023-09-20 21:42:37 +00:00
'$l10n' => [
2023-09-20 21:39:05 +00:00
'title' => $this -> t ( 'Channels' ),
2024-01-06 21:52:28 +00:00
'intro' => $intro ,
2023-09-20 21:39:05 +00:00
'addtitle' => $this -> t ( 'Add new entry to the channel list' ),
'addsubmit' => $this -> t ( 'Add' ),
'savechanges' => $this -> t ( 'Save' ),
'currenttitle' => $this -> t ( 'Current Entries in the channel list' ),
'thurl' => $this -> t ( 'Blocked server domain pattern' ),
'threason' => $this -> t ( 'Reason for the block' ),
'delentry' => $this -> t ( 'Delete entry from the channel list' ),
'confirm_delete' => $this -> t ( 'Delete entry from the channel list?' ),
],
2023-12-16 21:43:01 +00:00
'$entries' => $channels ,
2023-09-20 21:39:05 +00:00
'$baseurl' => $this -> baseUrl ,
'$form_security_token' => self :: getFormSecurityToken ( 'settings_channels' ),
]);
}
}