]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/User.php
Merge pull request #10800 from MrPetovan/task/10739-block
[friendica.git] / src / Model / User.php
index 5af5f7f58266b414622b67c1ec280f72ff95f8d5..f3f837eb2e6854a592587059c44d2fd5f027fdc7 100644 (file)
@@ -234,7 +234,7 @@ class User
                $system['closeness'] = 0;
                $system['baseurl'] = DI::baseUrl();
                $system['gsid'] = GServer::getID($system['baseurl']);
-               DBA::insert('contact', $system);
+               Contact::insert($system);
        }
 
        /**
@@ -840,6 +840,57 @@ class User
                return false;
        }
 
+       /**
+        * Get avatar link for given user id
+        *
+        * @param integer $uid     user id
+        * @param string  $size    One of the ProxyUtils::SIZE_* constants
+        * @return string avatar link
+        */
+       public static function getAvatarUrlForId(int $uid, string $size = ''):string
+       {
+               $url = DI::baseUrl() . '/photo/';
+
+               switch ($size) {
+                       case Proxy::SIZE_MICRO:
+                               $url .= 'micro/';
+                               $scale = 6;
+                               break;
+                       case Proxy::SIZE_THUMB:
+                               $url .= 'avatar/';
+                               $scale = 5;
+                               break;
+                       default:
+                               $url .= 'profile/';
+                               $scale = 4;
+                               break;
+               }
+
+               $updated =  '';
+               $imagetype = IMAGETYPE_JPEG;
+
+               $photo = Photo::selectFirst(['type', 'created', 'edited', 'updated'], ["scale" => $scale, 'uid' => $uid, 'profile' => true]);
+               if (!empty($photo)) {
+                       $updated = max($photo['created'], $photo['edited'], $photo['updated']);
+
+                       switch ($photo['type']) {
+                               case 'image/png':
+                                       $imagetype = IMAGETYPE_PNG;
+                                       break;
+
+                               case 'image/gif':
+                                       $imagetype = IMAGETYPE_PNG;
+                                       break;
+
+                               default:
+                                       $imagetype = IMAGETYPE_JPEG;
+                                       break;
+                       }
+               }
+
+               return $url . $uid . image_type_to_extension($imagetype) . ($updated ? '?ts=' . strtotime($updated) : '');
+       }
+
        /**
         * Catch-all user creation function
         *
@@ -1054,8 +1105,8 @@ class User
                $insert_result = DBA::insert('profile', [
                        'uid' => $uid,
                        'name' => $username,
-                       'photo' => DI::baseUrl() . "/photo/profile/{$uid}.jpg",
-                       'thumb' => DI::baseUrl() . "/photo/avatar/{$uid}.jpg",
+                       'photo' => self::getAvatarUrlForId($uid),
+                       'thumb' => self::getAvatarUrlForId($uid, Proxy::SIZE_THUMB),
                        'publish' => $publish,
                        'net-publish' => $netpublish,
                ]);
@@ -1098,7 +1149,7 @@ class User
                        $photo_failure = false;
 
                        $filename = basename($photo);
-                       $curlResult = DI::httpRequest()->get($photo);
+                       $curlResult = DI::httpClient()->get($photo);
                        if ($curlResult->isSuccess()) {
                                $img_str = $curlResult->getBody();
                                $type = $curlResult->getContentType();
@@ -1585,8 +1636,8 @@ class User
        /**
         * Check if the given user id has delegations or is delegated
         *
-        * @param int $uid 
-        * @return bool 
+        * @param int $uid
+        * @return bool
         */
        public static function hasIdentities(int $uid):bool
        {