From 1123ed958f7c22116a0c432779aaf1c27fcaf787 Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Mon, 27 Feb 2017 12:31:06 -0800 Subject: basic editing support * new route `/edit?url=` intended to be used as an edit button target, which detects the type of post and redirects to the appropriate editing interface * implemented the edit interface for favorites --- controllers/auth.php | 2 +- controllers/controllers.php | 127 +++++++++++++++++++++++++++++++++++++++++--- lib/helpers.php | 13 +++++ views/auth_start.php | 3 +- views/edit/error.php | 13 +++++ views/new-favorite.php | 13 ++--- 6 files changed, 157 insertions(+), 14 deletions(-) create mode 100644 views/edit/error.php diff --git a/controllers/auth.php b/controllers/auth.php index 9fd1493..299bde2 100644 --- a/controllers/auth.php +++ b/controllers/auth.php @@ -33,7 +33,7 @@ $app->get('/auth/start', function() use($app) { $tokenEndpoint = IndieAuth\Client::discoverTokenEndpoint($me); $micropubEndpoint = IndieAuth\Client::discoverMicropubEndpoint($me); - $defaultScope = 'create'; + $defaultScope = 'create update'; if($tokenEndpoint && $micropubEndpoint && $authorizationEndpoint) { // Generate a "state" parameter for the request diff --git a/controllers/controllers.php b/controllers/controllers.php index 5437ad7..215b932 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -125,10 +125,10 @@ $app->get('/favorite', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); - $url = ''; + $like_of = ''; if(array_key_exists('url', $params)) - $url = $params['url']; + $like_of = $params['url']; // Check if there was a login token in the query string and whether it has autosubmit=true $autosubmit = false; @@ -146,12 +146,24 @@ $app->get('/favorite', function() use($app) { } } + if(array_key_exists('edit', $params)) { + $edit_data = get_micropub_source($user, $params['edit'], 'like-of'); + $url = $params['edit']; + if(isset($edit_data['like-of'])) { + $like_of = $edit_data['like-of'][0]; + } + } else { + $edit_data = false; + $url = false; + } + render('new-favorite', array( 'title' => 'New Favorite', - 'url' => $url, + 'like_of' => $like_of, 'token' => generate_login_token(['autosubmit'=>true]), 'authorizing' => false, - 'autosubmit' => $autosubmit + 'autosubmit' => $autosubmit, + 'url' => $url )); } }); @@ -395,6 +407,18 @@ function create_favorite(&$user, $url) { return $r; } +function edit_favorite(&$user, $post_url, $like_of) { + $micropub_request = [ + 'action' => 'update', + 'url' => $post_url, + 'replace' => [ + 'like-of' => $like_of + ] + ]; + $r = micropub_post_for_user($user, $micropub_request, null, true); + return $r; +} + function create_repost(&$user, $url) { $micropub_request = array( 'repost-of' => $url @@ -417,11 +441,20 @@ $app->post('/favorite', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); - $r = create_favorite($user, $params['url']); + if(isset($params['edit'])) { + $r = edit_favorite($user, $params['edit'], $params['like_of']); + if(isset($r['location']) && $r['location']) + $location = $r['location']; + else + $location = $params['edit']; + } else { + $r = create_favorite($user, $params['like_of']); + $location = $r['location']; + } $app->response()['Content-type'] = 'application/json'; $app->response()->body(json_encode(array( - 'location' => $r['location'], + 'location' => $location, 'error' => $r['error'] ))); } @@ -519,3 +552,85 @@ $app->get('/reply/preview', function() use($app) { ])); } }); + +$app->get('/edit', function() use($app) { + if($user=require_login($app)) { + $params = $app->request()->params(); + + if(!isset($params['url']) || !$params['url']) { + $app->response()->body('no URL specified'); + } + + // Query the micropub endpoint for the source properties + $source = micropub_get($user->micropub_endpoint, [ + 'q' => 'source', + 'url' => $params['url'] + ], $user->micropub_access_token); + + $data = $source['data']; + + if(array_key_exists('error', $data)) { + render('edit/error', [ + 'title' => 'Error', + 'summary' => 'Your Micropub endpoint returned an error:', + 'error' => $data['error'], + 'error_description' => $data['error_description'] + ]); + return; + } + + if(!array_key_exists('properties', $data) || !array_key_exists('type', $data)) { + render('edit/error', [ + 'title' => 'Error', + 'summary' => '', + 'error' => 'Invalid Response', + 'error_description' => 'Your endpoint did not return "properties" and "type" in the response.' + ]); + return; + } + + // Start checking for content types + $type = $data['type'][0]; + $error = false; + $url = false; + + if($type == 'h-review') { + $url = '/review'; + } elseif($type == 'h-event') { + $url = '/event'; + } elseif($type != 'h-entry') { + $error = 'This type of post is not supported by any of Quill\'s editing interfaces. Type: '.$type; + } else { + if(array_key_exists('bookmark-of', $data['properties'])) { + $url = '/bookmark'; + } elseif(array_key_exists('like-of', $data['properties'])) { + $url = '/favorite'; + } elseif(array_key_exists('repost-of', $data['properties'])) { + $url = '/repost'; + } + } + + if($error) { + render('edit/error', [ + 'title' => 'Error', + 'summary' => '', + 'error' => 'There was a problem!', + 'error_description' => $error + ]); + return; + } + + // Until all interfaces are complete, show an error here for unsupported ones + if(!in_array($url, ['/favorite',])) { + render('edit/error', [ + 'title' => 'Not Yet Supported', + 'summary' => '', + 'error' => 'Not Yet Supported', + 'error_description' => 'Editing is not yet supported for this type of post.' + ]); + return; + } + + $app->redirect($url . '?edit=' . $params['url'], 302); + } +}); diff --git a/lib/helpers.php b/lib/helpers.php index b7ae78d..9a862d1 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -277,6 +277,19 @@ function get_micropub_config(&$user, $query=[]) { ]; } +function get_micropub_source(&$user, $url, $properties) { + $r = micropub_get($user->micropub_endpoint, [ + 'q' => 'source', + 'url' => $url, + 'properties' => $properties + ], $user->micropub_access_token); + if(isset($r['data']) && isset($r['data']['properties'])) { + return $r['data']['properties']; + } else { + return false; + } +} + function static_map($latitude, $longitude, $height=180, $width=700, $zoom=14) { return 'https://atlas.p3k.io/map/img?marker[]=lat:' . $latitude . ';lng:' . $longitude . ';icon:small-blue-cutout&basemap=gray&width=' . $width . '&height=' . $height . '&zoom=' . $zoom; } diff --git a/views/auth_start.php b/views/auth_start.php index 1fe0cdb..d41ffb4 100644 --- a/views/auth_start.php +++ b/views/auth_start.php @@ -56,7 +56,8 @@

Choose the scope to request:

diff --git a/views/edit/error.php b/views/edit/error.php new file mode 100644 index 0000000..ede6e0d --- /dev/null +++ b/views/edit/error.php @@ -0,0 +1,13 @@ +
+ + +

Error

+ +

summary) ?>

+ +
+

error) ?>

+ error_description) ?> +
+ +
\ No newline at end of file diff --git a/views/new-favorite.php b/views/new-favorite.php index 9f191e8..524bb79 100644 --- a/views/new-favorite.php +++ b/views/new-favorite.php @@ -2,21 +2,22 @@
- +
- - + +
- +
+
@@ -27,7 +28,6 @@ -