]> git.mxchange.org Git - friendica.git/commitdiff
Merge remote-tracking branch 'upstream/develop' into 1601-api-statuses-lookup
authorMichael Vogel <icarus@dabo.de>
Sun, 14 Feb 2016 20:04:59 +0000 (21:04 +0100)
committerMichael Vogel <icarus@dabo.de>
Sun, 14 Feb 2016 20:04:59 +0000 (21:04 +0100)
1  2 
include/api.php

diff --combined include/api.php
index f1294c690d0d1db79a42d9bb800e6e1f397b4080,d54e540f7966ca697eb72180e17978b7751dd254..e5ca8ca7878d573c7dfefc00361e81ed080d4289
@@@ -23,6 -23,7 +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";
                                        }
                                }
                        }
 +                      logger('API call not implemented: '.$a->query_string);
                        throw new NotImplementedException();
                } catch (HTTPException $e) {
                        header("HTTP/1.1 {$e->httpcode} {$e->httpdesc}");
         *              Contact url or False if contact id is unknown
         */
        function api_unique_id_to_url($id){
-               $r = q("SELECT `url` FROM `unique_contacts` WHERE `id`=%d LIMIT 1",
+               $r = q("SELECT `url` FROM `gcontact` WHERE `id`=%d LIMIT 1",
                        intval($id));
                if ($r)
                        return ($r[0]["url"]);
                        $r = array();
  
                        if ($url != "")
-                               $r = q("SELECT * FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", $url);
-                       elseif ($nick != "")
-                               $r = q("SELECT * FROM `unique_contacts` WHERE `nick`='%s' LIMIT 1", $nick);
+                               $r = q("SELECT * FROM `gcontact` WHERE `nurl`='%s' LIMIT 1", dbesc(normalise_link($url)));
  
                        if ($r) {
                                // If no nick where given, extract it from the address
                                        'id_str' => (string) $r[0]["id"],
                                        'name' => $r[0]["name"],
                                        'screen_name' => (($r[0]['nick']) ? $r[0]['nick'] : $r[0]['name']),
-                                       'location' => NULL,
-                                       'description' => NULL,
+                                       'location' => $r[0]["location"],
+                                       'description' => $r[0]["about"],
                                        'url' => $r[0]["url"],
                                        'protected' => false,
                                        'followers_count' => 0,
                                        'friends_count' => 0,
                                        'listed_count' => 0,
-                                       'created_at' => api_date(0),
+                                       'created_at' => api_date($r[0]["created"]),
                                        'favourites_count' => 0,
                                        'utc_offset' => 0,
                                        'time_zone' => 'UTC',
                                        'contributors_enabled' => false,
                                        'is_translator' => false,
                                        'is_translation_enabled' => false,
-                                       'profile_image_url' => $r[0]["avatar"],
-                                       'profile_image_url_https' => $r[0]["avatar"],
+                                       'profile_image_url' => $r[0]["photo"],
+                                       'profile_image_url_https' => $r[0]["photo"],
                                        'following' => false,
                                        'follow_request_sent' => false,
                                        'notifications' => false,
                                        'uid' => 0,
                                        'cid' => 0,
                                        'self' => 0,
-                                       'network' => '',
+                                       'network' => $r[0]["network"],
                                );
  
                                return $ret;
                        $uinfo[0]['nick'] = api_get_nick($uinfo[0]["url"]);
                }
  
-               // Fetching unique id
-               $r = q("SELECT id FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", dbesc(normalise_link($uinfo[0]['url'])));
-               // If not there, then add it
-               if (count($r) == 0) {
-                       q("INSERT INTO `unique_contacts` (`url`, `name`, `nick`, `avatar`) VALUES ('%s', '%s', '%s', '%s')",
-                               dbesc(normalise_link($uinfo[0]['url'])), dbesc($uinfo[0]['name']),dbesc($uinfo[0]['nick']), dbesc($uinfo[0]['micro']));
-                       $r = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1", dbesc(normalise_link($uinfo[0]['url'])));
-               }
                $network_name = network_to_name($uinfo[0]['network'], $uinfo[0]['url']);
  
+               $gcontact_id  = get_gcontact_id(array("url" => $uinfo[0]['url'], "network" => $uinfo[0]['network'],
+                                                       "photo" => $uinfo[0]['micro'], "name" => $uinfo[0]['name']));
                $ret = Array(
-                       'id' => intval($r[0]['id']),
-                       'id_str' => (string) intval($r[0]['id']),
+                       'id' => intval($gcontact_id),
+                       'id_str' => (string) intval($gcontact_id),
                        'name' => (($uinfo[0]['name']) ? $uinfo[0]['name'] : $uinfo[0]['nick']),
                        'screen_name' => (($uinfo[0]['nick']) ? $uinfo[0]['nick'] : $uinfo[0]['name']),
                        'location' => ($usr) ? $usr[0]['default-location'] : $network_name,
  
        function api_item_get_user(&$a, $item) {
  
-               $author = q("SELECT * FROM `unique_contacts` WHERE `url`='%s' LIMIT 1",
-                       dbesc(normalise_link($item['author-link'])));
-               if (count($author) == 0) {
-                       q("INSERT INTO `unique_contacts` (`url`, `name`, `avatar`) VALUES ('%s', '%s', '%s')",
-                               dbesc(normalise_link($item["author-link"])), dbesc($item["author-name"]), dbesc($item["author-avatar"]));
-                       $author = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1",
-                               dbesc(normalise_link($item['author-link'])));
-               } else if ($item["author-link"].$item["author-name"] != $author[0]["url"].$author[0]["name"]) {
-                       $r = q("SELECT `id` FROM `unique_contacts` WHERE `name` = '%s' AND `avatar` = '%s' AND url = '%s'",
-                               dbesc($item["author-name"]), dbesc($item["author-avatar"]),
-                               dbesc(normalise_link($item["author-link"])));
-                       if (!$r)
-                               q("UPDATE `unique_contacts` SET `name` = '%s', `avatar` = '%s' WHERE `url` = '%s'",
-                                       dbesc($item["author-name"]), dbesc($item["author-avatar"]),
-                                       dbesc(normalise_link($item["author-link"])));
-               }
-               $owner = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1",
-                       dbesc(normalise_link($item['owner-link'])));
-               if (count($owner) == 0) {
-                       q("INSERT INTO `unique_contacts` (`url`, `name`, `avatar`) VALUES ('%s', '%s', '%s')",
-                               dbesc(normalise_link($item["owner-link"])), dbesc($item["owner-name"]), dbesc($item["owner-avatar"]));
+               // Make sure that there is an entry in the global contacts for author and owner
+               get_gcontact_id(array("url" => $item['author-link'], "network" => $item['network'],
+                                       "photo" => $item['author-avatar'], "name" => $item['author-name']));
  
-                       $owner = q("SELECT `id` FROM `unique_contacts` WHERE `url`='%s' LIMIT 1",
-                               dbesc(normalise_link($item['owner-link'])));
-               } else if ($item["owner-link"].$item["owner-name"] != $owner[0]["url"].$owner[0]["name"]) {
-                       $r = q("SELECT `id` FROM `unique_contacts` WHERE `name` = '%s' AND `avatar` = '%s' AND url = '%s'",
-                               dbesc($item["owner-name"]), dbesc($item["owner-avatar"]),
-                               dbesc(normalise_link($item["owner-link"])));
-                       if (!$r)
-                               q("UPDATE `unique_contacts` SET `name` = '%s', `avatar` = '%s' WHERE `url` = '%s'",
-                                       dbesc($item["owner-name"]), dbesc($item["owner-avatar"]),
-                                       dbesc(normalise_link($item["owner-link"])));
-               }
+               get_gcontact_id(array("url" => $item['owner-link'], "network" => $item['network'],
+                                       "photo" => $item['owner-avatar'], "name" => $item['owner-name']));
  
                // Comments in threads may appear as wall-to-wall postings.
                // So only take the owner at the top posting.
        }
  
  
+       /**
+        * @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;
  
                if((strpos($txt,'<') !== false) || (strpos($txt,'>') !== false)) {
  
-                       require_once('library/HTMLPurifier.auto.php');
                        $txt = html2bb_video($txt);
                        $config = HTMLPurifier_Config::createDefault();
                        $config->set('Cache.DefinitionImpl', null);
                if(requestdata('htmlstatus')) {
                        $txt = requestdata('htmlstatus');
                        if((strpos($txt,'<') !== false) || (strpos($txt,'>') !== false)) {
-                               require_once('library/HTMLPurifier.auto.php');
                                $txt = html2bb_video($txt);
  
                                $config = HTMLPurifier_Config::createDefault();
                                $in_reply_to_status_id= intval($lastwall['parent']);
                                $in_reply_to_status_id_str = (string) intval($lastwall['parent']);
  
-                               $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($lastwall['item-author'])));
+                               $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($lastwall['item-author'])));
                                if ($r) {
                                        if ($r[0]['nick'] == "")
                                                $r[0]['nick'] = api_get_nick($r[0]["url"]);
                                        $in_reply_to_status_id = intval($lastwall['parent']);
                                        $in_reply_to_status_id_str = (string) intval($lastwall['parent']);
  
-                                       $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($reply[0]['item-author'])));
+                                       $r = q("SELECT * FROM `gcontact` WHERE `nurl` = '%s'", dbesc(normalise_link($reply[0]['item-author'])));
                                        if ($r) {
                                                if ($r[0]['nick'] == "")
                                                        $r[0]['nick'] = api_get_nick($r[0]["url"]);
                $userlist = array();
  
                if (isset($_GET["q"])) {
-                       $r = q("SELECT id FROM `unique_contacts` WHERE `name`='%s'", dbesc($_GET["q"]));
+                       $r = q("SELECT id FROM `gcontact` WHERE `name`='%s'", dbesc($_GET["q"]));
                        if (!count($r))
-                               $r = q("SELECT `id` FROM `unique_contacts` WHERE `nick`='%s'", dbesc($_GET["q"]));
+                               $r = q("SELECT `id` FROM `gcontact` WHERE `nick`='%s'", dbesc($_GET["q"]));
  
                        if (count($r)) {
                                foreach ($r AS $user) {
                        WHERE `item`.`visible` = 1 and `item`.`moderated` = 0 AND `item`.`deleted` = 0
                        AND `contact`.`id` = `item`.`contact-id`
                        AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
+                       AND NOT `item`.`private` AND `item`.`allow_cid` = '' AND `item`.`allow`.`gid` = ''
+                       AND `item`.`deny_cid` = '' AND `item`.`deny_gid` = ''
                        $sql_extra
                        AND `item`.`id`=%d",
                        intval($id)
                                $_REQUEST["source"] = api_source();
  
                        item_post($a);
-               }
+               } else
+                       throw new ForbiddenException();
  
                // this should output the last post (the one we just posted).
                $called_api = null;
                                        intval(api_user()),
                                        intval($in_reply_to_status_id));
                                if ($r) {
-                                       $r = q("SELECT * FROM `unique_contacts` WHERE `url` = '%s'", dbesc(normalise_link($r[0]['author-link'])));
+                                       $r = q("SELECT * FROM `gcontact` WHERE `url` = '%s'", dbesc(normalise_link($r[0]['author-link'])));
  
                                        if ($r) {
                                                if ($r[0]['nick'] == "")
                        killme();
                }
        }
 +      api_register_func('api/gnusocial/version','api_statusnet_version',false);
        api_register_func('api/statusnet/version','api_statusnet_version',false);
  
        /**
  
                $stringify_ids = (x($_REQUEST,'stringify_ids')?$_REQUEST['stringify_ids']:false);
  
-               $r = q("SELECT `unique_contacts`.`id` FROM `contact`, `unique_contacts` WHERE `contact`.`nurl` = `unique_contacts`.`url` AND `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` $sql_extra",
+               $r = q("SELECT `gcontact`.`id` FROM `contact`, `gcontact` WHERE `contact`.`nurl` = `gcontact`.`nurl` AND `uid` = %d AND NOT `self` AND NOT `blocked` AND NOT `pending` $sql_extra",
                        intval(api_user())
                );
  
  
                //}
  
-               if ($nick != "") {
-                       q("UPDATE `unique_contacts` SET `nick` = '%s' WHERE `nick` != '%s' AND url = '%s'",
-                               dbesc($nick), dbesc($nick), dbesc(normalise_link($profile)));
+               if ($nick != "")
                        return($nick);
-               }
  
                return(false);
        }
        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