summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Parecki <aaron@parecki.com>2015-05-10 16:49:22 +0200
committerAaron Parecki <aaron@parecki.com>2015-05-10 16:49:22 +0200
commit3b7d766c31314a3d42d651b8c8f643ff8d6e15bd (patch)
treedec9c9cbed6cb077ada14754f556c21eb51da2c8
parent3dc97d7478781550aa1a878120082a871988f02e (diff)
do the micropub post and redirect after it's created!medium
-rw-r--r--controllers/auth.php17
-rw-r--r--controllers/editor.php27
-rw-r--r--lib/helpers.php2
-rw-r--r--public/editor/editor.js35
-rw-r--r--public/editor/style.css23
-rw-r--r--views/auth_callback.php2
-rw-r--r--views/editor.php5
7 files changed, 103 insertions, 8 deletions
diff --git a/controllers/auth.php b/controllers/auth.php
index a2fa08f..26aa933 100644
--- a/controllers/auth.php
+++ b/controllers/auth.php
@@ -74,7 +74,7 @@ $app->get('/auth/start', function() use($app) {
$req = $app->request();
$params = $req->params();
-
+
// the "me" parameter is user input, and may be in a couple of different forms:
// aaronparecki.com http://aaronparecki.com http://aaronparecki.com/
// Normlize the value now (move this into a function in IndieAuth\Client later)
@@ -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);
}
diff --git a/controllers/editor.php b/controllers/editor.php
index a66c188..caac4a3 100644
--- a/controllers/editor.php
+++ b/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');
diff --git a/lib/helpers.php b/lib/helpers.php
index 6bbe327..073d1e2 100644
--- a/lib/helpers.php
+++ b/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
diff --git a/public/editor/editor.js b/public/editor/editor.js
index 2d99674..17cc24e 100644
--- a/public/editor/editor.js
+++ b/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;
diff --git a/public/editor/style.css b/public/editor/style.css
index 8b91825..8b0fae6 100644
--- a/public/editor/style.css
+++ b/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 */
diff --git a/views/auth_callback.php b/views/auth_callback.php
index 7274cd7..3bafa57 100644
--- a/views/auth_callback.php
+++ b/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: ?>
diff --git a/views/editor.php b/views/editor.php
index 8bef0e5..1b82ab4 100644
--- a/views/editor.php
+++ b/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>