"Channel" is split into three classes

This commit is contained in:
Michael 2023-09-04 22:22:25 +00:00
parent 41f3cbf727
commit 196219383e
3 changed files with 141 additions and 69 deletions

View File

@ -0,0 +1,48 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @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\Content\Entity\Conversation;
/**
* @property-read string $code Channel code
* @property-read string $label Channel label
* @property-read string $description Channel description
* @property-read string $accessKey Access key
*/
final class Channel extends \Friendica\BaseEntity
{
/** @var string */
protected $code;
/** @var string */
protected $label;
/** @var string */
protected $description;
/** @var string */
protected $accessKey;
public function __construct(string $code, string $label, string $description, string $accessKey)
{
$this->code = $code;
$this->label = $label;
$this->description = $description;
$this->accessKey = $accessKey;
}
}

72
src/Model/Channel.php Normal file
View File

@ -0,0 +1,72 @@
<?php
/**
* @copyright Copyright (C) 2010-2023, the Friendica project
*
* @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\Model;
use Friendica\Model\User;
use Friendica\Content\Entity\Conversation\Channel as ChannelEntity;
use Friendica\Core\L10n;
use Friendica\Database\Database;
use Psr\Log\LoggerInterface;
final class Channel extends \Friendica\BaseModel
{
const WHATSHOT = 'whatshot';
const FORYOU = 'foryou';
const FOLLOWERS = 'followers';
const IMAGE = 'image';
const VIDEO = 'video';
const AUDIO = 'audio';
const LANGUAGE = 'language';
/** @var L10n */
protected $l10n;
public function __construct(L10n $l10n, Database $database, LoggerInterface $logger, array $data = [])
{
parent::__construct($database, $logger, $data);
$this->l10n = $l10n;
}
/**
* List of available channels
*
* @param integer $uid
* @return array
*/
public function getForUser(int $uid): array
{
$language = User::getLanguageCode($uid);
$languages = $this->l10n->getAvailableLanguages(true);
$tabs = [
new ChannelEntity(self::FORYOU, $this->l10n->t('For you'), $this->l10n->t('Posts from contacts you interact with and who interact with you'), 'y'),
new ChannelEntity(self::WHATSHOT, $this->l10n->t('What\'s Hot'), $this->l10n->t('Posts with a lot of interactions'), 'h'),
new ChannelEntity(self::LANGUAGE, $languages[$language], $this->l10n->t('Posts in %s', $languages[$language]), 'g'),
new ChannelEntity(self::FOLLOWERS, $this->l10n->t('Followers'), $this->l10n->t('Posts from your followers that you don\'t follow'), 'f'),
new ChannelEntity(self::IMAGE, $this->l10n->t('Images'), $this->l10n->t('Posts with images'), 'i'),
new ChannelEntity(self::AUDIO, $this->l10n->t('Audio'), $this->l10n->t('Posts with audio'), 'd'),
new ChannelEntity(self::VIDEO, $this->l10n->t('Videos'), $this->l10n->t('Posts with videos'), 'v'),
];
return $tabs;
}
}

View File

