Expiry display and fixes (#16)

* Expiry display and fixes
* rm
* space
* stray tag
This commit is contained in:
jimm 2023-04-07 18:53:24 -07:00 committed by GitHub
parent aa2ac2c5a7
commit 81333996d4
4 changed files with 25 additions and 6 deletions

View file

@ -1,6 +1,7 @@
<?php
namespace PonePaste\Models;
use DateTime;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
@ -41,6 +42,23 @@ class Paste extends Model {
$this->save();
}
public function expiryDisplay() {
$expiry = $this->expiry;
if (!$expiry) {
return 'Never';
}
if ($expiry == 'SELF') {
return '<b>View Once</b>';
}
$dateTime = new DateTime();
$dateTime->setTimestamp($expiry);
$ret = $dateTime->format('Y-m-d H:i:s');
if ($dateTime->diff(new DateTime())->days < 1) {
$ret = "<b>$ret</b>";
}
return $ret;
}
public static function getRecent(int $count = 10) : Collection {
return Paste::with('user')
->orderBy('created_at', 'DESC')

View file

@ -27,9 +27,9 @@ function verifyCaptcha() : string|bool {
* Calculate the expiry of a paste based on user input.
*
* @param string $expiry Expiry time.
* SELF means to expire upon one view. +10M, +1H, +1D, +1W, +2W, +1M all do the obvious.
* SELF means to expire upon one view. +0Y0M0DT0H10M, +1H, +1D, +1W, +2W, +1M all do the obvious.
* Anything unhandled means to expire never.
* @return string|null Expiry time, or NULL if expires never.
* @return string|null 'SELF', Expiry time as Unix timestamp, or NULL if expires never.
*/
function calculatePasteExpiry(string $expiry) : ?string {
// used to use mktime
@ -37,7 +37,7 @@ function calculatePasteExpiry(string $expiry) : ?string {
return 'SELF';
}
$valid_expiries = ['10M', '1H', '1D', '1W', '2W', '1M'];
$valid_expiries = ['0Y0M0DT0H10M', '1H', '1D', '1W', '2W', '1M'];
return in_array($expiry, $valid_expiries)
? (new DateTime())->add(new DateInterval("P{$expiry}"))->format('U')

View file

@ -218,8 +218,8 @@
<select name="paste_expire_date">
<?= optionsForSelect(
['Never', 'View Once', '10 minutes', '1 hour', '1 day', '1 week', '2 weeks', '1 month'],
['N', 'self', '10M', '1H', '1D', '1W', '2W', '1M'],
['Never', 'View Once', '10 minutes', '1 hour', '1 day', '1 week', '2 weeks', '1 month'],
['N', 'self', '0Y0M0DT0H10M', '1H', '1D', '1W', '2W', '1M'],
$post_expire
); ?>
</select>

View file

@ -129,6 +129,7 @@ $selectedloader = "$bg[$i]"; // set variable equal to which random filename was
<?php if ($paste['updated_at'] != $paste['created_at']): ?>
Updated: <?= $paste['updated_at'] ?>
<?php endif; ?>
Expiry: <?= $paste->expiryDisplay() ?>
</small>
</div>
<div class="column is-4 has-text-right">
@ -298,7 +299,7 @@ $selectedloader = "$bg[$i]"; // set variable equal to which random filename was
<select name="paste_expire_date">
<option value="N" selected="selected">Never</option>
<option value="self">View Once</option>
<option value="10M">10 Minutes</option>
<option value="0Y0M0DT0H10M">10 Minutes</option>
<option value="1H">1 Hour</option>
<option value="1D">1 Day</option>
<option value="1W">1 Week</option>