]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/User.php
Merge pull request #13238 from annando/issue-13221
[friendica.git] / src / Model / User.php
index 5b749ec1dc7b8613a393f0c27be94245c0198336..854961154b5dfe56772dc11c70c7d4314a1eaae0 100644 (file)
@@ -35,7 +35,7 @@ use Friendica\Core\System;
 use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
-use Friendica\Module\Register;
+use Friendica\Module;
 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
 use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
 use Friendica\Network\HTTPException;
@@ -88,7 +88,7 @@ class User
         * ACCOUNT_TYPE_NEWS - the account is a news reflector
         *      Associated page type: PAGE_FLAGS_SOAPBOX
         *
-        * ACCOUNT_TYPE_COMMUNITY - the account is community forum
+        * ACCOUNT_TYPE_COMMUNITY - the account is community group
         *      Associated page types: PAGE_COMMUNITY, PAGE_FLAGS_PRVGROUP
         *
         * ACCOUNT_TYPE_RELAY - the account is a relay
@@ -167,7 +167,7 @@ class User
                $system['region'] = '';
                $system['postal-code'] = '';
                $system['country-name'] = '';
-               $system['homepage'] = DI::baseUrl();
+               $system['homepage'] = (string)DI::baseUrl();
                $system['dob'] = '0000-00-00';
 
                // Ensure that the user contains data
@@ -483,23 +483,41 @@ class User
        }
 
        /**
-        * Returns the default group for a given user and network
+        * Returns the default circle for a given user
         *
         * @param int $uid User id
         *
-        * @return int group id
+        * @return int circle id
         * @throws Exception
         */
-       public static function getDefaultGroup(int $uid): int
+       public static function getDefaultCircle(int $uid): int
        {
                $user = DBA::selectFirst('user', ['def_gid'], ['uid' => $uid]);
                if (DBA::isResult($user)) {
-                       $default_group = $user["def_gid"];
+                       $default_circle = $user['def_gid'];
                } else {
-                       $default_group = 0;
+                       $default_circle = 0;
                }
 
-               return $default_group;
+               return $default_circle;
+       }
+
+       /**
+        * Returns the default circle for groups for a given user
+        *
+        * @param int $uid User id
+        *
+        * @return int circle id
+        * @throws Exception
+        */
+       public static function getDefaultGroupCircle(int $uid): int
+       {
+               $default_circle = DI::pConfig()->get($uid, 'system', 'default-group-gid');
+               if (empty($default_circle)) {
+                       $default_circle = self::getDefaultCircle($uid);
+               }
+
+               return $default_circle;
        }
 
        /**
@@ -675,6 +693,10 @@ class User
         */
        public static function updateLastActivity(int $uid)
        {
+               if (!$uid) {
+                       return;
+               }
+
                $user = User::getById($uid, ['last-activity']);
                if (empty($user)) {
                        return;
@@ -816,14 +838,14 @@ class User
         * Empties the password reset token field just in case.
         *
         * @param int    $uid
-        * @param string $pasword_hashed
+        * @param string $password_hashed
         * @return bool
         * @throws Exception
         */
-       private static function updatePasswordHashed(int $uid, string $pasword_hashed): bool
+       private static function updatePasswordHashed(int $uid, string $password_hashed): bool
        {
                $fields = [
-                       'password' => $pasword_hashed,
+                       'password' => $password_hashed,
                        'pwdreset' => null,
                        'pwdreset_time' => null,
                        'legacy_password' => false
@@ -1188,13 +1210,13 @@ class User
                        throw new Exception(DI::l10n()->t('An error occurred creating your self contact. Please try again.'));
                }
 
-               // Create a group with no members. This allows somebody to use it
-               // right away as a default group for new contacts.
-               $def_gid = Group::create($uid, DI::l10n()->t('Friends'));
+               // Create a circle with no members. This allows somebody to use it
+               // right away as a default circle for new contacts.
+               $def_gid = Circle::create($uid, DI::l10n()->t('Friends'));
                if (!$def_gid) {
                        DBA::delete('user', ['uid' => $uid]);
 
-                       throw new Exception(DI::l10n()->t('An error occurred creating your default contact group. Please try again.'));
+                       throw new Exception(DI::l10n()->t('An error occurred creating your default contact circle. Please try again.'));
                }
 
                $fields = ['def_gid' => $def_gid];
@@ -1204,6 +1226,11 @@ class User
 
                DBA::update('user', $fields, ['uid' => $uid]);
 
+               $def_gid_groups = Circle::create($uid, DI::l10n()->t('Groups'));
+               if ($def_gid_groups) {
+                       DI::pConfig()->set($uid, 'system', 'default-group-gid', $def_gid_groups);
+               }
+
                // if we have no OpenID photo try to look up an avatar
                if (!strlen($photo)) {
                        $photo = Network::lookupAvatarByEmail($email);
@@ -1640,7 +1667,7 @@ class User
         */
        public static function identities(int $uid): array
        {
-               if (empty($uid)) {
+               if (!$uid) {
                        return [];
                }
 
@@ -1651,7 +1678,7 @@ class User
                        return $identities;
                }
 
-               if ($user['parent-uid'] == 0) {
+               if (!$user['parent-uid']) {
                        // First add our own entry
                        $identities = [[
                                'uid' => $user['uid'],
@@ -1712,7 +1739,7 @@ class User
         */
        public static function hasIdentities(int $uid): bool
        {
-               if (empty($uid)) {
+               if (!$uid) {
                        return false;
                }
 
@@ -1721,7 +1748,7 @@ class User
                        return false;
                }
 
-               if ($user['parent-uid'] != 0) {
+               if ($user['parent-uid']) {
                        return true;
                }
 
@@ -1848,8 +1875,8 @@ class User
        {
                $condition = [
                        'email'           => self::getAdminEmailList(),
-                       'parent-uid'      => 0,
-                       'blocked'         => 0,
+                       'parent-uid'      => null,
+                       'blocked'         => false,
                        'verified'        => true,
                        'account_removed' => false,
                        'account_expired' => false,
@@ -1890,17 +1917,17 @@ class User
                }
 
                $register_policy = DI::config()->get('config', 'register_policy');
-               if (!in_array($register_policy, [Register::OPEN, Register::CLOSED])) {
+               if (!in_array($register_policy, [Module\Register::OPEN, Module\Register::CLOSED])) {
                        Logger::debug('Unsupported register policy.', ['policy' => $register_policy]);
                        return;
                }
 
                $users = DBA::count('user', ['blocked' => false, 'account_removed' => false, 'account_expired' => false]);
-               if (($users >= $max_registered_users) && ($register_policy == Register::OPEN)) {
-                       DI::config()->set('config', 'register_policy', Register::CLOSED);
+               if (($users >= $max_registered_users) && ($register_policy == Module\Register::OPEN)) {
+                       DI::config()->set('config', 'register_policy', Module\Register::CLOSED);
                        Logger::notice('Max users reached, registration is closed.', ['users' => $users, 'max' => $max_registered_users]);
-               } elseif (($users < $max_registered_users) && ($register_policy == Register::CLOSED)) {
-                       DI::config()->set('config', 'register_policy', Register::OPEN);
+               } elseif (($users < $max_registered_users) && ($register_policy == Module\Register::CLOSED)) {
+                       DI::config()->set('config', 'register_policy', Module\Register::OPEN);
                        Logger::notice('Below maximum users, registration is opened.', ['users' => $users, 'max' => $max_registered_users]);
                } else {
                        Logger::debug('Unchanged register policy', ['policy' => $register_policy, 'users' => $users, 'max' => $max_registered_users]);