Database refactoring and improving blog comments, blog post history and more
This commit is contained in:
@@ -15,43 +15,30 @@
|
||||
return $this->addReply($userID, $content, NULL);
|
||||
}
|
||||
|
||||
public function addReply($userID, $content, $replyToUUID)
|
||||
public function addReply($userID, $content, $replyToHashID)
|
||||
{
|
||||
$content = $this->preparePostContent($content);
|
||||
$uuid = $this->generatePostUUID($userID, $content);
|
||||
$hashID = $this->generatePostHashID($userID, $content);
|
||||
$replyTo = NULL;
|
||||
if ($replyToUUID !== NULL) {
|
||||
$replyToID = $this->db->query('SELECT ID FROM user_posts WHERE uuid = ?', [$replyToUUID])->result_array();
|
||||
if ($replyToHashID !== NULL) {
|
||||
$replyToID = $this->db->query('SELECT ID FROM user_posts WHERE hashID = ?', [$replyToHashID])->result_array();
|
||||
$replyTo = !empty($replyToID) ? $replyToID[0]['ID'] : NULL;
|
||||
}
|
||||
$this->db->query('INSERT INTO user_posts (user_id, content, uuid, reply_to) VALUES (?, ?, ?, ?)', [$userID, $content, $uuid, $replyTo]);
|
||||
$this->db->query('INSERT INTO user_posts (userID, content, hashID, replyToPostID) VALUES (?, ?, ?, ?)', [$userID, $content, $hashID, $replyTo]);
|
||||
|
||||
|
||||
$insertedPost = $this->db->query('SELECT ID, user_id FROM user_posts WHERE uuid = ?', [$uuid])->result_array();
|
||||
$this->addPostMentions($insertedPost[0]['ID'], $content, $uuid);
|
||||
$insertedPost = $this->db->query('SELECT ID, userID FROM user_posts WHERE hashID = ?', [$hashID])->result_array();
|
||||
$this->addPostMentions($insertedPost[0]['ID'], $content, $hashID);
|
||||
$this->addPostHashtags($insertedPost[0]['ID'], $content);
|
||||
|
||||
// Send notification to user whose post was replied to
|
||||
if ($userID != $insertedPost[0]['user_id']) {
|
||||
$this->NotificationModel->userNotificationPostReply($userID, $insertedPost[0]['user_id'], $insertedPost[0]['ID'], $uuid);
|
||||
if ($userID != $insertedPost[0]['userID']) {
|
||||
$this->NotificationModel->userNotificationPostReply($userID, $insertedPost[0]['userID'], $insertedPost[0]['ID'], $hashID);
|
||||
}
|
||||
|
||||
return $insertedPost[0]['ID'];
|
||||
}
|
||||
|
||||
public function deletePost($userID, $uuid) {
|
||||
$postID = $this->db->query('SELECT ID FROM user_posts WHERE user_id = ? AND uuid = ?', [$userID, $uuid])->result_array()[0]['ID'];
|
||||
$this->db->query('DELETE FROM user_posts WHERE user_id = ? AND uuid = ?', [$userID, $uuid]);
|
||||
$this->db->query('DELETE FROM user_posts_hashtags WHERE postID = ?', [$postID]);
|
||||
$this->db->query('DELETE FROM user_posts_mentions WHERE postID = ?', [$postID]);
|
||||
$this->db->query('DELETE FROM user_posts_likes WHERE postID = ?', [$postID]);
|
||||
$this->db->query('DELETE FROM user_posts_media WHERE postID = ?', [$postID]);
|
||||
}
|
||||
|
||||
public function addMediaToPost($postID, $type, $fileID) {
|
||||
$this->db->query('INSERT INTO user_posts_media (postID, mediaType, fileID) VALUES (?, ?, ?)', [$postID, $type, $fileID]);
|
||||
}
|
||||
|
||||
public function preparePostContent($content)
|
||||
{
|
||||
if ($this->endsWith($content, '<br> ')) {
|
||||
@@ -69,12 +56,7 @@
|
||||
(substr($haystack, -$length) === $needle);
|
||||
}
|
||||
|
||||
function generatePostUUID($userID, $content)
|
||||
{
|
||||
return md5($userID . $content . date("Y-m-d H:i:s"));
|
||||
}
|
||||
|
||||
public function addPostMentions($postID, $content, $postUUID)
|
||||
public function addPostMentions($postID, $content, $postHashID)
|
||||
{
|
||||
preg_match_all('/@([A-Za-z0-9._]+)/', $content, $mentions, PREG_OFFSET_CAPTURE);
|
||||
foreach ($mentions[1] as $mention) {
|
||||
@@ -83,16 +65,16 @@
|
||||
continue;
|
||||
}
|
||||
$mentionedUser = $mentionedUser[0];
|
||||
$this->addMention($postID, $_SESSION['user']['ID'], $mentionedUser['ID'], $mention[1], $postUUID);
|
||||
$this->addMention($postID, $_SESSION['user']['ID'], $mentionedUser['ID'], $mention[1], $postHashID);
|
||||
}
|
||||
}
|
||||
|
||||
public function addMention($postID, $userID, $mentionedUserID, $position, $postUUID)
|
||||
public function addMention($postID, $userID, $mentionedUserID, $position, $postHashID)
|
||||
{
|
||||
$this->db->query('INSERT INTO user_posts_mentions (userID, postID, mentionedUserID, position) VALUES (?, ?, ?, ?)', [$userID, $postID, $mentionedUserID, $position]);
|
||||
|
||||
// Send notification
|
||||
$this->NotificationModel->userNotificationPostMentioned($userID, $mentionedUserID, $postID, $postUUID);
|
||||
$this->NotificationModel->userNotificationPostMentioned($userID, $mentionedUserID, $postID, $postHashID);
|
||||
}
|
||||
|
||||
public function addPostHashtags($postID, $content)
|
||||
@@ -108,10 +90,30 @@
|
||||
$this->db->query('INSERT INTO user_posts_hashtags (userID, postID, hashtag, position) VALUES (?, ?, ?, ?)', [$userID, $postID, $hashtag, $position]);
|
||||
}
|
||||
|
||||
public function deletePost($userID, $hashID)
|
||||
{
|
||||
$postID = $this->db->query('SELECT ID FROM user_posts WHERE userID = ? AND hashID = ?', [$userID, $hashID])->result_array()[0]['ID'];
|
||||
$this->db->query('DELETE FROM user_posts WHERE userID = ? AND hashID = ?', [$userID, $hashID]);
|
||||
$this->db->query('DELETE FROM user_posts_hashtags WHERE postID = ?', [$postID]);
|
||||
$this->db->query('DELETE FROM user_posts_mentions WHERE postID = ?', [$postID]);
|
||||
$this->db->query('DELETE FROM user_posts_likes WHERE postID = ?', [$postID]);
|
||||
$this->db->query('DELETE FROM user_posts_media WHERE postID = ?', [$postID]);
|
||||
}
|
||||
|
||||
public function addMediaToPost($postID, $type, $fileID)
|
||||
{
|
||||
$this->db->query('INSERT INTO user_posts_media (postID, mediaType, fileID) VALUES (?, ?, ?)', [$postID, $type, $fileID]);
|
||||
}
|
||||
|
||||
function generatePostHashID($userID, $content)
|
||||
{
|
||||
return md5($userID . $content . date("Y-m-d H:i:s"));
|
||||
}
|
||||
|
||||
function getUserPosts($id, $count, $offset, $maxLength = 250)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$posts = $this->db->query('SELECT * FROM user_posts p WHERE user_id = ? ORDER BY date DESC LIMIT ? OFFSET ?', [$id, $count, $offset])->result_array();
|
||||
$posts = $this->db->query('SELECT * FROM user_posts p WHERE userID = ? ORDER BY date DESC LIMIT ? OFFSET ?', [$id, $count, $offset])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
$posts = $this->preparePostList($posts, $maxLength);
|
||||
@@ -136,38 +138,32 @@
|
||||
$post = $this->mergeUserHasLiked($post, $_SESSION['user']['ID']);
|
||||
}
|
||||
|
||||
if($post['reply_to'] != NULL) {
|
||||
if ($post['replyToPostID'] != NULL) {
|
||||
$post = $this->mergeReplyData($post);
|
||||
}
|
||||
|
||||
$postList[$i] = $post;
|
||||
}
|
||||
|
||||
$postList = $this->UserModel->setDefaultImages($postList);
|
||||
|
||||
return $postList;
|
||||
}
|
||||
|
||||
public function getPostMedia($postID) {
|
||||
$result = $this->db->query('SELECT m.mediaType type, f.name name FROM user_posts_media m LEFT JOIN files f ON f.ID = m.fileID WHERE postID = ?', [$postID])->result_array();
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function mergePostUserData($post)
|
||||
{
|
||||
$user = $this->UserModel->getUserByID($post['user_id']);
|
||||
$user = $this->UserModel->getUserByID($post['userID']);
|
||||
$user = $user[0];
|
||||
|
||||
$post['username'] = $user['username'];
|
||||
$post['displayname'] = $user['displayname'];
|
||||
$post['profile_picture'] = $user['profile_picture'];
|
||||
$post['profilePicture'] = $user['profilePicture'];
|
||||
return $post;
|
||||
}
|
||||
|
||||
public function getPostReplyCountByID($postID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$data = $this->db->query('SELECT count(*) replyCount FROM user_posts WHERE reply_to = ?', [$postID])->result_array();
|
||||
$data = $this->db->query('SELECT count(*) replyCount FROM user_posts WHERE replyToPostID = ?', [$postID])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
return $data[0]['replyCount'];
|
||||
@@ -214,10 +210,16 @@
|
||||
return $finalContent;
|
||||
}
|
||||
|
||||
public function getPostMedia($postID)
|
||||
{
|
||||
$result = $this->db->query('SELECT m.mediaType type, f.name name FROM user_posts_media m LEFT JOIN files f ON f.ID = m.fileID WHERE postID = ?', [$postID])->result_array();
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function mergeUserHasLiked($post, $userID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$data = $this->db->query('SELECT * FROM user_posts_likes WHERE postID = (SELECT ID FROM user_posts WHERE uuid = ?) AND likerUserID = ?', [$post['uuid'], $userID])->result_array();
|
||||
$data = $this->db->query('SELECT * FROM user_posts_likes WHERE postID = (SELECT ID FROM user_posts WHERE hashID = ?) AND likerUserID = ?', [$post['hashID'], $userID])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
if (empty($data)) {
|
||||
@@ -228,10 +230,11 @@
|
||||
return $post;
|
||||
}
|
||||
|
||||
private function mergeReplyData($post) {
|
||||
$data = $this->db->query('SELECT p.*, username, displayname FROM user_posts p LEFT JOIN users ON users.ID = p.user_id WHERE p.ID = ?', [$post['reply_to']])->result_array();
|
||||
private function mergeReplyData($post)
|
||||
{
|
||||
$data = $this->db->query('SELECT p.* FROM user_posts p WHERE p.ID = ?', [$post['replyToPostID']])->result_array();
|
||||
$data = $this->preparePostList($data);
|
||||
if(!empty($data)) {
|
||||
if (!empty($data)) {
|
||||
$post['replyToPost'] = $data[0];
|
||||
} else {
|
||||
$post['replyToPost'] = [
|
||||
@@ -259,7 +262,7 @@
|
||||
public function getFeedPosts($userID, $amount, $offset)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$data = $this->db->query('SELECT * FROM user_posts WHERE user_id IN (SELECT followedUserID FROM user_followers WHERE followerUserID = ?) OR ID IN(SELECT postID FROM user_posts_mentions WHERE mentionedUserID = ?) OR ID IN (SELECT postID FROM user_posts_likes WHERE likedUserID = ?) OR user_id = ? ORDER BY date DESC LIMIT ? OFFSET ?', [$userID, $userID, $userID, $userID, $amount, $offset])->result_array();
|
||||
$data = $this->db->query('SELECT * FROM user_posts WHERE userID IN (SELECT followedUserID FROM user_followers WHERE followerUserID = ?) OR ID IN(SELECT postID FROM user_posts_mentions WHERE mentionedUserID = ?) OR ID IN (SELECT postID FROM user_posts_likes WHERE likedUserID = ?) OR userID = ? ORDER BY date DESC LIMIT ? OFFSET ?', [$userID, $userID, $userID, $userID, $amount, $offset])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
$data = $this->preparePostList($data);
|
||||
@@ -270,7 +273,7 @@
|
||||
public function getPopularPosts($amount, $offset)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$data = $this->db->query('SELECT * FROM user_posts p ORDER BY ((SELECT count(*) FROM user_posts_likes WHERE postID = p.ID) + (SELECT count(*) FROM user_posts WHERE reply_to = p.ID)) DESC, date DESC LIMIT ? OFFSET ?', [$amount, $offset])->result_array();
|
||||
$data = $this->db->query('SELECT * FROM user_posts p ORDER BY ((SELECT count(*) FROM user_posts_likes WHERE postID = p.ID) + (SELECT count(*) FROM user_posts WHERE replyToPostID = p.ID)) DESC, date DESC LIMIT ? OFFSET ?', [$amount, $offset])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
$data = $this->preparePostList($data);
|
||||
@@ -278,10 +281,10 @@
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function getPostDetails($userID, $uuid)
|
||||
public function getPostDetails($userID, $hashID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$data = $this->db->query('SELECT * FROM user_posts WHERE user_id = ? AND uuid = ?', [$userID, $uuid])->result_array();
|
||||
$data = $this->db->query('SELECT * FROM user_posts WHERE userID = ? AND hashID = ?', [$userID, $hashID])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
$data = $this->preparePostList($data, -1);
|
||||
@@ -292,7 +295,7 @@
|
||||
public function getPostReplies($postID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$replies = $this->db->query('SELECT * FROM user_posts WHERE reply_to = ? ORDER BY date', [$postID])->result_array();
|
||||
$replies = $this->db->query('SELECT * FROM user_posts WHERE replyToPostID = ? ORDER BY date', [$postID])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
$replies = $this->preparePostList($replies);
|
||||
@@ -300,33 +303,34 @@
|
||||
return $replies;
|
||||
}
|
||||
|
||||
public function getPostByUUID($uuid) {
|
||||
public function getPostByHashID($hashID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$result = $this->db->query('SELECT * FROM user_posts WHERE uuid = ?', [$uuid])->result_array();
|
||||
$result = $this->db->query('SELECT * FROM user_posts WHERE hashID = ?', [$hashID])->result_array();
|
||||
$this->db->cache_on();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function addPostLikeByUUID($uuid, $userID)
|
||||
public function addPostLikeByHashID($hashID, $userID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$data = $this->db->query('SELECT * FROM user_posts_likes WHERE postID = (SELECT ID FROM user_posts WHERE uuid = ?) AND likerUserID = ?', [$uuid, $userID])->result_array();
|
||||
$data = $this->db->query('SELECT * FROM user_posts_likes WHERE postID = (SELECT ID FROM user_posts WHERE hashID = ?) AND likerUserID = ?', [$hashID, $userID])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
// IDs needed for handling notifications later
|
||||
$postUser = $this->db->query('SELECT ID FROM users WHERE ID = (SELECT user_id FROM user_posts WHERE uuid = ?)', [$uuid])->result_array();
|
||||
$postID = $this->db->query('SELECT ID FROM user_posts WHERE uuid = ?', [$uuid])->result_array();
|
||||
$postUser = $this->db->query('SELECT ID FROM users WHERE ID = (SELECT userID FROM user_posts WHERE hashID = ?)', [$hashID])->result_array();
|
||||
$postID = $this->db->query('SELECT ID FROM user_posts WHERE hashID = ?', [$hashID])->result_array();
|
||||
if (empty($data)) {
|
||||
$this->db->query('INSERT INTO user_posts_likes (postID, likedUserID, likerUserID) VALUES ((SELECT ID FROM user_posts WHERE uuid = ?), (SELECT user_id FROM user_posts WHERE uuid = ?), ?)', [$uuid, $uuid, $userID]);
|
||||
$this->db->query('INSERT INTO user_posts_likes (postID, likedUserID, likerUserID) VALUES ((SELECT ID FROM user_posts WHERE hashID = ?), (SELECT userID FROM user_posts WHERE hashID = ?), ?)', [$hashID, $hashID, $userID]);
|
||||
|
||||
// Send like notification
|
||||
if ($postUser[0]['ID'] != $userID) {
|
||||
$this->NotificationModel->userNotificationPostLike($userID, $postUser[0]['ID'], $postID[0]['ID'], $uuid);
|
||||
$this->NotificationModel->userNotificationPostLike($userID, $postUser[0]['ID'], $postID[0]['ID'], $hashID);
|
||||
}
|
||||
|
||||
return true;
|
||||
} else {
|
||||
$this->db->query('DELETE FROM user_posts_likes WHERE postID = (SELECT ID FROM user_posts WHERE uuid = ?) AND likerUserID = ?', [$uuid, $userID]);
|
||||
$this->db->query('DELETE FROM user_posts_likes WHERE postID = (SELECT ID FROM user_posts WHERE hashID = ?) AND likerUserID = ?', [$hashID, $userID]);
|
||||
|
||||
// Remove existing notification
|
||||
$this->NotificationModel->removeNotification($userID, $postUser[0]['ID'], $postID[0]['ID'], 'users.likedPost');
|
||||
@@ -336,18 +340,18 @@
|
||||
|
||||
}
|
||||
|
||||
public function getPostLikeCountByUUID($uuid)
|
||||
public function getPostLikeCountByHashID($hashID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$data = $this->db->query('SELECT count(*) likeCount FROM user_posts_likes WHERE postID = (SELECT ID FROM user_posts WHERE uuid = ?)', [$uuid])->result_array();
|
||||
$data = $this->db->query('SELECT count(*) likeCount FROM user_posts_likes WHERE postID = (SELECT ID FROM user_posts WHERE hashID = ?)', [$hashID])->result_array();
|
||||
$this->db->cache_on();
|
||||
return $data[0]['likeCount'];
|
||||
}
|
||||
|
||||
public function isUUIDValid($uuid)
|
||||
public function isHashIDValid($hashID)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$data = $this->db->query('SELECT ID FROM user_posts WHERE uuid = ?', [$uuid])->result_array();
|
||||
$data = $this->db->query('SELECT ID FROM user_posts WHERE hashID = ?', [$hashID])->result_array();
|
||||
$this->db->cache_on();
|
||||
return !empty($data);
|
||||
}
|
||||
@@ -376,7 +380,7 @@
|
||||
public function searchPosts($query, $limit = 20, $offset = 0)
|
||||
{
|
||||
$this->db->cache_off();
|
||||
$results = $this->db->query('SELECT * FROM user_posts WHERE content RLIKE ? OR (SELECT username FROM users WHERE ID = user_id) RLIKE ? ORDER BY (SELECT count(*) FROM user_posts_likes WHERE postID = ID) DESC, (SELECT count(*) FROM user_posts WHERE user_posts.reply_to = ID) DESC, date DESC LIMIT ? OFFSET ?', [$query, $query, $limit, $offset])->result_array();
|
||||
$results = $this->db->query('SELECT * FROM user_posts WHERE content RLIKE ? OR (SELECT username FROM users WHERE ID = userID) RLIKE ? ORDER BY (SELECT count(*) FROM user_posts_likes WHERE postID = ID) DESC, (SELECT count(*) FROM user_posts WHERE replyToPostID = ID) DESC, date DESC LIMIT ? OFFSET ?', [$query, $query, $limit, $offset])->result_array();
|
||||
$this->db->cache_on();
|
||||
|
||||
$results = $this->preparePostList($results);
|
||||
@@ -384,14 +388,15 @@
|
||||
return $results;
|
||||
}
|
||||
|
||||
public function reportPost($uuid, $reason, $reasonText) {
|
||||
$this->db->query('INSERT INTO user_posts_reports (postID, reason, reasonText) VALUES ((SELECT ID FROM user_posts WHERE uuid = ?), ?, ?)', [$uuid, $reason, $reasonText]);
|
||||
public function reportPost($hashID, $reason, $reasonText)
|
||||
{
|
||||
$this->db->query('INSERT INTO user_posts_reports (postID, reason, reasonText) VALUES ((SELECT ID FROM user_posts WHERE hashID = ?), ?, ?)', [$hashID, $reason, $reasonText]);
|
||||
$this->db->cache_delete('admin', 'reports');
|
||||
|
||||
// Send notification
|
||||
$postID = $this->db->query('SELECT ID FROM user_posts WHERE uuid = ?', [$uuid])->result_array();
|
||||
$postID = $this->db->query('SELECT ID FROM user_posts WHERE hashID = ?', [$hashID])->result_array();
|
||||
$this->NotificationModel->rankNotificationPostReport(
|
||||
isset($_SESSION['user']) ? $_SESSION['user']['ID'] : -1,
|
||||
8, $postID[0]['ID'], $uuid);
|
||||
8, $postID[0]['ID'], $hashID);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user