]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
add missing query backticks
[friendica.git] / include / api.php
index 4d206da28e771d2de71a16df9e9d1a010b99ed30..d205451e5e6e0f13215bc87f56e52a15cdca2bb5 100644 (file)
@@ -23,6 +23,7 @@
        require_once('include/message.php');
        require_once('include/group.php');
        require_once('include/like.php');
+       require_once('include/NotificationsManager.php');
 
 
        define('API_METHOD_ANY','*');
         */
        function api_call(&$a){
                GLOBAL $API, $called_api;
-
+               
                $type="json";
                if (strpos($a->query_string, ".xml")>0) $type="xml";
                if (strpos($a->query_string, ".json")>0) $type="json";
        }
 
 
+       /**
+        * @brief transform $data array in xml without a template
+        *
+        * @param array $data
+        * @return string xml string
+        */
+       function api_array_to_xml($data, $ename="") {
+               $attrs="";
+               $childs="";
+               if (count($data)==1 && !is_array($data[0])) {
+                       $ename = array_keys($data)[0];
+                       $v = $data[$ename];
+                       return "<$ename>$v</$ename>";
+               }
+               foreach($data as $k=>$v) {
+                       $k=trim($k,'$');
+                       if (!is_array($v)) {
+                               $attrs .= sprintf('%s="%s" ', $k, $v);
+                       } else {
+                               if (is_numeric($k)) $k=trim($ename,'s');
+                               $childs.=api_array_to_xml($v, $k);
+                       }
+               }
+               $res = $childs;
+               if ($ename!="") $res = "<$ename $attrs>$res</$ename>";
+               return $res;
+       }
+
        /**
         *  load api $templatename for $type and replace $data array
         */
                        case "rss":
                        case "xml":
                                $data = array_xmlify($data);
-                               $tpl = get_markup_template("api_".$templatename."_".$type.".tpl");
-                               if(! $tpl) {
-                                       header ("Content-Type: text/xml");
-                                       echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<status><error>not implemented</error></status>';
-                                       killme();
+                               if ($templatename==="<auto>") {
+                                       $ret = api_array_to_xml($data); 
+                               } else {
+                                       $tpl = get_markup_template("api_".$templatename."_".$type.".tpl");
+                                       if(! $tpl) {
+                                               header ("Content-Type: text/xml");
+                                               echo '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<status><error>not implemented</error></status>';
+                                               killme();
+                                       }
+                                       $ret = replace_macros($tpl, $data);
                                }
-                               $ret = replace_macros($tpl, $data);
                                break;
                        case "json":
                                $ret = $data;
        api_register_func('api/friendica/activity/unattendno', 'api_friendica_activity', true, API_METHOD_POST);
        api_register_func('api/friendica/activity/unattendmaybe', 'api_friendica_activity', true, API_METHOD_POST);
 
+       /**
+        * @brief Returns notifications
+        *
+        * @param App $a
+        * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
+        * @return string
+       */
+       function api_friendica_notification(&$a, $type) {
+               if (api_user()===false) throw new ForbiddenException();
+               if ($a->argc!==3) throw new BadRequestException("Invalid argument count");
+               $nm = new NotificationsManager();
+               
+               $notes = $nm->getAll(array(), "+seen -date", 50);
+               return api_apply_template("<auto>", $type, array('$notes' => $notes));
+       }
+       
+       /**
+        * @brief Set notification as seen and returns associated item (if possible)
+        *
+        * POST request with 'id' param as notification id
+        * 
+        * @param App $a
+        * @param string $type Known types are 'atom', 'rss', 'xml' and 'json'
+        * @return string
+        */
+       function api_friendica_notification_seen(&$a, $type){
+               if (api_user()===false) throw new ForbiddenException();
+               if ($a->argc!==4) throw new BadRequestException("Invalid argument count");
+               
+               $id = (x($_REQUEST, 'id') ? intval($_REQUEST['id']) : 0);
+               
+               $nm = new NotificationsManager();               
+               $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
+                       $r = q("SELECT * FROM `item` WHERE `id`=%d AND `uid`=%d",
+                               intval($note['iid']),
+                               intval(local_user())
+                       );
+                       if ($r!==false) {
+                               // we found the item, return it to the user
+                               $user_info = api_get_user($a);
+                               $ret = api_format_items($r,$user_info);
+                               $data = array('$statuses' => $ret);
+                               return api_apply_template("timeline", $type, $data);
+                       }
+                       // the item can't be found, but we set the note as seen, so we count this as a success
+               } 
+               return api_apply_template('<auto>', $type, array('status' => "success"));
+       }
+       
+       api_register_func('api/friendica/notification/seen', 'api_friendica_notification_seen', true, API_METHOD_POST);
+       api_register_func('api/friendica/notification', 'api_friendica_notification', true, API_METHOD_GET);
+       
+
 /*
 To.Do:
     [pagename] => api/1.1/statuses/lookup.json