Rework Module\Profile\Contacts class

- Simplify imports
- Use router parameters
- Use exceptions
- Simplify querying and result processing
- Add no result label
This commit is contained in:
Hypolite Petovan 2020-08-04 23:02:43 -04:00
parent 4c5fe20018
commit 1723903219
3 changed files with 80 additions and 59 deletions

View file

@ -28,57 +28,53 @@ use Friendica\Core\Renderer;
use Friendica\Core\Session;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Model\Profile;
use Friendica\Module\BaseProfile;
use Friendica\Module\Contact as ModuleContact;
use Friendica\Model;
use Friendica\Module;
use Friendica\Network\HTTPException;
class Contacts extends BaseProfile
class Contacts extends Module\BaseProfile
{
public static function content(array $parameters = [])
{
if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
}
$a = DI::app();
//@TODO: Get value from router parameters
$nickname = $a->argv[1];
$type = ($a->argv[3] ?? '') ?: 'all';
$nickname = $parameters['nickname'];
$type = $parameters['type'] ?? 'all';
Nav::setSelected('home');
Model\Profile::load($a, $nickname);
$user = DBA::selectFirst('user', [], ['nickname' => $nickname, 'blocked' => false]);
if (!DBA::isResult($user)) {
throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
if (empty($a->profile)) {
throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
}
$a->profile_uid = $user['uid'];
if (!empty($a->profile['hide-friends'])) {
throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
}
Profile::load($a, $nickname);
Nav::setSelected('home');
$is_owner = $a->profile['uid'] == local_user();
$o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname);
if (!count($a->profile) || $a->profile['hide-friends']) {
notice(DI::l10n()->t('Permission denied.'));
return $o;
}
$condition = [
'uid' => $a->profile['uid'],
'blocked' => false,
'pending' => false,
'hidden' => false,
'archive' => false,
'self' => false,
'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
];
switch ($type) {
case 'followers': $condition['rel'] = [1, 3]; break;
case 'following': $condition['rel'] = [2, 3]; break;
case 'mutuals': $condition['rel'] = 3; break;
case 'followers': $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND]; break;
case 'following': $condition['rel'] = [Model\Contact::SHARING, Model\Contact::FRIEND]; break;
case 'mutuals': $condition['rel'] = Model\Contact::FRIEND; break;
}
$total = DBA::count('contact', $condition);
@ -87,42 +83,44 @@ class Contacts extends BaseProfile
$params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$contacts_stmt = DBA::select('contact', [], $condition, $params);
if (!DBA::isResult($contacts_stmt)) {
notice(DI::l10n()->t('No contacts.'));
return $o;
}
$contacts = [];
while ($contact = DBA::fetch($contacts_stmt)) {
if ($contact['self']) {
continue;
}
$contacts[] = ModuleContact::getContactTemplateVars($contact);
}
DBA::close($contacts_stmt);
$contacts = array_map(
[Module\Contact::class, 'getContactTemplateVars'],
Model\Contact::selectToArray([], $condition, $params)
);
$desc = '';
switch ($type) {
case 'followers': $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total); break;
case 'following': $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total); break;
case 'mutuals': $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total); break;
case 'all': default: $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total); break;
case 'followers':
$title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
break;
case 'following':
$title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total);
break;
case 'mutuals':
$title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
$desc = DI::l10n()->t(
'These contacts both follow and are followed by <strong>%s</strong>.',
htmlentities($a->profile['name'], ENT_COMPAT, 'UTF-8')
);
break;
case 'all':
default:
$title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total);
break;
}
$tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
$o .= Renderer::replaceMacros($tpl, [
'$title' => $title,
'$desc' => $desc,
'$nickname' => $nickname,
'$type' => $type,
'$all_label' => DI::l10n()->t('All contacts'),
'$all_label' => DI::l10n()->t('All contacts'),
'$followers_label' => DI::l10n()->t('Followers'),
'$following_label' => DI::l10n()->t('Following'),
'$mutuals_label' => DI::l10n()->t('Mutual friends'),
'$mutuals_label' => DI::l10n()->t('Mutual friends'),
'$noresult_label' => DI::l10n()->t('No contacts.'),
'$contacts' => $contacts,
'$paginate' => $pager->renderFull($total),

View file

@ -1,18 +1,26 @@
<div class="generic-page-wrapper">
{{include file="section_title.tpl"}}
{{if $desc}}
<p>{{$desc nofilter}}</p>
{{/if}}
<ul role="menubar" class="tabs">
<li role="menuitem"><a href="profile/{{$nickname}}/contacts" class="tab button{{if !$type || $type == 'all'}} active{{/if}}">{{$all_label}}</a></li>
<li role="menuitem"><a href="profile/{{$nickname}}/contacts/followers" class="tab button{{if $type == 'followers'}} active{{/if}}">{{$followers_label}}</a></li>
<li role="menuitem"><a href="profile/{{$nickname}}/contacts/following" class="tab button{{if $type == 'following'}} active{{/if}}">{{$following_label}}</a></li>
<li role="menuitem"><a href="profile/{{$nickname}}/contacts/mutuals" class="tab button{{if $type == 'mutuals'}} active{{/if}}">{{$mutuals_label}}</a></li>
</ul>
{{if $contacts}}
<div id="viewcontact_wrapper-{{$id}}">
{{foreach $contacts as $contact}}
{{foreach $contacts as $contact}}
{{include file="contact_template.tpl"}}
{{/foreach}}
{{/foreach}}
</div>
{{else}}
<div class="alert alert-info" role="alert">{{$noresult_label}}</div>
{{/if}}
<div class="clear"></div>
<div id="view-contact-end"></div>

View file

@ -1,18 +1,33 @@
<div class="generic-page-wrapper">
{{include file="section_title.tpl"}}
<ul class="nav nav-tabs">
<li role="presentation"{{if !$type || $type == 'all'}} class="active"{{/if}}><a href="profile/{{$nickname}}/contacts">{{$all_label}}</a></li>
<li role="presentation"{{if $type == 'followers'}} class="active"{{/if}}><a href="profile/{{$nickname}}/contacts/followers">{{$followers_label}}</a></li>
<li role="presentation"{{if $type == 'following'}} class="active"{{/if}}><a href="profile/{{$nickname}}/contacts/following">{{$following_label}}</a></li>
<li role="presentation"{{if $type == 'mutuals'}} class="active"{{/if}}><a href="profile/{{$nickname}}/contacts/mutuals">{{$mutuals_label}}</a></li>
</ul>
{{if $desc}}
<p>{{$desc nofilter}}</p>
{{/if}}
<ul id="viewcontact_wrapper{{if $id}}-{{$id}}{{/if}}" class="viewcontact_wrapper media-list">
{{foreach $contacts as $contact}}
<li>{{include file="contact_template.tpl"}}</li>
{{/foreach}}
<ul class="nav nav-tabs">
<li role="presentation"{{if !$type || $type == 'all'}} class="active"{{/if}}>
<a href="profile/{{$nickname}}/contacts">{{$all_label}}</a>
</li>
<li role="presentation"{{if $type == 'followers'}} class="active"{{/if}}>
<a href="profile/{{$nickname}}/contacts/followers">{{$followers_label}}</a>
</li>
<li role="presentation"{{if $type == 'following'}} class="active"{{/if}}>
<a href="profile/{{$nickname}}/contacts/following">{{$following_label}}</a>
</li>
<li role="presentation"{{if $type == 'mutuals'}} class="active"{{/if}}>
<a href="profile/{{$nickname}}/contacts/mutuals">{{$mutuals_label}}</a>
</li>
</ul>
{{if $contacts}}
<ul id="viewcontact_wrapper{{if $id}}-{{$id}}{{/if}}" class="viewcontact_wrapper media-list">
{{foreach $contacts as $contact}}
<li>{{include file="contact_template.tpl"}}</li>
{{/foreach}}
</ul>
{{else}}
<div class="alert alert-info" role="alert">{{$noresult_label}}</div>
{{/if}}
<div class="clear"></div>
<div id="view-contact-end"></div>