From c2bf7ac890b72e9ccaa0fa8fb5fbee7b998d6c5f Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 17 Aug 2025 01:58:33 +0000 Subject: [PATCH] populate date and timezone of the post for uploaded photos --- controllers/controllers.php | 24 ++++--- views/new-post.php | 139 +++++++++++++++++++----------------- 2 files changed, 87 insertions(+), 76 deletions(-) diff --git a/controllers/controllers.php b/controllers/controllers.php index 153c9e2..604b79d 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -81,7 +81,7 @@ $app->get('/new', function() use($app) { $app->get('/new/last-photo.json', function() use($app) { if($user=require_login($app)) { - $url = null; + $item = null; if($user->micropub_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'])) { $items = $response['data']['items']; 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 - $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()->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(); 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); $data = json_decode(curl_exec($ch), true); diff --git a/views/new-post.php b/views/new-post.php index 2190752..0301fe2 100644 --- a/views/new-post.php +++ b/views/new-post.php @@ -47,8 +47,7 @@
500
-
280
-
256
+
256
@@ -222,7 +221,7 @@ margin-bottom: 1em; } -#note_content_remaining, #mastodon_content_remaining, #bluesky_content_remaining { +#mastodon_content_remaining, #bluesky_content_remaining { float: right; font-size: 0.8em; font-weight: bold; @@ -398,9 +397,34 @@ $(function(){ alt: null, external: true }); + handle_uploaded_photo(response); 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){ @@ -415,13 +439,10 @@ $(function(){ if(request.readyState == XMLHttpRequest.DONE) { try { 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) { 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() { $("#content-type-selection select").val("text/markdown"); $("#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 = ""; + var map_template_small = ""; + + 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(){ @@ -621,12 +647,6 @@ $(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, ""); - 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").removeClass("lengthOK lengthWARN lengthOVER"); @@ -902,27 +922,12 @@ $(function(){ $("#note_location_msg").removeClass("img-visible"); } - var map_template_wide = ""; - var map_template_small = ""; - function fetch_location() { $("#note_location_loading").show(); 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){ if(err.code == 1) { @@ -934,7 +939,7 @@ $(function(){ } }); } - + $("#note_location_chk").click(function(){ if($(this).attr("checked") == "checked") { if(navigator.geolocation) {