make the backend more user friendly

This commit is contained in:
André Menrath 2021-08-04 12:28:43 +02:00
parent 5a4315befa
commit 845b0b6872
10 changed files with 335 additions and 59 deletions

File diff suppressed because one or more lines are too long

View File

@ -1 +1 @@
.wp-block-cgb-block-mobilizon{background:#bada55;border:0.2rem solid #292929;color:#292929;margin:0 auto;max-width:740px;padding:2rem}
.wp-block-cgb-block-mobilizon{margin:0 auto;max-width:600px;padding:1rem}.wp-block-cgb-block-mobilizon img.wp-block-cgb-mobilizon-icon{height:5rem;width:5rem;margin-right:0.7rem}

111
package-lock.json generated
View File

@ -8,7 +8,8 @@
"name": "mobilizon-cgb-guten-block",
"version": "1.0.0",
"dependencies": {
"cgb-scripts": "1.23.1"
"cgb-scripts": "1.23.1",
"graphql-request": "^3.5.0"
}
},
"node_modules/@babel/code-frame": {
@ -2364,6 +2365,14 @@
"node": ">=4.8"
}
},
"node_modules/cross-fetch": {
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.4.tgz",
"integrity": "sha512-1eAtFWdIubi6T4XPy6ei9iUFoKpUkIF971QLN8lIvvvwueI65+Nw5haMNKUwfJxabqlIIDODJKGrQ66gxC0PbQ==",
"dependencies": {
"node-fetch": "2.6.1"
}
},
"node_modules/cross-spawn": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz",
@ -3330,6 +3339,17 @@
"node": ">=0.10.0"
}
},
"node_modules/extract-files": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/extract-files/-/extract-files-9.0.0.tgz",
"integrity": "sha512-CvdFfHkC95B4bBBk36hcEmvdR2awOdhhVUYH6S/zrVj3477zven/fJMYg7121h4T1xHZC+tetUpubpAhxwI7hQ==",
"engines": {
"node": "^10.17.0 || ^12.0.0 || >= 13.7.0"
},
"funding": {
"url": "https://github.com/sponsors/jaydenseric"
}
},
"node_modules/extract-text-webpack-plugin": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extract-text-webpack-plugin/-/extract-text-webpack-plugin-3.0.2.tgz",
@ -3774,6 +3794,41 @@
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz",
"integrity": "sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ=="
},
"node_modules/graphql": {
"version": "15.5.1",
"resolved": "https://registry.npmjs.org/graphql/-/graphql-15.5.1.tgz",
"integrity": "sha512-FeTRX67T3LoE3LWAxxOlW2K3Bz+rMYAC18rRguK4wgXaTZMiJwSUwDmPFo3UadAKbzirKIg5Qy+sNJXbpPRnQw==",
"peer": true,
"engines": {
"node": ">= 10.x"
}
},
"node_modules/graphql-request": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/graphql-request/-/graphql-request-3.5.0.tgz",
"integrity": "sha512-Io89QpfU4rqiMbqM/KwMBzKaDLOppi8FU8sEccCE4JqCgz95W9Q8bvxQ4NfPALLSMvg9nafgg8AkYRmgKSlukA==",
"dependencies": {
"cross-fetch": "^3.0.6",
"extract-files": "^9.0.0",
"form-data": "^3.0.0"
},
"peerDependencies": {
"graphql": "14.x || 15.x"
}
},
"node_modules/graphql-request/node_modules/form-data": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/gzip-size": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/gzip-size/-/gzip-size-4.1.0.tgz",
@ -5331,6 +5386,14 @@
"resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz",
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ=="
},
"node_modules/node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==",
"engines": {
"node": "4.x || >=6.0.0"
}
},
"node_modules/node-gyp": {
"version": "3.8.0",
"resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-3.8.0.tgz",
@ -11096,6 +11159,14 @@
}
}
},
"cross-fetch": {
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/cross-fetch/-/cross-fetch-3.1.4.tgz",
"integrity": "sha512-1eAtFWdIubi6T4XPy6ei9iUFoKpUkIF971QLN8lIvvvwueI65+Nw5haMNKUwfJxabqlIIDODJKGrQ66gxC0PbQ==",
"requires": {
"node-fetch": "2.6.1"
}
},
"cross-spawn": {
"version": "5.1.0",
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-5.1.0.tgz",
@ -11900,6 +11971,11 @@
}
}
},
"extract-files": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/extract-files/-/extract-files-9.0.0.tgz",
"integrity": "sha512-CvdFfHkC95B4bBBk36hcEmvdR2awOdhhVUYH6S/zrVj3477zven/fJMYg7121h4T1xHZC+tetUpubpAhxwI7hQ=="
},
"extract-text-webpack-plugin": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/extract-text-webpack-plugin/-/extract-text-webpack-plugin-3.0.2.tgz",
@ -12239,6 +12315,34 @@
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.6.tgz",
"integrity": "sha512-nTnJ528pbqxYanhpDYsi4Rd8MAeaBA67+RZ10CM1m3bTAVFEDcd5AuA4a6W5YkGZ1iNXHzZz8T6TBKLeBuNriQ=="
},
"graphql": {
"version": "15.5.1",
"resolved": "https://registry.npmjs.org/graphql/-/graphql-15.5.1.tgz",
"integrity": "sha512-FeTRX67T3LoE3LWAxxOlW2K3Bz+rMYAC18rRguK4wgXaTZMiJwSUwDmPFo3UadAKbzirKIg5Qy+sNJXbpPRnQw==",
"peer": true
},
"graphql-request": {
"version": "3.5.0",
"resolved": "https://registry.npmjs.org/graphql-request/-/graphql-request-3.5.0.tgz",
"integrity": "sha512-Io89QpfU4rqiMbqM/KwMBzKaDLOppi8FU8sEccCE4JqCgz95W9Q8bvxQ4NfPALLSMvg9nafgg8AkYRmgKSlukA==",
"requires": {
"cross-fetch": "^3.0.6",
"extract-files": "^9.0.0",
"form-data": "^3.0.0"
},
"dependencies": {
"form-data": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-3.0.1.tgz",
"integrity": "sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==",
"requires": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
}
}
}
},
"gzip-size": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/gzip-size/-/gzip-size-4.1.0.tgz",
@ -13427,6 +13531,11 @@
"resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz",
"integrity": "sha512-1nh45deeb5olNY7eX82BkPO7SSxR5SSYJiPTrTdFUVYwAl8CKMA5N9PjTYkHiRjisVcxcQ1HXdLhx2qxxJzLNQ=="
},
"node-fetch": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.1.tgz",
"integrity": "sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw=="
},
"node-gyp": {
"version": "3.8.0",
"resolved": "https://registry.npmjs.org/node-gyp/-/node-gyp-3.8.0.tgz",

View File

@ -1,6 +1,6 @@
{
"name": "mobilizon-cgb-guten-block",
"version": "1.0.0",
"version": "0.2",
"private": true,
"scripts": {
"start": "cgb-scripts start",
@ -8,6 +8,7 @@
"eject": "cgb-scripts eject"
},
"dependencies": {
"cgb-scripts": "1.23.1"
"cgb-scripts": "1.23.1",
"graphql-request": "^3.5.0"
}
}

View File

@ -5,7 +5,7 @@
* Description: Mobilizon Block is a Gutenberg Block which can display events from Mobilizon.
* Author: André Menrath
* Author URI: https://graz.social/@linos
* Version: 0.1
* Version: 0.2
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
*

File diff suppressed because one or more lines are too long

View File

@ -5,11 +5,16 @@
* which makes it higher in priority.
*/
.wp-block-cgb-block-mobilizon {
background: $green;
border: 0.2rem solid $black;
color: $black;
.wp-block-cgb-block-mobilizon {
// background: $green;
// border: 0.2rem solid $black;
// color: $black;
margin: 0 auto;
max-width: 740px;
padding: 2rem;
max-width: 600px;
padding: 1rem;
img.wp-block-cgb-mobilizon-icon {
height: 5rem;
width: 5rem;
margin-right: 0.7rem;
}
}

View File

@ -9,4 +9,5 @@
* Webpack is compiling as the input file.
*/
import './mobilizon-helper.js'
import './block/block.js';

View File

@ -36,24 +36,47 @@ function addhttp($url) {
}
return $url;
}
function block_render_callback($attributes, $content) {
// Get maximum number of events to be returned
if (isset($attributes['mobilizonEventLimit']) && ($attributes['mobilizonEventLimit'] != 0)) {
$limit = $attributes['mobilizonEventLimit'];
$limit = "(limit: ${limit})";
} else {
$limit = "";
}
// Get API-URL from Instance URL
$base = $attributes['mobilizonBaseURL'];
$url_array = array($base, "api");
function mobilizon_query($baseURL, $query) {
// Get API-endpoint from Instance URL
$url_array = array($baseURL, "api");
array_walk_recursive($url_array, 'stripTrailingSlash');
$endpoint = implode('/', $url_array);
$endpoint = addhttp($endpoint);
// Define default GraphQL headers
$headers = ['Content-Type: application/json', 'User-Agent: Minimal GraphQL client'];
$body = array ('query' => $query);
$args = array(
'body' => $body,
'headers' => $headers,
);
// Get the user-setting, if we are getting the events of a group only, or not
// Send HTTP-Query and return the response
return(wp_remote_post($endpoint, $args));
}
ob_start();
function block_render_callback($attributes, $content) {
// var_dump($attributes); // Debugging only
// Check if we are supposed to render anything at all
if (array_key_exists("mobilizonInputFieldIsValid", $attributes) && $attributes["mobilizonInputFieldIsValid"]) {
// Set the string which limits how many events are queriend at maximum
if (isset($attributes['mobilizonEventLimit']) && ($attributes['mobilizonEventLimit'] != 0)) {
$limit = $attributes['mobilizonEventLimit'];
$limit = "(limit: ${limit})";
} else {
$limit = "";
}
}
// Get if we are getting the events of a group only, or not
// This is changes the way we query the events, as well as we parse the reponse array
if (isset($attributes['mobilizonGroupName']) && $attributes['mobilizonGroupName'] != '' ) {
$filter_by_group = True;
} else {
@ -111,19 +134,13 @@ function block_render_callback($attributes, $content) {
";
}
// Define default GraphQL headers
$headers = ['Content-Type: application/json', 'User-Agent: Minimal GraphQL client'];
$body = array ('query' => $query);
$args = array(
'body' => $body,
'headers' => $headers,
);
// Execute the event query to the mobilizon instance
$response = mobilizon_query($attributes['mobilizonBaseURL'], $query);
// Send HTTP-Query
$response = wp_remote_post($endpoint, $args);
// Check if the HTTP-Query was successful
if ( wp_remote_retrieve_response_code( $response ) != 200 ) { /* Handle error */ }
// Check if the HTTP-Query was successful, if not do nothing?
if ( wp_remote_retrieve_response_code( $response ) != 200 ) {
return ob_get_clean();;
}
// Extract the events as an array from the query's response body
$body = json_decode(wp_remote_retrieve_body( $response ), true);
@ -134,11 +151,9 @@ function block_render_callback($attributes, $content) {
}
// Display the event-array in as html list
ob_start();
echo '<ul class="wp-block-cgb-block-mobilizon">';
// Loop through each event
foreach ($events as $event) {
print_r ($event);
echo '<li>';
echo '<time>';
echo date_i18n( 'D, d. M. Y, G:i', strtotime($event['beginsOn']) );

69
src/mobilizon-helper.js Normal file
View File

@ -0,0 +1,69 @@
import { request } from 'graphql-request';
// export function addHttpsProtocolIfNeeded (baseURL) {
// if (!baseURL.startsWith("https://")) {
// var baseURL = "https://" + baseURL;
// }
// return baseURL
// }
export async function isMobilizonInstance(baseURL) {
try {
let response = await fetch(baseURL + '/.well-known/nodeinfo/2.1');
if (response.ok) {
let json = await response.json();
if (json.software.name === "Mobilizon") {
return true;
}
else {
return false;
}
}
} catch (error) {
return false;
}
}
export async function groupExists(baseURL, groupName) {
const url = baseURL + "/api";
const query = `
query ($groupName: String!) {
group(preferredUsername: $groupName) {
type
}
}
`;
try {
await request(url, query, { groupName });
// if the group is not found the response looks like:
// {
// "data": {
// "group": null
// },
// "errors": [
// {
// "code": "group_not_found",
// "field": null,
// "locations": [
// {
// "column": 3,
// "line": 7
// }
// ],
// "message": "Group not found",
// "path": [
// "group"
// ],
// "status_code": 404
// }
// ]
// }
//
// So if we didn't catch an error, true can be returned
return true;
} catch (error) {
return false;
}
}