new api for notifications

/api/friendica/notification
returns first 50 notifications for current user

/api/friendica&notification/<id>
set note <id> as seen and return item object if possible

new class NotificationsManager to query for notifications and set seen
state
This commit is contained in:
fabrixxm 2016-02-07 14:27:13 +01:00
parent d6cf791677
commit 44592611e1
4 changed files with 223 additions and 55 deletions

View 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','*');
@ -250,7 +251,7 @@
*/
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";
@ -680,6 +681,29 @@
}
/**
* @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="";
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
*/
@ -692,13 +716,17 @@
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;
@ -3386,6 +3414,43 @@
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);
/**
* returns notifications
* if called with note id set note seen and returns associated item (if possible)
*/
function api_friendica_notification(&$a, $type) {
if (api_user()===false) throw new ForbiddenException();
$nm = new NotificationsManager();
if ($a->argc==3) {
$notes = $nm->getAll(array(), "+seen -date", 50);
return api_apply_template("<auto>", $type, array('$notes' => $notes));
}
if ($a->argc==4) {
$note = $nm->getByID(intval($a->argv[3]));
if (is_null($note)) throw new BadRequestException("Invalid argument");
$nm->setSeen($note);
if ($note['otype']=='item') {
// would be really better with a 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) throw new NotFoundException();
$user_info = api_get_user($a);
$ret = api_format_items($r,$user_info);
$data = array('$statuses' => $ret);
return api_apply_template("timeline", $type, $data);
} else {
return api_apply_template('test', $type, array('ok' => $ok));
}
}
throw new BadRequestException("Invalid argument count");
}
api_register_func('api/friendica/notification', 'api_friendica_notification', true, API_METHOD_GET);
/*
To.Do:
[pagename] => api/1.1/statuses/lookup.json