Browse Source

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
pull/82/head
Aaron Parecki 8 years ago
parent
commit
1123ed958f
No known key found for this signature in database GPG Key ID: 276C2817346D6056
  1. 2
      controllers/auth.php
  2. 127
      controllers/controllers.php
  3. 13
      lib/helpers.php
  4. 3
      views/auth_start.php
  5. 13
      views/edit/error.php
  6. 13
      views/new-favorite.php

2
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

127
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);
}
});

13
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;
}

3
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>

13
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>

13
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) {

Loading…
Cancel
Save