]> git.mxchange.org Git - friendica.git/commitdiff
Centzralized functionality to update and publish profile changes
authorMichael <heluecht@pirati.ca>
Tue, 15 Jun 2021 11:12:44 +0000 (11:12 +0000)
committerMichael <heluecht@pirati.ca>
Tue, 15 Jun 2021 11:12:44 +0000 (11:12 +0000)
include/api.php
mod/settings.php
src/Model/Contact.php
src/Model/Profile.php
src/Model/User.php
src/Module/Settings/Profile/Index.php
src/Module/Settings/Profile/Photo/Crop.php
update.php

index 7da32f36dd469fb2f4f005defee67f68dd9aa9c5..492d3d1cb7a7fabb4fb65782aff129c40b955f2f 100644 (file)
@@ -42,6 +42,7 @@ use Friendica\Model\Mail;
 use Friendica\Model\Notification;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
+use Friendica\Model\Profile;
 use Friendica\Model\User;
 use Friendica\Model\Verb;
 use Friendica\Network\HTTPException;
@@ -4552,12 +4553,7 @@ function api_account_update_profile_image($type)
        Contact::updateSelfFromUserID(api_user(), true);
 
        // Update global directory in background
-       $url = DI::baseUrl() . '/profile/' . DI::app()->user['nickname'];
-       if ($url && strlen(DI::config()->get('system', 'directory'))) {
-               Worker::add(PRIORITY_LOW, "Directory", $url);
-       }
-
-       Worker::add(PRIORITY_LOW, 'ProfileUpdate', api_user());
+       Profile::publishUpdate(api_user());
 
        // output for client
        if ($data) {
@@ -4608,11 +4604,7 @@ function api_account_update_profile($type)
                DBA::update('contact', ['about' => $_POST['description']], ['id' => $api_user['id']]);
        }
 
-       Worker::add(PRIORITY_LOW, 'ProfileUpdate', $local_user);
-       // Update global directory in background
-       if ($api_user['url'] && strlen(DI::config()->get('system', 'directory'))) {
-               Worker::add(PRIORITY_LOW, "Directory", $api_user['url']);
-       }
+       Profile::publishUpdate($local_user);
 
        return api_account_verify_credentials($type);
 }
index ec813398489e3fbedeb482cad15a948de763bf27..b248f017e69a08e335761938558c92002504a58a 100644 (file)
@@ -30,9 +30,9 @@ use Friendica\Core\Renderer;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
-use Friendica\Model\Contact;
 use Friendica\Model\Group;
 use Friendica\Model\Notification;
+use Friendica\Model\Profile;
 use Friendica\Model\User;
 use Friendica\Module\BaseSettings;
 use Friendica\Module\Security\Login;
@@ -447,38 +447,15 @@ function settings_post(App $a)
                $fields['openidserver'] = '';
        }
 
-       if (!DBA::update('user', $fields, ['uid' => local_user()])) {
+       $profile_fields = ['publish' => $publish, 'net-publish' => $net_publish, 'hide-friends' => $hide_friends];
+
+       if (!User::update($fields, local_user()) || !Profile::update($profile_fields, local_user())) {
                notice(DI::l10n()->t('Settings were not updated.'));
        }
 
        // clear session language
        unset($_SESSION['language']);
 
-       q("UPDATE `profile`
-               SET `publish` = %d,
-               `name` = '%s',
-               `net-publish` = %d,
-               `hide-friends` = %d
-               WHERE `uid` = %d",
-               intval($publish),
-               DBA::escape($username),
-               intval($net_publish),
-               intval($hide_friends),
-               intval(local_user())
-       );
-
-       Contact::updateSelfFromUserID(local_user());
-
-       if (($old_visibility != $net_publish) || ($page_flags != $old_page_flags)) {
-               // Update global directory in background
-               $url = $_SESSION['my_url'];
-               if ($url && strlen(DI::config()->get('system', 'directory'))) {
-                       Worker::add(PRIORITY_LOW, "Directory", $url);
-               }
-       }
-
-       Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
-
        DI::baseUrl()->redirect('settings');
        return; // NOTREACHED
 }
index 246a2967b2edc9f57dea379f38865a7fc89f0825..1b2dd3c9c142349cdca799a4ef256a3154629f55 100644 (file)
@@ -623,6 +623,7 @@ class Contact
         *
         * @param int     $uid
         * @param boolean $update_avatar Force the avatar update
+        * @return bool   "true" if updated
         * @throws HTTPException\InternalServerErrorException
         */
        public static function updateSelfFromUserID($uid, $update_avatar = false)
