Replace dfrn_request by subscribe URL when available for the follow link

- Falls back to the `/remote_follow` module available since Friendica version 2020.03
- Falls back to the profile URL
- Remove unused atlas dependency in a couple API controllers
This commit is contained in:
Hypolite Petovan 2022-05-07 15:52:55 -04:00
parent 8988ad9f9d
commit 2627b54349
12 changed files with 80 additions and 31 deletions

View file

@ -47,7 +47,9 @@ Example:
"dfrn_request": "https://friendica.mrpetovan.com/dfrn_request/hypolite", "dfrn_request": "https://friendica.mrpetovan.com/dfrn_request/hypolite",
"photo": "https://friendica.mrpetovan.com/photo/27330388315ae4ed2b03e3c116980490-4.jpg?ts=1541567135", "photo": "https://friendica.mrpetovan.com/photo/27330388315ae4ed2b03e3c116980490-4.jpg?ts=1541567135",
"tags": "videogame gaming boardgame politics philosophy development programming php", "tags": "videogame gaming boardgame politics philosophy development programming php",
"last_activity": "2018-45" "last_activity": "2018-45",
"remote_follow": "https://friendica.mrpetovan.com/remote_follow/hypolite",
"subscribe": null
}, },
... ...
] ]

View file

@ -11,10 +11,6 @@ use Psr\Http\Message\ServerRequestInterface;
*/ */
class MatchSearch class MatchSearch
{ {
/**
* @var \Atlas\Pdo\Connection
*/
private $atlas;
/** /**
* @var \Friendica\Directory\Models\Profile * @var \Friendica\Directory\Models\Profile
*/ */
@ -25,12 +21,10 @@ class MatchSearch
private $l10n; private $l10n;
public function __construct( public function __construct(
\Atlas\Pdo\Connection $atlas,
\Friendica\Directory\Models\Profile $profileModel, \Friendica\Directory\Models\Profile $profileModel,
\Gettext\TranslatorInterface $l10n \Gettext\TranslatorInterface $l10n
) )
{ {
$this->atlas = $atlas;
$this->profileModel = $profileModel; $this->profileModel = $profileModel;
$this->l10n = $l10n; $this->l10n = $l10n;
} }
@ -53,7 +47,13 @@ class MatchSearch
$values = ['query' => $query]; $values = ['query' => $query];
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $sql_where, $values); $profiles = $this->profileModel->getListForDisplay(
null,
$pager->getItemsPerPage(),
$pager->getStart(),
$sql_where,
$values,
);
$results = []; $results = [];
foreach ($profiles as $profile) { foreach ($profiles as $profile) {

View file

@ -11,10 +11,6 @@ use Psr\Http\Message\ServerRequestInterface;
*/ */
class Search class Search
{ {
/**
* @var \Atlas\Pdo\Connection
*/
private $atlas;
/** /**
* @var \Friendica\Directory\Models\Profile * @var \Friendica\Directory\Models\Profile
*/ */
@ -25,12 +21,10 @@ class Search
private $l10n; private $l10n;
public function __construct( public function __construct(
\Atlas\Pdo\Connection $atlas,
\Friendica\Directory\Models\Profile $profileModel, \Friendica\Directory\Models\Profile $profileModel,
\Gettext\TranslatorInterface $l10n \Gettext\TranslatorInterface $l10n
) )
{ {
$this->atlas = $atlas;
$this->profileModel = $profileModel; $this->profileModel = $profileModel;
$this->l10n = $l10n; $this->l10n = $l10n;
} }
@ -64,7 +58,13 @@ AND `account_type` = :account_type';
$values['account_type'] = $account_type; $values['account_type'] = $account_type;
} }
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $sql_where, $values); $profiles = $this->profileModel->getListForDisplay(
null,
$pager->getItemsPerPage(),
$pager->getStart(),
$sql_where,
$values,
);
$count = $this->profileModel->getCountForDisplay($sql_where, $values); $count = $this->profileModel->getCountForDisplay($sql_where, $values);

View file

@ -18,6 +18,10 @@ class Directory extends BaseController
* @var \Atlas\Pdo\Connection * @var \Atlas\Pdo\Connection
*/ */
private $atlas; private $atlas;
/**
* @var \Friendica\Directory\Models\Server
*/
private $serverModel;
/** /**
* @var \Friendica\Directory\Models\Profile * @var \Friendica\Directory\Models\Profile
*/ */
@ -37,6 +41,7 @@ class Directory extends BaseController
public function __construct( public function __construct(
\Atlas\Pdo\Connection $atlas, \Atlas\Pdo\Connection $atlas,
\Friendica\Directory\Models\Server $serverModel,
\Friendica\Directory\Models\Profile $profileModel, \Friendica\Directory\Models\Profile $profileModel,
\Friendica\Directory\Views\Widget\AccountTypeTabs $accountTypeTabs, \Friendica\Directory\Views\Widget\AccountTypeTabs $accountTypeTabs,
\Friendica\Directory\Views\PhpRenderer $renderer, \Friendica\Directory\Views\PhpRenderer $renderer,
@ -44,6 +49,7 @@ class Directory extends BaseController
) )
{ {
$this->atlas = $atlas; $this->atlas = $atlas;
$this->serverModel = $serverModel;
$this->profileModel = $profileModel; $this->profileModel = $profileModel;
$this->accountTypeTabs = $accountTypeTabs; $this->accountTypeTabs = $accountTypeTabs;
$this->renderer = $renderer; $this->renderer = $renderer;
@ -58,16 +64,22 @@ class Directory extends BaseController
$pager = new Pager($this->l10n, $request, 20); $pager = new Pager($this->l10n, $request, 20);
$condition = ''; $sql_where = '';
$values = []; $values = [];
if (!empty($args['account_type'])) { if (!empty($args['account_type'])) {
$condition = '`account_type` = :account_type'; $sql_where = '`account_type` = :account_type';
$values = ['account_type' => $args['account_type']]; $values = ['account_type' => $args['account_type']];
} }
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $condition, $values); $profiles = $this->profileModel->getListForDisplay(
$this->serverModel->getSubscribeUrlByProfile($request->getQueryParam('zrl', '')),
$pager->getItemsPerPage(),
$pager->getStart(),
$sql_where,
$values,
);
$count = $this->profileModel->getCountForDisplay($condition, $values); $count = $this->profileModel->getCountForDisplay($sql_where, $values);
$vars = [ $vars = [
'title' => $this->l10n->gettext('People'), 'title' => $this->l10n->gettext('People'),
@ -82,7 +94,6 @@ class Directory extends BaseController
$content = $this->renderer->fetch('directory.phtml', $vars); $content = $this->renderer->fetch('directory.phtml', $vars);
// Render index view
return ['content' => $content]; return ['content' => $content];
} }
} }

View file

@ -13,6 +13,10 @@ class Search extends BaseController
* @var \Atlas\Pdo\Connection * @var \Atlas\Pdo\Connection
*/ */
private $atlas; private $atlas;
/**
* @var \Friendica\Directory\Models\Server
*/
private $serverModel;
/** /**
* @var \Friendica\Directory\Models\Profile * @var \Friendica\Directory\Models\Profile
*/ */
@ -32,6 +36,7 @@ class Search extends BaseController
public function __construct( public function __construct(
\Atlas\Pdo\Connection $atlas, \Atlas\Pdo\Connection $atlas,
\Friendica\Directory\Models\Server $serverModel,
\Friendica\Directory\Models\Profile $profileModel, \Friendica\Directory\Models\Profile $profileModel,
\Friendica\Directory\Views\Widget\AccountTypeTabs $accountTypeTabs, \Friendica\Directory\Views\Widget\AccountTypeTabs $accountTypeTabs,
\Friendica\Directory\Views\PhpRenderer $renderer, \Friendica\Directory\Views\PhpRenderer $renderer,
@ -39,6 +44,7 @@ class Search extends BaseController
) )
{ {
$this->atlas = $atlas; $this->atlas = $atlas;
$this->serverModel = $serverModel;
$this->profileModel = $profileModel; $this->profileModel = $profileModel;
$this->accountTypeTabs = $accountTypeTabs; $this->accountTypeTabs = $accountTypeTabs;
$this->renderer = $renderer; $this->renderer = $renderer;
@ -89,7 +95,13 @@ AND `account_type` = :account_type';
$values['account_type'] = $account_type; $values['account_type'] = $account_type;
} }
$profiles = $this->profileModel->getListForDisplay($pager->getItemsPerPage(), $pager->getStart(), $sql_where, $values); $profiles = $this->profileModel->getListForDisplay(
$this->serverModel->getSubscribeUrlByProfile($request->getQueryParam('zrl', '')),
$pager->getItemsPerPage(),
$pager->getStart(),
$sql_where,
$values,
);
$count = $this->profileModel->getCountForDisplay($sql_where, $values); $count = $this->profileModel->getCountForDisplay($sql_where, $values);
@ -106,7 +118,6 @@ AND `account_type` = :account_type';
$content = $this->renderer->fetch('search.phtml', $vars); $content = $this->renderer->fetch('search.phtml', $vars);
// Render index view
return ['content' => $content, 'noNavSearch' => true]; return ['content' => $content, 'noNavSearch' => true];
} }
} }

