Rework commonFriendsVisitor widget

- Use new Contact\Relation method to fetch common contacts
- Replace reference to /common by /{nickname}/contacts/common
This commit is contained in:
Hypolite Petovan 2020-08-04 23:06:11 -04:00
parent 71db6ab613
commit c26b72a426
3 changed files with 38 additions and 64 deletions

View file

@ -374,81 +374,59 @@ class Widget
} }
/** /**
* Return common friends visitor widget * Show a random selection of five common contacts between the visitor and the viewed profile user.
* *
* @param string $profile_uid uid * @param int $uid Viewed profile user ID
* @param string $nickname Viewed profile user nickname
* @return string|void * @return string|void
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException
*/ */
public static function commonFriendsVisitor($profile_uid) public static function commonFriendsVisitor(int $uid, string $nickname)
{ {
if (local_user() == $profile_uid) { if (local_user() == $uid) {
return; return '';
} }
$zcid = 0; $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
if (!$visitorPCid) {
$cid = Session::getRemoteContactID($profile_uid); return '';
if (!$cid) {
if (Profile::getMyURL()) {
$contact = DBA::selectFirst('contact', ['id'],
['nurl' => Strings::normaliseLink(Profile::getMyURL()), 'uid' => $profile_uid]);
if (DBA::isResult($contact)) {
$cid = $contact['id'];
} else {
$gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(Profile::getMyURL())]);
if (DBA::isResult($gcontact)) {
$zcid = $gcontact['id'];
}
}
}
} }
if ($cid == 0 && $zcid == 0) { $localPCid = Contact::getPublicIdByUserId($uid);
return;
$condition = [
'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
$localPCid,
];
$total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
if (!$total) {
return '';
} }
if ($cid) { $commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
$t = GContact::countCommonFriends($profile_uid, $cid); if (!DBA::isResult($commonContacts)) {
} else { return '';
$t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
}
if (!$t) {
return;
}
if ($cid) {
$r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
} else {
$r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
}
if (!DBA::isResult($r)) {
return;
} }
$entries = []; $entries = [];
foreach ($r as $rr) { foreach ($commonContacts as $contact) {
$contact = Contact::getByURL($rr['url']); $entries[] = [
$entry = [ 'url' => Contact::magicLink($contact['url']),
'url' => Contact::magicLink($rr['url']), 'name' => $contact['name'],
'name' => $contact['name'] ?? $rr['name'], 'photo' => Contact::getThumb($contact),
'photo' => Contact::getThumb($contact, $rr['photo']),
]; ];
$entries[] = $entry;
} }
$tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl'); $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl');
return Renderer::replaceMacros($tpl, [ return Renderer::replaceMacros($tpl, [
'$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $t), '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total),
'$base' => DI::baseUrl(), '$base' => DI::baseUrl(),
'$uid' => $profile_uid, '$nickname' => $nickname,
'$cid' => (($cid) ? $cid : '0'), '$linkmore' => $total > 5 ? 'true' : '',
'$linkmore' => (($t > 5) ? 'true' : ''),
'$more' => DI::l10n()->t('show more'), '$more' => DI::l10n()->t('show more'),
'$items' => $entries '$contacts' => $entries
]); ]);
} }

View file

@ -108,7 +108,7 @@ class Status extends BaseProfile
$o .= self::getTabsHTML($a, 'status', $is_owner, $a->profile['nickname']); $o .= self::getTabsHTML($a, 'status', $is_owner, $a->profile['nickname']);
$o .= Widget::commonFriendsVisitor($a->profile['uid']); $o .= Widget::commonFriendsVisitor($a->profile['uid'], $a->profile['nickname']);
$commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY; $commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
$commvisitor = $commpage && $remote_contact; $commvisitor = $commpage && $remote_contact;

View file

@ -1,22 +1,18 @@
<div id="remote-friends-in-common" class="bigwidget"> <div id="remote-friends-in-common" class="bigwidget">
<div id="rfic-desc">{{$desc nofilter}} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{if $linkmore}}<a href="{{$base}}/common/rem/{{$uid}}/{{$cid}}">{{$more}}</a>{{/if}}</div> <div id="rfic-desc">{{$desc nofilter}} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{if $linkmore}}<a href="profile/{{$nickname}}/contacts/common">{{$more}}</a>{{/if}}</div>
{{if $items}} {{foreach $contacts as $contact}}
{{foreach $items as $item}}
<div class="profile-match-wrapper"> <div class="profile-match-wrapper">
<div class="profile-match-photo"> <div class="profile-match-photo">
<a href="{{$item.url}}"> <a href="{{$contact.url}}">
<img src="{{$item.photo}}" width="80" height="80" alt="{{$item.name}}" title="{{$item.name}}" /> <img src="{{$contact.photo}}" width="80" height="80" alt="{{$contact.name}}" title="{{$contact.name}}" />
</a> </a>
</div> </div>
<div class="profile-match-break"></div> <div class="profile-match-break"></div>
<div class="profile-match-name"> <div class="profile-match-name">
<a href="{{$item.url}}" title="{{$item.name}}">{{$item.name}}</a> <a href="{{$contact.url}}" title="{{$contact.name}}">{{$contact.name}}</a>
</div> </div>
<div class="profile-match-end"></div> <div class="profile-match-end"></div>
</div> </div>
{{/foreach}} {{/foreach}}
{{/if}}
<div id="rfic-end" class="clear"></div> <div id="rfic-end" class="clear"></div>
</div> </div>