Skip to content

Commit

Permalink
Merge branch 'master' into bugfix/related_tickets
Browse files Browse the repository at this point in the history
  • Loading branch information
wescopeland authored Mar 28, 2024
2 parents ad9c135 + 47f4962 commit ff3aed6
Show file tree
Hide file tree
Showing 45 changed files with 1,270 additions and 523 deletions.
3 changes: 1 addition & 2 deletions app/Actions/ClearAccountDataAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ public function execute(User $user): void
DB::statement('DELETE FROM SetRequest WHERE User = :username', ['username' => $user->User]);
// TODO $user->badges()->delete();
DB::statement('DELETE FROM SiteAwards WHERE User = :username', ['username' => $user->User]);
// TODO $user->subscriptions()->delete();
DB::statement('DELETE FROM Subscription WHERE UserID = :userId', ['userId' => $user->ID]);
$user->subscriptions()->delete();

// use action to delete each participation so threads with no remaing active participants get cleaned up
$deleteMessageThreadAction = new DeleteMessageThreadAction();
Expand Down
9 changes: 9 additions & 0 deletions app/Community/Concerns/ActsAsCommunityMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Community\Enums\UserRelationship;
use App\Models\ForumTopicComment;
use App\Models\MessageThreadParticipant;
use App\Models\Subscription;
use App\Models\User;
use App\Models\UserActivity;
use App\Models\UserComment;
Expand Down Expand Up @@ -124,4 +125,12 @@ public function forumPosts(): HasMany
{
return $this->hasMany(ForumTopicComment::class, 'AuthorID', 'ID');
}

/**
* @return HasMany<Subscription>
*/
public function subscriptions(): HasMany
{
return $this->hasMany(Subscription::class, 'user_id', 'ID');
}
}
12 changes: 12 additions & 0 deletions app/Enums/PlayerGameActivityEventType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace App\Enums;

abstract class PlayerGameActivityEventType
{
public const Unlock = 'unlock';

public const RichPresence = 'rich-presence';
}
14 changes: 14 additions & 0 deletions app/Enums/PlayerGameActivitySessionType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace App\Enums;

abstract class PlayerGameActivitySessionType
{
public const Player = 'player-session';

public const Generated = 'generated';

public const ManualUnlock = 'manual-unlock';
}
10 changes: 5 additions & 5 deletions app/Helpers/database/forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function getForumTopics(int $forumID, int $offset, int $count, int $permissions,
$maxCountOut = (int) $data['COUNT(*)'];
}