@@ -632,20 +633,20 @@ class Contact
                        'photo', 'thumb', 'micro', 'addr', 'request', 'notify', 'poll', 'confirm', 'poco'];
                $self = DBA::selectFirst('contact', $fields, ['uid' => $uid, 'self' => true]);
                if (!DBA::isResult($self)) {
-                       return;
+                       return false;
                }
 
                $fields = ['nickname', 'page-flags', 'account-type', 'prvkey', 'pubkey'];
                $user = DBA::selectFirst('user', $fields, ['uid' => $uid, 'account_expired' => false]);
                if (!DBA::isResult($user)) {
-                       return;
+                       return false;
                }
 
                $fields = ['name', 'photo', 'thumb', 'about', 'address', 'locality', 'region',
                        'country-name', 'pub_keywords', 'xmpp', 'net-publish'];
                $profile = DBA::selectFirst('profile', $fields, ['uid' => $uid]);
                if (!DBA::isResult($profile)) {
-                       return;
+                       return false;
                }
 
                $file_suffix = 'jpg';
@@ -724,6 +725,7 @@ class Contact
                                'thumb' => DI::baseUrl() . '/photo/avatar/' . $uid .'.' . $file_suffix];
                        DBA::update('profile', $fields, ['uid' => $uid]);
                }
