Skip to content

Commit

Permalink
Update spam measures.
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismeonmounteverest committed Sep 8, 2024
1 parent eb95b12 commit 51bf68a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 36 deletions.
65 changes: 31 additions & 34 deletions src/Controller/Admin/CheckerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CheckerController extends AbstractController
private const MESSAGES_REPORTED = 1;
private const MESSAGES_PROCESSED = 2;
private const MESSAGES_BLOCK_WORDS = 3;
private const MESSAGES_BLOCK_WORDS_PROCESSED = 4;

private CheckerModel $checkerModel;
private EntityManagerInterface $entityManager;
Expand All @@ -39,48 +40,40 @@ public function __construct(CheckerModel $checkerModel, EntityManagerInterface $

/**
* @Route("/admin/spam/messages", name="admin_spam_messages")
*
* @throws AccessDeniedException
*
* @return Response
*/
public function showReportedMessages(Request $request)
public function showReportedMessages(Request $request): Response
{
return $this->handleMessages($request, self::MESSAGES_REPORTED);
}

/**
* @Route("/admin/spam/messages/processed", name="admin_spam_messages_processed")
*/
public function showProcessedMessages(Request $request): Response
{
return $this->handleMessages($request, self::MESSAGES_PROCESSED);
}

/**
* @Route("/admin/spam/messages/blocked", name="admin_spam_messages_block_words")
*
* @throws AccessDeniedException
*
* @return Response
*/
public function showBlockWordMessages(Request $request)
public function showBlockWordMessages(Request $request): Response
{
return $this->handleMessages($request, self::MESSAGES_BLOCK_WORDS);
}

/**
* @Route("/admin/spam/messages/processed", name="admin_spam_messages_processed")
*
* @throws AccessDeniedException
*
* @return Response
* @Route("/admin/spam/messages/blocked/processed", name="admin_spam_messages_block_words_processed")
*/
public function showProcessedMessages(Request $request)
public function showProcessedBlockWordMessages(Request $request): Response
{
return $this->handleMessages($request, self::MESSAGES_PROCESSED);
return $this->handleMessages($request, self::MESSAGES_BLOCK_WORDS_PROCESSED);
}

/**
* @Route("/admin/spam/activities", name="admin_spam_activities")
*
* @throws AccessDeniedException
*
* @return Response
*/
public function showActivities(Request $request, ActivityModel $activitiesModel)
public function showActivities(Request $request, ActivityModel $activitiesModel): Response
{
if (
!$this->isGranted(Member::ROLE_ADMIN_CHECKER)
Expand Down Expand Up @@ -123,12 +116,8 @@ public function showActivities(Request $request, ActivityModel $activitiesModel)

/**
* @Route("/admin/spam/communitynews", name="admin_spam_community_news")
*
* @throws AccessDeniedException
*
* @return Response
*/
public function showCommunityNewsComments(Request $request, CommunityNewsModel $communityNewsModel)
public function showCommunityNewsComments(Request $request, CommunityNewsModel $communityNewsModel): Response
{
if (
!$this->isGranted(Member::ROLE_ADMIN_CHECKER)
Expand Down Expand Up @@ -172,26 +161,30 @@ public function showCommunityNewsComments(Request $request, CommunityNewsModel $
/**
* @Route("/admin/spam", name="admin_spam")
*/
public function redirectToSpamMessages()
public function redirectToSpamMessages(): RedirectResponse
{
return new RedirectResponse($this->generateUrl('admin_spam_messages'));
}

private function getSubmenuItems()
private function getSubmenuItems(): array
{
return [
'messages' => [
'key' => 'reported.messages',
'url' => $this->generateUrl('admin_spam_messages'),
],
'blockwords' => [
'key' => 'block.words.messages',
'url' => $this->generateUrl('admin_spam_messages_block_words'),
],
'processed_messages' => [
'key' => 'reported.messages.processed',
'url' => $this->generateUrl('admin_spam_messages_processed'),
],
'blocked_words' => [
'key' => 'block.words.messages',
'url' => $this->generateUrl('admin_spam_messages_block_words'),
],
'processed_blocked_words' => [
'key' => 'block.words.processed',
'url' => $this->generateUrl('admin_spam_messages_block_words_processed'),
],
'activities' => [
'key' => 'activities',
'url' => $this->generateUrl('admin_spam_activities'),
Expand Down Expand Up @@ -227,9 +220,13 @@ private function handleMessages(Request $request, int $type): Response
$messages = $this->checkerModel->getProcessedReportedMessages($page, $limit);
break;
case self::MESSAGES_BLOCK_WORDS:
$active = 'block.words.messages';
$active = 'blocked_words';
$messages = $this->checkerModel->getBlockWordsMessages($page, $limit);
break;
case self::MESSAGES_BLOCK_WORDS_PROCESSED:
$active = 'processed_blocked_words';
$messages = $this->checkerModel->getProcessedBlockWordsMessages($page, $limit);
break;
default:
throw new InvalidArgumentException();
}
Expand Down
39 changes: 38 additions & 1 deletion src/Model/Admin/CheckerModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
use App\Doctrine\SpamInfoType;
use App\Entity\Message;
use App\Repository\MessageRepository;
use App\Service\Mailer;
use Doctrine\ORM\EntityManagerInterface;
use Pagerfanta\Pagerfanta;

class CheckerModel
{
private EntityManagerInterface $entityManager;
private Mailer $mailer;

public function __construct(EntityManagerInterface $entityManager)
public function __construct(EntityManagerInterface $entityManager, Mailer $mailer)
{
$this->entityManager = $entityManager;
$this->mailer = $mailer;
}

public function markAsSpamByChecker(array $messageIds): void
Expand Down Expand Up @@ -56,6 +59,7 @@ public function unmarkAsSpamByChecker(array $messageIds): void
$message->setStatus(MessageStatusType::CHECKED);
if (strpos($message->getSpamInfo(), SpamInfoType::SPAM_BLOCKED_WORD) !== false) {
$message->setFolder(InFolderType::NORMAL);
$this->sendNotification($message);
}
}
$this->entityManager->persist($message);
Expand Down Expand Up @@ -86,4 +90,37 @@ public function getBlockWordsMessages(int $page = 1, int $limit = 10): Pagerfant

return $repository->findBlockWordsMessages($page, $limit);
}

public function getProcessedBlockWordsMessages(int $page = 1, int $limit = 10): Pagerfanta
{
/** @var MessageRepository $repository */
$repository = $this->entityManager->getRepository(Message::class);

return $repository->findProcessedBlockWordsMessages($page, $limit);
}

private function sendNotification(Message $message): void
{
// Is this a message or a request?
if (null === $message->getRequest()) {
$this->mailer->sendMessageNotificationEmail(
$message->getSender(),
$message->getReceiver(),
'message',
[
'message' => $message,
'subject' => $message->getSubject()->getSubject(),
'body' => $message->getMessage(),
]
);
} else {
$this->mailer->sendMessageNotificationEmail($message->getSender(), $message->getReceiver(), 'request', [
'host' => $message->getReceiver(),
'subject' => $message->getSubject()->getSubject(),
'message' => $message,
'request' => $message->getRequest(),
'changed' => false,
]);
}
}
}
42 changes: 42 additions & 0 deletions src/Repository/MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Doctrine\DeleteRequestType;
use App\Doctrine\InFolderType;
use App\Doctrine\MemberStatusType;
use App\Doctrine\MessageResultSetMapping;
use App\Doctrine\MessageStatusType;
use App\Doctrine\SpamInfoType;
Expand Down Expand Up @@ -137,6 +138,23 @@ public function findBlockWordsMessages($page = 1, $items = 10): Pagerfanta
return $paginator;
}

/**
* Returns a Pagerfanta object encapsulating the matching paginated processed reported messages.
*
* @param mixed $page
* @param mixed $items
*/
public function findProcessedBlockWordsMessages($page = 1, $items = 10): Pagerfanta
{
$queryBuilder = $this->queryProcessedBlockWordsMessages();
$adapter = new QueryAdapter($queryBuilder);
$paginator = new Pagerfanta($adapter);
$paginator->setMaxPerPage($items);
$paginator->setCurrentPage($page);

return $paginator;
}

public function getThread(Message $message)
{
$qb = $this->createNativeNamedQuery('get_thread')
Expand Down Expand Up @@ -354,10 +372,34 @@ private function queryBlockWordsMessages(): QueryBuilder
{
$qb = $this->createQueryBuilder('m');
$qb
->join('App:Member', 'mb', Join::WITH, 'm.sender = mb.id')
->where('m.status = :status')
->setParameter('status', MessageStatusType::CHECK)
->andWhere('m.spamInfo LIKE :spamInfo')
->setParameter('spamInfo', '%' . SpamInfoType::SPAM_BLOCKED_WORD . '%')
->andWhere($qb->expr()->eq('mb.status', ':active'))
->setParameter('active', MemberStatusType::ACTIVE)
->orderBy('m.created', 'DESC')
;

return $qb;
}

private function queryProcessedBlockWordsMessages(): QueryBuilder
{
$qb = $this->createQueryBuilder('m');
$qb
->join('App:Member', 'mb', Join::WITH, 'm.sender = mb.id')
->where('m.spamInfo LIKE :spamInfo')
->setParameter('spamInfo', '%' . SpamInfoType::SPAM_BLOCKED_WORD . '%')
->andWhere(
$qb->expr()->orX(
$qb->expr()->eq('m.status', ':status'),
$qb->expr()->eq('mb.status', ':banned'),
)
)
->setParameter('status', MessageStatusType::CHECKED)
->setParameter('banned', MemberStatusType::BANNED)
->orderBy('m.created', 'DESC')
;

Expand Down
2 changes: 1 addition & 1 deletion templates/admin/checker/messages.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<tr>
<td>{{ macros.avatarstack(message.Sender.Username, 30) }}<br>
<small class="text-muted">{{ ('memberstatus' ~ message.Sender.Status)|lower|trans }}</small></td>
<td><strong>{{ message.subject.subject }}</strong><br>
<td><strong>{{ message.subject.subject }}</strong>&nbsp;{% if message.request is null %}<i class="fa fa-envelope"></i>{% else %}<i class="fa fa-bed"></i>{% endif %}<br>
{{ message.Message|purify }}</td>
<td>{{ macros.avatarstack(message.Receiver.Username, 30) }}<br>
<small class="text-muted">{{ ('memberstatus' ~ message.Receiver.Status)|lower|trans }}</small></td>
Expand Down
5 changes: 5 additions & 0 deletions translations/missing/checker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ block.words.messages:
- 'Menu item in Checker Volunteer tool. (Do not translate)'
- 'messages+intl-icu'
- 'no'
block.words.processed:
- 'Processed Blocked Notifications'
- 'Menu item in Checker Volunteer tool. (Do not translate)'
- 'messages+intl-icu'
- 'no'

0 comments on commit 51bf68a

Please sign in to comment.