$query = " SELECT f.Title AS ForumTitle, ft.ID AS ForumTopicID, ft.Title AS TopicTitle, LEFT( ftc2.Payload, 54 ) AS TopicPreview, ft.Author, ft.AuthorID, ft.DateCreated AS ForumTopicPostedDate, ftc.ID AS LatestCommentID, ftc.Author AS LatestCommentAuthor, ftc.AuthorID AS LatestCommentAuthorID, ftc.DateCreated AS LatestCommentPostedDate, (COUNT(ftc2.ID)-1) AS NumTopicReplies
$query = " SELECT f.Title AS ForumTitle, ft.ID AS ForumTopicID, ft.Title AS TopicTitle, LEFT( ftc2.Payload, 54 ) AS TopicPreview, ft.Author, ft.author_id AS AuthorID, ft.DateCreated AS ForumTopicPostedDate, ftc.ID AS LatestCommentID, ftc.Author AS LatestCommentAuthor, ftc.AuthorID AS LatestCommentAuthorID, ftc.DateCreated AS LatestCommentPostedDate, (COUNT(ftc2.ID)-1) AS NumTopicReplies
FROM ForumTopic AS ft
LEFT JOIN ForumTopicComment AS ftc ON ftc.ID = ft.LatestCommentID
LEFT JOIN Forum AS f ON f.ID = ft.ForumID
Expand Down Expand Up @@ -90,7 +90,7 @@ function getForumTopics(int $forumID, int $offset, int $count, int $permissions,

function getUnauthorisedForumLinks(): ?array
{
$query = " SELECT f.Title AS ForumTitle, ft.ID AS ForumTopicID, ft.Title AS TopicTitle, LEFT( ftc2.Payload, 60 ) AS TopicPreview, ft.Author, ft.AuthorID, ft.DateCreated AS ForumTopicPostedDate, ftc.ID AS LatestCommentID, ftc.Author AS LatestCommentAuthor, ftc.AuthorID AS LatestCommentAuthorID, ftc.DateCreated AS LatestCommentPostedDate, (COUNT(ftc2.ID)-1) AS NumTopicReplies
$query = " SELECT f.Title AS ForumTitle, ft.ID AS ForumTopicID, ft.Title AS TopicTitle, LEFT( ftc2.Payload, 60 ) AS TopicPreview, ft.Author, ft.author_id AS AuthorID, ft.DateCreated AS ForumTopicPostedDate, ftc.ID AS LatestCommentID, ftc.Author AS LatestCommentAuthor, ftc.AuthorID AS LatestCommentAuthorID, ftc.DateCreated AS LatestCommentPostedDate, (COUNT(ftc2.ID)-1) AS NumTopicReplies
FROM ForumTopic AS ft
LEFT JOIN ForumTopicComment AS ftc ON ftc.ForumTopicID = ft.ID
LEFT JOIN Forum AS f ON f.ID = ft.ForumID
Expand Down Expand Up @@ -118,7 +118,7 @@ function getUnauthorisedForumLinks(): ?array

function getTopicDetails(int $topicID, ?array &$topicDataOut = []): bool
{
$query = " SELECT ft.ID, ft.Author, ft.AuthorID, fc.ID AS CategoryID, fc.Name AS Category, fc.ID as CategoryID, f.ID AS ForumID, f.Title AS Forum, ft.Title AS TopicTitle, ft.RequiredPermissions
$query = " SELECT ft.ID, ft.Author, ft.author_id AS AuthorID, fc.ID AS CategoryID, fc.Name AS Category, fc.ID as CategoryID, f.ID AS ForumID, f.Title AS Forum, ft.Title AS TopicTitle, ft.RequiredPermissions
FROM ForumTopic AS ft
LEFT JOIN Forum AS f ON f.ID = ft.ForumID
LEFT JOIN ForumCategory AS fc ON fc.ID = f.CategoryID
Expand All @@ -137,7 +137,7 @@ function getTopicDetails(int $topicID, ?array &$topicDataOut = []): bool

function getSingleTopicComment(int $forumPostID, ?array &$dataOut): bool
{
$query = " SELECT ID, ForumTopicID, Payload, Author, AuthorID, DateCreated, DateModified
$query = " SELECT ID, ForumTopicID, Payload, Author, author_id AS AuthorID, DateCreated, DateModified
FROM ForumTopicComment
WHERE ID=$forumPostID";

Expand Down Expand Up @@ -173,7 +173,7 @@ function submitNewTopic(

// $authFlags = getUserForumPostAuth( $user );

$query = "INSERT INTO ForumTopic (ForumID, Title, Author, AuthorID, DateCreated, LatestCommentID, RequiredPermissions) VALUES ( $forumID, '$topicTitle', '$user', $userID, NOW(), 0, 0 )";
$query = "INSERT INTO ForumTopic (ForumID, Title, Author, author_id, DateCreated, LatestCommentID, RequiredPermissions) VALUES ( $forumID, '$topicTitle', '$user', $userID, NOW(), 0, 0 )";

$db = getMysqliConnection();
if (!mysqli_query($db, $query)) {
Expand Down
81 changes: 43 additions & 38 deletions app/Helpers/database/subscription.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
<?php

use App\Community\Enums\SubscriptionSubjectType;
use App\Models\Subscription;

/**
* Update a subscription, i.e, either subscribe or unsubscribe a given user to or from a subject.
*
* @param bool $state whether the user is to be subscribed (true) or unsubscribed (false)
*/
function updateSubscription(string $subjectType, int $subjectID, int $userID, bool $state): bool
function updateSubscription(string $subjectType, int $subjectId, int $userId, bool $state): bool
{
sanitize_sql_inputs($subjectType);
$state = (int) $state;

$query = "
INSERT INTO Subscription(SubjectType, SubjectID, UserID, State)
VALUES ('$subjectType', $subjectID, $userID, '$state')
ON DUPLICATE KEY UPDATE State = '$state'
";

$dbResult = s_mysql_query($query);
Subscription::updateOrCreate(
[
'subject_type' => $subjectType,
'subject_id' => $subjectId,
'user_id' => $userId,
],
[
'state' => $state,
]
);

return (bool) $dbResult;
return true;
}

/**
* @deprecated $implicitSubscriptionQry considered harmful. Use Eloquent ORM.
*
* Checks whether a given user is subscribed to a subject, whether implicitly or explicitly.
*
* @param string|null $implicitSubscriptionQry optional sql query capable of identifying the existence of an implicit
Expand All @@ -39,12 +42,12 @@ function isUserSubscribedTo(string $subjectType, int $subjectID, int $userID, ?s
if ($implicitSubscriptionQry === null) {
$query = "
SELECT 1
FROM Subscription
FROM subscriptions
WHERE
SubjectType = '$subjectType'
AND SubjectID = $subjectID
AND UserID = $userID
AND State = 1
subject_type = '$subjectType'
AND subject_id = $subjectID
AND user_id = $userID
AND state = 1
";
} else {
// either there's an explicit subscription...
Expand All @@ -53,29 +56,29 @@ function isUserSubscribedTo(string $subjectType, int $subjectID, int $userID, ?s
// subscription to the subject (must be usable inside an EXISTS clause)
$query = "
SELECT 1
FROM Subscription
FROM subscriptions
WHERE
EXISTS (
SELECT 1
FROM Subscription
FROM subscriptions
WHERE
SubjectType = '$subjectType'
AND SubjectID = $subjectID
AND UserID = $userID
AND State = 1
subject_type = '$subjectType'
AND subject_id = $subjectID
AND user_id = $userID
AND state = 1
)
OR (
EXISTS (
$implicitSubscriptionQry
)
AND NOT EXISTS (
SELECT 1
FROM Subscription
FROM subscriptions
WHERE
SubjectType = '$subjectType'
AND SubjectID = $subjectID
AND UserID = $userID
AND State = 0
subject_type = '$subjectType'
AND subject_id = $subjectID
AND user_id = $userID
AND state = 0
)
)
";
Expand All @@ -93,6 +96,8 @@ function isUserSubscribedTo(string $subjectType, int $subjectID, int $userID, ?s
}

/**
* @deprecated $implicitSubscriptionQry considered harmful. Use Eloquent ORM.
*
* Retrieves the list of users that are subscribed to a given subject either implicitly or explicitly.
*
* @param ?int $reqWebsitePrefs optional required website preferences for a user to be considered a subscriber
Expand All @@ -112,13 +117,13 @@ function getSubscribersOf(string $subjectType, int $subjectID, ?int $reqWebsiteP
_ua.User,
_ua.EmailAddress
FROM
Subscription AS _sub
subscriptions AS _sub
INNER JOIN UserAccounts AS _ua
ON _ua.ID = _sub.UserID
ON _ua.ID = _sub.user_id
WHERE
_sub.SubjectType = '$subjectType'
AND _sub.SubjectID = $subjectID
AND _sub.State = 1
_sub.subject_type = '$subjectType'
AND _sub.subject_id = $subjectID
AND _sub.state = 1
$websitePrefsFilter
";

Expand All @@ -133,12 +138,12 @@ function getSubscribersOf(string $subjectType, int $subjectID, ?int $reqWebsiteP
(
$implicitSubscriptionQry
) as _ua
LEFT JOIN Subscription AS _sub
ON (_sub.SubjectType = '$subjectType'
AND _sub.SubjectID = $subjectID
AND _sub.UserID = _ua.ID)
LEFT JOIN subscriptions AS _sub
ON (_sub.subject_type = '$subjectType'
AND _sub.subject_id = $subjectID
AND _sub.user_id = _ua.ID)
WHERE
COALESCE(_sub.State, 1) = 1
COALESCE(_sub.state, 1) = 1
$websitePrefsFilter
UNION
$explicitSubscriptionQry
Expand Down
38 changes: 19 additions & 19 deletions app/Helpers/database/ticket.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ function _createTicket(User $user, int $achID, int $reportType, ?int $hardcore,
$userId = $user->ID;
$username = $user->User;

$query = "INSERT INTO Ticket (AchievementID, ReportedByUserID, ReportType, Hardcore, ReportNotes, ReportedAt, ResolvedAt, ResolvedByUserID )
$query = "INSERT INTO Ticket (AchievementID, reporter_id, ReportType, Hardcore, ReportNotes, ReportedAt, ResolvedAt, resolver_id )
VALUES($achID, $userId, $reportType, $hardcoreValue, \"$noteSanitized\", NOW(), NULL, NULL )";

$db = getMysqliConnection();
Expand Down Expand Up @@ -179,7 +179,7 @@ function _createTicket(User $user, int $achID, int $reportType, ?int $hardcore,
function getExistingTicketID(User $user, int $achievementID): int
{
$userID = $user->ID;
$query = "SELECT ID FROM Ticket WHERE ReportedByUserID=$userID AND AchievementID=$achievementID"
$query = "SELECT ID FROM Ticket WHERE reporter_id=$userID AND AchievementID=$achievementID"
. " AND ReportState NOT IN (" . TicketState::Closed . "," . TicketState::Resolved . ")";
$dbResult = s_mysql_query($query);
if ($dbResult) {
Expand Down Expand Up @@ -283,8 +283,8 @@ function getAllTickets(
LEFT JOIN Achievements AS ach ON ach.ID = tick.AchievementID
LEFT JOIN GameData AS gd ON gd.ID = ach.GameID
LEFT JOIN Console AS c ON c.ID = gd.ConsoleID
LEFT JOIN UserAccounts AS ua ON ua.ID = tick.ReportedByUserID
LEFT JOIN UserAccounts AS ua2 ON ua2.ID = tick.ResolvedByUserID
LEFT JOIN UserAccounts AS ua ON ua.ID = tick.reporter_id
LEFT JOIN UserAccounts AS ua2 ON ua2.ID = tick.resolver_id
$devJoin
WHERE $innerCond $achFlagCond $stateCond $modeCond $reportTypeCond $hashCond $emulatorCond $devActiveCond $notAuthorCond $notReporterCond $progressionCond
ORDER BY tick.ID DESC
Expand All @@ -302,8 +302,8 @@ function getTicket(int $ticketID): ?array
LEFT JOIN Achievements AS ach ON ach.ID = tick.AchievementID
LEFT JOIN GameData AS gd ON gd.ID = ach.GameID
LEFT JOIN Console AS c ON c.ID = gd.ConsoleID
LEFT JOIN UserAccounts AS ua ON ua.ID = tick.ReportedByUserID
LEFT JOIN UserAccounts AS ua2 ON ua2.ID = tick.ResolvedByUserID
LEFT JOIN UserAccounts AS ua ON ua.ID = tick.reporter_id
LEFT JOIN UserAccounts AS ua2 ON ua2.ID = tick.resolver_id
WHERE tick.ID = $ticketID
";

Expand All @@ -319,9 +319,9 @@ function updateTicket(string $user, int $ticketID, int $ticketVal, ?string $reas

$resolvedFields = "";
if ($ticketVal == TicketState::Resolved || $ticketVal == TicketState::Closed) {
$resolvedFields = ", ResolvedAt=NOW(), ResolvedByUserID=$userID ";
$resolvedFields = ", ResolvedAt=NOW(), resolver_id=$userID ";
} elseif ($ticketData['ReportState'] == TicketState::Resolved || $ticketData['ReportState'] == TicketState::Closed) {
$resolvedFields = ", ResolvedAt=NULL, ResolvedByUserID=NULL ";
$resolvedFields = ", ResolvedAt=NULL, resolver_id=NULL ";
}

$query = "UPDATE Ticket
Expand Down Expand Up @@ -411,7 +411,7 @@ function countRequestTicketsByUser(?User $user = null): int

return Cache::remember($cacheKey, Carbon::now()->addHours(20), function () use ($user) {
return Ticket::where('ReportState', TicketState::Request)
->where('ReportedByUserID', $user->ID)
->where('reporter_id', $user->ID)
->count();
});
}
Expand Down Expand Up @@ -525,14 +525,14 @@ function countOpenTickets(
$reporterJoin = "";
$notReporterCond = getResolvedByNonReporterCondition($ticketFilters);
if ($notReporterCond != "") {
$reporterJoin = "LEFT JOIN UserAccounts AS ua ON ua.ID = tick.ReportedByUserID";
$reporterJoin = "LEFT JOIN UserAccounts AS ua ON ua.ID = tick.reporter_id";
}

// Not Author condition - warning: excludes unresolved tickets
$resolverJoin = "";
$notAuthorCond = getResolvedByNonAuthorCondition($ticketFilters);
if ($notAuthorCond != "" || $notReporterCond != "") {
$resolverJoin = "LEFT JOIN UserAccounts AS ua2 ON ua2.ID = tick.ResolvedByUserID AND tick.ReportState IN (" . TicketState::Closed . "," . TicketState::Resolved . ")";
$resolverJoin = "LEFT JOIN UserAccounts AS ua2 ON ua2.ID = tick.resolver_id AND tick.ReportState IN (" . TicketState::Closed . "," . TicketState::Resolved . ")";
}

// Author condition
Expand All @@ -545,7 +545,7 @@ function countOpenTickets(
// Reporter condition
$reporterCond = "";
if ($reportedByUser != null) {
$reporterJoin = "LEFT JOIN UserAccounts AS ua ON ua.ID = tick.ReportedByUserID";
$reporterJoin = "LEFT JOIN UserAccounts AS ua ON ua.ID = tick.reporter_id";
$reporterCond = " AND ua.User = :reportedByUsername";
$bindings['reportedByUsername'] = $reportedByUser;
}
Expand All @@ -555,7 +555,7 @@ function countOpenTickets(
if ($resolvedByUser != null) {
$resolverCond = " AND ua2.User = :resolvedByUsername";
$bindings['resolvedByUsername'] = $resolvedByUser;
$resolverJoin = "LEFT JOIN UserAccounts AS ua2 ON ua2.ID = tick.ResolvedByUserID AND tick.ReportState IN (" . TicketState::Closed . "," . TicketState::Resolved . ")";
$resolverJoin = "LEFT JOIN UserAccounts AS ua2 ON ua2.ID = tick.resolver_id AND tick.ReportState IN (" . TicketState::Closed . "," . TicketState::Resolved . ")";
}

// Progression condition
Expand Down Expand Up @@ -931,11 +931,11 @@ function getUserWhoCreatedMostTickets(string $user): ?array

$query = "SELECT ua.User as TicketCreator, COUNT(*) as TicketCount
FROM Ticket AS t
LEFT JOIN UserAccounts as ua ON ua.ID = t.ReportedByUserID
LEFT JOIN UserAccounts as ua ON ua.ID = t.reporter_id
LEFT JOIN Achievements as a ON a.ID = t.AchievementID
WHERE a.Author = '$user'
AND t.ReportState != " . TicketState::Closed . "
GROUP BY t.ReportedByUserID
GROUP BY t.reporter_id
ORDER BY TicketCount DESC
LIMIT 1";

Expand All @@ -959,8 +959,8 @@ function getNumberOfTicketsClosedForOthers(string $user): array
SUM(CASE WHEN t.ReportState = " . TicketState::Closed . " THEN 1 ELSE 0 END) AS ClosedCount,
SUM(CASE WHEN t.ReportState = " . TicketState::Resolved . " THEN 1 ELSE 0 END) AS ResolvedCount
FROM Ticket AS t
LEFT JOIN UserAccounts as ua ON ua.ID = t.ReportedByUserID
LEFT JOIN UserAccounts as ua2 ON ua2.ID = t.ResolvedByUserID
LEFT JOIN UserAccounts as ua ON ua.ID = t.reporter_id
LEFT JOIN UserAccounts as ua2 ON ua2.ID = t.resolver_id
LEFT JOIN Achievements as a ON a.ID = t.AchievementID
WHERE t.ReportState IN (" . TicketState::Closed . "," . TicketState::Resolved . ")
AND ua.User NOT LIKE '$user'
Expand Down Expand Up @@ -992,8 +992,8 @@ function getNumberOfTicketsClosed(string $user): array
SUM(CASE WHEN t.ReportState = " . TicketState::Closed . " THEN 1 ELSE 0 END) AS ClosedCount,
SUM(CASE WHEN t.ReportState = " . TicketState::Resolved . " THEN 1 ELSE 0 END) AS ResolvedCount
FROM Ticket AS t
LEFT JOIN UserAccounts as ua ON ua.ID = t.ReportedByUserID
LEFT JOIN UserAccounts as ua2 ON ua2.ID = t.ResolvedByUserID
LEFT JOIN UserAccounts as ua ON ua.ID = t.reporter_id
LEFT JOIN UserAccounts as ua2 ON ua2.ID = t.resolver_id
LEFT JOIN Achievements as a ON a.ID = t.AchievementID
WHERE t.ReportState IN (" . TicketState::Closed . "," . TicketState::Resolved . ")
AND ua.User NOT LIKE '$user'
Expand Down
Loading

0 comments on commit ff3aed6

Please sign in to comment.