2021-07-10 19:18:17 +01:00
|
|
|
<?php
|
2022-07-30 20:15:48 -04:00
|
|
|
/** @noinspection PhpDefineCanBeReplacedWithConstInspection */
|
2021-08-17 13:09:08 -04:00
|
|
|
define('IN_PONEPASTE', 1);
|
2022-07-30 20:15:48 -04:00
|
|
|
require_once(__DIR__ .'/../../includes/common.php');
|
2021-08-17 13:09:08 -04:00
|
|
|
|
2021-09-03 08:00:22 -04:00
|
|
|
use PonePaste\Models\Paste;
|
2021-08-17 13:09:08 -04:00
|
|
|
|
2022-04-18 13:34:00 -04:00
|
|
|
if (empty($_GET['q']) && $redis->exists('ajax_pastes')) {
|
|
|
|
header('Content-Type: application/json; charset=UTF-8');
|
|
|
|
echo $redis->get('ajax_pastes');
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
2021-09-02 08:55:14 -04:00
|
|
|
$pastes = Paste::with([
|
2021-09-03 08:00:22 -04:00
|
|
|
'user' => function($query) {
|
|
|
|
$query->select('users.id', 'username');
|
|
|
|
},
|
|
|
|
'tags' => function($query) {
|
|
|
|
$query->select('tags.id', 'name', 'slug');
|
|
|
|
}
|
2023-05-25 09:31:14 -04:00
|
|
|
])->select(['id', 'user_id', 'title', 'expiry', 'created_at', 'updated_at'])
|
2022-04-19 19:36:18 -04:00
|
|
|
->where('visible', Paste::VISIBILITY_PUBLIC)
|
2023-05-15 07:28:13 -04:00
|
|
|
->where('hidden', false)
|
2024-07-18 05:24:35 -04:00
|
|
|
->where('password', null)
|
2022-04-19 19:36:18 -04:00
|
|
|
->whereRaw("((expiry IS NULL) OR ((expiry != 'SELF') AND (expiry > NOW())))");
|
2021-11-23 03:17:29 -05:00
|
|
|
|
2024-04-27 00:43:09 -04:00
|
|
|
// if (!empty($_GET['q']) && is_string($_GET['q'])) {
|
|
|
|
// $tags = explode(',', $_GET['q']);
|
|
|
|
// $pastes = $pastes->whereHas('tags', function($query) use ($tags) {
|
|
|
|
// $query->where('name', $tags);
|
|
|
|
// });
|
|
|
|
// }
|
2021-11-23 03:17:29 -05:00
|
|
|
|
2023-06-01 15:18:44 -04:00
|
|
|
$pastes = $pastes->orderBy('id', 'desc')->get();
|
2021-07-10 19:18:17 +01:00
|
|
|
|
2021-09-03 08:00:22 -04:00
|
|
|
header('Content-Type: application/json; charset=UTF-8');
|
2021-11-04 11:04:22 -04:00
|
|
|
|
2022-04-18 13:34:00 -04:00
|
|
|
$pastes_json = json_encode(['data' => $pastes->map(function($paste) {
|
2021-09-03 08:00:22 -04:00
|
|
|
return [
|
|
|
|
'id' => $paste->id,
|
2023-05-25 09:31:14 -04:00
|
|
|
'created_at' => $paste->created_at,
|
|
|
|
'updated_at' => $paste->updated_at ?? $paste->created_at,
|
2021-09-03 08:00:22 -04:00
|
|
|
'title' => $paste->title,
|
|
|
|
'author' => $paste->user->username,
|
2022-04-19 19:36:18 -04:00
|
|
|
'author_id' => $paste->user->id,
|
2021-09-03 08:00:22 -04:00
|
|
|
'tags' => $paste->tags->map(function($tag) {
|
|
|
|
return ['slug' => $tag->slug, 'name' => $tag->name];
|
|
|
|
})
|
|
|
|
];
|
|
|
|
})]);
|
2022-04-18 13:34:00 -04:00
|
|
|
|
|
|
|
$redis->setEx('ajax_pastes', 3600, $pastes_json);
|
|
|
|
|
|
|
|
echo $pastes_json;
|