use Friendica\Util\Strings;
use Friendica\Util\XML;
-require_once 'include/conversation.php';
require_once 'mod/share.php';
require_once 'mod/item.php';
require_once 'mod/wall_upload.php';
*/
function api_error($type, $e)
{
- $a = get_app();
+ $a = \get_app();
$error = ($e->getMessage() !== "" ? $e->getMessage() : $e->httpdesc);
/// @TODO: https://dev.twitter.com/overview/api/response-codes
$ret = $data;
break;
}
-
return $ret;
}
function api_account_verify_credentials($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_statuses_mediap($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
Logger::log('api_statuses_update: no user');
*/
function api_statuses_update($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
Logger::log('api_statuses_update: no user');
*/
function api_media_upload()
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
Logger::log('no user');
$returndata["media_id_string"] = (string)$media["id"];
$returndata["size"] = $media["size"];
$returndata["image"] = ["w" => $media["width"],
- "h" => $media["height"],
- "image_type" => $media["type"]];
+ "h" => $media["height"],
+ "image_type" => $media["type"],
+ "friendica_preview_url" => $media["preview"]];
Logger::log("Media uploaded: " . print_r($returndata, true), Logger::DEBUG);
*/
function api_status_show($type, $item_id = 0)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
*/
function api_users_show($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
*/
function api_users_search($type)
{
- $a = get_app();
+ $a = \get_app();
$userlist = [];
*/
function api_search($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
- if (api_user() === false || $user_info === false) {
- throw new ForbiddenException();
- }
-
- $data = [];
+ if (api_user() === false || $user_info === false) { throw new ForbiddenException(); }
- if (empty($_REQUEST['q'])) {
- throw new BadRequestException("q parameter is required.");
- }
+ if (empty($_REQUEST['q'])) { throw new BadRequestException("q parameter is required."); }
+
+ $searchTerm = trim(rawurldecode($_REQUEST['q']));
+ $data = [];
+ $data['status'] = [];
+ $count = 15;
+ $exclude_replies = !empty($_REQUEST['exclude_replies']);
if (!empty($_REQUEST['rpp'])) {
$count = $_REQUEST['rpp'];
} elseif (!empty($_REQUEST['count'])) {
$count = $_REQUEST['count'];
- } else {
- $count = 15;
}
-
+
$since_id = defaults($_REQUEST, 'since_id', 0);
$max_id = defaults($_REQUEST, 'max_id', 0);
$page = (!empty($_REQUEST['page']) ? $_REQUEST['page'] - 1 : 0);
-
$start = $page * $count;
+ $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
+ if (preg_match('/^#(\w+)$/', $searchTerm, $matches) === 1 && isset($matches[1])) {
+ $searchTerm = $matches[1];
+ $condition = ["`oid` > ?
+ AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
+ AND `otype` = ? AND `type` = ? AND `term` = ?",
+ $since_id, local_user(), TERM_OBJ_POST, TERM_HASHTAG, $searchTerm];
+ if ($max_id > 0) {
+ $condition[0] .= " AND `oid` <= ?";
+ $condition[] = $max_id;
+ }
+ $terms = DBA::select('term', ['oid'], $condition, []);
+ $itemIds = [];
+ while ( $term = DBA::fetch($terms) ) {
+ $itemIds[] = $term['oid'];
+ }
+ DBA::close($terms);
- $condition = ["`gravity` IN (?, ?) AND `item`.`id` > ?
- AND (`item`.`uid` = 0 OR (`item`.`uid` = ? AND NOT `item`.`global`))
- AND `item`.`body` LIKE CONCAT('%',?,'%')",
- GRAVITY_PARENT, GRAVITY_COMMENT, $since_id, api_user(), $_REQUEST['q']];
+ if (empty($itemIds)) {
+ return api_format_data("statuses", $type, $data);
+ }
- if ($max_id > 0) {
- $condition[0] .= " AND `item`.`id` <= ?";
- $condition[] = $max_id;
+ $tmpAr = ['`id` IN ('.implode(", ", $itemIds).')'];
+ if ($exclude_replies) {
+ $tmpAr[] = "`id` = `parent`";
+ }
+
+ $condition = [ implode(" AND ", $tmpAr) ];
+ } else {
+ $condition = ["`id` > ?
+ " . ( $exclude_replies ? " AND `id` = `parent` " : ' ' ) . "
+ AND (`uid` = 0 OR (`uid` = ? AND NOT `global`))
+ AND `body` LIKE CONCAT('%',?,'%')",
+ $since_id, api_user(), $_REQUEST['q']];
+ if ($max_id > 0) {
+ $condition[0] .= " AND `id` <= ?";
+ $condition[] = $max_id;
+ }
}
- $params = ['order' => ['id' => true], 'limit' => [$start, $count]];
$statuses = Item::selectForUser(api_user(), [], $condition, $params);
$data['status'] = api_format_items(Item::inArray($statuses), $user_info);
+ bindComments($data['status']);
+
return api_format_data("statuses", $type, $data);
}
*/
function api_statuses_home_timeline($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
Item::update(['unseen' => false], ['unseen' => true, 'id' => $idarray]);
}
}
+
+ bindComments($ret);
$data = ['status' => $ret];
switch ($type) {
return api_format_data("statuses", $type, $data);
}
+
/// @TODO move to top of file or somewhere better
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_public_timeline($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
$ret = api_format_items($r, $user_info, false, $type);
+ bindComments($ret);
+
$data = ['status' => $ret];
switch ($type) {
case "atom":
*/
function api_statuses_networkpublic_timeline($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
$ret = api_format_items(Item::inArray($statuses), $user_info, false, $type);
+ bindComments($ret);
+
$data = ['status' => $ret];
switch ($type) {
case "atom":
*/
function api_statuses_show($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
*/
function api_conversation_show($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
{
global $called_api;
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_statuses_destroy($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_statuses_mentions($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
*/
function api_statuses_user_timeline($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
$ret = api_format_items(Item::inArray($statuses), $user_info, true, $type);
+ bindComments($ret);
+
$data = ['status' => $ret];
switch ($type) {
case "atom":
*/
function api_favorites_create_destroy($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
{
global $called_api;
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
$ret = api_format_items(Item::inArray($statuses), $user_info, false, $type);
}
+ bindComments($ret);
+
$data = ['status' => $ret];
switch ($type) {
case "atom":
*/
function api_format_items_activities($item, $type = "json")
{
- $a = get_app();
+ $a = \get_app();
$activities = [
'like' => [],
*/
function api_format_items($r, $user_info, $filter_user = false, $type = "json")
{
- $a = get_app();
+ $a = \get_app();
$ret = [];
- foreach ($r as $item) {
+ foreach ((array)$r as $item) {
localize_item($item);
list($status_user, $owner_user) = api_item_get_user($a, $item);
*/
function api_lists_ownerships($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_lists_statuses($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
*/
function api_statuses_f($qtype)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_statusnet_config($type)
{
- $a = get_app();
+ $a = \get_app();
$name = Config::get('config', 'sitename');
$server = $a->getHostName();
throw new ForbiddenException();
}
- $a = get_app();
+ $a = \get_app();
api_get_user($a);
*/
function api_direct_messages_new($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_direct_messages_destroy($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_direct_messages_box($type, $box, $verbose)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
}
}
// now let's delete all photos from the album
- $result = DBA::delete('photo', ['uid' => api_user(), 'album' => $album]);
+ $result = Photo::delete(['uid' => api_user(), 'album' => $album]);
// return success of deletion or error message
if ($result) {
throw new BadRequestException("no new albumname specified");
}
// check if album is existing
- if (!DBA::exists('photo', ['uid' => api_user(), 'album' => $album])) {
+ if (!Photo::exists(['uid' => api_user(), 'album' => $album])) {
throw new BadRequestException("album not available");
}
// now let's update all photos to the albumname
- $result = DBA::update('photo', ['album' => $album_new], ['uid' => api_user(), 'album' => $album]);
+ $result = Photo::update(['album' => $album_new], ['uid' => api_user(), 'album' => $album]);
// return success of updating or error message
if ($result) {
} else {
$mode = "update";
- // check if photo is existing in database
- $r = q(
- "SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s' AND `album` = '%s'",
- intval(api_user()),
- DBA::escape($photo_id),
- DBA::escape($album)
- );
- if (!DBA::isResult($r)) {
+ // check if photo is existing in databasei
+ if (!Photo::exists(['resource-id' => $photo_id, 'uid' => api_user(), 'album' => $album])) {
throw new BadRequestException("photo not available");
}
}
// now let's do the changes in update-mode
if ($mode == "update") {
- $sql_extra = "";
+ $updated_fields = [];
if (!is_null($desc)) {
- $sql_extra .= (($sql_extra != "") ? " ," : "") . "`desc` = '$desc'";
+ $updated_fields['desc'] = $desc;
}
if (!is_null($album_new)) {
- $sql_extra .= (($sql_extra != "") ? " ," : "") . "`album` = '$album_new'";
+ $updated_fields['album'] = $album_new;
}
if (!is_null($allow_cid)) {
$allow_cid = trim($allow_cid);
- $sql_extra .= (($sql_extra != "") ? " ," : "") . "`allow_cid` = '$allow_cid'";
+ $updated_fields['allow_cid'] = $allow_cid;
}
if (!is_null($deny_cid)) {
$deny_cid = trim($deny_cid);
- $sql_extra .= (($sql_extra != "") ? " ," : "") . "`deny_cid` = '$deny_cid'";
+ $updated_fields['deny_cid'] = $deny_cid;
}
if (!is_null($allow_gid)) {
$allow_gid = trim($allow_gid);
- $sql_extra .= (($sql_extra != "") ? " ," : "") . "`allow_gid` = '$allow_gid'";
+ $updated_fields['allow_gid'] = $allow_gid;
}
if (!is_null($deny_gid)) {
$deny_gid = trim($deny_gid);
- $sql_extra .= (($sql_extra != "") ? " ," : "") . "`deny_gid` = '$deny_gid'";
+ $updated_fields['deny_gid'] = $deny_gid;
}
$result = false;
- if ($sql_extra != "") {
+ if (count($updated_fields) > 0) {
$nothingtodo = false;
- $result = q(
- "UPDATE `photo` SET %s, `edited`='%s' WHERE `uid` = %d AND `resource-id` = '%s' AND `album` = '%s'",
- $sql_extra,
- DateTimeFormat::utcNow(), // update edited timestamp
- intval(api_user()),
- DBA::escape($photo_id),
- DBA::escape($album)
- );
+ $result = Photo::update($updated_fields, ['uid' => api_user(), 'resource-id' => $photo_id, 'album' => $album]);
} else {
$nothingtodo = true;
}
throw new BadRequestException("no photo_id specified");
}
// check if photo is existing in database
- $r = q(
- "SELECT `id` FROM `photo` WHERE `uid` = %d AND `resource-id` = '%s'",
- intval(api_user()),
- DBA::escape($photo_id)
- );
- if (!DBA::isResult($r)) {
+ $r = Photo::exists(['resource-id' => $photo_id, 'uid' => api_user()]);
+ if (!$r) {
throw new BadRequestException("photo not available");
}
// now we can perform on the deletion of the photo
- $result = DBA::delete('photo', ['uid' => api_user(), 'resource-id' => $photo_id]);
+ $result = Photo::delete(['uid' => api_user(), 'resource-id' => $photo_id]);
// return success of deletion or error message
if ($result) {
// change specified profile or all profiles to the new resource-id
if ($is_default_profile) {
$condition = ["`profile` AND `resource-id` != ? AND `uid` = ?", $data['photo']['id'], api_user()];
- DBA::update('photo', ['profile' => false], $condition);
+ Photo::update(['profile' => false], $condition);
} else {
$fields = ['photo' => System::baseUrl() . '/photo/' . $data['photo']['id'] . '-4.' . $filetype,
'thumb' => System::baseUrl() . '/photo/' . $data['photo']['id'] . '-5.' . $filetype];
Contact::updateSelfFromUserID(api_user(), true);
// Update global directory in background
- $url = System::baseUrl() . '/profile/' . get_app()->user['nickname'];
+ $url = System::baseUrl() . '/profile/' . \get_app()->user['nickname'];
if ($url && strlen(Config::get('system', 'directory'))) {
Worker::add(PRIORITY_LOW, "Directory", $url);
}
*/
function prepare_photo_data($type, $scale, $photo_id)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if ($user_info === false) {
*/
function api_friendica_group_show($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_friendica_group_delete($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_lists_destroy($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_friendica_group_create($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_lists_create($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_friendica_group_update($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_lists_update($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_friendica_activity($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_friendica_notification($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
$notes = $xmlnotes;
}
-
return api_format_data("notes", $type, ['note' => $notes]);
}
*/
function api_friendica_notification_seen($type)
{
- $a = get_app();
+ $a = \get_app();
$user_info = api_get_user($a);
if (api_user() === false || $user_info === false) {
*/
function api_friendica_direct_messages_setseen($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
}
*/
function api_friendica_direct_messages_search($type, $box = "")
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
*/
function api_friendica_profile_show($type)
{
- $a = get_app();
+ $a = \get_app();
if (api_user() === false) {
throw new ForbiddenException();
/// @TODO move to top of file or somewhere better
api_register_func('api/saved_searches/list', 'api_saved_searches_list', true);
+/*
+ * Bind comment numbers(friendica_comments: Int) on each statuses page of *_timeline / favorites / search
+ *
+ * @brief Number of comments
+ *
+ * @param object $data [Status, Status]
+ *
+ * @return void
+ */
+function bindComments(&$data)
+{
+ if (count($data) == 0) {
+ return;
+ }
+
+ $ids = [];
+ $comments = [];
+ foreach ($data as $item) {
+ $ids[] = $item['id'];
+ }
+
+ $idStr = DBA::escape(implode(", ", $ids));
+ $sql = "SELECT `parent`, COUNT(*) as comments FROM `item` WHERE `parent` IN ($idStr) AND `deleted` = ? AND `gravity`= ? GROUP BY `parent`";
+ $items = DBA::p($sql, 0, GRAVITY_COMMENT);
+ $itemsData = DBA::toArray($items);
+
+ foreach ($itemsData as $item) {
+ $comments[$item['parent']] = $item['comments'];
+ }
+
+ foreach ($data as $idx => $item) {
+ $id = $item['id'];
+ $data[$idx]['friendica_comments'] = isset($comments[$id]) ? $comments[$id] : 0;
+ }
+}
+
/*
@TODO Maybe open to implement?
To.Do: