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
* @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) {
return;
if (local_user() == $uid) {
return '';
}
$zcid = 0;
$cid = Session::getRemoteContactID($profile_uid);
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'];
}
}
}
$visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
if (!$visitorPCid) {
return '';
}
if ($cid == 0 && $zcid == 0) {
return;
$localPCid = Contact::getPublicIdByUserId($uid);
$condition = [
'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
$localPCid,
];
$total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
if (!$total) {
return '';
}
if ($cid) {
$t = GContact::countCommonFriends($profile_uid, $cid);
} else {
$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;
$commonContacts = Contact\Relation::listCommon($localPCid, $visitorPCid, $condition, 0, 5, true);
if (!DBA::isResult($commonContacts)) {
return '';
}
$entries = [];
foreach ($r as $rr) {
$contact = Contact::getByURL($rr['url']);
$entry = [
'url' => Contact::magicLink($rr['url']),
'name' => $contact['name'] ?? $rr['name'],
'photo' => Contact::getThumb($contact, $rr['photo']),
foreach ($commonContacts as $contact) {
$entries[] = [
'url' => Contact::magicLink($contact['url']),
'name' => $contact['name'],
'photo' => Contact::getThumb($contact),
];
$entries[] = $entry;
}
$tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.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(),
'$uid' => $profile_uid,
'$cid' => (($cid) ? $cid : '0'),
'$linkmore' => (($t > 5) ? 'true' : ''),
'$nickname' => $nickname,
'$linkmore' => $total > 5 ? 'true' : '',
'$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 .= Widget::commonFriendsVisitor($a->profile['uid']);
$o .= Widget::commonFriendsVisitor($a->profile['uid'], $a->profile['nickname']);
$commpage = $a->profile['page-flags'] == User::PAGE_FLAGS_COMMUNITY;
$commvisitor = $commpage && $remote_contact;

View file

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