@ -45,6 +45,7 @@ use Friendica\Module\Security\Login;
use Friendica\Network\HTTPException;
use Friendica\Core\Session\Model\UserSession;
use Friendica\Database\Database;
use Friendica\Model\Channel as ChannelModel;
use Friendica\Model\Item;
use Friendica\Module\Response;
use Friendica\Navigation\SystemMessages;
@ -87,12 +88,15 @@ class Channel extends BaseModule
protected $pConfig;
/** @var Database */
protected $database;
/** @var ChannelModel */
protected $channel;
public function __construct(SystemMessages $systemMessages, Database $database, IManagePersonalConfigValues $pConfig, Mode $mode, Conversation $conversation, App\Page $page, IManageConfigValues $config, ICanCache $cache, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
public function __construct(ChannelModel $channel, SystemMessages $systemMessages, Database $database, IManagePersonalConfigValues $pConfig, Mode $mode, Conversation $conversation, App\Page $page, IManageConfigValues $config, ICanCache $cache, IHandleUserSessions $session, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
{
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
$this->channel = $channel;
$this->systemMessages = $systemMessages;
$this->database = $database;
$this->pConfig = $pConfig;
@ -126,71 +130,16 @@ class Channel extends BaseModule
if (empty($request['mode']) || ($request['mode'] != 'raw')) {
$tabs = [];
$tabs[] = [
'label' => $this->l10n->t('For you'),
'url' => 'channel/' . self::FORYOU,
'sel' => self::$content == self::FORYOU ? 'active' : '',
'title' => $this->l10n->t('Posts from contacts you interact with and who interact with you'),
'id' => 'channel-foryou-tab',
'accesskey' => 'y'
];
$tabs[] = [
'label' => $this->l10n->t('What\'s Hot'),
'url' => 'channel/' . self::WHATSHOT,
'sel' => self::$content == self::WHATSHOT ? 'active' : '',
'title' => $this->l10n->t('Posts with a lot of interactions'),
'id' => 'channel-whatshot-tab',
'accesskey' => 'h'
];
$language = User::getLanguageCode($this->session->getLocalUserId());
$languages = $this->l10n->getAvailableLanguages(true);
$tabs[] = [
'label' => $languages[$language],
'url' => 'channel/' . self::LANGUAGE,
'sel' => self::$content == self::LANGUAGE ? 'active' : '',
'title' => $this->l10n->t('Posts in %s', $languages[$language]),
'id' => 'channel-language-tab',
'accesskey' => 'g'
];
$tabs[] = [
'label' => $this->l10n->t('Followers'),
'url' => 'channel/' . self::FOLLOWERS,
'sel' => self::$content == self::FOLLOWERS ? 'active' : '',
'title' => $this->l10n->t('Posts from your followers that you don\'t follow'),
'id' => 'channel-followers-tab',
'accesskey' => 'f'
];
$tabs[] = [
'label' => $this->l10n->t('Images'),
'url' => 'channel/' . self::IMAGE,
'sel' => self::$content == self::IMAGE ? 'active' : '',
'title' => $this->l10n->t('Posts with images'),
'id' => 'channel-image-tab',
'accesskey' => 'i'
];
$tabs[] = [
'label' => $this->l10n->t('Audio'),
'url' => 'channel/' . self::AUDIO,
'sel' => self::$content == self::AUDIO ? 'active' : '',
'title' => $this->l10n->t('Posts with audio'),
'id' => 'channel-audio-tab',
'accesskey' => 'd'
];
$tabs[] = [
'label' => $this->l10n->t('Videos'),
'url' => 'channel/' . self::VIDEO,
'sel' => self::$content == self::VIDEO ? 'active' : '',
'title' => $this->l10n->t('Posts with videos'),
'id' => 'channel-video-tab',
'accesskey' => 'v'
];
foreach ($this->channel->getForUser($this->session->getLocalUserId()) as $tab) {
$tabs[] = [
'label' => $tab->label,
'url' => 'channel/' . $tab->code,
'sel' => self::$content == $tab->code ? 'active' : '',
'title' => $tab->description,
'id' => 'channel-' . $tab->code . '-tab',
'accesskey' => $tab->accessKey,
];
}
$tab_tpl = Renderer::getMarkupTemplate('common_tabs.tpl');
$o .= Renderer::replaceMacros($tab_tpl, ['$tabs' => $tabs]);
@ -431,7 +380,8 @@ class Channel extends BaseModule
return 0;
}
$this->cache->set($cache_key, $comments, Duration::HOUR);
$this->cache->set($cache_key, $comments, Duration::HALF_HOUR);
$this->logger->debug('Calculated median comments', ['divider' => $divider, 'median' => $comments]);
return $comments;
}
@ -450,7 +400,8 @@ class Channel extends BaseModule
return 0;
}
$this->cache->set($cache_key, $activities, Duration::HOUR);
$this->cache->set($cache_key, $activities, Duration::HALF_HOUR);
$this->logger->debug('Calculated median activities', ['divider' => $divider, 'median' => $activities]);
return $activities;
}
@ -469,7 +420,8 @@ class Channel extends BaseModule
return 0;
}
$this->cache->set($cache_key, $score, Duration::HOUR);
$this->cache->set($cache_key, $score, Duration::HALF_HOUR);
$this->logger->debug('Calculated median score', ['cid' => $cid, 'divider' => $divider, 'median' => $score]);
return $score;
}
}