From d544554b223ff500e3a88199b7d52570222e822e Mon Sep 17 00:00:00 2001 From: Parham R Date: Fri, 24 Mar 2023 15:59:41 +0000 Subject: [PATCH 1/8] add (controller) for contacts actions, add route for requesting to delete contact, add delete button on template --- appinfo/.env | 5 + appinfo/routes.php | 5 +- js/contacts.js | 240 +++++++++++++++----------- lib/Controller/ContactsController.php | 11 ++ 4 files changed, 158 insertions(+), 103 deletions(-) create mode 100644 appinfo/.env create mode 100644 lib/Controller/ContactsController.php diff --git a/appinfo/.env b/appinfo/.env new file mode 100644 index 00000000..86e60b3d --- /dev/null +++ b/appinfo/.env @@ -0,0 +1,5 @@ + +VERSION="1" + +NEXTCLOUD="1" +OWNCLOUD="0" diff --git a/appinfo/routes.php b/appinfo/routes.php index da8ccd15..e0fc429b 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -79,7 +79,10 @@ ['name' => 'app#invitationsGenerate', 'url' => '/invitations/generate', 'verb' => 'GET'], ['name' => 'app#contactsAccept', 'url' => '/contacts/accept', 'verb' => 'POST'], ['name' => 'app#contactsFindUsers', 'url' => '/contacts/users', 'verb' => 'GET'], - + + // contacts routes + ['name' => 'contacts#deleteContact', 'url' => '/deleteContact/~{userId}', 'verb' => 'GET'], + // page routes ['name' => 'page#get_internal_metrics', 'url' => '/internal_metrics', 'verb' => 'GET'], ['name' => 'page#get_metrics', 'url' => '/metrics', 'verb' => 'GET'], diff --git a/js/contacts.js b/js/contacts.js index f2f7fb73..65d26e3d 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -1,114 +1,150 @@ -//Everything will be for working with contacts -var baseUrl = OC.generateUrl('/apps/sciencemesh'); -$('#test_error').hide(); -$.ajax({ - url: baseUrl + '/contacts/users', - type: 'GET', - contentType: 'application/json', -}).done(function (response) { - if(response == '' || response === false) { - var element = document.getElementById("show_result"); - element.innerHTML= ` - - - No Sciencemesh Connection - - `; - $('#show_result').show(); - } else { - let token = JSON.parse(response); - - for(tokenData in token) { - if(token.hasOwnProperty(tokenData)) { - console.log(tokenData); - if(tokenData === 'accepted_users') { - let accepted_users = token.accepted_users - var result = ''; - for(accept in accepted_users) { - const displayName = accepted_users[accept].display_name; - const username = accepted_users[accept].id.opaque_id; - const idp = accepted_users[accept].id.idp; - const provider = new URL(idp).host; - result += ` - - -

- - -

${displayName}

- - -

${username}

- - - `; - } - - var element = document.getElementById("show_result"); - element.innerHTML = result; - - $('#show_result').show(); - }else{ - const result = ` - - -

There are no contacts!

- - `; - var element = document.getElementById("show_result"); - element.innerHTML = result; - $('#show_result').show(); - - } - } - } -} -}).fail(function (response, code) { - console.log(response) - //alert('The token is invalid') -}); -document.getElementById('token-generator').onclick = function () { +document.addEventListener("DOMContentLoaded", function(event) { + //Everything will be for working with contacts var baseUrl = OC.generateUrl('/apps/sciencemesh'); + $('#test_error').hide(); $.ajax({ - url: baseUrl + '/invitations/generate', + url: baseUrl + '/contacts/users', type: 'GET', contentType: 'application/json', - //data: JSON.stringify(note) }).done(function (response) { - if (response === '' || response === false) { - var element = document.getElementById("test_1"); - element.innerHTML = 'No Sciencemesh Connection'; + if(response == '' || response === false) { + var element = document.getElementById("show_result"); + element.innerHTML= ` + + + No Sciencemesh Connection + + `; + $('#show_result').show(); } else { - var element = document.getElementById("invitation-details"); - element.innerHTML = `

New Token Generated!

