summaryrefslogtreecommitdiff
path: root/controllers
diff options
context:
space:
mode:
authorAaron Parecki <aaron@parecki.com>2016-12-19 10:39:23 -0800
committerAaron Parecki <aaron@parecki.com>2016-12-19 10:39:23 -0800
commit4aa06023f0c25a10d5eaeafaeb30034e0a4f2e95 (patch)
treee4a694fad5254166d01934aed9f344fc2903a2df /controllers
parent53964f2622828bc5b67546dbc24bb455e4d165cb (diff)
clean up note UI, show reply context
* shows reply context of the URL you're replying to * autocomplete nicknames from the post when replying * moved debug info to the settings screen to clean up the UI
Diffstat (limited to 'controllers')
-rw-r--r--controllers/auth.php20
-rw-r--r--controllers/controllers.php71
2 files changed, 81 insertions, 10 deletions
diff --git a/controllers/auth.php b/controllers/auth.php
index fb8b6da..baf5c2f 100644
--- a/controllers/auth.php
+++ b/controllers/auth.php
@@ -1,4 +1,5 @@
<?php
+use Abraham\TwitterOAuth\TwitterOAuth;
function buildRedirectURI() {
return Config::$base_url . 'auth/callback';
@@ -260,9 +261,11 @@ $app->post('/auth/twitter', function() use($app) {
});
function getTwitterLoginURL(&$twitter) {
- $request_token = $twitter->getRequestToken(Config::$base_url . 'auth/twitter/callback');
+ $request_token = $twitter->oauth('oauth/request_token', [
+ 'oauth_callback' => Config::$base_url . 'auth/twitter/callback'
+ ]);
$_SESSION['twitter_auth'] = $request_token;
- return $twitter->getAuthorizeURL($request_token['oauth_token']);
+ return $twitter->url('oauth/authorize', ['oauth_token' => $request_token['oauth_token']]);
}
$app->get('/auth/twitter', function() use($app) {
@@ -272,15 +275,16 @@ $app->get('/auth/twitter', function() use($app) {
// If there is an existing Twitter token, check if it is valid
// Otherwise, generate a Twitter login link
$twitter_login_url = false;
- $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
- $user->twitter_access_token, $user->twitter_token_secret);
if(array_key_exists('login', $params)) {
- $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret);
+ $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret);
$twitter_login_url = getTwitterLoginURL($twitter);
} else {
+ $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
+ $user->twitter_access_token, $user->twitter_token_secret);
+
if($user->twitter_access_token) {
- if ($twitter->get('account/verify_credentials')) {
+ if($twitter->get('account/verify_credentials')) {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'result' => 'ok'
@@ -312,9 +316,9 @@ $app->get('/auth/twitter/callback', function() use($app) {
if($user=require_login($app)) {
$params = $app->request()->params();
- $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
+ $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
$_SESSION['twitter_auth']['oauth_token'], $_SESSION['twitter_auth']['oauth_token_secret']);
- $credentials = $twitter->getAccessToken($params['oauth_verifier']);
+ $credentials = $twitter->oauth('oauth/access_token', ['oauth_verifier' => $params['oauth_verifier']]);
$user->twitter_access_token = $credentials['oauth_token'];
$user->twitter_token_secret = $credentials['oauth_token_secret'];
diff --git a/controllers/controllers.php b/controllers/controllers.php
index 09133cf..a9bc0ab 100644
--- a/controllers/controllers.php
+++ b/controllers/controllers.php
@@ -1,4 +1,5 @@
<?php
+use Abraham\TwitterOAuth\TwitterOAuth;
function require_login(&$app, $redirect=true) {
$params = $app->request()->params();
@@ -336,7 +337,7 @@ function create_favorite(&$user, $url) {
// POSSE favorites to Twitter
if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
$tweet_id = $match[1];
- $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
+ $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
$user->twitter_access_token, $user->twitter_token_secret);
$result = $twitter->post('favorites/create', array(
'id' => $tweet_id
@@ -356,7 +357,7 @@ function create_repost(&$user, $url) {
if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
$tweet_id = $match[1];
- $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret,
+ $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
$user->twitter_access_token, $user->twitter_token_secret);
$result = $twitter->post('statuses/retweet/'.$tweet_id);
}
@@ -392,6 +393,72 @@ $app->post('/repost', function() use($app) {
}
});
+$app->get('/reply/preview', function() use($app) {
+ if($user=require_login($app)) {
+ $params = $app->request()->params();
+
+ $reply_url = trim($params['url']);
+
+ if(preg_match('/twtr\.io\/([0-9a-z]+)/i', $reply_url, $match)) {
+ $twtr = 'https://twitter.com/_/status/' . sxg_to_num($match[1]);
+ $ch = curl_init($twtr);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+ curl_exec($ch);
+ $expanded_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
+ if($expanded_url) $reply_url = $expanded_url;
+ }
+
+ $entry = false;
+ // Convert Tweets to h-entry
+ if(preg_match('/twitter\.com\/(?:[^\/]+)\/statuse?s?\/(.+)/', $reply_url, $match)) {
+ $tweet_id = $match[1];
+ if($user->twitter_access_token) {
+ $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
+ $user->twitter_access_token, $user->twitter_token_secret);
+ } else {
+ $twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret);
+ }
+ $tweet = $twitter->get('statuses/show/'.$tweet_id);
+ $entry = tweet_to_h_entry($tweet);
+ } else {
+ // Pass to X-Ray to see if it can expand the entry
+ $ch = curl_init('https://xray.p3k.io/parse?url='.urlencode($reply_url));
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+ $response = curl_exec($ch);
+ $data = @json_decode($response, true);
+ if($data && $data['data']['type'] == 'entry') {
+ $entry = $data['data'];
+ // Create a nickname based on the author URL
+ if($entry['author']['url']) {
+ $entry['author']['nickname'] = display_url($entry['author']['url']);
+ }
+ }
+ }
+
+ $mentions = [];
+ if($entry) {
+ // Find all @-names in the post, as well as the author name
+ $mentions[] = $entry['author']['nickname'];
+
+ if(preg_match_all('/(^|(?<=[\s\/]))@([a-z0-9_]+([a-z0-9_\.]*)?)/i', $entry['content']['text'], $matches)) {
+ foreach($matches[0] as $nick) {
+ if(trim($nick,'@') != $user->twitter_username && trim($nick,'@') != display_url($user->url))
+ $mentions[] = trim($nick,'@');
+ }
+ }
+
+ }
+
+ $app->response()['Content-type'] = 'application/json';
+ $app->response()->body(json_encode([
+ 'canonical_reply_url' => $reply_url,
+ 'entry' => $entry,
+ 'mentions' => $mentions
+ ]));
+ }
+});
+
$app->get('/micropub/syndications', function() use($app) {
if($user=require_login($app)) {
$data = get_micropub_config($user, ['q'=>'syndicate-to']);