Commit b9d748b9 authored by Emma's avatar Emma 🦉

optimise moderator check for performance

parent 23f3917e
Pipeline #55208134 passed with stages
in 10 minutes and 35 seconds
......@@ -304,8 +304,7 @@ final class ForumController extends AbstractController {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$forum->addModerator($data->toModerator());
$em->persist($data->toModerator());
$em->flush();
$this->addFlash('success', 'flash.forum_moderator_added');
......
......@@ -241,10 +241,14 @@ class Forum {
return true;
}
// optimised to significantly lessen the number of SQL queries performed
// when logged in as the user being checked.
$user->getModeratorTokens()->get(-1);
$criteria = Criteria::create()
->where(Criteria::expr()->eq('user', $user));
->where(Criteria::expr()->eq('forum', $this));
return count($this->moderators->matching($criteria)) > 0;
return count($user->getModeratorTokens()->matching($criteria)) > 0;
}
public function addModerator(Moderator $moderator) {
......
......@@ -4,6 +4,7 @@ namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
/**
* @ORM\Entity()
......@@ -16,7 +17,7 @@ class Moderator {
* @ORM\Column(type="uuid")
* @ORM\Id()
*
* @var Uuid
* @var UuidInterface
*/
private $id;
......@@ -48,10 +49,11 @@ class Moderator {
$this->forum = $forum;
$this->user = $user;
$this->timestamp = new \DateTime('@'.time());
$forum->addModerator($this);
$forum->getModerators()->add($this);
$user->getModeratorTokens()->add($this);
}
public function getId(): Uuid {
public function getId(): UuidInterface {
return $this->id;
}
......
......@@ -285,6 +285,7 @@ class User implements UserInterface, EquatableInterface {
$this->bans = new ArrayCollection();
$this->ipBans = new ArrayCollection();
$this->blocks = new ArrayCollection();
$this->moderatorTokens = new ArrayCollection();
}
public function getId(): ?int {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment