Skip to content

Commit

Permalink
Merge branch 'master' into feature/player_game_activity
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamiras authored Mar 26, 2024
2 parents 898bde4 + a6be61b commit 5fbd0f0
Show file tree
Hide file tree
Showing 30 changed files with 948 additions and 248 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');
}
}
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
4 changes: 2 additions & 2 deletions app/Helpers/database/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function performSearch(

if ($articleTypes !== []) {
$counts[] = "SELECT COUNT(*) AS Count FROM Comment AS c
LEFT JOIN UserAccounts AS cua ON cua.ID=c.UserID
LEFT JOIN UserAccounts AS cua ON cua.ID=c.user_id
LEFT JOIN UserAccounts AS ua ON ua.ID=c.ArticleID AND c.articletype=" . ArticleType::User . "
WHERE c.Payload LIKE '%$searchQuery%'
AND cua.User != 'Server' AND c.articletype IN (" . implode(',', $articleTypes) . ")
Expand Down Expand Up @@ -173,7 +173,7 @@ function performSearch(
ELSE CONCAT( '...', MID( c.Payload, GREATEST( LOCATE('$searchQuery', c.Payload)-25, 1), 60 ), '...' )
END AS Title
FROM Comment AS c
LEFT JOIN UserAccounts AS cua ON cua.ID=c.UserID
LEFT JOIN UserAccounts AS cua ON cua.ID=c.user_id
LEFT JOIN UserAccounts AS ua ON ua.ID=c.ArticleID AND c.articletype in (" . ArticleType::User . "," . ArticleType::UserModeration . ")
WHERE c.Payload LIKE '%$searchQuery%'
AND cua.User != 'Server' AND c.articletype IN (" . implode(',', $articleTypes) . ")
Expand Down
87 changes: 46 additions & 41 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 Expand Up @@ -237,7 +242,7 @@ function getSubscribersOfArticle(
$qry = "
SELECT DISTINCT _ua.*
FROM Comment AS _c
INNER JOIN UserAccounts as _ua ON _ua.ID = _c.UserID
INNER JOIN UserAccounts as _ua ON _ua.ID = _c.user_id
WHERE _c.ArticleType = $articleType
AND _c.ArticleID = $articleID
$websitePrefsFilter
Expand Down Expand Up @@ -284,11 +289,11 @@ function isUserSubscribedToArticleComments(int $articleType, int $articleID, int
SELECT DISTINCT ua.*
FROM
Comment AS c
LEFT JOIN UserAccounts AS ua ON ua.ID = c.UserID
LEFT JOIN UserAccounts AS ua ON ua.ID = c.user_id
WHERE
c.ArticleType = $articleType
AND c.ArticleID = $articleID
AND c.UserID = $userID
AND c.user_id = $userID
"
);
}
Loading

0 comments on commit 5fbd0f0

Please sign in to comment.