Merge pull request #8179 from MrPetovan/bug/notices
[friendica.git/.git] / include / api.php
index 583573b..a69bd01 100644 (file)
@@ -11,9 +11,7 @@ use Friendica\Content\ContactSelector;
 use Friendica\Content\Feature;
 use Friendica\Content\Text\BBCode;
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Config;
 use Friendica\Core\Hook;
-use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\Session;
@@ -25,6 +23,7 @@ use Friendica\Model\Contact;
 use Friendica\Model\Group;
 use Friendica\Model\Item;
 use Friendica\Model\Mail;
+use Friendica\Model\Notify;
 use Friendica\Model\Photo;
 use Friendica\Model\Profile;
 use Friendica\Model\User;
@@ -313,9 +312,7 @@ function api_call(App $a, App\Arguments $args = null)
                                }
 
                                $called_api = explode("/", $p);
-                               //unset($_SERVER['PHP_AUTH_USER']);
 
-                               /// @TODO should be "true ==[=] $info['auth']", if you miss only one = character, you assign a variable (only with ==). Let's make all this even.
                                if (!empty($info['auth']) && api_user() === false) {
                                        api_login($a);
                                }
@@ -1097,7 +1094,7 @@ function api_statuses_update($type)
 
        if (!$parent) {
                // Check for throttling (maximum posts per day, week and month)
-               $throttle_day = Config::get('system', 'throttle_limit_day');
+               $throttle_day = DI::config()->get('system', 'throttle_limit_day');
                if ($throttle_day > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60);
 
@@ -1111,7 +1108,7 @@ function api_statuses_update($type)
                        }
                }
 
-               $throttle_week = Config::get('system', 'throttle_limit_week');
+               $throttle_week = DI::config()->get('system', 'throttle_limit_week');
                if ($throttle_week > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*7);
 
@@ -1125,7 +1122,7 @@ function api_statuses_update($type)
                        }
                }
 
