summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Parecki <aaron@parecki.com>2017-02-27 12:31:06 -0800
committerAaron Parecki <aaron@parecki.com>2017-02-27 12:31:06 -0800
commit1123ed958f7c22116a0c432779aaf1c27fcaf787 (patch)
tree3000bb267e65efb8561c459b7a38d07bda690231
parentbb5fd837d12d125f908feb09808df3f6db1fe817 (diff)
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
-rw-r--r--controllers/auth.php2
-rw-r--r--controllers/controllers.php127
-rw-r--r--lib/helpers.php13
-rw-r--r--views/auth_start.php3
-rw-r--r--views/edit/error.php13
-rw-r--r--views/new-favorite.php13
6 files changed, 157 insertions, 14 deletions
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 @@
<form action="/auth/redirect" method="get">
<p>Choose the scope to request:</p>
<ul style="list-style-type: none;">
- <li><input type="radio" name="scope" value="create" checked="checked"> create</li>
+ <li><input type="radio" name="scope" value="create update" checked="checked"> create update</li>
+ <li><input type="radio" name="scope" value="create"> create</li>
<li><input type="radio" name="scope" value="post"> post (legacy)</li>
</ul>
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 @@
+<div class="narrow">
+ <?= partial('partials/header') ?>
+
+ <h1>Error</h1>
+
+ <p><?= htmlspecialchars($this->summary) ?></p>
+
+ <div class="bs-callout bs-callout-danger">
+ <h4><?= htmlspecialchars($this->error) ?></h4>
+ <?= htmlspecialchars($this->error_description) ?>
+ </div>
+
+</div> \ 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 @@
<?= partial('partials/header') ?>
<div style="clear: both;" class="notice-pad">
- <div class="alert alert-success hidden" id="test_success"><strong>Success! We found a Location header in the response!</strong><br>Your post should be on your website now!<br><a href="" id="post_href">View your post</a></div>
+ <div class="alert alert-success hidden" id="test_success"><strong>Success!</strong><br>Your post should be on your website now!<br><a href="" id="post_href">View your post</a></div>
<div class="alert alert-danger hidden" id="test_error"><strong>Your endpoint did not return a Location header.</strong><br>See <a href="/creating-a-micropub-endpoint">Creating a Micropub Endpoint</a> for more information.</div>
</div>
<form role="form" style="margin-top: 20px;" id="note_form">
<div class="form-group">
- <label for="note_url">URL to Favorite (<code>like-of</code>)</label>
- <input type="text" id="note_url" value="<?= $this->url ?>" class="form-control">
+ <label for="like_of">URL to Favorite (<code>like-of</code>)</label>
+ <input type="text" id="like_of" value="<?= $this->like_of ?>" class="form-control">
</div>
<div style="float: right; margin-top: 6px;">
- <button class="btn btn-success" id="btn_post">Post</button>
+ <button class="btn btn-success" id="btn_post"><?= $this->url ? 'Save' : 'Post' ?></button>
</div>
+ <input type="hidden" id="edit_url" value="<?= $this->url ?>">
</form>
<div style="clear: both;"></div>
@@ -27,7 +28,6 @@
</div>
</div>
-
<script>
$(function(){
@@ -40,7 +40,8 @@ $(function(){
});
$.post("/favorite", {
- url: $("#note_url").val()
+ like_of: $("#like_of").val(),
+ edit: $("#edit_url").val()
}, function(response){
if(response.location != false) {