From c22ef3e79e0fb22f41f8d425e3d89c50e27f7de9 Mon Sep 17 00:00:00 2001 From: very-ape Date: Fri, 21 May 2021 14:28:01 -0700 Subject: [PATCH 1/4] Fix message button using a more targeted approach. --- src/Model/Profile.php | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 1476d67a5..0efdb7ae6 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -145,7 +145,7 @@ class Profile */ public static function load(App $a, $nickname, array $profiledata = [], $show_connect = true) { - $user = User::getByNickname($nickname); + $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname, 'account_removed' => false]); if (!DBA::isResult($user) && empty($profiledata)) { Logger::log('profile error: ' . DI::args()->getQueryString(), Logger::DEBUG); @@ -166,17 +166,7 @@ class Profile } } - if (empty($user['uid'])) { - $profile = []; - } else { - $contact_id = Contact::getIdForURL(Strings::normaliseLink(DI::baseurl() . '/profile/' . $nickname), local_user()); - $profile = array_merge( - $user, - Contact::getById($contact_id), - Profile::getByUID($user['uid']), - ); - $profile['cid'] = $contact_id; - } + $profile = !empty($user['uid']) ? User::getOwnerDataById($user['uid'], false) : []; if (empty($profile) && empty($profiledata)) { Logger::log('profile error: ' . DI::args()->getQueryString(), Logger::DEBUG); @@ -273,8 +263,17 @@ class Profile $o = ''; $location = false; - // This function can also use contact information in $profile - $is_contact = !empty($profile['cid']); + // This function can also use contact information in $profile, but the 'cid' + // value is going to be coming from 'owner-view', which means it's the wrong + // contact ID for the user viewing this page. Use 'nurl' to look up the + // correct contact table entry for the logged-in user. + $is_contact = !empty($profile['nurl']); + $contact = []; + + if ($is_contact) { + $contact_id = Contact::getIdForURL($profile['nurl'], local_user()); + $contact = Contact::getById($contact_id); + } if (empty($profile['nickname'])) { Logger::warning('Received profile with no nickname', ['profile' => $profile, 'callstack' => System::callstack(10)]); @@ -302,14 +301,16 @@ class Profile $subscribe_feed_link = null; $wallmessage_link = null; + // Who is the logged-in user to this profile? $visitor_contact = []; if (!empty($profile['uid']) && self::getMyURL()) { $visitor_contact = Contact::selectFirst(['rel'], ['uid' => $profile['uid'], 'nurl' => Strings::normaliseLink(self::getMyURL())]); } + // Who is this profile to the logged-in user? $profile_contact = []; - if (!empty($profile['cid']) && self::getMyURL()) { - $profile_contact = Contact::selectFirst(['rel'], ['id' => $profile['cid']]); + if (!empty($contact) && self::getMyURL()) { + $profile_contact = $contact['rel']; } $profile_is_dfrn = $profile['network'] == Protocol::DFRN; @@ -342,9 +343,9 @@ class Profile $subscribe_feed_link = 'dfrn_poll/' . $profile['nickname']; } - if (Contact::canReceivePrivateMessages($profile)) { + if (Contact::canReceivePrivateMessages($contact)) { if ($visitor_is_followed || $visitor_is_following) { - $wallmessage_link = $visitor_base_path . '/message/new/' . $profile['cid']; + $wallmessage_link = $visitor_base_path . '/message/new/' . $contact['id']; } elseif ($visitor_is_authenticated && !empty($profile['unkmail'])) { $wallmessage_link = 'wallmessage/' . $profile['nickname']; } From 34eb81a18786dd9b2acef301465a2e4bb47ba2c8 Mon Sep 17 00:00:00 2001 From: very-ape Date: Fri, 21 May 2021 22:49:35 -0700 Subject: [PATCH 2/4] Don't retrieve a contact record we've already been given. --- src/Model/Profile.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 0efdb7ae6..e883078f0 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -271,8 +271,11 @@ class Profile $contact = []; if ($is_contact) { - $contact_id = Contact::getIdForURL($profile['nurl'], local_user()); - $contact = Contact::getById($contact_id); + if (local_user() && ($profile['uid'] ?? '') != local_user()) { + $contact = Contact::getById(Contact::getIdForURL($profile['nurl'], local_user())); + } else { + $contact = $profile; + } } if (empty($profile['nickname'])) { From ceb409dae8f997230f891150ccf638e93633d67f Mon Sep 17 00:00:00 2001 From: very-ape Date: Sat, 22 May 2021 15:30:46 -0700 Subject: [PATCH 3/4] Replace $contact with $profile_contact in sidebar function. --- src/Model/Profile.php | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/Model/Profile.php b/src/Model/Profile.php index e883078f0..4ce5cfcca 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -268,13 +268,13 @@ class Profile // contact ID for the user viewing this page. Use 'nurl' to look up the // correct contact table entry for the logged-in user. $is_contact = !empty($profile['nurl']); - $contact = []; + $profile_contact = []; if ($is_contact) { if (local_user() && ($profile['uid'] ?? '') != local_user()) { - $contact = Contact::getById(Contact::getIdForURL($profile['nurl'], local_user())); + $profile_contact = Contact::getById(Contact::getIdForURL($profile['nurl'], local_user())); } else { - $contact = $profile; + $profile_contact = $profile; } } @@ -310,12 +310,6 @@ class Profile $visitor_contact = Contact::selectFirst(['rel'], ['uid' => $profile['uid'], 'nurl' => Strings::normaliseLink(self::getMyURL())]); } - // Who is this profile to the logged-in user? - $profile_contact = []; - if (!empty($contact) && self::getMyURL()) { - $profile_contact = $contact['rel']; - } - $profile_is_dfrn = $profile['network'] == Protocol::DFRN; $profile_is_native = in_array($profile['network'], Protocol::NATIVE_SUPPORT); $local_user_is_self = self::getMyURL() && ($profile['url'] == self::getMyURL()); @@ -346,9 +340,9 @@ class Profile $subscribe_feed_link = 'dfrn_poll/' . $profile['nickname']; } - if (Contact::canReceivePrivateMessages($contact)) { + if (Contact::canReceivePrivateMessages($profile_contact)) { if ($visitor_is_followed || $visitor_is_following) { - $wallmessage_link = $visitor_base_path . '/message/new/' . $contact['id']; + $wallmessage_link = $visitor_base_path . '/message/new/' . $profile_contact['id']; } elseif ($visitor_is_authenticated && !empty($profile['unkmail'])) { $wallmessage_link = 'wallmessage/' . $profile['nickname']; } From 7776c5f690ec11779af2343654482534b1364135 Mon Sep 17 00:00:00 2001 From: very-ape Date: Sun, 23 May 2021 13:25:25 -0700 Subject: [PATCH 4/4] Implement code changes from review. --- src/Model/Profile.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 4ce5cfcca..194386185 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -267,14 +267,14 @@ class Profile // value is going to be coming from 'owner-view', which means it's the wrong // contact ID for the user viewing this page. Use 'nurl' to look up the // correct contact table entry for the logged-in user. - $is_contact = !empty($profile['nurl']); $profile_contact = []; - if ($is_contact) { + if (!empty($profile['nurl'] ?? '')) { if (local_user() && ($profile['uid'] ?? '') != local_user()) { $profile_contact = Contact::getById(Contact::getIdForURL($profile['nurl'], local_user())); - } else { - $profile_contact = $profile; + } + if (!empty($profile['cid']) && self::getMyURL()) { + $profile_contact = Contact::selectFirst(['rel'], ['id' => $profile['cid']]); } } @@ -349,8 +349,10 @@ class Profile } } - // show edit profile to yourself - if (!$is_contact && $local_user_is_self) { + // show edit profile to yourself, but only if this is not meant to be + // rendered as a "contact". i.e., if 'self' (a "contact" table column) isn't + // set in $profile. + if (!isset($profile['self']) && $local_user_is_self) { $profile['edit'] = [DI::baseUrl() . '/settings/profile', DI::l10n()->t('Edit profile'), '', DI::l10n()->t('Edit profile')]; $profile['menu'] = [ 'chg_photo' => DI::l10n()->t('Change profile photo'),