View file

@ -74,7 +74,7 @@ class Profile extends \Friendica\Directory\Model
]; ];
} }
public function getListForDisplay(int $limit = 30, int $start = 0, string $condition = '', array $values = []): array public function getListForDisplay(string $subscribeUrl = null, int $limit = 30, int $start = 0, string $condition = '', array $values = []): array
{ {
if ($condition) { if ($condition) {
$condition = 'AND ' . $condition; $condition = 'AND ' . $condition;
@ -87,7 +87,7 @@ class Profile extends \Friendica\Directory\Model
$stmt = 'SELECT p.`id`, p.`name`, p.`username`, p.`addr`, p.`account_type`, p.`language`, $stmt = 'SELECT p.`id`, p.`name`, p.`username`, p.`addr`, p.`account_type`, p.`language`,
p.`pdesc`, p.`locality`, p.`region`, p.`country`, p.`profile_url`, p.`dfrn_request`, p.`pdesc`, p.`locality`, p.`region`, p.`country`, p.`profile_url`, p.`dfrn_request`,
p.`photo`, p.`tags`, p.`last_activity` p.`photo`, p.`tags`, p.`last_activity`, s.`version`
FROM `profile` p FROM `profile` p
JOIN `server` s ON s.`id` = p.`server_id` AND s.`available` AND NOT s.`hidden` JOIN `server` s ON s.`id` = p.`server_id` AND s.`available` AND NOT s.`hidden`
WHERE p.`available` WHERE p.`available`
@ -98,6 +98,12 @@ class Profile extends \Friendica\Directory\Model
LIMIT :start, :limit'; LIMIT :start, :limit';
$profiles = $this->atlas->fetchAll($stmt, $values); $profiles = $this->atlas->fetchAll($stmt, $values);
array_walk($profiles, function (array &$profile) use ($subscribeUrl) {
$profile['remote_follow'] = version_compare($profile['version'], '2020.03', '>=') ? str_replace('/profile/', '/remote_follow/', $profile['profile_url']) : null;
$profile['subscribe'] = $subscribeUrl ? str_replace('{uri}', urlencode($profile['profile_url']), $subscribeUrl): null;
unset($profile['version']);
});
return $profiles; return $profiles;
} }

View file

@ -41,4 +41,20 @@ class Server extends \Friendica\Directory\Model
'alias' => strtolower($server_alias) 'alias' => strtolower($server_alias)
]); ]);
} }
/**
* Returns the complete subscribe URL of the given profile URL if we have it for the related server
*
* @param string $profile_url
* @return mixed|null
*/
public function getSubscribeUrlByProfile(string $profile_url)
{
if (preg_match('#^(.+)/profile/#', $profile_url, $matches)) {
$server = $this->getByUrlAlias($matches[1]);
return $server['subscribe_url'] ?? null;
}
return null;
}
} }