-               $throttle_month = Config::get('system', 'throttle_limit_month');
+               $throttle_month = DI::config()->get('system', 'throttle_limit_month');
                if ($throttle_month > 0) {
                        $datefrom = date(DateTimeFormat::MYSQL, time() - 24*60*60*30);
 
@@ -2740,7 +2737,7 @@ function api_get_entitities(&$text, $bbcode)
                        if ($image) {
                                // If image cache is activated, then use the following sizes:
                                // thumb  (150), small (340), medium (600) and large (1024)
-                               if (!Config::get("system", "proxy_disabled")) {
+                               if (!DI::config()->get("system", "proxy_disabled")) {
                                        $media_url = ProxyUtils::proxifyUrl($url);
 
                                        $sizes = [];
@@ -3573,15 +3570,15 @@ api_register_func('api/friendships/incoming', 'api_friendships_incoming', true);
  */
 function api_statusnet_config($type)
 {
-       $name      = Config::get('config', 'sitename');
+       $name      = DI::config()->get('config', 'sitename');
        $server    = DI::baseUrl()->getHostname();
        $logo      = DI::baseUrl() . '/images/friendica-64.png';
-       $email     = Config::get('config', 'admin_email');
-       $closed    = intval(Config::get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED ? 'true' : 'false';
-       $private   = Config::get('system', 'block_public') ? 'true' : 'false';
-       $textlimit = (string) Config::get('config', 'api_import_size', Config::get('config', 'max_import_size', 200000));
-       $ssl       = Config::get('system', 'have_ssl') ? 'true' : 'false';
-       $sslserver = Config::get('system', 'have_ssl') ? str_replace('http:', 'https:', DI::baseUrl()) : '';
+       $email     = DI::config()->get('config', 'admin_email');
+       $closed    = intval(DI::config()->get('config', 'register_policy')) === \Friendica\Module\Register::CLOSED ? 'true' : 'false';
+       $private   = DI::config()->get('system', 'block_public') ? 'true' : 'false';
+       $textlimit = (string) DI::config()->get('config', 'api_import_size', DI::config()->get('config', 'max_import_size', 200000));
+       $ssl       = DI::config()->get('system', 'have_ssl') ? 'true' : 'false';
+       $sslserver = DI::config()->get('system', 'have_ssl') ? str_replace('http:', 'https:', DI::baseUrl()) : '';
 
        $config = [
                'site' => ['name' => $name,'server' => $server, 'theme' => 'default', 'path' => '',
@@ -4628,7 +4625,7 @@ function api_account_update_profile_image($type)
 
        // Update global directory in background
        $url = DI::baseUrl() . '/profile/' . DI::app()->user['nickname'];
-       if ($url && strlen(Config::get('system', 'directory'))) {
+       if ($url && strlen(DI::config()->get('system', 'directory'))) {
                Worker::add(PRIORITY_LOW, "Directory", $url);
        }
 
@@ -4685,7 +4682,7 @@ function api_account_update_profile($type)
 
        Worker::add(PRIORITY_LOW, 'ProfileUpdate', $local_user);
        // Update global directory in background
-       if ($api_user['url'] && strlen(Config::get('system', 'directory'))) {
+       if ($api_user['url'] && strlen(DI::config()->get('system', 'directory'))) {
                Worker::add(PRIORITY_LOW, "Directory", $api_user['url']);
        }
 
@@ -4793,7 +4790,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
                throw new InternalServerErrorException("image size exceeds PHP config settings, file was rejected by server");
        }
        // check against max upload size within Friendica instance
-       $maximagesize = Config::get('system', 'maximagesize');
+       $maximagesize = DI::config()->get('system', 'maximagesize');
        if ($maximagesize && ($filesize > $maximagesize)) {
                $formattedBytes = Strings::formatBytes($maximagesize);
                throw new InternalServerErrorException("image size exceeds Friendica config setting (uploaded size: $formattedBytes)");
@@ -4811,7 +4808,7 @@ function save_media_to_database($mediatype, $media, $type, $album, $allow_cid, $
        @unlink($src);
 
        // check max length of images on server
-       $max_length = Config::get('system', 'max_image_length');
+       $max_length = DI::config()->get('system', 'max_image_length');
        if (!$max_length) {
                $max_length = MAX_IMAGE_LENGTH;
        }
@@ -5029,6 +5026,9 @@ function prepare_photo_data($type, $scale, $photo_id)
        // retrieve item element for getting activities (like, dislike etc.) related to photo
        $condition = ['uid' => local_user(), 'resource-id' => $photo_id, 'type' => 'photo'];
        $item = Item::selectFirstForUser(local_user(), ['id'], $condition);
+       if (!DBA::isResult($item)) {
+               throw new NotFoundException('Photo-related item not found.');
+       }
 
        $data['photo']['friendica_activities'] = api_format_items_activities($item, $type);
 
@@ -5907,19 +5907,25 @@ function api_friendica_notification($type)
        if ($a->argc!==3) {
                throw new BadRequestException("Invalid argument count");
        }
-       $notes = DI::notify()->getAll([], ['seen' => 'ASC', 'date' => 'DESC'], 50);
+
+       $notifications = DI::notify()->select(['uid' => api_user()], ['order' => ['seen' => 'ASC', 'date' => 'DESC'], 'limit' => 50]);
 
        if ($type == "xml") {
-               $xmlnotes = [];
-               if (!empty($notes)) {
-                       foreach ($notes as $note) {
-                               $xmlnotes[] = ["@attributes" => $note];
+               $xmlnotes = false;
+               if (!empty($notifications)) {
+                       foreach ($notifications as $notification) {
+                               $xmlnotes[] = ["@attributes" => $notification->toArray()];
                        }
                }
 
-               $notes = $xmlnotes;
+               $result = $xmlnotes;
+       } elseif (count($notifications) > 0) {
+               $result = $notifications->getArrayCopy();
+       } else {
+               $result = false;
        }
-       return api_format_data("notes", $type, ['note' => $notes]);
+
+       return api_format_data("notes", $type, ['note' => $result]);
 }
 
 /**
@@ -5937,37 +5943,36 @@ function api_friendica_notification($type)
  */
 function api_friendica_notification_seen($type)
 {
-       $a = DI::app();
+       $a         = DI::app();
        $user_info = api_get_user($a);
 
        if (api_user() === false || $user_info === false) {
                throw new ForbiddenException();
        }
-       if ($a->argc!==4) {
+       if ($a->argc !== 4) {
                throw new BadRequestException("Invalid argument count");
        }
 
        $id = (!empty($_REQUEST['id']) ? intval($_REQUEST['id']) : 0);
 
-       $nm = DI::notify();
-       $note = $nm->getByID($id);
-       if (is_null($note)) {
-               throw new BadRequestException("Invalid argument");
-       }
-
-       $nm->setSeen($note);
-       if ($note['otype']=='item') {
-               // would be really better with an ItemsManager and $im->getByID() :-P
-               $item = Item::selectFirstForUser(api_user(), [], ['id' => $note['iid'], 'uid' => api_user()]);
-               if (DBA::isResult($item)) {
-                       // we found the item, return it to the user
-                       $ret = api_format_items([$item], $user_info, false, $type);
-                       $data = ['status' => $ret];
-                       return api_format_data("status", $type, $data);
+       try {
+               $notification = DI::notify()->getByID($id);
+               $notification->setSeen();
+
+               if ($notification->otype === Notify::OTYPE_ITEM) {
+                       $item = Item::selectFirstForUser(api_user(), [], ['id' => $notification->iid, 'uid' => api_user()]);
+                       if (DBA::isResult($item)) {
+                               // we found the item, return it to the user
+                               $ret  = api_format_items([$item], $user_info, false, $type);
+                               $data = ['status' => $ret];
+                               return api_format_data("status", $type, $data);
+                       }
+                       // the item can't be found, but we set the notification as seen, so we count this as a success
                }
-               // the item can't be found, but we set the note as seen, so we count this as a success
+               return api_format_data('result', $type, ['result' => "success"]);
+       } catch (NotFoundException $e) {
+               throw new BadRequestException('Invalid argument');
        }
-       return api_format_data('result', $type, ['result' => "success"]);
 }
 
 /// @TODO move to top of file or somewhere better
@@ -6120,7 +6125,7 @@ function api_friendica_profile_show($type)
 
        // retrieve general information about profiles for user
        $multi_profiles = Feature::isEnabled(api_user(), 'multi_profiles');
-       $directory = Config::get('system', 'directory');
+       $directory = DI::config()->get('system', 'directory');
 
        // get data of the specified profile id or all profiles of the user if not specified
        if ($profile_id != 0) {