summaryrefslogtreecommitdiff
path: root/src/web/_controller/_util
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/web/_controller/_util/post.php (renamed from web/_controller/_util/post.php)97
1 files changed, 83 insertions, 14 deletions
diff --git a/web/_controller/_util/post.php b/src/web/_controller/_util/post.php
index b128d67..b48816d 100644
--- a/web/_controller/_util/post.php
+++ b/src/web/_controller/_util/post.php
@@ -21,6 +21,39 @@ class Post_controller extends Controller {
$this->view('template/posts');
}
+ public function post(): void {
+ $pid = $this->request_model->get_int('id', 0);
+
+ $post = $this->db
+ ->select('p.*, l.id as like_id')
+ ->from('api.post p')
+ ->join('api.like l', 'p.id = l.post_id AND l.user_id')
+ ->eq($pid)
+ ->where('p.id')
+ ->eq($pid)
+ ->row();
+
+ if (!$post) {
+ return;
+ }
+
+ $users = $this->cache_model->get_users([$post]);
+ $uid = $post['user_id'];
+
+ if (!array_key_exists($uid, $users)) {
+ return;
+ }
+
+ $user = $users[$uid];
+
+ $data = array(
+ 'user' => $user,
+ 'page_size' => $this->page_size,
+ 'post' => $post
+ );
+ $this->view('template/post', $data);
+ }
+
/**
* @return array<string,mixed>
*/
@@ -30,28 +63,23 @@ class Post_controller extends Controller {
$offset = $page * $this->page_size;
$user = $this->main->user();
+ $uid = isset($user) ? $user['id'] : NULL;
$query = $this->db;
- if ($user) {
- $query = $query->select('p.*, l.post_id IS NOT NULL as liked');
- } else {
- $query = $query->select('p.*, FALSE as liked');
- }
-
- $query = $query->from('api.post p');
-
- if ($user) {
- $query = $query->join('admin.like l', 'p.id = l.post_id AND l.user_id')
- ->eq($user['id']);
- }
+ $query = $this->db
+ ->select('p.*, l.id as like_id')
+ ->from('api.post p')
+ ->join('api.like l', 'p.id = l.post_id AND l.user_id')
+ ->eq($uid);
if ($max) {
$query = $query
- ->where('id')->le($max);
+ ->where('p.id')->le($max);
}
$posts = $query
+ ->order_by('p.id', 'DESC')
->limit($this->page_size)
->offset($offset)
->rows();
@@ -73,7 +101,6 @@ class Post_controller extends Controller {
->from('api.post p')
->row()['pc'];
-
return array(
'loaded' => count($posts),
'total' => $pc,
@@ -82,6 +109,36 @@ class Post_controller extends Controller {
);
}
+ public function comment(): void {
+ $cid = $this->request_model->get_int('id', 0);
+
+ $comment = $this->db
+ ->select('*')
+ ->from('api.comment')
+ ->where('id')
+ ->eq($cid)
+ ->row();
+
+ if (!$comment) {
+ return;
+ }
+
+ $users = $this->cache_model->get_users([$comment]);
+ $uid = $comment['user_id'];
+
+ if (!array_key_exists($uid, $users)) {
+ return;
+ }
+
+ $user = $users[$uid];
+
+ $data = array(
+ 'user' => $user,
+ 'comment' => $comment
+ );
+ $this->view('template/comment', $data);
+ }
+
/**
* @return array<string,mixed>
*/
@@ -105,6 +162,7 @@ class Post_controller extends Controller {
}
$comments = $query
+ ->order_by('id', 'ASC')
->limit($this->page_size)
->offset($offset)
->rows();
@@ -112,6 +170,17 @@ class Post_controller extends Controller {
$users = $this->cache_model->get_users($comments);
$max = 0;
+ // only add this hr when not logged in
+ // otherwise its added automatically by
+ // the like and comment buttons
+ if (
+ count($comments) &&
+ $page == 0 &&
+ $this->main->session === NULL
+ ) {
+ echo '<hr>';
+ }
+
foreach ($comments as $comment) {
$max = max($max, $comment['id']);
$data = array();