`; - $('#test').show(); - var button = document.querySelector("#share-token-btn"); - button.addEventListener("click", function() { - copyToClipboard(); - }); + let token = JSON.parse(response); + + for(tokenData in token) { + if(token.hasOwnProperty(tokenData)) { + console.log(tokenData); + if(tokenData === 'accepted_users') { + let accepted_users = token.accepted_users + var result = ''; + for(accept in accepted_users) { + console.log(accepted_users); + const displayName = accepted_users[accept].display_name; + const username = accepted_users[accept].id.opaque_id; + const idp = accepted_users[accept].id.idp; + const provider = new URL(idp).host; + result += ` + + +

+ + +

${displayName}

+ + +

${username}

+ + + + `; + } + + var element = document.getElementById("show_result"); + element.innerHTML = result; + + var button = document.querySelector(".deleteContact"); + button.addEventListener("click", function() { + deleteContact(); + }); + + $('#show_result').show(); + }else{ + const result = ` + + +

There are no contacts!

+ + `; + var element = document.getElementById("show_result"); + element.innerHTML = result; + $('#show_result').show(); + + } + } } + + } }).fail(function (response, code) { - alert('The token is invalid') + console.log(response) + //alert('The token is invalid') }); -} -function copyToClipboard() { - var input = document.querySelector("input[name='meshtoken']"); - input.select(); - document.execCommand("copy"); -} - + document.getElementById('token-generator').onclick = function () { + var baseUrl = OC.generateUrl('/apps/sciencemesh'); + $.ajax({ + url: baseUrl + '/invitations/generate', + type: 'GET', + contentType: 'application/json', + //data: JSON.stringify(note) + }).done(function (response) { + if (response === '' || response === false) { + var element = document.getElementById("test_1"); + element.innerHTML = 'No Sciencemesh Connection'; + } else { + var element = document.getElementById("invitation-details"); + element.innerHTML = `

New Token Generated!

`; + $('#test').show(); + var button = document.querySelector("#share-token-btn"); + button.addEventListener("click", function() { + copyToClipboard(); + }); + } + }).fail(function (response, code) { + alert('The token is invalid') + }); + } -function secondsToDhms(seconds) { - seconds = Number(seconds); - var d = Math.floor(seconds / (3600 * 24)); - var h = Math.floor(seconds % (3600 * 24) / 3600); - var m = Math.floor(seconds % 3600 / 60); - var s = Math.floor(seconds % 60); + function copyToClipboard() { + var input = document.querySelector("input[name='meshtoken']"); + input.select(); + document.execCommand("copy"); + } + + + function secondsToDhms(seconds) { + seconds = Number(seconds); + var d = Math.floor(seconds / (3600 * 24)); + var h = Math.floor(seconds % (3600 * 24) / 3600); + var m = Math.floor(seconds % 3600 / 60); + var s = Math.floor(seconds % 60); + + var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : ""; + var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : ""; + var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : ""; + var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : ""; + return dDisplay + hDisplay + mDisplay + sDisplay; + } - var dDisplay = d > 0 ? d + (d == 1 ? " day, " : " days, ") : ""; - var hDisplay = h > 0 ? h + (h == 1 ? " hour, " : " hours, ") : ""; - var mDisplay = m > 0 ? m + (m == 1 ? " minute, " : " minutes, ") : ""; - var sDisplay = s > 0 ? s + (s == 1 ? " second" : " seconds") : ""; - return dDisplay + hDisplay + mDisplay + sDisplay; -} \ No newline at end of file + function deleteContact(){ + alert('test'); + var baseUrl = OC.generateUrl('/apps/sciencemesh'); + $.ajax({ + url: baseUrl + '/deleteContact/', + type: 'GET', + contentType: 'application/json' + }).done(function (response) { + if (response === '' || response === false) { + var element = document.getElementById("test_1"); + element.innerHTML = 'No Sciencemesh Connection'; + } else { + var element = document.getElementById("invitation-details"); + element.innerHTML = `

New Token Generated!

