From 05aa1f102f50ac1f294f727b4841b7e0f6eb7ee2 Mon Sep 17 00:00:00 2001 From: ghost Date: Fri, 14 Jan 2022 11:58:26 +0200 Subject: [PATCH] add comments, update getPosts response processing --- src/application/controller/api/post/get.php | 30 ++++++++++++++------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/application/controller/api/post/get.php b/src/application/controller/api/post/get.php index 956dfe9..36a551b 100644 --- a/src/application/controller/api/post/get.php +++ b/src/application/controller/api/post/get.php @@ -1,5 +1,6 @@ false, 'message' => _('Internal server error'), @@ -7,16 +8,20 @@ $response = [ 'page' => 0, ]; +// Authorization required if (isset($_SESSION['userName'])) { + // Define page number $page = isset($_GET['page']) ? Filter::int($_GET['page']) : 1; $userNames = []; + // Single user posts mode if (isset($_GET['userName']) && !empty($_GET['userName'])) { $userNames[] = Filter::userName($_GET['userName']); + // Following feed by default (when userName attribute not provided) } else { foreach ((array) $_twister->getFollowing($_SESSION['userName']) as $followingUserName) { @@ -25,6 +30,7 @@ if (isset($_SESSION['userName'])) { } } + // Get posts from the node (pre-collected from DHT) if ($result = $_twister->getPosts($userNames, APPLICATION_MAX_POST_FEED * $page)) { $postsTotal = 0; @@ -34,23 +40,26 @@ if (isset($_SESSION['userName'])) { // Count posts $postsTotal++; - // Process reTwists + // Format reTwists $reTwist = []; - if ($post['reTwist']) { + if (isset($post['userpost']['rt'])) { $reTwist = [ - 'message' => Format::post($post['reTwist']['message']), - 'time' => Format::time($post['reTwist']['time']), - 'userName' => $post['reTwist']['userName'], + 'userName' => isset($post['userpost']['rt']['n']) ? $post['userpost']['rt']['n'] : false, + 'message' => Format::post((isset($post['userpost']['rt']['msg']) ? $post['userpost']['rt']['msg'] : false) . (isset($post['userpost']['rt']['msg2']) ? $post['userpost']['rt']['msg2'] : false)), + 'time' => Format::time((isset($post['userpost']['rt']['time']) ? $post['userpost']['rt']['time'] : false)), ]; } - // Process posts + // Format posts $posts[] = [ - 'message' => Format::post($post['message']), - 'time' => Format::time($post['time']), - 'userName' => $post['userName'], + 'userName' => isset($post['userpost']['n']) ? $post['userpost']['n'] : false, + 'message' => Format::post((isset($post['userpost']['msg']) ? $post['userpost']['msg'] : false) . (isset($post['userpost']['msg2']) ? $post['userpost']['msg2'] : false)), + 'time' => Format::time((isset($post['userpost']['time']) ? $post['userpost']['time'] : false)), + 'reTwist' => $reTwist, + + 'meta' => base64_encode(json_encode($post)), ]; } @@ -58,6 +67,9 @@ if (isset($_SESSION['userName'])) { 'success' => true, 'message' => _('Posts successfully loaded'), 'posts' => $posts, + + // Increase page index for ajax pagination + // (would be moved to JS, but next page button better to hide on empty results @TODO) 'page' => $postsTotal == $page * APPLICATION_MAX_POST_FEED ? $page + 1 : 0 ];