From d7d698bef4bc0487ae41b61f178f1db18230481b Mon Sep 17 00:00:00 2001 From: gerhard6380 Date: Wed, 10 Aug 2016 23:56:17 +0200 Subject: [PATCH 1/8] new API calls for private messsages in Win10 app new API calls used for Windows 10 app (similar calls to existing but extended to include seen id and parent-uri) --- include/api.php | 383 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 383 insertions(+) diff --git a/include/api.php b/include/api.php index a856b747f8..8b8ec4c6f5 100644 --- a/include/api.php +++ b/include/api.php @@ -3664,6 +3664,389 @@ api_register_func('api/friendica/notification', 'api_friendica_notification', true, API_METHOD_GET); + /** + * @brief same as api_format_messages, but output extended by seen and parent-uri as needed + * in Windows 10 client + * + * @param array $item + * @param array $recipient + * @param array $sender + * @return array $ret + */ + function api_format_messages_win($item, $recipient, $sender) { + // standard meta information + $ret=Array( + 'id' => $item['id'], + 'sender_id' => $sender['id'] , + 'text' => "", + 'recipient_id' => $recipient['id'], + 'created_at' => api_date($item['created']), + 'sender_screen_name' => $sender['screen_name'], + 'recipient_screen_name' => $recipient['screen_name'], + 'sender' => $sender, + 'recipient' => $recipient, + 'title' => "", + 'seen' => $item['seen'], + 'parent_uri' => $item['parent-uri'], + ); + + // "uid" and "self" are only needed for some internal stuff, so remove it from here + unset($ret["sender"]["uid"]); + unset($ret["sender"]["self"]); + unset($ret["recipient"]["uid"]); + unset($ret["recipient"]["self"]); + + //don't send title to regular StatusNET requests to avoid confusing these apps + if (x($_GET, 'getText')) { + $ret['title'] = $item['title'] ; + if ($_GET["getText"] == "html") { + $ret['text'] = bbcode($item['body'], false, false); + } + elseif ($_GET["getText"] == "plain") { + $ret['text'] = trim(html2plain(bbcode(api_clean_plain_items($item['body']), false, false, 2, true), 0)); + } + } + else { + $ret['text'] = $item['title']."\n".html2plain(bbcode(api_clean_plain_items($item['body']), false, false, 2, true), 0); + } + if (isset($_GET["getUserObjects"]) && $_GET["getUserObjects"] == "false") { + unset($ret['sender']); + unset($ret['recipient']); + } + + return $ret; + } + + /** + * @brief return direct_messages for Windows 10 App (similar to direct_messages/all, but seen + * and parent-uri added to output + * + * @param App $a + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string + */ + function api_friendica_direct_messages_all($type){ + $a = get_app(); + + if (api_user()===false) throw new ForbiddenException(); + + // params + $count = (x($_GET,'count')?$_GET['count']:20); + $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); + if ($page<0) $page=0; + + $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); + $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); + + $user_id = (x($_REQUEST,'user_id')?$_REQUEST['user_id']:""); + $screen_name = (x($_REQUEST,'screen_name')?$_REQUEST['screen_name']:""); + + // caller user info + unset($_REQUEST["user_id"]); + unset($_GET["user_id"]); + + unset($_REQUEST["screen_name"]); + unset($_GET["screen_name"]); + + $user_info = api_get_user($a); + $profile_url = $user_info["url"]; + + // pagination + $start = $page*$count; + + // filters + $sql_extra = "true"; + + if ($max_id > 0) + $sql_extra .= ' AND `mail`.`id` <= '.intval($max_id); + + if ($user_id !="") { + $sql_extra .= ' AND `mail`.`contact-id` = ' . intval($user_id); + } + elseif($screen_name !=""){ + $sql_extra .= " AND `contact`.`nick` = '" . dbesc($screen_name). "'"; + } + + $r = q("SELECT `mail`.*, `contact`.`nurl` AS `contact-url` FROM `mail`,`contact` WHERE `mail`.`contact-id` = `contact`.`id` AND `mail`.`uid`=%d AND $sql_extra AND `mail`.`id` > %d ORDER BY `mail`.`id` DESC LIMIT %d,%d", + intval(api_user()), + intval($since_id), + intval($start), intval($count) + ); + + // stop execution and return error message if no mails available + if($r == null) { + $answer = array('result' => 'error', 'message' => 'no mails available'); + return api_format_data("direct_messages_all", $type, array('$result' => $answer)); + } + + $ret = Array(); + foreach($r as $item) { + if ($box == "inbox" || $item['from-url'] != $profile_url){ + $recipient = $user_info; + $sender = api_get_user($a,normalise_link($item['contact-url'])); + } + elseif ($box == "sentbox" || $item['from-url'] == $profile_url){ + $recipient = api_get_user($a,normalise_link($item['contact-url'])); + $sender = $user_info; + + } + $ret[]=api_format_messages_win($item, $recipient, $sender); + } + + + $data = array('$messages' => $ret); + switch($type){ + case "atom": + case "rss": + $data = api_rss_extra($a, $data, $user_info); + } + + return api_format_data("direct_messages_all", $type, $data); + + } + api_register_func('api/friendica/direct_messages_all', 'api_friendica_direct_messages_all', true); + + + /** + * @brief update a direct_message to seen state for Windows 10 App + * + * @param App $a + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string + */ + function api_friendica_direct_messages_setseen($type){ + $a = get_app(); + if (api_user()===false) throw new ForbiddenException(); + + // params + $user_info = api_get_user($a); + $uid = $user_info['uid']; + $id = (x($_REQUEST, 'id') ? $_REQUEST['id'] : 0); + + // return error if id is zero + if ($id == "") { + $answer = array('result' => 'error', 'message' => 'message id not specified'); + return api_format_data("direct_messages_setseen", $type, array('$result' => $answer)); + } + + // get data of the specified message id + $r = q("SELECT * FROM `mail` WHERE `id` = %d AND `uid` = %d", + intval($id), + intval($uid)); + // error message if specified id is not in database + if (count($r) == 0) { + $answer = array('result' => 'error', 'message' => 'message id not in database'); + return api_format_data("direct_messages_setseen", $type, array('$result' => $answer)); + } + + // update seen indicator + $result = q("UPDATE `mail` SET `seen` = 1 WHERE `id` = %d AND `uid` = %d", + intval($id), + intval($uid)); + + if ($result) { + // return success + $answer = array('result' => 'ok', 'message' => 'message set to seen'); + return api_format_data("direct_message_setseen", $type, array('$result' => $answer)); + } else { + $answer = array('result' => 'error', 'message' => 'unknown error'); + return api_format_data("direct_messages_setseen", $type, array('$result' => $answer)); + } + } + api_register_func('api/friendica/direct_messages_setseen', 'api_friendica_direct_messages_setseen', true); + + + /** + * @brief delete a direct_message from mail table through api + * + * @param App $a + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string + */ + function api_friendica_direct_messages_delete($type){ + $a = get_app(); + + if (api_user()===false) throw new ForbiddenException(); + + // params + $user_info = api_get_user($a); + $id = (x($_REQUEST,'id') ? $_REQUEST['id'] : 0); + $parenturi = (x($_REQUEST, 'parenturi') ? $_REQUEST['parenturi'] : ""); + $uid = $user_info['uid']; + + // error if no id or parenturi specified + if ($id == 0 || $parenturi == "") { + $answer = array('result' => 'error', 'message' => 'message id or parenturi not specified'); + return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); + } + + // get data of the specified message id + $r = q("SELECT * FROM `mail` WHERE `uid` = %d AND `id` = %d", + intval($uid), + intval($id)); + // error message if specified id is not in database + if (count($r) == 0) { + $answer = array('result' => 'error', 'message' => 'message id not in database'); + return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); + } + + // delete message + $result = q("DELETE FROM `mail` WHERE `uid` = %d AND `id` = %d AND `parent-uri` = '%s'", + intval($uid), + intval($id), + dbesc($parenturi)); + + if ($result) { + // return success + $answer = array('result' => 'ok', 'message' => 'message deleted'); + return api_format_data("direct_message_delete", $type, array('$result' => $answer)); + } + else { + $answer = array('result' => 'error', 'message' => 'unknown error'); + return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); + } + } + api_register_func('api/friendica/direct_messages_delete', 'api_friendica_direct_messages_delete', true); + + + /** + * @brief search for direct_messages containing a searchstring through api + * + * @param App $a + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string + */ + function api_friendica_direct_messages_search($type){ + $a = get_app(); + + if (api_user()===false) throw new ForbiddenException(); + + // params + $user_info = api_get_user($a); + $searchstring = (x($_REQUEST,'searchstring') ? $_REQUEST['searchstring'] : ""); + $uid = $user_info['uid']; + + // error if no searchstring specified + if ($searchstring == "") { + $answer = array('result' => 'error', 'message' => 'searchstring not specified'); + return api_format_data("direct_messages_search", $type, array('$result' => $answer)); + } + + // get data for the specified searchstring + $r = q("SELECT `mail`.*, `contact`.`nurl` AS `contact-url` FROM `mail`,`contact` WHERE `mail`.`contact-id` = `contact`.`id` AND `mail`.`uid`=%d AND `body` LIKE '%s' ORDER BY `mail`.`id` DESC", + intval($uid), + dbesc('%'.$searchstring.'%') + ); + + $profile_url = $user_info["url"]; + // message if nothing was found + if (count($r) == 0) + $success = array('success' => false, 'search_results' => 'nothing found'); + else { + $ret = Array(); + foreach($r as $item) { + if ($box == "inbox" || $item['from-url'] != $profile_url){ + $recipient = $user_info; + $sender = api_get_user($a,normalise_link($item['contact-url'])); + } + elseif ($box == "sentbox" || $item['from-url'] == $profile_url){ + $recipient = api_get_user($a,normalise_link($item['contact-url'])); + $sender = $user_info; + } + $ret[]=api_format_messages_win($item, $recipient, $sender); + } + $success = array('success' => true, 'search_results' => $ret); + } + + return api_format_data("direct_message_search", $type, array('$result' => $success)); + } + api_register_func('api/friendica/direct_messages_search', 'api_friendica_direct_messages_search', true); + + + /** + * @brief returns all messages for a specified parenturi, similar to api/direct_messages/conversation but enhanced to return parenturi and seen state + * + * @param App $a + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string + */ + function api_friendica_direct_messages_conversation($type) { + $a = get_app(); + if (api_user()===false) return false; + + // params + $count = (x($_GET,'count')?$_GET['count']:20); + $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); + if ($page<0) $page=0; + + $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); + $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); + + $user_id = (x($_REQUEST,'user_id')?$_REQUEST['user_id']:""); + $screen_name = (x($_REQUEST,'screen_name')?$_REQUEST['screen_name']:""); + + // caller user info + unset($_REQUEST["user_id"]); + unset($_GET["user_id"]); + + unset($_REQUEST["screen_name"]); + unset($_GET["screen_name"]); + + $user_info = api_get_user($a); + $profile_url = $user_info["url"]; + + + // pagination + $start = $page*$count; + + $sql_extra = "`mail`.`parent-uri`='".dbesc( $_GET["uri"] ) ."'"; + + if ($max_id > 0) + $sql_extra .= ' AND `mail`.`id` <= '.intval($max_id); + + if ($user_id !="") { + $sql_extra .= ' AND `mail`.`contact-id` = ' . intval($user_id); + } + elseif($screen_name !=""){ + $sql_extra .= " AND `contact`.`nick` = '" . dbesc($screen_name). "'"; + } + + $r = q("SELECT `mail`.*, `contact`.`nurl` AS `contact-url` FROM `mail`,`contact` WHERE `mail`.`contact-id` = `contact`.`id` AND `mail`.`uid`=%d AND $sql_extra AND `mail`.`id` > %d ORDER BY `mail`.`id` DESC LIMIT %d,%d", + intval(api_user()), + intval($since_id), + intval($start), intval($count) + ); + + + $ret = Array(); + foreach($r as $item) { + if ($box == "inbox" || $item['from-url'] != $profile_url){ + $recipient = $user_info; + $sender = api_get_user($a,normalise_link($item['contact-url'])); + } + elseif ($box == "sentbox" || $item['from-url'] == $profile_url){ + $recipient = api_get_user($a,normalise_link($item['contact-url'])); + $sender = $user_info; + + } + $ret[]=api_format_messages_win($item, $recipient, $sender); + } + + + $data = array('$messages' => $ret); + switch($type){ + case "atom": + case "rss": + $data = api_rss_extra($a, $data, $user_info); + } + + return api_format_data("direct_messages", $type, $data); + + } + + api_register_func('api/friendica/direct_messages_conversation','api_friendica_direct_messages_conversation',true); + + /* To.Do: [pagename] => api/1.1/statuses/lookup.json From db6fb8efe262b8142ec74d7132c67241d69b739d Mon Sep 17 00:00:00 2001 From: gerhard6380 Date: Thu, 11 Aug 2016 17:34:33 +0200 Subject: [PATCH 2/8] remove api_format_messages_win and include new fields in api_format_messages --- include/api.php | 62 +++++-------------------------------------------- 1 file changed, 6 insertions(+), 56 deletions(-) diff --git a/include/api.php b/include/api.php index 8b8ec4c6f5..c60575638a 100644 --- a/include/api.php +++ b/include/api.php @@ -2049,6 +2049,9 @@ 'recipient_screen_name' => $recipient['screen_name'], 'sender' => $sender, 'recipient' => $recipient, + 'title' => "", + 'friendica_seen' => $item['seen'], + 'friendica_parent_uri' => $item['parent-uri'], ); // "uid" and "self" are only needed for some internal stuff, so remove it from here @@ -3664,59 +3667,6 @@ api_register_func('api/friendica/notification', 'api_friendica_notification', true, API_METHOD_GET); - /** - * @brief same as api_format_messages, but output extended by seen and parent-uri as needed - * in Windows 10 client - * - * @param array $item - * @param array $recipient - * @param array $sender - * @return array $ret - */ - function api_format_messages_win($item, $recipient, $sender) { - // standard meta information - $ret=Array( - 'id' => $item['id'], - 'sender_id' => $sender['id'] , - 'text' => "", - 'recipient_id' => $recipient['id'], - 'created_at' => api_date($item['created']), - 'sender_screen_name' => $sender['screen_name'], - 'recipient_screen_name' => $recipient['screen_name'], - 'sender' => $sender, - 'recipient' => $recipient, - 'title' => "", - 'seen' => $item['seen'], - 'parent_uri' => $item['parent-uri'], - ); - - // "uid" and "self" are only needed for some internal stuff, so remove it from here - unset($ret["sender"]["uid"]); - unset($ret["sender"]["self"]); - unset($ret["recipient"]["uid"]); - unset($ret["recipient"]["self"]); - - //don't send title to regular StatusNET requests to avoid confusing these apps - if (x($_GET, 'getText')) { - $ret['title'] = $item['title'] ; - if ($_GET["getText"] == "html") { - $ret['text'] = bbcode($item['body'], false, false); - } - elseif ($_GET["getText"] == "plain") { - $ret['text'] = trim(html2plain(bbcode(api_clean_plain_items($item['body']), false, false, 2, true), 0)); - } - } - else { - $ret['text'] = $item['title']."\n".html2plain(bbcode(api_clean_plain_items($item['body']), false, false, 2, true), 0); - } - if (isset($_GET["getUserObjects"]) && $_GET["getUserObjects"] == "false") { - unset($ret['sender']); - unset($ret['recipient']); - } - - return $ret; - } - /** * @brief return direct_messages for Windows 10 App (similar to direct_messages/all, but seen * and parent-uri added to output @@ -3790,7 +3740,7 @@ $sender = $user_info; } - $ret[]=api_format_messages_win($item, $recipient, $sender); + $ret[]=api_format_messages($item, $recipient, $sender); } @@ -3953,7 +3903,7 @@ $recipient = api_get_user($a,normalise_link($item['contact-url'])); $sender = $user_info; } - $ret[]=api_format_messages_win($item, $recipient, $sender); + $ret[]=api_format_messages($item, $recipient, $sender); } $success = array('success' => true, 'search_results' => $ret); } @@ -4029,7 +3979,7 @@ $sender = $user_info; } - $ret[]=api_format_messages_win($item, $recipient, $sender); + $ret[]=api_format_messages($item, $recipient, $sender); } From fb087ce89eba07836bfb5fb192b2b1e31e468e62 Mon Sep 17 00:00:00 2001 From: gerhard6380 Date: Thu, 11 Aug 2016 23:53:00 +0200 Subject: [PATCH 3/8] Removing api_friendica_direct_messages_conversations due to implementing 'seen' and 'parent-uri' into standard api_format_messages() this call is not needed anymore. Adapting comment blocks. --- include/api.php | 108 +++++------------------------------------------- 1 file changed, 11 insertions(+), 97 deletions(-) diff --git a/include/api.php b/include/api.php index c60575638a..fdaf50e661 100644 --- a/include/api.php +++ b/include/api.php @@ -3668,12 +3668,11 @@ /** - * @brief return direct_messages for Windows 10 App (similar to direct_messages/all, but seen - * and parent-uri added to output + * @brief return direct_messages (similar to direct_messages/all, but additional + * error string returned if no mails available to react in client with notification) * - * @param App $a * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string + * @return string (error -> No Mails available, success -> return messages) */ function api_friendica_direct_messages_all($type){ $a = get_app(); @@ -3744,25 +3743,24 @@ } - $data = array('$messages' => $ret); + $data = array('direct-messages' => $ret); switch($type){ case "atom": case "rss": $data = api_rss_extra($a, $data, $user_info); } - return api_format_data("direct_messages_all", $type, $data); + return api_format_data("direct-messages", $type, $data); } api_register_func('api/friendica/direct_messages_all', 'api_friendica_direct_messages_all', true); /** - * @brief update a direct_message to seen state for Windows 10 App + * @brief update a direct_message to seen state * - * @param App $a * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string + * @return string (success result=ok, error result=error with error message) */ function api_friendica_direct_messages_setseen($type){ $a = get_app(); @@ -3809,9 +3807,8 @@ /** * @brief delete a direct_message from mail table through api * - * @param App $a * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string + * @return string (success result=ok, error result=error with error message) */ function api_friendica_direct_messages_delete($type){ $a = get_app(); @@ -3862,9 +3859,10 @@ /** * @brief search for direct_messages containing a searchstring through api * - * @param App $a * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string + * @return string (success: success=true if found and search_result contains found messages + * success=false if nothing was found, search_result='nothing found', + * error: result=error with error message) */ function api_friendica_direct_messages_search($type){ $a = get_app(); @@ -3913,90 +3911,6 @@ api_register_func('api/friendica/direct_messages_search', 'api_friendica_direct_messages_search', true); - /** - * @brief returns all messages for a specified parenturi, similar to api/direct_messages/conversation but enhanced to return parenturi and seen state - * - * @param App $a - * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string - */ - function api_friendica_direct_messages_conversation($type) { - $a = get_app(); - if (api_user()===false) return false; - - // params - $count = (x($_GET,'count')?$_GET['count']:20); - $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); - if ($page<0) $page=0; - - $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); - $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); - - $user_id = (x($_REQUEST,'user_id')?$_REQUEST['user_id']:""); - $screen_name = (x($_REQUEST,'screen_name')?$_REQUEST['screen_name']:""); - - // caller user info - unset($_REQUEST["user_id"]); - unset($_GET["user_id"]); - - unset($_REQUEST["screen_name"]); - unset($_GET["screen_name"]); - - $user_info = api_get_user($a); - $profile_url = $user_info["url"]; - - - // pagination - $start = $page*$count; - - $sql_extra = "`mail`.`parent-uri`='".dbesc( $_GET["uri"] ) ."'"; - - if ($max_id > 0) - $sql_extra .= ' AND `mail`.`id` <= '.intval($max_id); - - if ($user_id !="") { - $sql_extra .= ' AND `mail`.`contact-id` = ' . intval($user_id); - } - elseif($screen_name !=""){ - $sql_extra .= " AND `contact`.`nick` = '" . dbesc($screen_name). "'"; - } - - $r = q("SELECT `mail`.*, `contact`.`nurl` AS `contact-url` FROM `mail`,`contact` WHERE `mail`.`contact-id` = `contact`.`id` AND `mail`.`uid`=%d AND $sql_extra AND `mail`.`id` > %d ORDER BY `mail`.`id` DESC LIMIT %d,%d", - intval(api_user()), - intval($since_id), - intval($start), intval($count) - ); - - - $ret = Array(); - foreach($r as $item) { - if ($box == "inbox" || $item['from-url'] != $profile_url){ - $recipient = $user_info; - $sender = api_get_user($a,normalise_link($item['contact-url'])); - } - elseif ($box == "sentbox" || $item['from-url'] == $profile_url){ - $recipient = api_get_user($a,normalise_link($item['contact-url'])); - $sender = $user_info; - - } - $ret[]=api_format_messages($item, $recipient, $sender); - } - - - $data = array('$messages' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra($a, $data, $user_info); - } - - return api_format_data("direct_messages", $type, $data); - - } - - api_register_func('api/friendica/direct_messages_conversation','api_friendica_direct_messages_conversation',true); - - /* To.Do: [pagename] => api/1.1/statuses/lookup.json From 41c1f71d4d609e0db38494b9cc8f319b3aa1b8ce Mon Sep 17 00:00:00 2001 From: gerhard6380 Date: Fri, 12 Aug 2016 15:40:22 +0200 Subject: [PATCH 4/8] remove api_friendica_direct_messages_all instead included 'friendica_verbose' parameter into standard function api_direct_messages_box() --- include/api.php | 111 +++++++----------------------------------------- 1 file changed, 16 insertions(+), 95 deletions(-) diff --git a/include/api.php b/include/api.php index fdaf50e661..df93c2aefb 100644 --- a/include/api.php +++ b/include/api.php @@ -2808,7 +2808,7 @@ } api_register_func('api/direct_messages/new','api_direct_messages_new',true, API_METHOD_POST); - function api_direct_messages_box($type, $box) { + function api_direct_messages_box($type, $box, $verbose) { $a = get_app(); @@ -2868,7 +2868,13 @@ intval($since_id), intval($start), intval($count) ); - + if ($verbose == "true") { + // stop execution and return error message if no mails available + if($r == null) { + $answer = array('result' => 'error', 'message' => 'no mails available'); + return api_format_data("direct_messages_all", $type, array('$result' => $answer)); + } + } $ret = Array(); foreach($r as $item) { @@ -2897,16 +2903,20 @@ } function api_direct_messages_sentbox($type){ - return api_direct_messages_box($type, "sentbox"); + $verbose = (x($_GET,'friendica_verbose')?strtolower($_GET['friendica_verbose']):"false"); + return api_direct_messages_box($type, "sentbox", $verbose); } function api_direct_messages_inbox($type){ - return api_direct_messages_box($type, "inbox"); + $verbose = (x($_GET,'friendica_verbose')?strtolower($_GET['friendica_verbose']):"false"); + return api_direct_messages_box($type, "inbox", $verbose); } function api_direct_messages_all($type){ - return api_direct_messages_box($type, "all"); + $verbose = (x($_GET,'friendica_verbose')?strtolower($_GET['friendica_verbose']):"false"); + return api_direct_messages_box($type, "all", $verbose); } function api_direct_messages_conversation($type){ - return api_direct_messages_box($type, "conversation"); + $verbose = (x($_GET,'friendica_verbose')?strtolower($_GET['friendica_verbose']):"false"); + return api_direct_messages_box($type, "conversation", $verbose); } api_register_func('api/direct_messages/conversation','api_direct_messages_conversation',true); api_register_func('api/direct_messages/all','api_direct_messages_all',true); @@ -3667,95 +3677,6 @@ api_register_func('api/friendica/notification', 'api_friendica_notification', true, API_METHOD_GET); - /** - * @brief return direct_messages (similar to direct_messages/all, but additional - * error string returned if no mails available to react in client with notification) - * - * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string (error -> No Mails available, success -> return messages) - */ - function api_friendica_direct_messages_all($type){ - $a = get_app(); - - if (api_user()===false) throw new ForbiddenException(); - - // params - $count = (x($_GET,'count')?$_GET['count']:20); - $page = (x($_REQUEST,'page')?$_REQUEST['page']-1:0); - if ($page<0) $page=0; - - $since_id = (x($_REQUEST,'since_id')?$_REQUEST['since_id']:0); - $max_id = (x($_REQUEST,'max_id')?$_REQUEST['max_id']:0); - - $user_id = (x($_REQUEST,'user_id')?$_REQUEST['user_id']:""); - $screen_name = (x($_REQUEST,'screen_name')?$_REQUEST['screen_name']:""); - - // caller user info - unset($_REQUEST["user_id"]); - unset($_GET["user_id"]); - - unset($_REQUEST["screen_name"]); - unset($_GET["screen_name"]); - - $user_info = api_get_user($a); - $profile_url = $user_info["url"]; - - // pagination - $start = $page*$count; - - // filters - $sql_extra = "true"; - - if ($max_id > 0) - $sql_extra .= ' AND `mail`.`id` <= '.intval($max_id); - - if ($user_id !="") { - $sql_extra .= ' AND `mail`.`contact-id` = ' . intval($user_id); - } - elseif($screen_name !=""){ - $sql_extra .= " AND `contact`.`nick` = '" . dbesc($screen_name). "'"; - } - - $r = q("SELECT `mail`.*, `contact`.`nurl` AS `contact-url` FROM `mail`,`contact` WHERE `mail`.`contact-id` = `contact`.`id` AND `mail`.`uid`=%d AND $sql_extra AND `mail`.`id` > %d ORDER BY `mail`.`id` DESC LIMIT %d,%d", - intval(api_user()), - intval($since_id), - intval($start), intval($count) - ); - - // stop execution and return error message if no mails available - if($r == null) { - $answer = array('result' => 'error', 'message' => 'no mails available'); - return api_format_data("direct_messages_all", $type, array('$result' => $answer)); - } - - $ret = Array(); - foreach($r as $item) { - if ($box == "inbox" || $item['from-url'] != $profile_url){ - $recipient = $user_info; - $sender = api_get_user($a,normalise_link($item['contact-url'])); - } - elseif ($box == "sentbox" || $item['from-url'] == $profile_url){ - $recipient = api_get_user($a,normalise_link($item['contact-url'])); - $sender = $user_info; - - } - $ret[]=api_format_messages($item, $recipient, $sender); - } - - - $data = array('direct-messages' => $ret); - switch($type){ - case "atom": - case "rss": - $data = api_rss_extra($a, $data, $user_info); - } - - return api_format_data("direct-messages", $type, $data); - - } - api_register_func('api/friendica/direct_messages_all', 'api_friendica_direct_messages_all', true); - - /** * @brief update a direct_message to seen state * From 3b8db28a4311b57c6a59f3845bc35f740cc0ae09 Mon Sep 17 00:00:00 2001 From: gerhard6380 Date: Fri, 12 Aug 2016 18:15:03 +0200 Subject: [PATCH 5/8] Implement api/direct_messages/destroy Twitter compliant call replaces api/friendica/direct_messages_delete; JSON return expected by Twitter API and parameter include_entities not yet implemented --- include/api.php | 123 ++++++++++++++++++++++++++++-------------------- 1 file changed, 73 insertions(+), 50 deletions(-) diff --git a/include/api.php b/include/api.php index df93c2aefb..666bafcec9 100644 --- a/include/api.php +++ b/include/api.php @@ -2808,6 +2808,79 @@ } api_register_func('api/direct_messages/new','api_direct_messages_new',true, API_METHOD_POST); + + /** + * @brief delete a direct_message from mail table through api + * + * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' + * @return string + */ + function api_direct_messages_destroy($type){ + $a = get_app(); + + if (api_user()===false) throw new ForbiddenException(); + + // params + $user_info = api_get_user($a); + //required + $id = (x($_REQUEST,'id') ? $_REQUEST['id'] : 0); + // optional + $parenturi = (x($_REQUEST, 'friendica_parenturi') ? $_REQUEST['friendica_parenturi'] : ""); + $verbose = (x($_GET,'friendica_verbose')?strtolower($_GET['friendica_verbose']):"false"); + // TODO: optional parameter 'include_entities' from Twitter API not yet implemented + + $uid = $user_info['uid']; + // error if no id or parenturi specified (for clients posting parent-uri as well) + if ($verbose == "true") { + if ($id == 0 || $parenturi == "") { + $answer = array('result' => 'error', 'message' => 'message id or parenturi not specified'); + return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); + } + } + + // BadRequestException if no id specified (for clients using Twitter API) + if ($id == 0) throw new BadRequestException('Message id not specified'); + + // add parent-uri to sql command if specified by calling app + $sql_extra = ($parenturi != "" ? " AND `parent-uri` = '" . dbesc($parenturi) . "'" : ""); + + // get data of the specified message id + $r = q("SELECT * FROM `mail` WHERE `uid` = %d AND `id` = %d" . $sql_extra, + intval($uid), + intval($id)); + + // error message if specified id is not in database + if (count($r) == 0) { + if ($verbose == "true") { + $answer = array('result' => 'error', 'message' => 'message id not in database'); + return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); + } + // TODO: BadRequestException ok for Twitter API clients? + throw new BadRequestException('message id not in database'); + } + + // delete message + $result = q("DELETE FROM `mail` WHERE `uid` = %d AND `id` = %d" . $sql_extra, + intval($uid), + intval($id)); + + if ($verbose == "true") { + if ($result) { + // return success + $answer = array('result' => 'ok', 'message' => 'message deleted'); + return api_format_data("direct_message_delete", $type, array('$result' => $answer)); + } + else { + $answer = array('result' => 'error', 'message' => 'unknown error'); + return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); + } + } + // TODO: return JSON data like Twitter API not yet implemented + + } + api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true, API_METHOD_DELETE); + + function api_direct_messages_box($type, $box, $verbose) { $a = get_app(); @@ -3725,56 +3798,6 @@ api_register_func('api/friendica/direct_messages_setseen', 'api_friendica_direct_messages_setseen', true); - /** - * @brief delete a direct_message from mail table through api - * - * @param string $type Known types are 'atom', 'rss', 'xml' and 'json' - * @return string (success result=ok, error result=error with error message) - */ - function api_friendica_direct_messages_delete($type){ - $a = get_app(); - - if (api_user()===false) throw new ForbiddenException(); - - // params - $user_info = api_get_user($a); - $id = (x($_REQUEST,'id') ? $_REQUEST['id'] : 0); - $parenturi = (x($_REQUEST, 'parenturi') ? $_REQUEST['parenturi'] : ""); - $uid = $user_info['uid']; - - // error if no id or parenturi specified - if ($id == 0 || $parenturi == "") { - $answer = array('result' => 'error', 'message' => 'message id or parenturi not specified'); - return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); - } - - // get data of the specified message id - $r = q("SELECT * FROM `mail` WHERE `uid` = %d AND `id` = %d", - intval($uid), - intval($id)); - // error message if specified id is not in database - if (count($r) == 0) { - $answer = array('result' => 'error', 'message' => 'message id not in database'); - return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); - } - - // delete message - $result = q("DELETE FROM `mail` WHERE `uid` = %d AND `id` = %d AND `parent-uri` = '%s'", - intval($uid), - intval($id), - dbesc($parenturi)); - - if ($result) { - // return success - $answer = array('result' => 'ok', 'message' => 'message deleted'); - return api_format_data("direct_message_delete", $type, array('$result' => $answer)); - } - else { - $answer = array('result' => 'error', 'message' => 'unknown error'); - return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); - } - } - api_register_func('api/friendica/direct_messages_delete', 'api_friendica_direct_messages_delete', true); /** From 23393c05ee47a39bcc69b22a6eb5d199e4dc3888 Mon Sep 17 00:00:00 2001 From: gerhard6380 Date: Sat, 13 Aug 2016 13:08:16 +0200 Subject: [PATCH 6/8] improved sql statements for direct_messages calls --- include/api.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/api.php b/include/api.php index 666bafcec9..a380845ed1 100644 --- a/include/api.php +++ b/include/api.php @@ -2827,7 +2827,7 @@ // optional $parenturi = (x($_REQUEST, 'friendica_parenturi') ? $_REQUEST['friendica_parenturi'] : ""); $verbose = (x($_GET,'friendica_verbose')?strtolower($_GET['friendica_verbose']):"false"); - // TODO: optional parameter 'include_entities' from Twitter API not yet implemented + /// @todo optional parameter 'include_entities' from Twitter API not yet implemented $uid = $user_info['uid']; // error if no id or parenturi specified (for clients posting parent-uri as well) @@ -2845,17 +2845,17 @@ $sql_extra = ($parenturi != "" ? " AND `parent-uri` = '" . dbesc($parenturi) . "'" : ""); // get data of the specified message id - $r = q("SELECT * FROM `mail` WHERE `uid` = %d AND `id` = %d" . $sql_extra, + $r = q("SELECT `id` FROM `mail` WHERE `uid` = %d AND `id` = %d" . $sql_extra, intval($uid), intval($id)); // error message if specified id is not in database - if (count($r) == 0) { + if (!dbm::is_result($r)) { if ($verbose == "true") { $answer = array('result' => 'error', 'message' => 'message id not in database'); return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); } - // TODO: BadRequestException ok for Twitter API clients? + /// @todo BadRequestException ok for Twitter API clients? throw new BadRequestException('message id not in database'); } @@ -2875,7 +2875,7 @@ return api_format_data("direct_messages_delete", $type, array('$result' => $answer)); } } - // TODO: return JSON data like Twitter API not yet implemented + /// @todo return JSON data like Twitter API not yet implemented } api_register_func('api/direct_messages/destroy', 'api_direct_messages_destroy', true, API_METHOD_DELETE); @@ -3772,11 +3772,11 @@ } // get data of the specified message id - $r = q("SELECT * FROM `mail` WHERE `id` = %d AND `uid` = %d", + $r = q("SELECT `id` FROM `mail` WHERE `id` = %d AND `uid` = %d", intval($id), intval($uid)); // error message if specified id is not in database - if (count($r) == 0) { + if (!dbm::is_result($r)) { $answer = array('result' => 'error', 'message' => 'message id not in database'); return api_format_data("direct_messages_setseen", $type, array('$result' => $answer)); } From e5874aa34cd546a334023e77ab23c89d1461a5fe Mon Sep 17 00:00:00 2001 From: gerhard6380 Date: Sat, 13 Aug 2016 13:19:53 +0200 Subject: [PATCH 7/8] updated api documentation to reflect changes on direct_messages added direct_messages/destroy, friendica/direct_messages_setseen --- doc/api.md | 64 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 8 deletions(-) diff --git a/doc/api.md b/doc/api.md index 7d6f440c58..02295a95fe 100644 --- a/doc/api.md +++ b/doc/api.md @@ -104,6 +104,7 @@ Unofficial Twitter command. It shows all direct answers (excluding the original * max_id: maximum id * getText: Defines the format of the status field. Can be "html" or "plain" * include_entities: "true" shows entities for pictures and links (Default: false) +* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") #### Unsupported parameters * skip_status @@ -116,6 +117,7 @@ Unofficial Twitter command. It shows all direct answers (excluding the original * since_id: minimal id * max_id: maximum id * getText: Defines the format of the status field. Can be "html" or "plain" +* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") --- ### direct_messages/conversation (*; AUTH) @@ -127,6 +129,18 @@ Shows all direct messages of a conversation * max_id: maximum id * getText: Defines the format of the status field. Can be "html" or "plain" * uri: URI of the conversation +* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") + +--- +### direct_messages/sent (*; AUTH) +#### Parameters +* count: Items per page (default: 20) +* page: page number +* since_id: minimal id +* max_id: maximum id +* getText: Defines the format of the status field. Can be "html" or "plain" +* include_entities: "true" shows entities for pictures and links (Default: false) +* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") --- ### direct_messages/new (POST,PUT; AUTH) @@ -138,14 +152,22 @@ Shows all direct messages of a conversation * title: Title of the direct message --- -### direct_messages/sent (*; AUTH) +### direct_messages/destroy (POST,DELETE; AUTH) #### Parameters -* count: Items per page (default: 20) -* page: page number -* since_id: minimal id -* max_id: maximum id -* getText: Defines the format of the status field. Can be "html" or "plain" -* include_entities: "true" shows entities for pictures and links (Default: false) +* id: id of the message to be deleted +* include_entities: optional, currently not yet implemented +* friendica_parenturi: optional, can be used for increased safety to delete only intended messages +* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") + +#### Return values + +On success: +* JSON return as defined for Twitter API not yet implemented +* on friendica_verbose=true: JSON return {"result":"ok","message":"message deleted"} + +On error: +HTTP 400 BadRequest +* on friendica_verbose=true: different JSON returns {"result":"error","message":"xyz"} --- ### favorites (*; AUTH) @@ -694,6 +716,33 @@ xml ``` +--- +### friendica/direct_messages_setseen (GET; AUTH) +#### Parameters +* id: id of the message to be updated as seen + +#### Return values + +On success: +* JSON return {"result":"ok","message":"message set to seen"} + +On error: +* different JSON returns {"result":"error","message":"xyz"} + +--- +### friendica/direct_messages_search (GET; AUTH) +#### Parameters +* searchstring: string for which the API call should search as '%searchstring%' in field 'body' of all messages of the authenticated user (caption ignored) + +#### Return values +Returns only tested with JSON, XML might work as well. + +On success: +* JSON return {"success":"true","search_results": array of found messages} +* JSOn return {"success":"false","search_results":"nothing found"} + +On error: +* different JSON returns {"result":"error","message":"searchstring not specified"} --- ## Not Implemented API calls @@ -718,7 +767,6 @@ The following API calls from the Twitter API aren't implemented neither in Frien * statuses/lookup * direct_messages/show * search/tweets -* direct_messages/destroy * friendships/no_retweets/ids * friendships/incoming * friendships/outgoing From 30ecd7cf38d160079d954e976888b7125a3f14ef Mon Sep 17 00:00:00 2001 From: gerhard6380 Date: Sat, 13 Aug 2016 16:14:12 +0200 Subject: [PATCH 8/8] adapted documentation removed reference to Windows 10 app --- doc/api.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/api.md b/doc/api.md index 02295a95fe..0570e8e990 100644 --- a/doc/api.md +++ b/doc/api.md @@ -104,7 +104,7 @@ Unofficial Twitter command. It shows all direct answers (excluding the original * max_id: maximum id * getText: Defines the format of the status field. Can be "html" or "plain" * include_entities: "true" shows entities for pictures and links (Default: false) -* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") +* friendica_verbose: "true" enables different error returns (default: "false") #### Unsupported parameters * skip_status @@ -117,7 +117,7 @@ Unofficial Twitter command. It shows all direct answers (excluding the original * since_id: minimal id * max_id: maximum id * getText: Defines the format of the status field. Can be "html" or "plain" -* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") +* friendica_verbose: "true" enables different error returns (default: "false") --- ### direct_messages/conversation (*; AUTH) @@ -129,7 +129,7 @@ Shows all direct messages of a conversation * max_id: maximum id * getText: Defines the format of the status field. Can be "html" or "plain" * uri: URI of the conversation -* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") +* friendica_verbose: "true" enables different error returns (default: "false") --- ### direct_messages/sent (*; AUTH) @@ -140,7 +140,7 @@ Shows all direct messages of a conversation * max_id: maximum id * getText: Defines the format of the status field. Can be "html" or "plain" * include_entities: "true" shows entities for pictures and links (Default: false) -* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") +* friendica_verbose: "true" enables different error returns (default: "false") --- ### direct_messages/new (POST,PUT; AUTH) @@ -157,7 +157,7 @@ Shows all direct messages of a conversation * id: id of the message to be deleted * include_entities: optional, currently not yet implemented * friendica_parenturi: optional, can be used for increased safety to delete only intended messages -* friendica_verbose: "true" enables different error returns for Windows 10 app (default: "false") +* friendica_verbose: "true" enables different error returns (default: "false") #### Return values