]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/User.php
Meaningful notification message
[friendica.git] / src / Model / User.php
index 641148f74292f50e66e7dd7583c85ea56cdb17fc..49423ce9ef74792b17423aabd52380ef7ec5006e 100644 (file)
@@ -146,10 +146,21 @@ class User
                $system['sprvkey'] = $system['uprvkey'] = $system['prvkey'];
                $system['spubkey'] = $system['upubkey'] = $system['pubkey'];
                $system['nickname'] = $system['nick'];
+               $system['page-flags'] = User::PAGE_FLAGS_SOAPBOX;
+               $system['account-type'] = $system['contact-type'];
+               $system['guid'] = '';
+               $system['nickname'] = $system['nick'];
+               $system['pubkey'] = $system['pubkey'];
+               $system['locality'] = '';
+               $system['region'] = '';
+               $system['country-name'] = '';
+               $system['net-publish'] = false;
+               $system['picdate'] = '';
+               $system['theme'] = '';
 
                // Ensure that the user contains data
-               $user = DBA::selectFirst('user', ['prvkey'], ['uid' => 0]);
-               if (empty($user['prvkey'])) {
+               $user = DBA::selectFirst('user', ['prvkey', 'guid'], ['uid' => 0]);
+               if (empty($user['prvkey']) || empty($user['guid'])) {
                        $fields = [
                                'username' => $system['name'],
                                'nickname' => $system['nick'],
@@ -158,12 +169,17 @@ class User
                                'prvkey' => $system['prvkey'],
                                'spubkey' => $system['spubkey'],
                                'sprvkey' => $system['sprvkey'],
+                               'guid' => System::createUUID(),
                                'verified' => true,
                                'page-flags' => User::PAGE_FLAGS_SOAPBOX,
                                'account-type' => User::ACCOUNT_TYPE_RELAY,
                        ];
 
                        DBA::update('user', $fields, ['uid' => 0]);
+
+                       $system['guid'] = $fields['guid'];
+               } else {
+                       $system['guid'] = $user['guid'];
                }
 
                return $system;
@@ -391,12 +407,12 @@ class User
                        if (!DBA::exists('user', ['uid' => $uid]) || !$repairMissing) {
                                return false;
                        }
-                       if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
-                               Contact::createSelfFromUserId($uid);
-                       }
                        if (!DBA::exists('profile', ['uid' => $uid])) {
                                DBA::insert('profile', ['uid' => $uid]);
                        }
+                       if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
+                               Contact::createSelfFromUserId($uid);
+                       }
                        $owner = self::getOwnerDataById($uid, false);
                }
 
@@ -412,7 +428,7 @@ class User
 
                // Check for correct url and normalised nurl
                $url = DI::baseUrl() . '/profile/' . $owner['nickname'];
-               $repair = ($owner['url'] != $url) || ($owner['nurl'] != Strings::normaliseLink($owner['url']));
+               $repair = empty($owner['network']) || ($owner['url'] != $url) || ($owner['nurl'] != Strings::normaliseLink($owner['url']));
 
                if (!$repair) {
                        // Check if "addr" is present and correct
@@ -467,20 +483,11 @@ class User
         */
        public static function getDefaultGroup($uid, $network = '')
        {
-               $default_group = 0;
-
-               if ($network == Protocol::OSTATUS) {
-                       $default_group = DI::pConfig()->get($uid, "ostatus", "default_group");
-               }
-
-               if ($default_group != 0) {
-                       return $default_group;
-               }
-
                $user = DBA::selectFirst('user', ['def_gid'], ['uid' => $uid]);
-
                if (DBA::isResult($user)) {
                        $default_group = $user["def_gid"];
+               } else {
+                       $default_group = 0;
                }
 
                return $default_group;
@@ -1138,6 +1145,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
         *
@@ -1469,6 +1512,10 @@ class User
         */
        public static function identities($uid)
        {
+               if (empty($uid)) {
+                       return [];
+               }
+
                $identities = [];
 
                $user = DBA::selectFirst('user', ['uid', 'nickname', 'username', 'parent-uid'], ['uid' => $uid]);
@@ -1529,6 +1576,38 @@ class User
                return $identities;
        }
 
+       /**
+        * Check if the given user id has delegations or is delegated
+        *
+        * @param int $uid 
+        * @return bool 
+        */
+       public static function hasIdentities(int $uid):bool
+       {
+               if (empty($uid)) {
+                       return false;
+               }
+
+               $user = DBA::selectFirst('user', ['parent-uid'], ['uid' => $uid, 'account_removed' => false]);
+               if (!DBA::isResult($user)) {
+                       return false;
+               }
+
+               if ($user['parent-uid'] != 0) {
+                       return true;
+               }
+
+               if (DBA::exists('user', ['parent-uid' => $uid, 'account_removed' => false])) {
+                       return true;
+               }
+
+               if (DBA::exists('manage', ['uid' => $uid])) {
+                       return true;
+               }
+
+               return false;
+       }
+
        /**
         * Returns statistical information about the current users of this node
         *