Remove DI dependency from PermissionTooltip module

- Update PHPDoc of APContact::getByURL
This commit is contained in:
Hypolite Petovan 2024-05-08 22:21:25 -04:00
parent f566c52624
commit c19a68dc64
2 changed files with 91 additions and 78 deletions

View file

@ -105,14 +105,12 @@ class APContact
/** /**
* Fetches a profile from a given url * Fetches a profile from a given url
* *
* @param string $url profile url * @param string $url profile url
* @param boolean $update true = always update, false = never update, null = update when not found or outdated * @param ?boolean $update true = always update, false = never update, null = update when not found or outdated
* @return array profile array * @return array profile array
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
* @todo Rewrite parameter $update to avoid true|false|null (boolean is binary, null adds a third case)
*/ */
public static function getByURL(string $url, $update = null): array public static function getByURL(string $url, bool $update = null): array
{ {
if (empty($url) || Network::isUrlBlocked($url)) { if (empty($url) || Network::isUrlBlocked($url)) {
Logger::info('Domain is blocked', ['url' => $url]); Logger::info('Domain is blocked', ['url' => $url]);

View file

@ -21,24 +21,43 @@
namespace Friendica\Module; namespace Friendica\Module;
use Friendica\App;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Protocol; use Friendica\Core\Protocol;
use Friendica\Core\System; use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Database\DBA; use Friendica\Database\Database;
use Friendica\DI; use Friendica\Model;
use Friendica\Model\APContact;
use Friendica\Model\Circle;
use Friendica\Model\Item;
use Friendica\Model\Post;
use Friendica\Model\Tag;
use Friendica\Model\User;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Security\PermissionSet\Repository\PermissionSet;
use Friendica\Util\ACLFormatter;
use Friendica\Util\Profiler;
use Psr\Log\LoggerInterface;
/** /**
* Outputs the permission tooltip HTML content for the provided item, photo or event id. * Outputs the permission tooltip HTML content for the provided item, photo or event id.
*/ */
class PermissionTooltip extends \Friendica\BaseModule class PermissionTooltip extends \Friendica\BaseModule
{ {
private Database $dba;
private ACLFormatter $aclFormatter;
private IHandleUserSessions $session;
private IManageConfigValues $config;
private PermissionSet $permissionSet;
public function __construct(PermissionSet $permissionSet, IManageConfigValues $config, IHandleUserSessions $session, ACLFormatter $aclFormatter, Database $dba, 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->dba = $dba;
$this->aclFormatter = $aclFormatter;
$this->session = $session;
$this->config = $config;
$this->permissionSet = $permissionSet;
}
protected function rawContent(array $request = []) protected function rawContent(array $request = [])
{ {
$type = $this->parameters['type']; $type = $this->parameters['type'];
@ -46,16 +65,16 @@ class PermissionTooltip extends \Friendica\BaseModule
$expectedTypes = ['item', 'photo', 'event']; $expectedTypes = ['item', 'photo', 'event'];
if (!in_array($type, $expectedTypes)) { if (!in_array($type, $expectedTypes)) {
throw new HTTPException\BadRequestException(DI::l10n()->t('Wrong type "%s", expected one of: %s', $type, implode(', ', $expectedTypes))); throw new HTTPException\BadRequestException($this->t('Wrong type "%s", expected one of: %s', $type, implode(', ', $expectedTypes)));
} }
$condition = ['id' => $referenceId, 'uid' => [0, DI::userSession()->getLocalUserId()]]; $condition = ['id' => $referenceId, 'uid' => [0, $this->session->getLocalUserId()]];
if ($type == 'item') { if ($type == 'item') {
$fields = ['uid', 'psid', 'private', 'uri-id', 'origin', 'network']; $fields = ['uid', 'psid', 'private', 'uri-id', 'origin', 'network'];
$model = Post::selectFirst($fields, $condition, ['order' => ['uid' => true]]); $model = Model\Post::selectFirst($fields, $condition, ['order' => ['uid' => true]]);
if ($model['origin'] || ($model['network'] != Protocol::ACTIVITYPUB)) { if ($model['origin'] || ($model['network'] != Protocol::ACTIVITYPUB)) {
$permissionSet = DI::permissionSet()->selectOneById($model['psid'], $model['uid']); $permissionSet = $this->permissionSet->selectOneById($model['psid'], $model['uid']);
$model['allow_cid'] = $permissionSet->allow_cid; $model['allow_cid'] = $permissionSet->allow_cid;
$model['allow_gid'] = $permissionSet->allow_gid; $model['allow_gid'] = $permissionSet->allow_gid;
$model['deny_cid'] = $permissionSet->deny_cid; $model['deny_cid'] = $permissionSet->deny_cid;
@ -68,15 +87,15 @@ class PermissionTooltip extends \Friendica\BaseModule
} }
} else { } else {
$fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid']; $fields = ['uid', 'allow_cid', 'allow_gid', 'deny_cid', 'deny_gid'];
$model = DBA::selectFirst($type, $fields, $condition); $model = $this->dba->selectFirst($type, $fields, $condition);
$model['allow_cid'] = DI::aclFormatter()->expand($model['allow_cid']); $model['allow_cid'] = $this->aclFormatter->expand($model['allow_cid']);
$model['allow_gid'] = DI::aclFormatter()->expand($model['allow_gid']); $model['allow_gid'] = $this->aclFormatter->expand($model['allow_gid']);
$model['deny_cid'] = DI::aclFormatter()->expand($model['deny_cid']); $model['deny_cid'] = $this->aclFormatter->expand($model['deny_cid']);
$model['deny_gid'] = DI::aclFormatter()->expand($model['deny_gid']); $model['deny_gid'] = $this->aclFormatter->expand($model['deny_gid']);
} }
if (!DBA::isResult($model)) { if (!$this->dba->isResult($model)) {
throw new HttpException\NotFoundException(DI::l10n()->t('Model not found')); throw new HttpException\NotFoundException($this->t('Model not found'));
} }
// Kept for backwards compatibility // Kept for backwards compatibility
@ -86,16 +105,16 @@ class PermissionTooltip extends \Friendica\BaseModule
$receivers = $this->fetchReceivers($model['uri-id']); $receivers = $this->fetchReceivers($model['uri-id']);
if (empty($receivers)) { if (empty($receivers)) {
switch ($model['private']) { switch ($model['private']) {
case Item::PUBLIC: case Model\Item::PUBLIC:
$receivers = DI::l10n()->t('Public'); $receivers = $this->t('Public');
break; break;
case Item::UNLISTED: case Model\Item::UNLISTED:
$receivers = DI::l10n()->t('Unlisted'); $receivers = $this->t('Unlisted');
break; break;
case Item::PRIVATE: case Model\Item::PRIVATE:
$receivers = DI::l10n()->t('Limited/Private'); $receivers = $this->t('Limited/Private');
break; break;
} }
} }
@ -109,7 +128,7 @@ class PermissionTooltip extends \Friendica\BaseModule
&& empty($model['deny_gid']) && empty($model['deny_gid'])
&& empty($receivers)) && empty($receivers))
{ {
echo DI::l10n()->t('Remote privacy information not available.'); echo $this->t('Remote privacy information not available.');
exit; exit;
} }
@ -117,16 +136,14 @@ class PermissionTooltip extends \Friendica\BaseModule
$receivers = $this->fetchReceiversFromACL($model); $receivers = $this->fetchReceiversFromACL($model);
} }
$this->httpExit(DI::l10n()->t('Visible to:') . '<br />' . $receivers); $this->httpExit($this->t('Visible to:') . '<br />' . $receivers);
} }
/** /**
* Fetch a list of receivers based on the ACL data * Fetch a list of receivers based on the ACL data
* * @throws \Exception
* @param array $model
* @return string
*/ */
private function fetchReceiversFromACL(array $model) private function fetchReceiversFromACL(array $model): string
{ {
$allowed_users = $model['allow_cid']; $allowed_users = $model['allow_cid'];
$allowed_circles = $model['allow_gid']; $allowed_circles = $model['allow_gid'];
@ -136,46 +153,46 @@ class PermissionTooltip extends \Friendica\BaseModule
$l = []; $l = [];
if (count($allowed_circles)) { if (count($allowed_circles)) {
$key = array_search(Circle::FOLLOWERS, $allowed_circles); $key = array_search(Model\Circle::FOLLOWERS, $allowed_circles);
if ($key !== false) { if ($key !== false) {
$l[] = '<b>' . DI::l10n()->t('Followers') . '</b>'; $l[] = '<b>' . $this->t('Followers') . '</b>';
unset($allowed_circles[$key]); unset($allowed_circles[$key]);
} }
$key = array_search(Circle::MUTUALS, $allowed_circles); $key = array_search(Model\Circle::MUTUALS, $allowed_circles);
if ($key !== false) { if ($key !== false) {
$l[] = '<b>' . DI::l10n()->t('Mutuals') . '</b>'; $l[] = '<b>' . $this->t('Mutuals') . '</b>';
unset($allowed_circles[$key]); unset($allowed_circles[$key]);
} }
foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_circles]) as $circle) { foreach ($this->dba->selectToArray('group', ['name'], ['id' => $allowed_circles]) as $circle) {
$l[] = '<b>' . $circle['name'] . '</b>'; $l[] = '<b>' . $circle['name'] . '</b>';
} }
} }
foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $allowed_users]) as $contact) { foreach ($this->dba->selectToArray('contact', ['name'], ['id' => $allowed_users]) as $contact) {
$l[] = $contact['name']; $l[] = $contact['name'];
} }
if (count($deny_circles)) { if (count($deny_circles)) {
$key = array_search(Circle::FOLLOWERS, $deny_circles); $key = array_search(Model\Circle::FOLLOWERS, $deny_circles);
if ($key !== false) { if ($key !== false) {
$l[] = '<b><strike>' . DI::l10n()->t('Followers') . '</strike></b>'; $l[] = '<b><strike>' . $this->t('Followers') . '</strike></b>';
unset($deny_circles[$key]); unset($deny_circles[$key]);
} }
$key = array_search(Circle::MUTUALS, $deny_circles); $key = array_search(Model\Circle::MUTUALS, $deny_circles);
if ($key !== false) { if ($key !== false) {
$l[] = '<b><strike>' . DI::l10n()->t('Mutuals') . '</strike></b>'; $l[] = '<b><strike>' . $this->t('Mutuals') . '</strike></b>';
unset($deny_circles[$key]); unset($deny_circles[$key]);
} }
foreach (DI::dba()->selectToArray('group', ['name'], ['id' => $allowed_circles]) as $circle) { foreach ($this->dba->selectToArray('group', ['name'], ['id' => $allowed_circles]) as $circle) {
$l[] = '<b><strike>' . $circle['name'] . '</strike></b>'; $l[] = '<b><strike>' . $circle['name'] . '</strike></b>';
} }
} }
foreach (DI::dba()->selectToArray('contact', ['name'], ['id' => $deny_users]) as $contact) { foreach ($this->dba->selectToArray('contact', ['name'], ['id' => $deny_users]) as $contact) {
$l[] = '<strike>' . $contact['name'] . '</strike>'; $l[] = '<strike>' . $contact['name'] . '</strike>';
} }
@ -184,41 +201,39 @@ class PermissionTooltip extends \Friendica\BaseModule
/** /**
* Fetch a list of receivers * Fetch a list of receivers
* * @throws InternalServerErrorException
* @param int $uriId
* @return string
*/ */
private function fetchReceivers(int $uriId): string private function fetchReceivers(int $uriId): string
{ {
$own_url = ''; $own_url = '';
$uid = DI::userSession()->getLocalUserId(); $uid = $this->session->getLocalUserId();
if ($uid) { if ($uid) {
$owner = User::getOwnerDataById($uid); $owner = Model\User::getOwnerDataById($uid);
if (!empty($owner['url'])) { if (!empty($owner['url'])) {
$own_url = $owner['url']; $own_url = $owner['url'];
} }
} }
$receivers = []; $receivers = [];
foreach (Tag::getByURIId($uriId, [Tag::TO, Tag::CC, Tag::BCC, Tag::AUDIENCE, Tag::ATTRIBUTED]) as $receiver) { foreach (Model\Tag::getByURIId($uriId, [Model\Tag::TO, Model\Tag::CC, Model\Tag::BCC, Model\Tag::AUDIENCE, Model\Tag::ATTRIBUTED]) as $receiver) {
// We only display BCC when it contains the current user // We only display BCC when it contains the current user
if (($receiver['type'] == Tag::BCC) && ($receiver['url'] != $own_url)) { if (($receiver['type'] == Model\Tag::BCC) && ($receiver['url'] != $own_url)) {
continue; continue;
} }
switch (Tag::getTargetType($receiver['url'], false)) { switch (Model\Tag::getTargetType($receiver['url'], false)) {
case Tag::PUBLIC_COLLECTION: case Model\Tag::PUBLIC_COLLECTION:
$receivers[$receiver['type']][] = DI::l10n()->t('Public'); $receivers[$receiver['type']][] = $this->t('Public');
break; break;
case Tag::GENERAL_COLLECTION: case Model\Tag::GENERAL_COLLECTION:
$receivers[$receiver['type']][] = DI::l10n()->t('Collection (%s)', $receiver['name']); $receivers[$receiver['type']][] = $this->t('Collection (%s)', $receiver['name']);
break; break;
case Tag::FOLLOWER_COLLECTION: case Model\Tag::FOLLOWER_COLLECTION:
$apcontact = DBA::selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]); $apcontact = $this->dba->selectFirst('apcontact', ['name'], ['followers' => $receiver['url']]);
$receivers[$receiver['type']][] = DI::l10n()->t('Followers (%s)', $apcontact['name'] ?? $receiver['name']); $receivers[$receiver['type']][] = $this->t('Followers (%s)', $apcontact['name'] ?? $receiver['name']);
break; break;
case Tag::ACCOUNT: case Model\Tag::ACCOUNT:
$apcontact = APContact::getByURL($receiver['url'], false); $apcontact = Model\APContact::getByURL($receiver['url'], false);
$receivers[$receiver['type']][] = $apcontact['name'] ?? $receiver['name']; $receivers[$receiver['type']][] = $apcontact['name'] ?? $receiver['name'];
break; break;
default: default:
@ -230,27 +245,27 @@ class PermissionTooltip extends \Friendica\BaseModule
$output = ''; $output = '';
foreach ($receivers as $type => $receiver) { foreach ($receivers as $type => $receiver) {
$max = DI::config()->get('system', 'max_receivers'); $max = $this->config->get('system', 'max_receivers');
$total = count($receiver); $total = count($receiver);
if ($total > $max) { if ($total > $max) {
$receiver = array_slice($receiver, 0, $max); $receiver = array_slice($receiver, 0, $max);
$receiver[] = DI::l10n()->t('%d more', $total - $max); $receiver[] = $this->t('%d more', $total - $max);
} }
switch ($type) { switch ($type) {
case Tag::TO: case Model\Tag::TO:
$output .= DI::l10n()->t('<b>To:</b> %s<br>', implode(', ', $receiver)); $output .= $this->t('<b>To:</b> %s<br>', implode(', ', $receiver));
break; break;
case Tag::CC: case Model\Tag::CC:
$output .= DI::l10n()->t('<b>CC:</b> %s<br>', implode(', ', $receiver)); $output .= $this->t('<b>CC:</b> %s<br>', implode(', ', $receiver));
break; break;
case Tag::BCC: case Model\Tag::BCC:
$output .= DI::l10n()->t('<b>BCC:</b> %s<br>', implode(', ', $receiver)); $output .= $this->t('<b>BCC:</b> %s<br>', implode(', ', $receiver));
break; break;
case Tag::AUDIENCE: case Model\Tag::AUDIENCE:
$output .= DI::l10n()->t('<b>Audience:</b> %s<br>', implode(', ', $receiver)); $output .= $this->t('<b>Audience:</b> %s<br>', implode(', ', $receiver));
break; break;
case Tag::ATTRIBUTED: case Model\Tag::ATTRIBUTED:
$output .= DI::l10n()->t('<b>Attributed To:</b> %s<br>', implode(', ', $receiver)); $output .= $this->t('<b>Attributed To:</b> %s<br>', implode(', ', $receiver));
break; break;
} }
} }