]> git.mxchange.org Git - friendica.git/commitdiff
Rework commonFriendsVisitor widget
authorHypolite Petovan <hypolite@mrpetovan.com>
Wed, 5 Aug 2020 03:06:11 +0000 (23:06 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Wed, 5 Aug 2020 04:19:48 +0000 (00:19 -0400)
- Use new Contact\Relation method to fetch common contacts
- Replace reference to /common by /{nickname}/contacts/common

src/Content/Widget.php
src/Module/Profile/Status.php
view/templates/widget/remote_friends_common.tpl

index e8e70c0e11cf097fee20a05757e92f4e75674be2..a7ce52cc466107f8ecaa1cbf1b9eb7c3ad799d24 100644 (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;
-               }
-
-               $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'];
-                                       }
-                               }
-                       }
+               if (local_user() == $uid) {
+                       return '';
                }
 
-               if ($cid == 0 && $zcid == 0) {
-                       return;
+               $visitorPCid = local_user() ? Contact::getPublicIdByUserId(local_user()) : remote_user();
+               if (!$visitorPCid) {
+                       return '';
                }
 
-               if ($cid) {
-                       $t = GContact::countCommonFriends($profile_uid, $cid);
-               } else {
-                       $t = GContact::countCommonFriendsZcid($profile_uid, $zcid);
-               }
+               $localPCid = Contact::getPublicIdByUserId($uid);
 
-               if (!$t) {
-                       return;
-               }
+               $condition = [
+                       'NOT `self` AND NOT `blocked` AND NOT `hidden` AND `id` != ?',
+                       $localPCid,
+               ];
 
-               if ($cid) {
-                       $r = GContact::commonFriends($profile_uid, $cid, 0, 5, true);
-               } else {
-                       $r = GContact::commonFriendsZcid($profile_uid, $zcid, 0, 5, true);
+               $total = Contact\Relation::countCommon($localPCid, $visitorPCid, $condition);
+               if (!$total) {
+                       return '';
                }
 
-               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
                ]);
        }
 
index 4b36cdd4de72213a2c73856e5342d1fe9f994db6..200e03ca752876d86edb4ea9805183259a3ad5ac 100644 (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;
index 4ae682f4361bb453b3ac5d4a8e5c12faab79555c..74d8e668049e911761ead4a6b3cbe2b5d07b3826 100644 (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>
-