diff --git a/src/application/controller/api/follow/delete.php b/src/application/controller/api/follow/delete.php index e69de29..963e1da 100644 --- a/src/application/controller/api/follow/delete.php +++ b/src/application/controller/api/follow/delete.php @@ -0,0 +1,29 @@ + false, + 'message' => _('Internal server error'), +]; + +if (isset($_SESSION['userName'])) { + + $userName = isset($_POST['userName']) ? $_POST['userName'] : false; + + $result = $_twister->unFollow($_SESSION['userName'], [$_POST['userName']]); + + $response = [ + 'success' => true, + 'message' => _('Unfollowed successfully'), + ]; + +} else { + + $response = [ + 'success' => false, + 'message' => _('Session expired. Please, reload the page.'), + ]; + +} + +header('Content-Type: application/json; charset=utf-8'); +echo json_encode($response); \ No newline at end of file diff --git a/src/bootstrap.php b/src/bootstrap.php index 73922c6..b803c51 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -79,6 +79,9 @@ if (isset($_GET['_route_'])) { case 'api/follow/get': require(PROJECT_DIR . '/application/controller/api/follow/get.php'); break; + case 'api/follow/delete': + require(PROJECT_DIR . '/application/controller/api/follow/delete.php'); + break; default: require(PROJECT_DIR . '/application/controller/error/404.php'); } diff --git a/src/public/js/module/following.js b/src/public/js/module/following.js index 2669422..26988c7 100644 --- a/src/public/js/module/following.js +++ b/src/public/js/module/following.js @@ -40,7 +40,7 @@ var ModuleFollowing = { $('', { 'class': 'bi bi-x-circle', 'title': 'Unfollow', - 'onclick': '', + 'onclick': 'ModuleFollowing.unFollow(\'' + list + '\', \'' + userName + '\', true)', }) ) ) @@ -76,4 +76,29 @@ var ModuleFollowing = { } }); }, + unFollow: function(list, userName, reFresh) { + $.ajax({ + url: 'api/follow/delete', + type: 'POST', + data: { + userName: userName + }, + success: function (response) { + if (response.success) { + + if (reFresh) { + ModuleFollowing.load(list, reFresh); + } + + } else { + + alert(response.message); + + } + }, + error: function(jqXHR, textStatus, errorThrown) { + console.log(textStatus, errorThrown); + } + }); + }, } \ No newline at end of file diff --git a/src/system/twister.php b/src/system/twister.php index 6a93a34..7f5c91b 100644 --- a/src/system/twister.php +++ b/src/system/twister.php @@ -180,6 +180,38 @@ class Twister { return false; } + public function unFollow(string $userName, array $userNames) { + + $this->_curl->prepare( + '/', + 'POST', + 30, + [ + 'jsonrpc' => '2.0', + 'method' => 'unfollow', + 'params' => [ + $userName, + $userNames + ], + 'id' => time() + rand() + ] + ); + + if ($response = $this->_curl->execute()) { + + if ($response['error']) { + + $this->_error = _($response['error']['message']); + + } else { + + return $response['result']; + } + } + + return false; + } + public function getFollowing(string $userName) { $this->_curl->prepare(