Browse Source

initial webmention sending interface which loads all outgoing links from a page

pull/3/head
Aaron Parecki 9 years ago
parent
commit
abc4a870e4
5 changed files with 128 additions and 15 deletions
  1. +69
    -13
      controllers/Controller.php
  2. +3
    -1
      public/index.php
  3. +8
    -1
      views/dashboard.php
  4. +2
    -0
      views/layout-loggedin.php
  5. +46
    -0
      views/webmention-send.php

+ 69
- 13
controllers/Controller.php View File

@ -4,7 +4,13 @@ use Symfony\Component\HttpFoundation\Response;
class Controller { class Controller {
private function _is_logged_in(&$request, &$response) {
public $http;
public function __construct() {
$this->http = new Telegraph\HTTP();
}
private function _is_logged_in(Request $request, Response $response) {
session_start(); session_start();
if(!session('user_id')) { if(!session('user_id')) {
session_destroy(); session_destroy();
@ -16,6 +22,24 @@ class Controller {
} }
} }
private function _get_role(Request $request) {
// Default to load their first site, but let the query string override it
$role = ORM::for_table('roles')->join('sites', 'roles.site_id = sites.id')
->where('user_id', session('user_id'))->order_by_asc('sites.created_at')->find_one();
if($request->get('account')) {
$role = ORM::for_table('roles')->where('user_id', session('user_id'))->where('site_id', $request->get('account'))->find_one();
// Check that the user has permission to access this account
if(!$role) {
$response->setStatusCode(302);
$response->headers->set('Location', '/dashboard');
return false;
}
}
return $role;
}
public function index(Request $request, Response $response) { public function index(Request $request, Response $response) {
$response->setContent(view('index', [ $response->setContent(view('index', [
'title' => 'Telegraph' 'title' => 'Telegraph'
@ -28,18 +52,8 @@ class Controller {
return $response; return $response;
} }
// Default to load their first site, but let the query string override it
$role = ORM::for_table('roles')->join('sites', 'roles.site_id = sites.id')
->where('user_id', session('user_id'))->order_by_asc('sites.created_at')->find_one();
if($request->get('account')) {
$role = ORM::for_table('roles')->where('user_id', session('user_id'))->where('site_id', $request->get('account'))->find_one();
// Check that the user has permission to access this account
if(!$role) {
$response->setStatusCode(302);
$response->headers->set('Location', '/dashboard');
return $response;
}
if(!$role=$this->_get_role($request, $response)) {
return $response;
} }
$site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one(); $site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
@ -117,6 +131,48 @@ class Controller {
return $response; return $response;
} }
public function dashboard_send(Request $request, Response $response) {
if(!$this->_is_logged_in($request, $response)) {
return $response;
}
if(!$role=$this->_get_role($request, $response)) {
return $response;
}
$site = ORM::for_table('sites')->where_id_is($role->site_id)->find_one();
$response->setContent(view('webmention-send', [
'title' => 'Webmention Details',
'user' => $this->_user(),
'accounts' => $this->_accounts(),
'site' => $site,
'role' => $role,
'url' => $request->get('url')
]));
return $response;
}
public function get_outgoing_links(Request $request, Response $response) {
if(!$this->_is_logged_in($request, $response)) {
return $response;
}
$sourceURL = $request->get('url');
$client = new IndieWeb\MentionClient();
$source = $this->http->get($sourceURL);
$parsed = \Mf2\parse($source['body'], $sourceURL);
$links = $client->findOutgoingLinks($parsed);
$response->headers->set('Content-Type', 'application/json');
$response->setContent(json_encode([
'links' => array_values($links)
]));
return $response;
}
private function _user() { private function _user() {
return ORM::for_table('users')->where_id_is(session('user_id'))->find_one(); return ORM::for_table('users')->where_id_is(session('user_id'))->find_one();
} }

+ 3
- 1
public/index.php View File

@ -10,10 +10,12 @@ $templates = new League\Plates\Engine(dirname(__FILE__).'/../views');
$router->addRoute('GET', '/', 'Controller::index'); $router->addRoute('GET', '/', 'Controller::index');
$router->addRoute('GET', '/dashboard', 'Controller::dashboard'); $router->addRoute('GET', '/dashboard', 'Controller::dashboard');
$router->addRoute('GET', '/api', 'Controller::api'); $router->addRoute('GET', '/api', 'Controller::api');
$router->addRoute('GET', '/webmention/{code}/details', 'Controller::webmention_details');
$router->addRoute('GET', '/dashboard/send', 'Controller::dashboard_send');
$router->addRoute('POST', '/dashboard/get_outgoing_links.json', 'Controller::get_outgoing_links');
$router->addRoute('POST', '/webmention', 'API::webmention'); $router->addRoute('POST', '/webmention', 'API::webmention');
$router->addRoute('GET', '/webmention/{code}', 'API::webmention_status'); $router->addRoute('GET', '/webmention/{code}', 'API::webmention_status');
$router->addRoute('GET', '/webmention/{code}/details', 'Controller::webmention_details');
$router->addRoute('GET', '/login', 'Auth::login'); $router->addRoute('GET', '/login', 'Auth::login');
$router->addRoute('GET', '/logout', 'Auth::logout'); $router->addRoute('GET', '/logout', 'Auth::logout');

+ 8
- 1
views/dashboard.php View File

@ -2,8 +2,15 @@
<div class="ui main text container" style="margin-top: 80px;"> <div class="ui main text container" style="margin-top: 80px;">
<form action="/dashboard/send" method="get">
<div class="ui fluid action input">
<input type="url" name="url" placeholder="enter a URL" value="https://aaronparecki.com/articles/2015/10/05/1/fun-with-qr-codes">
<button class="ui button">Send Webmentions</button>
</div>
<input type="hidden" name="account" value="<?= $site->id ?>">
</form>
<table class="ui striped table single line">
<table class="ui striped single line table">
<thead> <thead>
<th>Status</th> <th>Status</th>
<th>Date</th> <th>Date</th>

+ 2
- 0
views/layout-loggedin.php View File

@ -15,9 +15,11 @@
<? foreach($accounts as $account): ?> <? foreach($accounts as $account): ?>
<a class="item" href="/dashboard?account=<?= $account->id ?>"><?= $this->e($account->name) ?></a> <a class="item" href="/dashboard?account=<?= $account->id ?>"><?= $this->e($account->name) ?></a>
<? endforeach; ?> <? endforeach; ?>
<!--
<div class="divider"></div> <div class="divider"></div>
<a class="item" href="/new-site"><i class="plus icon"></i> New Site</a> <a class="item" href="/new-site"><i class="plus icon"></i> New Site</a>
<a class="item" href="/profile"><i class="user icon"></i> Profile</a> <a class="item" href="/profile"><i class="user icon"></i> Profile</a>
-->
<div class="divider"></div> <div class="divider"></div>
<a class="item" href="/logout"><i class="sign out icon"></i> Log Out</a> <a class="item" href="/logout"><i class="sign out icon"></i> Log Out</a>
</div> </div>

+ 46
- 0
views/webmention-send.php View File

@ -0,0 +1,46 @@
<?php $this->layout('layout-loggedin', ['title' => $title, 'accounts' => $accounts, 'user' => $user]); ?>
<div class="ui main text container" style="margin-top: 80px;">
<h2>Send Webmentions</h2>
<table class="ui very basic fixed single line unstackable table" id="send-table">
<thead>
<th class="twelve wide">URL</th>
<th class="four wide">Status</th>
</thead>
<tbody>
<tr><td colspan="2">Looking for URLs...</td></tr>
</tbody>
</table>
</div>
<script>
var url = "<?= $url ?>";
var token = "<?= $role->token ?>";
$(function(){
$.post('/dashboard/get_outgoing_links.json', {
url: url
}, function(data) {
$("#send-table tbody").html('<tr><td colspan="2"></td></tr>');
for(var i in data.links) {
console.log(data.links[i]);
$("#send-table tr:last").after('<tr>'
+'<td class="target-url">'
+'<div class="popup" data-content="'+data.links[i]+'">'+data.links[i]+'</div>'
+'</td>'
+'<td><button class="ui button">Send</button></td>'
+'</tr>');
}
$("#send-table tbody tr:first").remove();
$(".popup").popup();
});
});
</script>
<style type="text/css">
.popup {
word-wrap: break-word;
}
</style>

Loading…
Cancel
Save