Browse Source

remove all the twitter junk

main
Aaron Parecki 1 month ago
parent
commit
8ecaed3d2f
8 changed files with 4 additions and 411 deletions
  1. +0
    -104
      controllers/auth.php
  2. +1
    -171
      controllers/controllers.php
  3. +0
    -3
      lib/config.template.php
  4. +2
    -6
      views/docs/syndication.php
  5. +1
    -12
      views/new-post.php
  6. +0
    -2
      views/privacy.php
  7. +0
    -30
      views/settings.php
  8. +0
    -83
      views/twitter.php

+ 0
- 104
controllers/auth.php View File

@ -1,5 +1,4 @@
<?php
use Abraham\TwitterOAuth\TwitterOAuth;
IndieAuth\Client::$clientID = Config::$base_url;
IndieAuth\Client::$redirectURL = Config::$base_url.'auth/callback';
@ -249,107 +248,4 @@ $app->post('/auth/reset', function() use($app) {
$app->redirect('/', 302);
});
$app->post('/auth/twitter', function() use($app) {
if(!Config::$twitterClientID) {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'result' => 'error'
)));
return;
}
if($user=require_login($app, false)) {
$params = $app->request()->params();
// User just auth'd with twitter, store the access token
$user->twitter_access_token = $params['twitter_token'];
$user->twitter_token_secret = $params['twitter_secret'];
$user->save();
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'result' => 'ok'
)));
} else {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'result' => 'error'
)));
}
});
function getTwitterLoginURL(&$twitter) {
$request_token = $twitter->oauth('oauth/request_token', [
'oauth_callback' => Config::$base_url . 'auth/twitter/callback'
]);
$_SESSION['twitter_auth'] = $request_token;
return $twitter->url('oauth/authorize', ['oauth_token' => $request_token['oauth_token']]);
}
$app->get('/auth/twitter', function() use($app) {
if(!Config::$twitterClientID) {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'result' => 'error'
)));
return;
}
$params = $app->request()->params();
if($user=require_login($app, false)) {
// If there is an existing Twitter token, check if it is valid
// Otherwise, generate a Twitter login link
$twitter_login_url = false;
if(array_key_exists('login', $params)) {
$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')) {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'result' => 'ok'
)));
return;
} else {
// If the existing twitter token is not valid, generate a login link
$twitter_login_url = getTwitterLoginURL($twitter);
}
} else {
$twitter_login_url = getTwitterLoginURL($twitter);
}
}
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'url' => $twitter_login_url
)));
} else {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'result' => 'error'
)));
}
});
$app->get('/auth/twitter/callback', function() use($app) {
if($user=require_login($app)) {
$params = $app->request()->params();
$twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
$_SESSION['twitter_auth']['oauth_token'], $_SESSION['twitter_auth']['oauth_token_secret']);
$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'];
$user->twitter_username = $credentials['screen_name'];
$user->save();
$app->redirect('/settings');
}
});

+ 1
- 171
controllers/controllers.php View File

