From 077b57ecb38e7114539a54c0670a4930d434c276 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 6 Aug 2020 10:29:19 -0400 Subject: [PATCH 1/6] Actually destroy session on logout --- src/Core/Session/Native.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Core/Session/Native.php b/src/Core/Session/Native.php index 49550a27c0..83ed0f6e6e 100644 --- a/src/Core/Session/Native.php +++ b/src/Core/Session/Native.php @@ -53,4 +53,9 @@ class Native extends AbstractSession implements ISession session_start(); return $this; } + + public function clear() + { + session_destroy(); + } } From b45ba63dbf4ab9e5c2842907a0aa5123ab474771 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 6 Aug 2020 10:30:06 -0400 Subject: [PATCH 2/6] Add mutuals and all methods in Contact\Relation - Remove unused $fields parameters from list methods - Fix wrong SQL condition in listCommon --- src/Model/Contact/Relation.php | 103 +++++++++++++++++++++++++++++---- 1 file changed, 93 insertions(+), 10 deletions(-) diff --git a/src/Model/Contact/Relation.php b/src/Model/Contact/Relation.php index cc0570ec63..d1e51811ff 100644 --- a/src/Model/Contact/Relation.php +++ b/src/Model/Contact/Relation.php @@ -335,7 +335,6 @@ class Relation * Returns a paginated list of contacts that are followed the provided public contact. * * @param int $cid Public contact id - * @param array $field Field list * @param array $condition Additional condition on the contact table * @param int $count * @param int $offset @@ -343,14 +342,14 @@ class Relation * @return array * @throws Exception */ - public static function listFollows(int $cid, array $fields = [], array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) + public static function listFollows(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) { $condition = DBA::mergeConditions($condition, ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)', $cid] ); - return DI::dba()->selectToArray('contact', $fields, $condition, + return DI::dba()->selectToArray('contact', [], $condition, ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']] ); } @@ -377,7 +376,6 @@ class Relation * Returns a paginated list of contacts that follow the provided public contact. * * @param int $cid Public contact id - * @param array $field Field list * @param array $condition Additional condition on the contact table * @param int $count * @param int $offset @@ -385,17 +383,104 @@ class Relation * @return array * @throws Exception */ - public static function listFollowers(int $cid, array $fields = [], array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) + public static function listFollowers(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) { $condition = DBA::mergeConditions($condition, ['`id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)', $cid] ); - return DI::dba()->selectToArray('contact', $fields, $condition, + return DI::dba()->selectToArray('contact', [], $condition, ['limit' => [$offset, $count], 'order' => [$shuffle ? 'RAND()' : 'name']] ); } + /** + * Counts the number of contacts that are known mutuals with the provided public contact. + * + * @param int $cid Public contact id + * @param array $condition Additional condition array on the contact table + * @return int + * @throws Exception + */ + public static function countMutuals(int $cid, array $condition = []) + { + $condition = DBA::mergeConditions($condition, + ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) + AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)', + $cid, $cid] + ); + + return DI::dba()->count('contact', $condition); + } + + /** + * Returns a paginated list of contacts that are known mutuals with the provided public contact. + * + * @param int $cid Public contact id + * @param array $condition Additional condition on the contact table + * @param int $count + * @param int $offset + * @param bool $shuffle + * @return array + * @throws Exception + */ + public static function listMutuals(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) + { + $condition = DBA::mergeConditions($condition, + ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) + AND `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)', + $cid, $cid] + ); + + return DI::dba()->selectToArray('contact', [], $condition, + ['limit' => [$offset, $count], 'order' => [$shuffle ? 'name' : 'RAND()']] + ); + } + + + /** + * Counts the number of contacts with any relationship with the provided public contact. + * + * @param int $cid Public contact id + * @param array $condition Additional condition array on the contact table + * @return int + * @throws Exception + */ + public static function countAll(int $cid, array $condition = []) + { + $condition = DBA::mergeConditions($condition, + ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) + OR `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)', + $cid, $cid] + ); + + return DI::dba()->count('contact', $condition); + } + + /** + * Returns a paginated list of contacts with any relationship with the provided public contact. + * + * @param int $cid Public contact id + * @param array $condition Additional condition on the contact table + * @param int $count + * @param int $offset + * @param bool $shuffle + * @return array + * @throws Exception + */ + public static function listAll(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) + { + $condition = DBA::mergeConditions($condition, + ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) + OR `id` IN (SELECT `cid` FROM `contact-relation` WHERE `relation-cid` = ? AND `follows`)', + $cid, $cid] + ); + + return DI::dba()->selectToArray('contact', [], $condition, + ['limit' => [$offset, $count], 'order' => [$shuffle ? 'name' : 'RAND()']] + ); + } + /** * Counts the number of contacts that both provided public contacts have interacted with at least once. * Interactions include follows and likes and comments on public posts. @@ -423,7 +508,6 @@ class Relation * * @param int $sourceId Public contact id * @param int $targetId Public contact id - * @param array $field Field list * @param array $condition Additional condition on the contact table * @param int $count * @param int $offset @@ -434,8 +518,8 @@ class Relation public static function listCommon(int $sourceId, int $targetId, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false) { $condition = DBA::mergeConditions($condition, - ["`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`) - AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)", + ["`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?) + AND `id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ?)", $sourceId, $targetId] ); @@ -444,7 +528,6 @@ class Relation ); } - /** * Counts the number of contacts that are followed by both provided public contacts. * From f5ea07c73118560ee485aa669e23768eb597a00a Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 6 Aug 2020 10:31:40 -0400 Subject: [PATCH 3/6] Remove unused App parameter from Module\Contact::getTabsHTML --- mod/common.php | 2 +- src/Module/AllFriends.php | 2 +- src/Module/Contact.php | 10 +++++----- src/Module/Contact/Advanced.php | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/mod/common.php b/mod/common.php index d4ee97c4ff..fe63a653be 100644 --- a/mod/common.php +++ b/mod/common.php @@ -120,7 +120,7 @@ function common_content(App $a) $title = ''; $tab_str = ''; if ($cmd === 'loc' && $cid && local_user() == $uid) { - $tab_str = Module\Contact::getTabsHTML($a, $contact, 5); + $tab_str = Module\Contact::getTabsHTML($contact, 5); } else { $title = DI::l10n()->t('Common Friends'); } diff --git a/src/Module/AllFriends.php b/src/Module/AllFriends.php index f41b8b0d71..9f1152f26e 100644 --- a/src/Module/AllFriends.php +++ b/src/Module/AllFriends.php @@ -72,7 +72,7 @@ class AllFriends extends BaseModule return DI::l10n()->t('No friends to display.'); } - $tab_str = Contact::getTabsHTML($app, $contact, 4); + $tab_str = Contact::getTabsHTML($contact, 4); $entries = []; foreach ($friends as $friend) { diff --git a/src/Module/Contact.php b/src/Module/Contact.php index 750a927e88..3085bbd0b8 100644 --- a/src/Module/Contact.php +++ b/src/Module/Contact.php @@ -531,7 +531,7 @@ class Contact extends BaseModule $nettype = DI::l10n()->t('Network type: %s', ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol'])); // tabs - $tab_str = self::getTabsHTML($a, $contact, 3); + $tab_str = self::getTabsHTML($contact, 3); $lost_contact = (($contact['archive'] && $contact['term-date'] > DBA::NULL_DATETIME && $contact['term-date'] < DateTimeFormat::utcNow()) ? DI::l10n()->t('Communications lost with this contact!') : ''); @@ -855,14 +855,14 @@ class Contact extends BaseModule * * Available Pages are 'Status', 'Profile', 'Contacts' and 'Common Friends' * - * @param App $a * @param array $contact The contact array * @param int $active_tab 1 if tab should be marked as active * * @return string HTML string of the contact page tabs buttons. * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException */ - public static function getTabsHTML($a, $contact, $active_tab) + public static function getTabsHTML(array $contact, int $active_tab) { $cid = $pcid = $contact['id']; $data = Model\Contact::getPublicAndUserContacID($contact['id'], local_user()); @@ -964,7 +964,7 @@ class Contact extends BaseModule $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]); if (!$update) { - $o .= self::getTabsHTML($a, $contact, 1); + $o .= self::getTabsHTML($contact, 1); } if (DBA::isResult($contact)) { @@ -988,7 +988,7 @@ class Contact extends BaseModule { $contact = DBA::selectFirst('contact', ['uid', 'url', 'id'], ['id' => $contact_id, 'deleted' => false]); - $o = self::getTabsHTML($a, $contact, 2); + $o = self::getTabsHTML($contact, 2); if (DBA::isResult($contact)) { DI::page()['aside'] = ''; diff --git a/src/Module/Contact/Advanced.php b/src/Module/Contact/Advanced.php index be1e874a57..2ba7fb185a 100644 --- a/src/Module/Contact/Advanced.php +++ b/src/Module/Contact/Advanced.php @@ -125,7 +125,7 @@ class Advanced extends BaseModule $remote_self_options = ['0' => DI::l10n()->t('No mirroring'), '2' => DI::l10n()->t('Mirror as my own posting')]; } - $tab_str = Contact::getTabsHTML(DI::app(), $contact, 6); + $tab_str = Contact::getTabsHTML($contact, 6); $tpl = Renderer::getMarkupTemplate('contact/advanced.tpl'); return Renderer::replaceMacros($tpl, [ From 383ddb10ed0184cdbd628b91ff73cc07b214b1c2 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 6 Aug 2020 10:34:11 -0400 Subject: [PATCH 4/6] Create new page_tabs template - Replace explicit tabs in profile contacts page with new template --- src/BaseModule.php | 36 +++++++++++++++++++ src/Module/Profile/Common.php | 15 +++----- src/Module/Profile/Contacts.php | 11 ++---- view/templates/page_tabs.tpl | 1 + view/templates/profile/contacts.tpl | 11 ++---- view/theme/frio/templates/common_tabs.tpl | 4 +-- view/theme/frio/templates/page_tabs.tpl | 7 ++++ .../theme/frio/templates/profile/contacts.tpl | 21 ++--------- 8 files changed, 57 insertions(+), 49 deletions(-) create mode 100644 view/templates/page_tabs.tpl create mode 100644 view/theme/frio/templates/page_tabs.tpl diff --git a/src/BaseModule.php b/src/BaseModule.php index 0e0fedb80c..ce7774bfd0 100644 --- a/src/BaseModule.php +++ b/src/BaseModule.php @@ -171,4 +171,40 @@ abstract class BaseModule throw new \Friendica\Network\HTTPException\ForbiddenException(); } } + + protected static function getContactFilterTabs(string $baseUrl, string $current, bool $displayCommonTab) + { + $tabs = [ + [ + 'label' => DI::l10n()->t('All contacts'), + 'url' => $baseUrl . '/contacts', + 'sel' => !$current || $current == 'all' ? 'active' : '', + ], + [ + 'label' => DI::l10n()->t('Followers'), + 'url' => $baseUrl . '/contacts/followers', + 'sel' => $current == 'followers' ? 'active' : '', + ], + [ + 'label' => DI::l10n()->t('Following'), + 'url' => $baseUrl . '/contacts/following', + 'sel' => $current == 'following' ? 'active' : '', + ], + [ + 'label' => DI::l10n()->t('Mutual friends'), + 'url' => $baseUrl . '/contacts/mutuals', + 'sel' => $current == 'mutuals' ? 'active' : '', + ], + ]; + + if ($displayCommonTab) { + $tabs[] = [ + 'label' => DI::l10n()->t('Common'), + 'url' => $baseUrl . '/contacts/common', + 'sel' => $current == 'common' ? 'active' : '', + ]; + } + + return $tabs; + } } diff --git a/src/Module/Profile/Common.php b/src/Module/Profile/Common.php index 0b08d327c9..a9d00fce5f 100644 --- a/src/Module/Profile/Common.php +++ b/src/Module/Profile/Common.php @@ -53,8 +53,6 @@ class Common extends BaseProfile throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.')); } - $o = self::getTabsHTML($a, 'contacts', false, $nickname); - if (!empty($a->profile['hide-friends'])) { throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.')); } @@ -65,6 +63,10 @@ class Common extends BaseProfile $a->redirect('profile/' . $nickname . '/contacts'); }; + $o = self::getTabsHTML($a, 'contacts', false, $nickname); + + $tabs = self::getContactFilterTabs('profile/' . $nickname, 'common', $displayCommonTab); + $sourceId = Contact::getIdForURL(Profile::getMyURL()); $targetId = Contact::getPublicIdByUserId($a->profile['uid']); @@ -92,15 +94,8 @@ class Common extends BaseProfile $o .= Renderer::replaceMacros($tpl, [ '$title' => $title, '$desc' => $desc, - '$nickname' => $nickname, - '$type' => 'common', - '$displayCommonTab' => $displayCommonTab, + '$tabs' => $tabs, - '$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'), - '$common_label' => DI::l10n()->t('Common'), '$noresult_label' => DI::l10n()->t('No common contacts.'), '$contacts' => $contacts, diff --git a/src/Module/Profile/Contacts.php b/src/Module/Profile/Contacts.php index 938dbd3d05..6981b43a42 100644 --- a/src/Module/Profile/Contacts.php +++ b/src/Module/Profile/Contacts.php @@ -61,6 +61,8 @@ class Contacts extends Module\BaseProfile $o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname); + $tabs = self::getContactFilterTabs('profile/' . $nickname, $type, Session::isAuthenticated() && $a->profile['uid'] != local_user()); + $condition = [ 'uid' => $a->profile['uid'], 'blocked' => false, @@ -113,15 +115,8 @@ class Contacts extends Module\BaseProfile $o .= Renderer::replaceMacros($tpl, [ '$title' => $title, '$desc' => $desc, - '$nickname' => $nickname, - '$type' => $type, - '$displayCommonTab' => Session::isAuthenticated() && $a->profile['uid'] != local_user(), + '$tabs' => $tabs, - '$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'), - '$common_label' => DI::l10n()->t('Common'), '$noresult_label' => DI::l10n()->t('No contacts.'), '$contacts' => $contacts, diff --git a/view/templates/page_tabs.tpl b/view/templates/page_tabs.tpl new file mode 100644 index 0000000000..d5bf9c5761 --- /dev/null +++ b/view/templates/page_tabs.tpl @@ -0,0 +1 @@ +{{include file="common_tabs.tpl" tabs=$tabs}} \ No newline at end of file diff --git a/view/templates/profile/contacts.tpl b/view/templates/profile/contacts.tpl index 4a1379f17b..7e2ae9e83d 100644 --- a/view/templates/profile/contacts.tpl +++ b/view/templates/profile/contacts.tpl @@ -5,15 +5,8 @@

{{$desc nofilter}}

{{/if}} - + {{include file="page_tabs.tpl" tabs=$tabs}} + {{if $contacts}}
{{foreach $contacts as $contact}} diff --git a/view/theme/frio/templates/common_tabs.tpl b/view/theme/frio/templates/common_tabs.tpl index 76c6db0398..27ae11686b 100644 --- a/view/theme/frio/templates/common_tabs.tpl +++ b/view/theme/frio/templates/common_tabs.tpl @@ -4,7 +4,7 @@