`; + $('#test').show(); + var button = document.querySelector("#share-token-btn"); + button.addEventListener("click", function() { + copyToClipboard(); + }); + } + }).fail(function (response, code) { + alert('The token is invalid') + }); + } +}) \ No newline at end of file diff --git a/lib/Controller/ContactsController.php b/lib/Controller/ContactsController.php new file mode 100644 index 00000000..7f873f4a --- /dev/null +++ b/lib/Controller/ContactsController.php @@ -0,0 +1,11 @@ + Date: Fri, 7 Apr 2023 12:31:15 +0000 Subject: [PATCH 2/8] add routes for delete contact, adding styles, add template ui, js function for deleting contact request, adding a contact class+methods --- appinfo/routes.php | 2 +- css/style.css | 5 +++++ js/contacts.js | 30 ++++++++++++--------------- lib/Controller/ContactsController.php | 13 ++++++++++-- templates/contacts.php | 1 + 5 files changed, 31 insertions(+), 20 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index e0fc429b..25253c38 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -81,7 +81,7 @@ ['name' => 'app#contactsFindUsers', 'url' => '/contacts/users', 'verb' => 'GET'], // contacts routes - ['name' => 'contacts#deleteContact', 'url' => '/deleteContact/~{userId}', 'verb' => 'GET'], + ['name' => 'contacts#deleteContact', 'url' => '/contact/deleteContact', 'verb' => 'POST'], // page routes ['name' => 'page#get_internal_metrics', 'url' => '/internal_metrics', 'verb' => 'GET'], diff --git a/css/style.css b/css/style.css index b6d69abe..4755b1ab 100644 --- a/css/style.css +++ b/css/style.css @@ -269,6 +269,11 @@ width: 45% !important; border-bottom: 1px solid var(--color-placeholder-light); } +.contacts-table tbody tr td:nth-child(4){ + width: 5% !important; + border-bottom: 1px solid var(--color-placeholder-light); + text-align: center; +} .contacts-profile-img{ height: 35px; margin: 2.5px 0px; diff --git a/js/contacts.js b/js/contacts.js index 65d26e3d..73b81b2f 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -41,7 +41,9 @@ document.addEventListener("DOMContentLoaded", function(event) {

${username}

- + + + `; @@ -52,7 +54,7 @@ document.addEventListener("DOMContentLoaded", function(event) { var button = document.querySelector(".deleteContact"); button.addEventListener("click", function() { - deleteContact(); + deleteContact($(this).data('idp'),$(this).data('username')); }); $('#show_result').show(); @@ -123,25 +125,19 @@ document.addEventListener("DOMContentLoaded", function(event) { return dDisplay + hDisplay + mDisplay + sDisplay; } - function deleteContact(){ - alert('test'); + function deleteContact(idp,username){ var baseUrl = OC.generateUrl('/apps/sciencemesh'); + var data = 'idp=' + encodeURIComponent(idp) + '&username=' + encodeURIComponent(username); $.ajax({ - url: baseUrl + '/deleteContact/', - type: 'GET', - contentType: 'application/json' + url: baseUrl + '/contact/deleteContact', + type: 'POST', + contentType: 'application/x-www-form-urlencoded', + data:data }).done(function (response) { if (response === '' || response === false) { - var element = document.getElementById("test_1"); - element.innerHTML = 'No Sciencemesh Connection'; - } else { - var element = document.getElementById("invitation-details"); - element.innerHTML = `

New Token Generated!

`; - $('#test').show(); - var button = document.querySelector("#share-token-btn"); - button.addEventListener("click", function() { - copyToClipboard(); - }); + console.log('failed'); + }else{ + console.log(response); } }).fail(function (response, code) { alert('The token is invalid') diff --git a/lib/Controller/ContactsController.php b/lib/Controller/ContactsController.php index 7f873f4a..d7576633 100644 --- a/lib/Controller/ContactsController.php +++ b/lib/Controller/ContactsController.php @@ -2,10 +2,19 @@ namespace OCA\ScienceMesh\Controller; +use OCP\IRequest; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\TextPlainResponse; +use OCP\AppFramework\Controller; +use OCA\ScienceMesh\RevaHttpClient; +use OCA\ScienceMesh\Plugins\ScienceMeshGenerateTokenPlugin; +use OCA\ScienceMesh\Plugins\ScienceMeshAcceptTokenPlugin; +use OCA\ScienceMesh\Controller\RevaController; class ContactsController extends Controller { - + public function deleteContact() { - return new TextPlainResponse('test', Http::STATUS_OK); + error_log('contact '.$_POST['username'].' is deleted'); + return new TextPlainResponse(true, Http::STATUS_OK); } } \ No newline at end of file diff --git a/templates/contacts.php b/templates/contacts.php index 97f8ea42..e587a2c8 100644 --- a/templates/contacts.php +++ b/templates/contacts.php @@ -18,6 +18,7 @@ Name Open Cloud Mesh Address + From f6091ff213723ee6c0c9e78582609721c59ffe75 Mon Sep 17 00:00:00 2001 From: Parham R Date: Thu, 18 May 2023 12:07:17 +0000 Subject: [PATCH 3/8] add js for render search contacts, changes on php method for render the contacts --- js/contacts.js | 71 +++++++++++--------------------- lib/Controller/AppController.php | 17 +++++--- 2 files changed, 35 insertions(+), 53 deletions(-) diff --git a/js/contacts.js b/js/contacts.js index 92e52eca..0d73474c 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -87,9 +87,7 @@ document.addEventListener("DOMContentLoaded", function(event) { const searchInput = document.getElementById('contact-search-input'); const inputHandler = function(e) { const value = e.target.value; - if(value.length > 0){ loadData(value); - } } function debounce(callback, wait) { @@ -141,52 +139,31 @@ document.addEventListener("DOMContentLoaded", function(event) { `; $('#show_result').show(); } else { - let token = JSON.parse(response); - - for(tokenData in token) { - if(token.hasOwnProperty(tokenData)) { - console.log(tokenData); - if(tokenData === 'accepted_users') { - let accepted_users = token.accepted_users - var result = ''; - for(accept in accepted_users) { - const displayName = accepted_users[accept].display_name; - const username = accepted_users[accept].id.opaque_id; - const idp = accepted_users[accept].id.idp; - const provider = new URL(idp).host; - result += ` - - -

- - -

${displayName}

- - -

${username}

- - - `; - } - - var element = document.getElementById("show_result"); - element.innerHTML = result; + let acceptedUsers = JSON.parse(response); + let result = ''; + for(i in acceptedUsers) { + var displayName = acceptedUsers[i].display_name; + var username = acceptedUsers[i].id.opaque_id; + var idp = acceptedUsers[i].id.idp; + var provider = (idp.startsWith("http") ? new URL(idp).host : idp); + result += ` + + +

+ + +

${displayName}

+ + +

${username}@${provider}

+ + + `; + } + var element = document.getElementById("show_result"); + element.innerHTML = result; - $('#show_result').show(); - }else{ - const result = ` - - -

There are no contacts!

- - `; - var element = document.getElementById("show_result"); - element.innerHTML = result; - $('#show_result').show(); - - } - } - } + $('#show_result').show(); } }).fail(function (response, code) { console.log(response) diff --git a/lib/Controller/AppController.php b/lib/Controller/AppController.php index f1185174..ae3dc6f7 100644 --- a/lib/Controller/AppController.php +++ b/lib/Controller/AppController.php @@ -154,20 +154,25 @@ public function contactsAccept() { * @NoAdminRequired * @NoCSRFRequired */ + public function contactsFindUsers($searchToken = "") { $find_users_json = $this->httpClient->findAcceptedUsers($this->userId); $find_users = json_decode($find_users_json, false); - + $return_users = array(); if(strlen($searchToken) > 0){ - for($i = count($find_users->accepted_users); $i >= 0 ; $i--){ - if(!str_contains($find_users->accepted_users[$i]->display_name, $searchToken)){ - array_splice($find_users->accepted_users, $i, 1); + if(!empty($find_users)){ + for($i = count($find_users); $i >= 0 ; $i--){ + if(str_contains($find_users[$i]->display_name, $searchToken) and !is_null($find_users[$i])){ + $return_users[] = $find_users[$i]; + } } } + }else{ + $return_users = json_decode($find_users_json, false); } - - return new TextPlainResponse(json_encode($find_users), Http::STATUS_OK); + + return new TextPlainResponse(json_encode($return_users), Http::STATUS_OK); } From be441e27d044a71b74158b5d4fc63a1bcfa0833a Mon Sep 17 00:00:00 2001 From: Parham R Date: Thu, 18 May 2023 16:47:56 +0300 Subject: [PATCH 4/8] Update .env --- appinfo/.env | 4 ---- 1 file changed, 4 deletions(-) diff --git a/appinfo/.env b/appinfo/.env index 86e60b3d..8b137891 100644 --- a/appinfo/.env +++ b/appinfo/.env @@ -1,5 +1 @@ -VERSION="1" - -NEXTCLOUD="1" -OWNCLOUD="0" From 27c489c31f3ed38c218fa1e5a3833839c6f81c2e Mon Sep 17 00:00:00 2001 From: Parham R Date: Thu, 18 May 2023 14:46:55 +0000 Subject: [PATCH 5/8] modify js for multiple elements --- js/contacts.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/js/contacts.js b/js/contacts.js index 0d73474c..64f5a047 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -74,9 +74,13 @@ document.addEventListener("DOMContentLoaded", function(event) { var element = document.getElementById("invitation-details"); element.innerHTML = `

New Token Generated!

`; $('#test').show(); - var button = document.querySelector("#share-token-btn"); - button.addEventListener("click", function() { - copyToClipboard(); + + + var button = $(".deleteContact"); + button.each(function( index , ele) { + ele.addEventListener("click", function() { + deleteContact($(this).data('idp'),$(this).data('username')); + }); }); } }).fail(function (response, code) { From e5275b29db408a2b81ff6a5a7f936e2c467bedb5 Mon Sep 17 00:00:00 2001 From: Parham R Date: Thu, 18 May 2023 17:49:54 +0300 Subject: [PATCH 6/8] Update contacts.js fix multiple elements --- js/contacts.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/js/contacts.js b/js/contacts.js index 7ce684e6..890b51e8 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -117,11 +117,13 @@ document.addEventListener("DOMContentLoaded", function(event) { var element = document.getElementById("show_result"); element.innerHTML = result; - - var button = document.querySelector(".deleteContact"); - button.addEventListener("click", function() { - deleteContact($(this).data('idp'),$(this).data('username')); - }); + + var button = $(".deleteContact"); + button.each(function( index , ele) { + ele.addEventListener("click", function() { + deleteContact($(this).data('idp'),$(this).data('username')); + }); + }); $('#show_result').show(); }else{ From 9751d8e2b5f6e70df52c77bb5d7962c57a749a4f Mon Sep 17 00:00:00 2001 From: Parham R Date: Thu, 25 May 2023 11:54:00 +0330 Subject: [PATCH 7/8] Update contacts.js --- js/contacts.js | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/js/contacts.js b/js/contacts.js index 64f5a047..b895d8a8 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -74,13 +74,9 @@ document.addEventListener("DOMContentLoaded", function(event) { var element = document.getElementById("invitation-details"); element.innerHTML = `

New Token Generated!

`; $('#test').show(); - - - var button = $(".deleteContact"); - button.each(function( index , ele) { - ele.addEventListener("click", function() { - deleteContact($(this).data('idp'),$(this).data('username')); - }); + var button = document.querySelector("#share-token-btn"); + button.addEventListener("click", function() { + copyToClipboard(); }); } }).fail(function (response, code) { @@ -174,4 +170,4 @@ document.addEventListener("DOMContentLoaded", function(event) { //alert('The token is invalid') }); } -}); \ No newline at end of file +}); From f84a77d3f9b39f56636dbfb9a9c17adeff553f52 Mon Sep 17 00:00:00 2001 From: Parham R Date: Sat, 27 May 2023 09:27:15 +0000 Subject: [PATCH 8/8] fix unfriend event button --- js/contacts.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/js/contacts.js b/js/contacts.js index c2c72b84..945a613f 100644 --- a/js/contacts.js +++ b/js/contacts.js @@ -46,12 +46,22 @@ document.addEventListener("DOMContentLoaded", function(event) {

${username}@${provider}

+ + + `; } var element = document.getElementById("show_result"); element.innerHTML = result; + var button = $(".deleteContact"); + button.each(function( index , ele) { + ele.addEventListener("click", function() { + deleteContact($(this).data('idp'),$(this).data('username')); + }); + }); + $('#show_result').show(); } } @@ -163,6 +173,9 @@ document.addEventListener("DOMContentLoaded", function(event) {

${username}@${provider}

+ + + `; }