Browse Source

populate date and timezone of the post for uploaded photos

main
Aaron Parecki 6 days ago
parent
commit
c2bf7ac890
2 changed files with 87 additions and 76 deletions
  1. +15
    -9
      controllers/controllers.php
  2. +72
    -67
      views/new-post.php

+ 15
- 9
controllers/controllers.php View File

@ -81,7 +81,7 @@ $app->get('/new', function() use($app) {
$app->get('/new/last-photo.json', function() use($app) { $app->get('/new/last-photo.json', function() use($app) {
if($user=require_login($app)) { if($user=require_login($app)) {
$url = null;
$item = null;
if($user->micropub_media_endpoint) { if($user->micropub_media_endpoint) {
// Request the last file uploaded from the media endpoint // Request the last file uploaded from the media endpoint
@ -89,20 +89,23 @@ $app->get('/new/last-photo.json', function() use($app) {
if(isset($response['data']['items'])) { if(isset($response['data']['items'])) {
$items = $response['data']['items']; $items = $response['data']['items'];
if(isset($items[0])) { if(isset($items[0])) {
$item = $items[0];
// Only show the file if it was uploaded in the last 5 minutes or if no published date available // Only show the file if it was uploaded in the last 5 minutes or if no published date available
$show = !isset($item['published']) || (strtotime($item['published']) >= (time()-300));
if($show && isset($item['url'])) {
$url = $item['url'];
$show = !isset($items[0]['published']) || (strtotime($items[0]['published']) >= (time()-900));
if($show && isset($items[0]['url'])) {
$item = $items[0];
} }
} }
} }
} }
if($item && !empty($item['latitude'])) {
$timezone = p3k\Timezone::timezone_for_location($item['latitude'], $item['longitude']);
$tz = new DateTimeZone($timezone);
$item['tz_offset'] = tz_seconds_to_offset($tz->getOffset(new DateTime($item['date_created'])));
}
$app->response()['Content-type'] = 'application/json'; $app->response()['Content-type'] = 'application/json';
$app->response()->body(json_encode(array(
'url' => $url
)));
$app->response()->body(json_encode($item, JSON_PRETTY_PRINT+JSON_UNESCAPED_SLASHES));
} }
}); });
@ -830,7 +833,10 @@ $app->get('/airport-info', function() use($app){
$params = $app->request()->params(); $params = $app->request()->params();
if(!isset($params['code'])) return; if(!isset($params['code'])) return;
$ch = curl_init('https://atlas.p3k.io/api/timezone?airport='.urlencode($params['code']));
$ch = curl_init('https://atlas.p3k.io/api/timezone?'.http_build_query([
'airport' => $params['code'],
'date' => $params['date'],
]));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = json_decode(curl_exec($ch), true); $data = json_decode(curl_exec($ch), true);

+ 72
- 67
views/new-post.php View File

@ -47,8 +47,7 @@
<div class="form-group"> <div class="form-group">
<div id="mastodon_content_remaining" class="mcheck500"><img src="/images/mastodon.ico" width="16"> <span>500</span></div> <div id="mastodon_content_remaining" class="mcheck500"><img src="/images/mastodon.ico" width="16"> <span>500</span></div>
<div id="note_content_remaining" class="pcheck206"><img src="/images/twitter.ico"> <span>280</span></div>
<div id="bluesky_content_remaining" class="bcheck256"><img src="/images/bluesky.png" width="16"> <span>256</span></div>
<div id="bluesky_content_remaining" class="bcheck256"><img src="/images/bluesky.svg" width="16"> <span>256</span></div>
<label for="note_content">Content</label> <label for="note_content">Content</label>
<textarea id="note_content" value="" class="form-control" style="height: 4em;"></textarea> <textarea id="note_content" value="" class="form-control" style="height: 4em;"></textarea>
</div> </div>
@ -222,7 +221,7 @@
margin-bottom: 1em; margin-bottom: 1em;
} }
#note_content_remaining, #mastodon_content_remaining, #bluesky_content_remaining {
#mastodon_content_remaining, #bluesky_content_remaining {
float: right; float: right;
font-size: 0.8em; font-size: 0.8em;
font-weight: bold; font-weight: bold;
@ -398,9 +397,34 @@ $(function(){
alt: null, alt: null,
external: true external: true
}); });
handle_uploaded_photo(response);
refreshPhotoPreviews(); refreshPhotoPreviews();
} }
}); });
function handle_uploaded_photo(photo) {
if(photo.url) {
if(photo.date_created) {
set_post_date(new Date(photo.date_created));
if(photo.tz_offset) {
$("#note_published_date").val( $("#note_published_date").val()+photo.tz_offset );
}
$("#published-field").removeClass("hidden");
if(photo.latitude) {
set_post_location(photo.latitude, photo.longitude, 10);
} else {
// Remove location since presumably this is a backdated post
$("#note_location_chk").removeAttr("checked");
}
}
} else {
console.log("Endpoint did not return a location header", photo);
}
}
$("#note_photo").on("change", function(e){ $("#note_photo").on("change", function(e){
@ -415,13 +439,10 @@ $(function(){
if(request.readyState == XMLHttpRequest.DONE) { if(request.readyState == XMLHttpRequest.DONE) {
try { try {
var response = JSON.parse(request.responseText); var response = JSON.parse(request.responseText);
if(response.location) {
$("#modal_photo_preview img").attr("src", response.location);
$("#note_photo_url").removeClass("hidden").val(response.location);
$("#note_photo_button").addClass("hidden");
} else {
console.log("Endpoint did not return a location header", response);
}
$("#modal_photo_preview img").attr("src", photo.url);
$("#note_photo_url").removeClass("hidden").val(photo.url);
$("#note_photo_button").addClass("hidden");
handle_uploaded_photo(response);
} catch(e) { } catch(e) {
console.log(e); console.log(e);
} }
@ -509,6 +530,25 @@ $(function(){
}); });
function set_post_date(d) {
function tz_seconds_to_offset(seconds) {
var tz_offset = '';
var hours = zero_pad(Math.floor(Math.abs(seconds / 60 / 60)));
var minutes = zero_pad(Math.floor(seconds / 60) % 60);
return (seconds < 0 ? '-' : '+') + hours + ":" + minutes;
}
function zero_pad(num) {
num = "" + num;
if(num.length == 1) {
num = "0" + num;
}
return num;
}
$("#note_published_date").val(d.getFullYear()+"-"+zero_pad(d.getMonth()+1)+"-"+zero_pad(d.getDate())
+"T"+zero_pad(d.getHours())+":"+zero_pad(d.getMinutes())+":"+zero_pad(d.getSeconds()));
}
function switchToMarkdown() { function switchToMarkdown() {
$("#content-type-selection select").val("text/markdown"); $("#content-type-selection select").val("text/markdown");
$("#content-type-selection").removeClass("hidden"); $("#content-type-selection").removeClass("hidden");
@ -544,40 +584,26 @@ function refreshPhotoPreviews() {
}); });
} }
/*
$("#note_photo").on("change", function(e){
// If the user has a media endpoint, upload the photo to it right now
if(hasMediaEndpoint) {
// TODO: add loading state indicator here
console.log("Uploading file to media endpoint...");
var formData = new FormData();
formData.append("null","null");
formData.append("photo", e.target.files[0]);
var request = new XMLHttpRequest();
request.open("POST", "/micropub/media");
request.onreadystatechange = function() {
if(request.readyState == XMLHttpRequest.DONE) {
try {
var response = JSON.parse(request.responseText);
if(response.location) {
// Replace the file upload form with the URL
replacePhotoWithPhotoURL(response.location);
saveNoteState();
} else {
console.log("Endpoint did not return a location header", response);
}
} catch(e) {
console.log(e);
}
}
}
request.send(formData);
} else {
$("#photo_preview").attr("src", URL.createObjectURL(e.target.files[0]) );
$("#photo_preview_container").removeClass("hidden");
}
});
*/
var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
function set_post_location(latitude, longitude, accuracy) {
$("#note_location_loading").hide();
var geo = "geo:" + (Math.round(latitude * 100000) / 100000) + "," + (Math.round(longitude * 100000) / 100000) + ";u=" + accuracy;
$("#note_location_msg").val(geo);
$("#note_location").val(geo);
$("#note_location_img_small").attr("src", map_template_small.replace('{lat}', latitude).replace('{lng}', longitude));
$("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', latitude).replace('{lng}', longitude));
$("#note_location_img").show();
$("#note_location_msg").addClass("img-visible");
$.post("/prefs/timezone", {
latitude: latitude,
longitude: longitude
});
}
$(function(){ $(function(){
@ -621,12 +647,6 @@ $(function(){
$("#note_content").on('change keyup', function(e){ $("#note_content").on('change keyup', function(e){
var text = $("#note_content").val(); var text = $("#note_content").val();
var tweet_length = tw_text_proxy(text).length;
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");
$("#note_content_remaining").addClass("pcheck"+tweet_check);
$("#mastodon_content_remaining span").text(500 - text.length); $("#mastodon_content_remaining span").text(500 - text.length);
$("#mastodon_content_remaining").removeClass("lengthOK lengthWARN lengthOVER"); $("#mastodon_content_remaining").removeClass("lengthOK lengthWARN lengthOVER");
@ -902,27 +922,12 @@ $(function(){
$("#note_location_msg").removeClass("img-visible"); $("#note_location_msg").removeClass("img-visible");
} }
var map_template_wide = "<?= static_map('{lat}', '{lng}', 180, 700, 15) ?>";
var map_template_small = "<?= static_map('{lat}', '{lng}', 320, 480, 15) ?>";
function fetch_location() { function fetch_location() {
$("#note_location_loading").show(); $("#note_location_loading").show();
navigator.geolocation.getCurrentPosition(function(position){ navigator.geolocation.getCurrentPosition(function(position){
$("#note_location_loading").hide();
var geo = "geo:" + (Math.round(position.coords.latitude * 100000) / 100000) + "," + (Math.round(position.coords.longitude * 100000) / 100000) + ";u=" + position.coords.accuracy;
$("#note_location_msg").val(geo);
$("#note_location").val(geo);
$("#note_location_img_small").attr("src", map_template_small.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
$("#note_location_img_wide").attr("src", map_template_wide.replace('{lat}', position.coords.latitude).replace('{lng}', position.coords.longitude));
$("#note_location_img").show();
$("#note_location_msg").addClass("img-visible");
$.post("/prefs/timezone", {
latitude: position.coords.latitude,
longitude: position.coords.longitude
});
set_post_location(position.coords.latitude, position.coords.longitude, position.coords.accuracy);
}, function(err){ }, function(err){
if(err.code == 1) { if(err.code == 1) {
@ -934,7 +939,7 @@ $(function(){
} }
}); });
} }
$("#note_location_chk").click(function(){ $("#note_location_chk").click(function(){
if($(this).attr("checked") == "checked") { if($(this).attr("checked") == "checked") {
if(navigator.geolocation) { if(navigator.geolocation) {

Loading…
Cancel
Save