Remove DI dependency in Contact\Contacts module

This commit is contained in:
Hypolite Petovan 2022-12-17 00:38:02 -05:00
parent cef408c842
commit a633532475
1 changed files with 39 additions and 21 deletions

View File

@ -21,54 +21,72 @@
namespace Friendica\Module\Contact; namespace Friendica\Module\Contact;
use Friendica\App;
use Friendica\BaseModule; use Friendica\BaseModule;
use Friendica\Content\Pager; use Friendica\Content\Pager;
use Friendica\Content\Widget; use Friendica\Content\Widget;
use Friendica\Core\L10n;
use Friendica\Core\Renderer; use Friendica\Core\Renderer;
use Friendica\DI; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Model; use Friendica\Model;
use Friendica\Model\User; use Friendica\Model\User;
use Friendica\Module; use Friendica\Module;
use Friendica\Module\Response;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
class Contacts extends BaseModule class Contacts extends BaseModule
{ {
/** @var IHandleUserSessions */
private $userSession;
/** @var App\Page */
private $page;
public function __construct(App\Page $page, IHandleUserSessions $userSession, 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->userSession = $userSession;
$this->page = $page;
}
protected function content(array $request = []): string protected function content(array $request = []): string
{ {
if (!DI::userSession()->getLocalUserId()) { if (!$this->userSession->getLocalUserId()) {
throw new HTTPException\ForbiddenException(); throw new HTTPException\ForbiddenException();
} }
$cid = $this->parameters['id']; $cid = $this->parameters['id'];
$type = $this->parameters['type'] ?? 'all'; $type = $this->parameters['type'] ?? 'all';
$accounttype = $_GET['accounttype'] ?? ''; $accounttype = $request['accounttype'] ?? '';
$accounttypeid = User::getAccountTypeByString($accounttype); $accounttypeid = User::getAccountTypeByString($accounttype);
if (!$cid) { if (!$cid) {
throw new HTTPException\BadRequestException(DI::l10n()->t('Invalid contact.')); throw new HTTPException\BadRequestException($this->t('Invalid contact.'));
} }
$contact = Model\Contact::getById($cid, []); $contact = Model\Contact::getById($cid, []);
if (empty($contact)) { if (empty($contact)) {
throw new HTTPException\NotFoundException(DI::l10n()->t('Contact not found.')); throw new HTTPException\NotFoundException($this->t('Contact not found.'));
} }
$localContactId = Model\Contact::getPublicIdByUserId(DI::userSession()->getLocalUserId()); $localContactId = Model\Contact::getPublicIdByUserId($this->userSession->getLocalUserId());
DI::page()['aside'] = Widget\VCard::getHTML($contact); $this->page['aside'] = Widget\VCard::getHTML($contact);
$condition = [ $condition = [
'blocked' => false, 'blocked' => false,
'self' => false, 'self' => false,
'hidden' => false, 'hidden' => false,
'failed' => false, 'failed' => false,
]; ];
if (isset($accounttypeid)) { if (isset($accounttypeid)) {
$condition['contact-type'] = $accounttypeid; $condition['contact-type'] = $accounttypeid;
} }
$noresult_label = DI::l10n()->t('No known contacts.'); $noresult_label = $this->t('No known contacts.');
switch ($type) { switch ($type) {
case 'followers': case 'followers':
@ -82,43 +100,43 @@ class Contacts extends BaseModule
break; break;
case 'common': case 'common':
$total = Model\Contact\Relation::countCommon($localContactId, $cid, $condition); $total = Model\Contact\Relation::countCommon($localContactId, $cid, $condition);
$noresult_label = DI::l10n()->t('No common contacts.'); $noresult_label = $this->t('No common contacts.');
break; break;
default: default:
$total = Model\Contact\Relation::countAll($cid, $condition); $total = Model\Contact\Relation::countAll($cid, $condition);
} }
$pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 30); $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
$desc = ''; $desc = '';
switch ($type) { switch ($type) {
case 'followers': case 'followers':
$friends = Model\Contact\Relation::listFollowers($cid, $condition, $pager->getItemsPerPage(), $pager->getStart()); $friends = Model\Contact\Relation::listFollowers($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
$title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total); $title = $this->tt('Follower (%s)', 'Followers (%s)', $total);
break; break;
case 'following': case 'following':
$friends = Model\Contact\Relation::listFollows($cid, $condition, $pager->getItemsPerPage(), $pager->getStart()); $friends = Model\Contact\Relation::listFollows($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
$title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total); $title = $this->tt('Following (%s)', 'Following (%s)', $total);
break; break;
case 'mutuals': case 'mutuals':
$friends = Model\Contact\Relation::listMutuals($cid, $condition, $pager->getItemsPerPage(), $pager->getStart()); $friends = Model\Contact\Relation::listMutuals($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
$title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total); $title = $this->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
$desc = DI::l10n()->t( $desc = $this->t(
'These contacts both follow and are followed by <strong>%s</strong>.', 'These contacts both follow and are followed by <strong>%s</strong>.',
htmlentities($contact['name'], ENT_COMPAT, 'UTF-8') htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
); );
break; break;
case 'common': case 'common':
$friends = Model\Contact\Relation::listCommon($localContactId, $cid, $condition, $pager->getItemsPerPage(), $pager->getStart()); $friends = Model\Contact\Relation::listCommon($localContactId, $cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
$title = DI::l10n()->tt('Common contact (%s)', 'Common contacts (%s)', $total); $title = $this->tt('Common contact (%s)', 'Common contacts (%s)', $total);
$desc = DI::l10n()->t( $desc = $this->t(
'Both <strong>%s</strong> and yourself have publicly interacted with these contacts (follow, comment or likes on public posts).', 'Both <strong>%s</strong> and yourself have publicly interacted with these contacts (follow, comment or likes on public posts).',
htmlentities($contact['name'], ENT_COMPAT, 'UTF-8') htmlentities($contact['name'], ENT_COMPAT, 'UTF-8')
); );
break; break;
default: default:
$friends = Model\Contact\Relation::listAll($cid, $condition, $pager->getItemsPerPage(), $pager->getStart()); $friends = Model\Contact\Relation::listAll($cid, $condition, $pager->getItemsPerPage(), $pager->getStart());
$title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total); $title = $this->tt('Contact (%s)', 'Contacts (%s)', $total);
} }
$o = Module\Contact::getTabsHTML($contact, Module\Contact::TAB_CONTACTS); $o = Module\Contact::getTabsHTML($contact, Module\Contact::TAB_CONTACTS);
@ -139,7 +157,7 @@ class Contacts extends BaseModule
'$paginate' => $pager->renderFull($total), '$paginate' => $pager->renderFull($total),
]); ]);
DI::page()['aside'] .= Widget::accountTypes($_SERVER['REQUEST_URI'], $accounttype); $this->page['aside'] .= Widget::accountTypes($_SERVER['REQUEST_URI'], $accounttype);
return $o; return $o;
} }