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\Core\Session;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Profile; use Friendica\Model;
use Friendica\Module\BaseProfile; use Friendica\Module;
use Friendica\Module\Contact as ModuleContact; use Friendica\Network\HTTPException;
class Contacts extends BaseProfile class Contacts extends Module\BaseProfile
{ {
public static function content(array $parameters = []) public static function content(array $parameters = [])
{ {
if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) { 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(); $a = DI::app();
//@TODO: Get value from router parameters $nickname = $parameters['nickname'];
$nickname = $a->argv[1]; $type = $parameters['type'] ?? 'all';
$type = ($a->argv[3] ?? '') ?: 'all';
Nav::setSelected('home'); Model\Profile::load($a, $nickname);
$user = DBA::selectFirst('user', [], ['nickname' => $nickname, 'blocked' => false]); if (empty($a->profile)) {
if (!DBA::isResult($user)) { throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
throw new \Friendica\Network\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(); $is_owner = $a->profile['uid'] == local_user();
$o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname); $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 = [ $condition = [
'uid' => $a->profile['uid'], 'uid' => $a->profile['uid'],
'blocked' => false, 'blocked' => false,
'pending' => false, 'pending' => false,
'hidden' => false, 'hidden' => false,
'archive' => false, 'archive' => false,
'self' => false,
'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED] 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
]; ];
switch ($type) { switch ($type) {
case 'followers': $condition['rel'] = [1, 3]; break; case 'followers': $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND]; break;
case 'following': $condition['rel'] = [2, 3]; break; case 'following': $condition['rel'] = [Model\Contact::SHARING, Model\Contact::FRIEND]; break;
case 'mutuals': $condition['rel'] = 3; break; case 'mutuals': $condition['rel'] = Model\Contact::FRIEND; break;
} }
$total = DBA::count('contact', $condition); $total = DBA::count('contact', $condition);
@ -87,35 +83,36 @@ class Contacts extends BaseProfile
$params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]]; $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
$contacts_stmt = DBA::select('contact', [], $condition, $params); $contacts = array_map(
[Module\Contact::class, 'getContactTemplateVars'],
if (!DBA::isResult($contacts_stmt)) { Model\Contact::selectToArray([], $condition, $params)
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);
$desc = '';
switch ($type) { switch ($type) {
case 'followers': $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total); break; case 'followers':
case 'following': $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total); break; $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
case 'mutuals': $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total); break; break;
case 'following':
case 'all': default: $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total); break; $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'); $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
$o .= Renderer::replaceMacros($tpl, [ $o .= Renderer::replaceMacros($tpl, [
'$title' => $title, '$title' => $title,
'$desc' => $desc,
'$nickname' => $nickname, '$nickname' => $nickname,
'$type' => $type, '$type' => $type,
@ -123,6 +120,7 @@ class Contacts extends BaseProfile
'$followers_label' => DI::l10n()->t('Followers'), '$followers_label' => DI::l10n()->t('Followers'),
'$following_label' => DI::l10n()->t('Following'), '$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, '$contacts' => $contacts,
'$paginate' => $pager->renderFull($total), '$paginate' => $pager->renderFull($total),

View file

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

View file

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