+               return $update;
        }
 
        /**
index 194386185603aef4d14faabae7145c0be80d9bca..63d83a8dab043a2bf53468fa320d7229d7c59b40 100644 (file)
@@ -29,8 +29,10 @@ use Friendica\Core\Hook;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
+use Friendica\Core\Search;
 use Friendica\Core\Session;
 use Friendica\Core\System;
+use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Protocol\Activity;
@@ -84,6 +86,69 @@ class Profile
                return DBA::selectToArray('profile', $fields, ['uid' => $uid]);
        }
 
+       /**
+        * Update a profile entry and distribute the changes if needed
+        *
+        * @param array $fields
+        * @param integer $uid
+        * @return boolean
+        */
+       public static function update(array $fields, int $uid): bool
+       {
+               $old_owner = User::getOwnerDataById($uid);
+               if (empty($old_owner)) {
+                       return false;
+               }
+
+               if (!DBA::update('profile', $fields, ['uid' => $uid])) {
+                       return false;
+               }
+
+               $update = Contact::updateSelfFromUserID($uid);
+
+               $owner = User::getOwnerDataById($uid);
+               if (empty($owner)) {
+                       return false;
+               }
+
+               if ($old_owner['name'] != $owner['name']) {
+                       User::update(['username' => $owner['name']], $uid);
+               }
+
+               $profile_fields = ['postal-code', 'dob', 'prv_keywords', 'homepage'];
+               foreach ($profile_fields as $field) {
+                       if ($old_owner[$field] != $owner[$field]) {
+                               $update = true;
+                       }
+               }
+
+               if ($update) {
+                       self::publishUpdate($uid);
+               }
+
+               return true;
+       }
+
+       /**
+        * Publish a changed profile
+        * @param int $uid 
+        */
+       public static function publishUpdate(int $uid)
+       {
+               $owner = User::getOwnerDataById($uid);
+               if (empty($owner)) {
+                       return;
+               }
+               if ($owner['net-publish']) {
+                       // Update global directory in background
+                       if (Search::getGlobalDirectory()) {
+                               Worker::add(PRIORITY_LOW, 'Directory', $owner['url']);
+                       }
+               }
+
+               Worker::add(PRIORITY_LOW, 'ProfileUpdate', $uid);
+       }
+
        /**
         * Returns a formatted location string from the given profile array
         *
index 029613b17b0b692e19e3301fcd6d2b841d54640e..062537330c89e7d894b7ba3cf385c77c1a64c284 100644 (file)
@@ -1133,6 +1133,42 @@ class User
                return $return;
        }
 
+       /**
+        * Update a user entry and distribute the changes if needed
+        *
+        * @param array $fields
+        * @param integer $uid
+        * @return boolean
+        */
+       public static function update(array $fields, int $uid): bool
+       {
+               $old_owner = self::getOwnerDataById($uid);
+               if (empty($old_owner)) {
+                       return false;
+               }
+
+               if (!DBA::update('user', $fields, ['uid' => $uid])) {
+                       return false;
+               }
+
+               $update = Contact::updateSelfFromUserID($uid);
+
+               $owner = self::getOwnerDataById($uid);
+               if (empty($owner)) {
+                       return false;
+               }
+
+               if ($old_owner['name'] != $owner['name']) {
+                       Profile::update(['name' => $owner['name']], $uid);
+               }
+
+               if ($update) {
+                       Profile::publishUpdate($uid);
+               }
+
+               return true;
+       }
+
        /**
         * Sets block state for a given user
         *
index 93d2bbd23cc2ee08b8c3e7103ee2e9da07555a32..35740a0a7e0f375f806ecab3846b84ded471103d 100644 (file)
@@ -86,8 +86,6 @@ class Index extends BaseSettings
                        return;
                }
 
-               $namechanged = $profile['name'] != $name;
-
                $about = Strings::escapeTags(trim($_POST['about']));
                $address = Strings::escapeTags(trim($_POST['address']));
                $locality = Strings::escapeTags(trim($_POST['locality']));
@@ -114,8 +112,7 @@ class Index extends BaseSettings
 
                DI::profileField()->saveCollection($profileFields);
 
-               $result = DBA::update(
-                       'profile',
+               $result = Profile::update(
                        [
                                'name'         => $name,
                                'about'        => $about,
@@ -130,26 +127,13 @@ class Index extends BaseSettings
                                'pub_keywords' => $pub_keywords,
                                'prv_keywords' => $prv_keywords,
                        ],
-                       ['uid' => local_user()]
+                       local_user()
                );
 
                if (!$result) {
                        notice(DI::l10n()->t('Profile couldn\'t be updated.'));
                        return;
                }
-
-               if ($namechanged) {
-                       DBA::update('user', ['username' => $name], ['uid' => local_user()]);
-               }
-
-               Contact::updateSelfFromUserID(local_user());
-
-               // Update global directory in background
-               if (Session::get('my_url') && strlen(DI::config()->get('system', 'directory'))) {
-                       Worker::add(PRIORITY_LOW, 'Directory', Session::get('my_url'));
-               }
-
-               Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
        }
 
        public static function content(array $parameters = [])
index c3663e59cabd674620bd4d7a2749e1ebca399193..adbf83311aabce90c50693bb8d150dd4a82fb2e2 100644 (file)
@@ -28,6 +28,7 @@ use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Model\Contact;
 use Friendica\Model\Photo;
+use Friendica\Model\Profile;
 use Friendica\Module\BaseSettings;
 use Friendica\Network\HTTPException;
 
@@ -137,12 +138,9 @@ class Crop extends BaseSettings
                                Contact::updateSelfFromUserID(local_user(), true);
 
                                info(DI::l10n()->t('Shift-reload the page or clear browser cache if the new photo does not display immediately.'));
-                               // Update global directory in background
-                               if ($path && strlen(DI::config()->get('system', 'directory'))) {
-                                       Worker::add(PRIORITY_LOW, 'Directory', DI::baseUrl()->get() . '/' . $path);
-                               }
 
-                               Worker::add(PRIORITY_LOW, 'ProfileUpdate', local_user());
+                               // Update global directory in background
+                               Profile::publishUpdate(local_user());
                        } else {
                                notice(DI::l10n()->t('Unable to process image'));
                        }
@@ -183,9 +181,7 @@ class Crop extends BaseSettings
                        Contact::updateSelfFromUserID(local_user(), true);
 
                        // Update global directory in background
-                       if (Session::get('my_url') && strlen(DI::config()->get('system', 'directory'))) {
-                               Worker::add(PRIORITY_LOW, 'Directory', Session::get('my_url'));
-                       }
+                       Profile::publishUpdate(local_user());
 
                        info(DI::l10n()->t('Profile picture successfully updated.'));
 
index 551d208fe4230500c86afc1b8748525e9248c340..aa043468caa9e40ba64483ffba8a406c5fcd666f 100644 (file)
@@ -53,6 +53,7 @@ use Friendica\Model\ItemURI;
 use Friendica\Model\Notification;
 use Friendica\Model\Photo;
 use Friendica\Model\Post;
+use Friendica\Model\Profile;
 use Friendica\Model\Storage;
 use Friendica\Worker\Delivery;
 
@@ -98,8 +99,9 @@ function update_1298()
                                        DBA::update('profile', [$translateKey => $key], ['id' => $data['id']]);
                                        Logger::notice('Updated contact', ['action' => 'update', 'contact' => $data['id'], "$translateKey" => $key,
                                                'was' => $data[$translateKey]]);
-                                       Worker::add(PRIORITY_LOW, 'ProfileUpdate', $data['id']);
+
                                        Contact::updateSelfFromUserID($data['id']);
+                                       Profile::publishUpdate($data['id']);
                                        $success++;
                                }
                        }
@@ -153,7 +155,9 @@ function update_1323()
 {
        $users = DBA::select('user', ['uid']);
        while ($user = DBA::fetch($users)) {
-               Contact::updateSelfFromUserID($user['uid']);
+               if (Contact::updateSelfFromUserID($user['uid'])) {
+                       Profile::publishUpdate($user['uid']);
+               }
        }
        DBA::close($users);