]> git.mxchange.org Git - friendica.git/blobdiff - include/api.php
some api enhancements
[friendica.git] / include / api.php
index bc981646d36da12e7dd35bd1b365dc1d6154dd74..62dc7850649f7ffebac285f0251471b8f97f8155 100644 (file)
@@ -10,7 +10,6 @@
        $API = Array();
         
 
-       
        function api_date($str){
                //Wed May 23 06:01:13 +0000 2007
                return datetime_convert('UTC', 'UTC', $str, "D M d h:i:s +0000 Y" );
                                if ($info['auth']===true && local_user()===false) {
                                                api_login($a);
                                }
-               
+
+                               load_contact_links(local_user());
+
+                               logger('API call for ' . $a->user['username'] . ': ' . $a->query_string);               
                                $type="json";           
                                if (strpos($a->query_string, ".xml")>0) $type="xml";
                                if (strpos($a->query_string, ".json")>0) $type="json";
                $arr['$rss'] = array(
                        'alternate' => $user_info['url'],
                        'self' => $a->get_baseurl(). "/". $a->query_string,
+                       'base' => $a->get_baseurl(),
                        'updated' => api_date(null),
+                       'atom_updated' => datetime_convert('UTC','UTC','now',ATOM_TIME),
                        'language' => $user_info['language'],
                        'logo'  => $a->get_baseurl()."/images/friendika-32.png",
                );
        /**
         * Returns user info array.
         */
-       function api_get_user(&$a, $contact_id=Null){
+       function api_get_user(&$a, $contact_id = Null){
                $user = null;
                $extra_query = "";
+
                if(!is_null($contact_id)){
                        $user=$contact_id;
                        $extra_query = "AND `contact`.`id` = %d ";
                                api_login($a); return False;
                        } else {
                                $user = $_SESSION['uid'];
-                               $extra_query = "AND `contact`.`uid` = %d ";
+                               $extra_query = "AND `contact`.`uid` = %d AND `contact`.`self` = 1 ";
                        }
                        
                }
                
        }
 
+       function api_item_get_user(&$a, $item) {
+               // The author is our direct contact, in a conversation with us.
+               if(link_compare($item['url'],$item['author-link'])) {
+                       return api_get_user($a,$item['cid']);
+               }
+               else {
+                       // The author may be a contact of ours, but is replying to somebody else. 
+                       // Figure out if we know him/her.
+                       $normalised = normalise_link((strlen($item['author-link'])) ? $item['author-link'] : $item['url']);
+            if(($normalised != 'mailbox') && (x($a->contacts[$normalised])))
+                               return api_get_user($a,$a->contacts[$normalised]['id']);
+               }
+               // We don't know this person directly.
+               $ret = array(
+                       'uid' => 0,
+                       'id' => 0,
+                       'name' => $item['author-name'],
+                       'screen_name' => '',
+                       'location' => '', //$uinfo[0]['default-location'],
+                       'profile_image_url' => $item['author-avatar'],
+                       'url' => $item['author-link'],
+                       'contact_url' => 0,
+                       'protected' => false,   #
+                       'friends_count' => 0,
+                       'created_at' => '',
+                       'utc_offset' => 0, #XXX: fix me
+                       'time_zone' => '', //$uinfo[0]['timezone'],
+                       'geo_enabled' => false,
+                       'statuses_count' => 0,
+                       'lang' => 'en', #XXX: fix me
+                       'description' => '',
+                       'followers_count' => 0,
+                       'favourites_count' => 0,
+                       'contributors_enabled' => false,
+                       'follow_request_sent' => false,
+                       'profile_background_color' => 'cfe8f6',
+                       'profile_text_color' => '000000',
+                       'profile_link_color' => 'FF8500',
+                       'profile_sidebar_fill_color' =>'AD0066',
+                       'profile_sidebar_border_color' => 'AD0066',
+                       'profile_background_image_url' => '',
+                       'profile_background_tile' => false,
+                       'profile_use_background_image' => false,
+                       'notifications' => false,
+                       'verified' => true, #XXX: fix me
+                       'followers' => '', #XXX: fix me
+                       #'status' => null
+               );
+
+               return $ret; 
+       }
+
        /**
         * apply xmlify() to all values of array $val, recursively
         */
         */
        function api_apply_template($templatename, $type, $data){
 
+               $a = get_app();
+
                switch($type){
-                       case "rss":
                        case "atom":
+                       case "rss":
                        case "xml":
                                $data = api_xmlify($data);
                                $tpl = get_markup_template("api_".$templatename."_".$type.".tpl");
                
                $user_info = api_get_user($a);
                // get last newtork messages
-               $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` ) ";
+//             $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` ) ";
 
                $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
                        `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
                        AND `contact`.`id` = `item`.`contact-id`
                        AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
                        $sql_extra
-                       ORDER BY `item`.`created` DESC LIMIT %d ,%d ",
+                       ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
                        intval($user_info['uid']),
                        0,20
                );
+
+               $ret = api_format_items($r,$user_info);
+
+               
+               $data = array('$statuses' => $ret);
+               switch($type){
+                       case "atom":
+                       case "rss":
+                               $data = api_rss_extra($a, $data, $user_info);
+               }
+                               
+               return  api_apply_template("timeline", $type, $data);
+       }
+       api_register_func('api/statuses/home_timeline','api_statuses_home_timeline', true);
+       api_register_func('api/statuses/friends_timeline','api_statuses_home_timeline', true);
+
+
+
+       function api_statuses_user_timeline(&$a, $type){
+               if (local_user()===false) return false;
+               
+               $user_info = api_get_user($a);
+               // get last newtork messages
+//             $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` ) ";
+
+               $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
+                       `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
+                       `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
+                       `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
+                       FROM `item`, `contact`
+                       WHERE `item`.`uid` = %d
+                       AND `item`.`visible` = 1 AND `item`.`deleted` = 0
+                       AND `item`.`wall` = 1
+                       AND `contact`.`id` = `item`.`contact-id`
+                       AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
+                       $sql_extra
+                       ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
+                       intval($user_info['uid']),
+                       0,20
+               );
+
+               $ret = api_format_items($r,$user_info);
+
+               
+               $data = array('$statuses' => $ret);
+               switch($type){
+                       case "atom":
+                       case "rss":
+                               $data = api_rss_extra($a, $data, $user_info);
+               }
+                               
+               return  api_apply_template("timeline", $type, $data);
+       }
+
+       api_register_func('api/statuses/user_timeline','api_statuses_user_timeline', true);
+
+
+       function api_favorites(&$a, $type){
+               if (local_user()===false) return false;
+               
+               $user_info = api_get_user($a);
+               // get last newtork messages
+//             $sql_extra = " AND `item`.`parent` IN ( SELECT `parent` FROM `item` WHERE `id` = `parent` ) ";
+
+               $r = q("SELECT `item`.*, `item`.`id` AS `item_id`, 
+                       `contact`.`name`, `contact`.`photo`, `contact`.`url`, `contact`.`rel`,
+                       `contact`.`network`, `contact`.`thumb`, `contact`.`dfrn-id`, `contact`.`self`,
+                       `contact`.`id` AS `cid`, `contact`.`uid` AS `contact-uid`
+                       FROM `item`, `contact`
+                       WHERE `item`.`uid` = %d
+                       AND `item`.`visible` = 1 AND `item`.`deleted` = 0
+                       AND `item`.`starred` = 1
+                       AND `contact`.`id` = `item`.`contact-id`
+                       AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
+                       $sql_extra
+                       ORDER BY `item`.`received` DESC LIMIT %d ,%d ",
+                       intval($user_info['uid']),
+                       0,20
+               );
+
+               $ret = api_format_items($r,$user_info);
+
+               
+               $data = array('$statuses' => $ret);
+               switch($type){
+                       case "atom":
+                       case "rss":
+                               $data = api_rss_extra($a, $data, $user_info);
+               }
+                               
+               return  api_apply_template("timeline", $type, $data);
+       }
+
+       api_register_func('api/favorites','api_favorites', true);
+
+       
+       function api_format_items($r,$user_info) {
+               $a = get_app();
                $ret = Array();
 
                foreach($r as $item) {
-                       $status_user = (($item['cid']==$user_info['id'])?$user_info: api_get_user($a,$item['cid']));
+                       $status_user = (($item['cid']==$user_info['id'])?$user_info: api_item_get_user($a,$item));
                        $status = array(
                                'created_at'=> api_date($item['created']),
                                'published' => datetime_convert('UTC','UTC',$item['created'],ATOM_TIME),
                                'updated'   => datetime_convert('UTC','UTC',$item['edited'],ATOM_TIME),
-                               'id'            => $item['id'],
+                               'id'            => $item['uri'],
                                'text'          => strip_tags(bbcode($item['body'])),
                                'html'          => bbcode($item['body']),
                                'source'    => (($item['app']) ? $item['app'] : 'web'),
                                'annotations'  => '',
                                'entities'  => '',
                                'user' =>  $status_user ,
-                               'objecttype' => $item['object-type'],
-                               'verb' => $item['verb'],
-                               'self' => $a->get_baseurl()."/api/statuses/show/".$ite['id'].".".$type,
-                               'edit' => $a->get_baseurl()."/api/statuses/show/".$ite['id'].".".$type,                         
+                               'objecttype' => (($item['object-type']) ? $item['object-type'] : ACTIVITY_OBJ_NOTE),
+                               'verb' => (($item['verb']) ? $item['verb'] : ACTIVITY_POST),
+                               'self' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,
+                               'edit' => $a->get_baseurl()."/api/statuses/show/".$item['id'].".".$type,                                
                        );
                        $ret[]=$status;
                };
-               
-               $data = array('$statuses' => $ret);
-               switch($type){
-                       case "atom":
-                       case "rss":
-                               $data = api_rss_extra($a, $data, $user_info);
-               }
-                               
-               return  api_apply_template("timeline", $type, $data);
+               return $ret;
        }
-       api_register_func('api/statuses/home_timeline','api_statuses_home_timeline', true);
-       api_register_func('api/statuses/friends_timeline','api_statuses_home_timeline', true);
-       api_register_func('api/statuses/user_timeline','api_statuses_home_timeline', true);
-       # TODO: user_timeline should be profile view
-       
+
 
        function api_account_rate_limit_status(&$a,$type) {
 
                return api_apply_template('config', $type, array('$config' => $config));
 
        }
-       api_register_func('api/statusnet/config','api_statusnet_config',true);
+       api_register_func('api/statusnet/config','api_statusnet_config',false);
+
+
+       function api_statusnet_version(&$a,$type) {
 
+               // liar
+
+               if($type === 'xml') {
+                       header("Content-type: application/xml");
+                       echo '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n" . '<version>0.9.7</version>' . "\r\n";
+                       killme();
+               }
+               elseif($type === 'json') {
+                       header("Content-type: application/json");
+                       echo '"0.9.7"';
+                       killme();
+               }
+       }
+       api_register_func('api/statusnet/version','api_statusnet_version',false);