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');
|
|
|
|
}
|
2022-04-19 19:36:18 -04:00
|
|
|
])->select(['id', 'user_id', 'title', 'expiry'])
|
|
|
|
->where('visible', Paste::VISIBILITY_PUBLIC)
|
|
|
|
->whereRaw("((expiry IS NULL) OR ((expiry != 'SELF') AND (expiry > NOW())))");
|
2021-11-23 03:17:29 -05: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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$pastes = $pastes->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,
|
|
|
|
'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;
|