@ -1,5 +1,4 @@
<?php
use Abraham\TwitterOAuth\TwitterOAuth;
use IndieWeb\DateFormatter;
function require_login(&$app, $redirect=true) {
@ -235,23 +234,6 @@ $app->get('/review', function() use($app) {
});
$app->get('/twitter', function() use($app) {
if($user=require_login($app)) {
$params = $app->request()->params();
$tweet_url = '';
if(array_key_exists('tweet_url', $params))
$tweet_url = $params['tweet_url'];
render('twitter', array(
'title' => 'Import Tweet',
'tweet_url' => $tweet_url,
'authorizing' => false
));
}
});
$app->get('/repost', function() use($app) {
if($user=require_login($app)) {
$params = $app->request()->params();
@ -443,125 +425,14 @@ $app->get('/settings/html-content', function() use($app) {
}
});
$app->post('/twitter/preview', function() use($app) {
if($user=require_login($app)) {
$params = $app->request()->params();
if($user->twitter_access_token) {
$xray_opts['twitter_api_key'] = Config::$twitterClientID;
$xray_opts['twitter_api_secret'] = Config::$twitterClientSecret;
$xray_opts['twitter_access_token'] = $user->twitter_access_token;
$xray_opts['twitter_access_token_secret'] = $user->twitter_token_secret;
}
$tweet_url = $params['tweet_url'];
// Pass to X-Ray to download all the twitter data in a useful format
$xray = new p3k\XRay();
$xray->http = new p3k\HTTP('Quill ('.Config::$base_url.')');
$data = $xray->parse($tweet_url, $xray_opts);
$postdata = tweet_to_micropub_request($data['data']);
$response = [
'json' => json_encode($postdata, JSON_PRETTY_PRINT+JSON_UNESCAPED_SLASHES)
];
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode($response));
}
});
$app->post('/twitter', function() use($app) {
if($user=require_login($app)) {
$params = $app->request()->params();
if($user->twitter_access_token) {
$xray_opts['twitter_api_key'] = Config::$twitterClientID;
$xray_opts['twitter_api_secret'] = Config::$twitterClientSecret;
$xray_opts['twitter_access_token'] = $user->twitter_access_token;
$xray_opts['twitter_access_token_secret'] = $user->twitter_token_secret;
}
$tweet_url = $params['tweet_url'];
// Pass to X-Ray to download all the twitter data in a useful format
$xray = new p3k\XRay();
$xray->http = new p3k\HTTP('Quill ('.Config::$base_url.')');
$data = $xray->parse($tweet_url, $xray_opts);
$location = null;
if(isset($data['data']) && $data['data']['type'] == 'entry') {
$tweet = $data['data'];
$postdata = tweet_to_micropub_request($tweet);
$r = micropub_post_for_user($user, $postdata, null, true);
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode([
'location' => (isset($r['location']) && $r['location'] ? Mf2\resolveUrl($user->micropub_endpoint, $r['location']) : null),
'error' => $r['error'],
'response' => $r['response']
]));
} else {
$app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode([
'location' => null,
'error' => 'Error fetching tweet',
]));
}
}
});
function tweet_to_micropub_request($tweet) {
// Convert to a micropub post
$postdata = [
'type' => ['h-entry'],
'properties' => [
'content' => [$tweet['content']['text']],
'published' => [$tweet['published']],
'syndication' => [$tweet['url']],
]
];
if(isset($tweet['in-reply-to']))
$postdata['properties']['in-reply-to'] = $tweet['in-reply-to'];
if(isset($tweet['category']))
$postdata['properties']['category'] = $tweet['category'];
if(isset($tweet['photo']))
$postdata['properties']['photo'] = $tweet['photo'];
if(isset($tweet['video']))
$postdata['properties']['video'] = $tweet['video'];
return $postdata;
}
function create_favorite(&$user, $url) {
$tweet_id = false;
$twitter_syndication = false;
// 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(Config::$twitterClientID, Config::$twitterClientSecret,
$user->twitter_access_token, $user->twitter_token_secret);
$result = $twitter->post('favorites/create', array(
'id' => $tweet_id
));
if(property_exists($result, 'id_str')) {
$twitter_syndication = 'https://twitter.com/'.$user->twitter_username.'/status/'.$result->id_str;
}
}
$micropub_request = array(
'like-of' => $url
);
if($twitter_syndication) {
$micropub_request['syndication'] = $twitter_syndication;
}
$r = micropub_post_for_user($user, $micropub_request);
return $r;
@ -581,25 +452,9 @@ function edit_favorite(&$user, $post_url, $like_of) {
function create_repost(&$user, $url) {
$tweet_id = false;
$twitter_syndication = false;
if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) {
$tweet_id = $match[1];
$twitter = new TwitterOAuth(Config::$twitterClientID, Config::$twitterClientSecret,
$user->twitter_access_token, $user->twitter_token_secret);
$result = $twitter->post('statuses/retweet/'.$tweet_id);
if(property_exists($result, 'id_str')) {
$twitter_syndication = 'https://twitter.com/'.$user->twitter_username.'/status/'.$result->id_str;
}
}
$micropub_request = array(
'repost-of' => $url
);
if($twitter_syndication) {
$micropub_request['syndication'] = $twitter_syndication;
}
$r = micropub_post_for_user($user, $micropub_request);
return $r;
@ -795,29 +650,10 @@ $app->get('/reply/preview', function() use($app) {
$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;
$xray_opts = [];
if(preg_match('/twitter\.com\/(?:[^\/]+)\/statuse?s?\/(.+)/', $reply_url, $match)) {
if($user->twitter_access_token) {
$xray_opts['twitter_api_key'] = Config::$twitterClientID;
$xray_opts['twitter_api_secret'] = Config::$twitterClientSecret;
$xray_opts['twitter_access_token'] = $user->twitter_access_token;
$xray_opts['twitter_access_token_secret'] = $user->twitter_token_secret;
}
}
// Pass to X-Ray to see if it can expand the entry
$xray = new p3k\XRay();
$xray->http = new p3k\HTTP('Quill ('.Config::$base_url.')');
@ -870,7 +706,7 @@ $app->get('/reply/preview', function() use($app) {
if(isset($entry['content']) && $entry['content'] && isset($entry['content']['text'])) {
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))
if(trim($nick,'@') != display_url($user->url))
$mentions[] = strtolower(trim($nick,'@'));
}
}
@ -884,12 +720,6 @@ $app->get('/reply/preview', function() use($app) {
foreach($entry['syndication'] as $s) {
$host = parse_url($s, PHP_URL_HOST);
switch($host) {
case 'twitter.com':
case 'www.twitter.com':
$icon = 'twitter.ico'; break;
case 'facebook.com':
case 'www.facebook.com':
$icon = 'facebook.ico'; break;
case 'github.com':
case 'www.github.com':
$icon = 'github.ico'; break;

+ 0
- 3
lib/config.template.php View File

@ -17,9 +17,6 @@ class Config {
public static $jwtSecret = 'xxx';
public static $twitterClientID = '';
public static $twitterClientSecret = '';
public static $atlasToken = '';
public static $mapTileURL = '';

+ 2
- 6
views/docs/syndication.php View File

@ -17,10 +17,6 @@ Content-type: application/json
{
"syndicate-to": [
{
"uid": "https://twitter.com/aaronpk",
"name": "twitter.com/aaronpk"
},
{
"uid": "https://news.indieweb.org/en",
"name": "IndieNews"
@ -29,10 +25,10 @@ Content-type: application/json
}
</code></pre>
<p>The specific values of names and uids are up to your Micropub endpoint, but a good convention is to use the domain name of the service (e.g. https://twitter.com), or domain name and username (e.g. https://twitter.com/aaronpk) for the uid, and a friendly name like "Twitter" or "twitter.com/aaronpk" as the name.</p>
<p>The specific values of names and uids are up to your Micropub endpoint, but a good convention is to use the domain name of the service (e.g. https://news.indieweb.org), or domain name and username (e.g. https://mastodon.social/@aaronpk) for the uid, and a friendly name like "IndieNews" or "mastodon.social/@aaronpk" as the name.</p>
<p>Quill will check for your supported syndication targets when you sign in, but there is also a link on the new post screen to manually re-check if you'd like.</p>
<p>When you create a post and tap one of the syndication options, the value of <code>uid</code> is sent in a property called <code>mp-syndicate-to</code>, which instructs your endpoint to syndicate to that target. Note that Quill doesn't know whether the target is Twitter, Facebook, or something else, and doesn't talk to the service directly. It's just an instruction to your endpoint to syndicate to that destination.</p>
<p>When you create a post and tap one of the syndication options, the value of <code>uid</code> is sent in a property called <code>mp-syndicate-to</code>, which instructs your endpoint to syndicate to that target. Note that Quill doesn't know whether the target is Mastodon, IndieNews, or something else, and doesn't talk to the service directly. It's just an instruction to your endpoint to syndicate to that destination.</p>
</div>

+ 1
- 12
views/new-post.php View File

@ -622,7 +622,7 @@ $(function(){
$("#note_content").on('change keyup', function(e){
var text = $("#note_content").val();
var tweet_length = tw_text_proxy(text).length;
var tweet_check = tw_length_check(text, 280, "<?= $this->user->twitter_username ?>");
var tweet_check = tw_length_check(text, 280, "");
var remaining = 280 - tweet_length;
$("#note_content_remaining span").text(remaining);
$("#note_content_remaining").removeClass("pcheck200 pcheck206 pcheck207 pcheck208 pcheck413");
@ -683,17 +683,6 @@ $(function(){
// }
// $("#note_category").val(category.join(", "));
/*
// stop auto-populating usernames in replies, since Twitter no longer requires it
if($("#note_content").val() == "" && data.mentions) {
var mentions = '';
for(var i in data.mentions) {
mentions += '@'+data.mentions[i]+' ';
}
$("#note_content").val(mentions);
}
*/
if(data.entry) {
$(".reply-context .content").text(data.entry.content.text);
if(data.entry.name) {

+ 0
- 2
views/privacy.php View File

@ -7,6 +7,4 @@
<p>Quill does not store your posts itself, but does cache the last response from your website and provides it to you for debugging purposes.</p>
<p>If you connect Quill to your Twitter account, Quill can favorite tweets on your behalf when you favorite a Twitter URL. Quill will never post anything to your accounts without an explicit action on your part.</p>
</div>

+ 0
- 30
views/settings.php View File

@ -123,13 +123,6 @@
<?php if(!Config::$twitterClientID): ?>
<h3>Twitter</h3>
<p>Connecting a Twitter account will automatically "favorite" and "retweet" tweets on Twitter when you favorite and retweet a Twitter URL in Quill.</p>
<input type="button" id="twitter-button" value="Checking" class="btn">
<?php endif ?>
<h3>Backwards Compatibility</h3>
<p>You can customize some of the properties that are sent in the Micropub request to work with older software.</p>
@ -168,29 +161,6 @@
<script>
$(function(){
<?php if(!Config::$twitterClientID): ?>
$.getJSON("/auth/twitter", function(data){
// Check if we're already authorized with twitter
if(data && data.result == 'ok') {
$("#twitter-button").val("Connected").addClass("btn-success");
} else if(data && data.url) {
$("#twitter-button").val("Sign In").data("url", data.url).addClass("btn-warning");
} else {
$("#twitter-button").val("Error").addClass("btn-danger");
}
});
$("#twitter-button").click(function(){
if($(this).data('url')) {
window.location = $(this).data('url');
} else {
$.getJSON("/auth/twitter", {login: 1}, function(data){
window.location = data.url;
});
}
});
<?php endif ?>
$("#send-html-content").click(function(){
var enabled = $(this).attr("checked") == "checked";
$.post("/settings/save", {

+ 0
- 83
views/twitter.php View File

@ -1,83 +0,0 @@
<div class="narrow">
<?= partial('partials/header') ?>
<div style="clear: both;" class="notice-pad">
<div class="alert alert-success hidden" id="test_success"><strong>Success! </strong><a href="" id="post_href">View your post</a></div>
<div class="alert alert-danger hidden" id="test_error"><strong>Something went wrong!</strong><br>Your Micropub endpoint indicated that something went wrong creating the post.</div>
</div>
<form role="form" style="margin-top: 20px;" id="note_form">
<div class="form-group">
<label for="tweet_url">Tweet to Import</label>
<input type="text" id="tweet_url" value="<?= $this->tweet_url ?>" class="form-control">
</div>
<div style="float: right; margin-top: 6px;">
<button class="btn btn-success" id="btn_post">Import</button>
</div>
<div style="float: right; margin-top: 6px; margin-right: 6px;">
<button class="btn btn-default" id="btn_preview">Preview</button>
</div>
</form>
<div style="clear: both;"></div>
<div id="preview_data" class="hidden">
<pre></pre>
</div>
</div>
<script>
$(function(){
$("#btn_preview").click(function(e){
$("#btn_preview").addClass("loading disabled");
$.post("/twitter/preview", {
tweet_url: $("#tweet_url").val(),
}, function(response){
$("#preview_data pre").text(response.json);
$("#preview_data").removeClass("hidden");
$("#btn_preview").removeClass("loading disabled");
});
return false;
});
$("#btn_post").click(function(){
$("#btn_post").addClass("loading disabled");
$.post("/twitter", {
tweet_url: $("#tweet_url").val(),
}, function(response){
if(response.location != false) {
$("#test_success").removeClass('hidden');
$("#test_error").addClass('hidden');
$("#post_href").attr("href", response.location);
$("#note_form").addClass('hidden');
window.location = response.location;
} else {
$("#test_success").addClass('hidden');
$("#test_error").removeClass('hidden');
if(response.error_details) {
$("#test_error").text(response.error_details);
}
$("#btn_post").removeClass("loading disabled");
}
});
return false;
});
});
</script>

Loading…
Cancel
Save