Browse Source

do the micropub post and redirect after it's created!

medium
Aaron Parecki 10 years ago
parent
commit
3b7d766c31
  1. 15
      controllers/auth.php
  2. 27
      controllers/editor.php
  3. 2
      lib/helpers.php
  4. 35
      public/editor/editor.js
  5. 23
      public/editor/style.css
  6. 2
      views/auth_callback.php
  7. 5
      views/editor.php

15
controllers/auth.php

@ -88,6 +88,10 @@ $app->get('/auth/start', function() use($app) {
return;
}
if(k($params, 'redirect')) {
$_SESSION['redirect_after_login'] = $params['redirect'];
}
$authorizationEndpoint = IndieAuth\Client::discoverAuthorizationEndpoint($me);
$tokenEndpoint = IndieAuth\Client::discoverTokenEndpoint($me);
$micropubEndpoint = IndieAuth\Client::discoverMicropubEndpoint($me);
@ -244,7 +248,13 @@ $app->get('/auth/callback', function() use($app) {
unset($_SESSION['auth_state']);
if($redirectToDashboardImmediately) {
$app->redirect('/new', 301);
if(k($_SESSION, 'redirect_after_login')) {
$dest = $_SESSION['redirect_after_login'];
unset($_SESSION['redirect_after_login']);
$app->redirect($dest, 301);
} else {
$app->redirect('/new', 301);
}
} else {
$html = render('auth_callback', array(
'title' => 'Sign In',
@ -254,7 +264,8 @@ $app->get('/auth/callback', function() use($app) {
'tokenEndpoint' => $tokenEndpoint,
'auth' => $token['auth'],
'response' => $token['response'],
'curl_error' => (array_key_exists('error', $token) ? $token['error'] : false)
'curl_error' => (array_key_exists('error', $token) ? $token['error'] : false),
'destination' => (k($_SESSION, 'redirect_after_login') ?: '/new')
));
$app->response()->body($html);
}

27
controllers/editor.php

@ -6,6 +6,26 @@ $app->get('/editor', function() use($app) {
$app->response()->body($html);
});
$app->post('/editor/publish', function() use($app) {
if($user=require_login($app)) {
$params = $app->request()->params();
$micropub_request = array(
'h' => 'entry',
'name' => $params['name'],
'content' => $params['body']
);
$r = micropub_post_for_user($user, $micropub_request);
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode([
'location' => $r['location']
]));
}
});
$app->post('/editor/upload', function() use($app) {
// Fake a file uploader by echo'ing back the data URI
$fn = $_FILES['files']['tmp_name'][0];
@ -21,6 +41,7 @@ $app->post('/editor/upload', function() use($app) {
]
]));
});
$app->post('/editor/delete-file', function() use($app) {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(['result'=>'deleted']));
@ -33,6 +54,12 @@ $app->get('/editor/oembed', function() use($app) {
$app->response()->body($json);
});
$app->post('/editor/test-login', function() use($app) {
$logged_in = array_key_exists('user_id', $_SESSION);
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(['logged_in'=>$logged_in]));
});
// $app->get('/appcache.manifest', function() use($app) {
// $content = partial('partials/appcache');

2
lib/helpers.php

@ -74,7 +74,7 @@ function micropub_post_for_user(&$user, $params) {
// Now send to the micropub endpoint
$r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token);
$user->last_micropub_response = json_encode($r);
$user->last_micropub_response = substr(json_encode($r), 0, 1024);
$user->last_micropub_response_date = date('Y-m-d H:i:s');
// Check the response and look for a "Location" header containing the URL

35
public/editor/editor.js

@ -29,8 +29,43 @@ $(function () {
$('.editable').focus(function(){
$('.placeholder').removeClass('placeholder');
});
$.post('/editor/test-login', {}, function(response) {
$('#publish_btn').text(response.logged_in ? 'Publish' : 'Sign In');
});
$('#publish_btn').click(function(){
if($('#publish_btn').text() == 'Publish') {
$.post('/editor/publish', {
name: $("#post-name").val(),
body: editor.serialize().content.value
}, function(response) {
if(response.location) {
reset_page().then(function(){
window.location = response.location;
});
}
});
} else {
var url = prompt("Enter your URL");
window.location = '/auth/start?me=' + encodeURIComponent(url) + '&redirect=/editor';
}
});
$('#new_btn').click(function(){
reset_page();
});
});
function reset_page() {
$("#post-name").val('');
$("#content").html('<p class="placeholder">Write something nice...</p>');
$("#draft-status").text("New");
return localforage.setItem('currentdraft', {});
}
/* ************************************************ */
/* autosave loop */
var autosaveTimeout = false;

23
public/editor/style.css

@ -39,19 +39,40 @@ img { border: 0; }
}
.toolbar-right {
float: right;
margin-right: 40px;
}
.toolbar-left .item {
margin-right: 8px;
display: inline-block;
}
.toolbar-left .logo {
vertical-align: middle;
}
#draft-status {
font-size: 18px;
color: #aaa;
font-family: sans-serif;
font-size: 16px;
}
.toolbar .clear {
clear: both;
}
.toolbar .btn {
height: 38px;
-webkit-border-radius: 999px;
-moz-border-radius: 999px;
border-radius: 999px;
display: inline-block;
padding: 0 24px;
background: rgba(0,0,0,0);
font-size: 14px;
text-decoration: none;
text-align: center;
border: 1px #93dee5 solid;
color: #60b1b8;
vertical-align: middle;
cursor: pointer;
}
/* ************************************** */
/* Editor CSS */

2
views/auth_callback.php

@ -27,7 +27,7 @@
<h3>Success!</h3>
<p>All required values were found! You are now signed in.</p>
<p><a href="/new" class="btn btn-primary">Continue</a></p>
<p><a href="<?= $this->destination ?>" class="btn btn-primary">Continue</a></p>
<?php else: ?>

5
views/editor.php

@ -43,11 +43,12 @@
<div class="toolbar">
<div class="toolbar-left">
<span class="item"><a href="/"><img src="/editor/quill-logo-36.png" width="36" height="31"></a></span>
<span class="item"><a href="/"><img src="/editor/quill-logo-36.png" width="36" height="31" class="logo"></a></span>
<span class="item text"><span id="draft-status">Draft</span></span>
</div>
<div class="toolbar-right">
<button class="btn" id="publish_btn">Publish</button>
<button class="btn" id="new_btn">New</button>
</div>
<div class="clear"></div>
</div>

Loading…
Cancel
Save