Skip to content

Commit

Permalink
refactor: implement almost all forum table TODOs (#2946)
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland authored Dec 30, 2024
1 parent f85d70a commit 4a96cf5
Show file tree
Hide file tree
Showing 45 changed files with 676 additions and 762 deletions.
62 changes: 31 additions & 31 deletions app/Community/Actions/BuildAggregateRecentForumPostsDataAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ public function execute(

private function getTotalRecentForumTopics(int $permissions = Permissions::Unregistered): int
{
return ForumTopic::where("RequiredPermissions", "<=", $permissions)
return ForumTopic::where("required_permissions", "<=", $permissions)
->whereNull("deleted_at")
->where(function ($query) {
$query
->whereNotNull("LatestCommentID")
->orWhereIn("ID", function ($subQuery) {
->whereNotNull("latest_comment_id")
->orWhereIn("id", function ($subQuery) {
$subQuery
->select("ForumTopicID")
->select("forum_topic_id")
->distinct()
->from("ForumTopicComment")
->where("Authorised", 1);
->from("forum_topic_comments")
->where("is_authorized", 1);
});
})
->count();
Expand All @@ -78,44 +78,44 @@ private function getRecentForumTopics(int $page = 1, int $permissions = Permissi
// Heredoc syntax with <<<SQL gives us proper SQL syntax highlighting.
$results = DB::select(<<<SQL
SELECT
ft.ID AS ForumTopicID,
ft.Title AS ForumTopicTitle,
f.ID AS ForumID,
f.Title AS ForumTitle,
lftc.ID AS CommentID,
lftc.DateCreated AS PostedAt,
ft.id AS ForumTopicID,
ft.title AS ForumTopicTitle,
f.id AS ForumID,
f.title AS ForumTitle,
lftc.id AS CommentID,
lftc.created_at AS PostedAt,
lftc.author_id,
ua.User AS Author,
ua.display_name AS AuthorDisplayName,
LEFT(lftc.Payload, 260) AS ShortMsg,
LENGTH(lftc.Payload) > 260 AS IsTruncated,
LEFT(lftc.body, 260) AS ShortMsg,
LENGTH(lftc.body) > 260 AS IsTruncated,
d1.CommentID AS CommentID_1d,
d1.Count AS Count_1d,
d7.CommentID AS CommentID_7d,
d7.Count AS Count_7d
FROM (
SELECT ft.ID, ft.Title, ft.ForumID, ft.LatestCommentID
FROM ForumTopic ft
SELECT ft.id, ft.title, ft.forum_id, ft.latest_comment_id
FROM forum_topics ft
FORCE INDEX (idx_permissions_deleted_latest)
WHERE ft.RequiredPermissions <= ? AND ft.deleted_at IS NULL
ORDER BY ft.LatestCommentID DESC
WHERE ft.required_permissions <= ? AND ft.deleted_at IS NULL
ORDER BY ft.latest_comment_id DESC
) AS ft
INNER JOIN Forum AS f ON f.ID = ft.ForumID
INNER JOIN ForumTopicComment AS lftc ON lftc.ID = ft.LatestCommentID AND lftc.Authorised = 1
INNER JOIN forums AS f ON f.id = ft.forum_id
INNER JOIN forum_topic_comments AS lftc ON lftc.id = ft.latest_comment_id AND lftc.is_authorized = 1
LEFT JOIN UserAccounts AS ua ON ua.ID = lftc.author_id
LEFT JOIN (
SELECT ForumTopicId, MIN(ID) AS CommentID, COUNT(*) AS Count
FROM ForumTopicComment
WHERE Authorised = 1 AND DateCreated >= NOW() - INTERVAL 1 DAY
GROUP BY ForumTopicId
) AS d1 ON d1.ForumTopicId = ft.ID
SELECT forum_topic_id, MIN(id) AS CommentID, COUNT(*) AS Count
FROM forum_topic_comments
WHERE is_authorized = 1 AND created_at >= NOW() - INTERVAL 1 DAY
GROUP BY forum_topic_id
) AS d1 ON d1.forum_topic_id = ft.id
LEFT JOIN (
SELECT ForumTopicId, MIN(ID) AS CommentID, COUNT(*) AS Count
FROM ForumTopicComment
WHERE Authorised = 1 AND DateCreated >= NOW() - INTERVAL 7 DAY
GROUP BY ForumTopicId
) AS d7 ON d7.ForumTopicId = ft.ID
ORDER BY lftc.DateCreated DESC
SELECT forum_topic_id, MIN(id) AS CommentID, COUNT(*) AS Count
FROM forum_topic_comments
WHERE is_authorized = 1 AND created_at >= NOW() - INTERVAL 7 DAY
GROUP BY forum_topic_id
) AS d7 ON d7.forum_topic_id = ft.id
ORDER BY lftc.created_at DESC
LIMIT ?, ?
SQL, [$permissions, $offset, $count]);

Expand Down
24 changes: 12 additions & 12 deletions app/Community/Actions/BuildThinRecentForumPostsDataAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ public function execute(
): Collection {
$userClause = $this->buildUserClause($fromAuthorId, $permissions);

$subQuery = DB::table('ForumTopicComment as ftc')
$subQuery = DB::table('forum_topic_comments as ftc')
->select('*')
->whereRaw($userClause)
->orderBy('ftc.DateCreated', 'desc')
->orderBy('ftc.created_at', 'desc')
->limit($limit + 20); // cater for spam messages

$latestComments = DB::table(DB::raw("({$subQuery->toSql()}) as LatestComments"))
->mergeBindings($subQuery)
->join('ForumTopic as ft', 'ft.ID', '=', 'LatestComments.ForumTopicID')
->leftJoin('Forum as f', 'f.ID', '=', 'ft.ForumID')
->join('forum_topics as ft', 'ft.id', '=', 'LatestComments.forum_topic_id')
->leftJoin('forums as f', 'f.id', '=', 'ft.forum_id')
->leftJoin('UserAccounts as ua', 'ua.ID', '=', 'LatestComments.author_id')
->select([
'LatestComments.DateCreated as PostedAt',
'LatestComments.Payload',
'LatestComments.created_at as PostedAt',
'LatestComments.body as Payload',
'ua.User as Author',
'ua.display_name as AuthorDisplayName',
'ua.RAPoints',
'ua.Motto',
'ft.ID as ForumTopicID',
'ft.Title as ForumTopicTitle',
'ft.id as ForumTopicID',
'ft.title as ForumTopicTitle',
'LatestComments.author_id as author_id',
'LatestComments.ID as CommentID',
])
->where('ft.RequiredPermissions', '<=', $permissions ?? Permissions::Unregistered)
->where('ft.required_permissions', '<=', $permissions ?? Permissions::Unregistered)
->whereNull('ft.deleted_at')
->orderBy('LatestComments.DateCreated', 'desc')
->orderBy('LatestComments.created_at', 'desc')
->limit($limit)
->get();

Expand All @@ -62,12 +62,12 @@ public function execute(
private function buildUserClause(?int $fromAuthorId, ?int $permissions): string
{
if (empty($fromAuthorId)) {
return 'ftc.Authorised = 1';
return 'ftc.is_authorized = 1';
}

$clause = 'ftc.author_id = ?';
if ($permissions < Permissions::Moderator) {
$clause .= ' AND ftc.Authorised = 1';
$clause .= ' AND ftc.is_authorized = 1';
}

return $clause;
Expand Down
4 changes: 2 additions & 2 deletions app/Community/Actions/GenerateAnnualRecapAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ private function summarizePosts(User $user, Carbon $startDate, Carbon $endDate):
{
$numForumPosts = (!$user->forum_verified_at) ? 0 :
ForumTopicComment::where('author_id', $user->id)
->where('DateCreated', '>=', $startDate)
->where('DateCreated', '<', $endDate)
->where('created_at', '>=', $startDate)
->where('created_at', '<', $endDate)
->count();
$numComments = Comment::where('user_id', $user->id)
->whereIn('ArticleType', [ArticleType::Game, ArticleType::Achievement, ArticleType::Leaderboard])
Expand Down
3 changes: 0 additions & 3 deletions app/Community/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Community;

use App\Community\Commands\ConvertUserShortcodesToUseIds;
use App\Community\Commands\GenerateAnnualRecap;
use App\Community\Commands\SyncComments;
use App\Community\Commands\SyncForumCategories;
Expand Down Expand Up @@ -55,8 +54,6 @@ public function boot(): void
SyncTickets::class,
SyncUserRelations::class,

ConvertUserShortcodesToUseIds::class,

GenerateAnnualRecap::class,
]);
}
Expand Down
Loading

0 comments on commit 4a96cf5

Please sign in to comment.