View file

@ -10,7 +10,6 @@ class MatchSearch extends BaseRoute
public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
{ {
return (new \Friendica\Directory\Controllers\Api\MatchSearch( return (new \Friendica\Directory\Controllers\Api\MatchSearch(
$this->container->atlas,
$this->container->get(\Friendica\Directory\Models\Profile::class), $this->container->get(\Friendica\Directory\Models\Profile::class),
$this->container->l10n $this->container->l10n
))->render($request, $response, $args); ))->render($request, $response, $args);

View file

@ -10,7 +10,6 @@ class Search extends BaseRoute
public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response public function __invoke(\Slim\Http\Request $request, \Slim\Http\Response $response, array $args): \Slim\Http\Response
{ {
return (new \Friendica\Directory\Controllers\Api\Search( return (new \Friendica\Directory\Controllers\Api\Search(
$this->container->atlas,
$this->container->get(\Friendica\Directory\Models\Profile::class), $this->container->get(\Friendica\Directory\Models\Profile::class),
$this->container->l10n $this->container->l10n
))->render($request, $response, $args); ))->render($request, $response, $args);

View file

@ -13,6 +13,7 @@ class Directory extends BaseRoute
$this->controller = new \Friendica\Directory\Controllers\Web\Directory( $this->controller = new \Friendica\Directory\Controllers\Web\Directory(
$this->container->atlas, $this->container->atlas,
$this->container->get(\Friendica\Directory\Models\Server::class),
$this->container->get(\Friendica\Directory\Models\Profile::class), $this->container->get(\Friendica\Directory\Models\Profile::class),
$this->container->get(\Friendica\Directory\Views\Widget\AccountTypeTabs::class), $this->container->get(\Friendica\Directory\Views\Widget\AccountTypeTabs::class),
$this->container->renderer, $this->container->renderer,

View file

@ -13,6 +13,7 @@ class Search extends BaseRoute
$this->controller = new \Friendica\Directory\Controllers\Web\Search( $this->controller = new \Friendica\Directory\Controllers\Web\Search(
$this->container->atlas, $this->container->atlas,
$this->container->get(\Friendica\Directory\Models\Server::class),
$this->container->get(\Friendica\Directory\Models\Profile::class), $this->container->get(\Friendica\Directory\Models\Profile::class),
$this->container->get(\Friendica\Directory\Views\Widget\AccountTypeTabs::class), $this->container->get(\Friendica\Directory\Views\Widget\AccountTypeTabs::class),
$this->container->renderer, $this->container->renderer,

View file

@ -26,13 +26,16 @@ if (!empty($profile['country'])) {
</a> </a>
<div class="media-body"> <div class="media-body">
<h5 class="name"> <h5 class="name">
<?php if ($profile['dfrn_request']): ?> <?php if ($profile['subscribe']): ?>
<a href="<?php echo $this->escapeHtmlAttr($this->u($profile['dfrn_request'])); ?>" class="card-link btn btn-primary float-right"> <a href="<?php echo $this->escapeHtmlAttr($profile['subscribe']); ?>" class="card-link btn btn-primary float-right" target="_blank" rel="noopener noreferrer">
<i class="fa fa-external-link-alt"></i> <?php echo $this->p__('verb', 'Follow')?> <i class="fa fa-external-link-alt"></i> <?php echo $this->p__('verb', 'Follow')?>
</a> </a>
<?php endif; ?> <?php elseif ($profile['remote_follow']): ?>
<?php if ($profile['follow']): ?> <a href="<?php echo $this->escapeHtmlAttr($this->u($profile['remote_follow'])); ?>" class="card-link btn btn-primary float-right" target="_blank" rel="noopener noreferrer">
<a href="<?php echo $this->escapeHtmlAttr($profile['follow']); ?>" class="card-link btn btn-primary float-right" target="_blank" rel="noopener noreferrer"> <i class="fa fa-external-link-alt"></i> <?php echo $this->p__('verb', 'Follow')?>
</a>
<?php else: ?>
<a href="<?php echo $this->escapeHtmlAttr($this->u($profile['profile_url'])); ?>" class="card-link btn btn-primary float-right" target="_blank" rel="noopener noreferrer">
<i class="fa fa-external-link-alt"></i> <?php echo $this->p__('verb', 'Follow')?> <i class="fa fa-external-link-alt"></i> <?php echo $this->p__('verb', 'Follow')?>
</a> </a>
<?php endif; ?> <?php endif; ?>