Skip to content

Internal: Add missing sender entries in message_rel_user migration - refs BT#21887 #5700

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20240731120000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a single space around assignment operators


/* For licensing terms, see /license.txt */

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Entity\MessageRelUser;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20240731120000 extends AbstractMigration

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing class doc comment

{
public function getDescription(): string

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function doc comment

{
return 'Add entries in message_rel_user for the sender during migration and update existing messages.';
}

public function up(Schema $schema): void

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function doc comment

{
$senderType = MessageRelUser::TYPE_SENDER;

// Add entries for the sender in message_rel_user
$this->addSql("
INSERT INTO message_rel_user (message_id, user_id, receiver_type, msg_read, starred)
SELECT m.id, m.user_sender_id, $senderType, false, false
FROM message m
LEFT JOIN message_rel_user mru
ON m.id = mru.message_id
AND m.user_sender_id = mru.user_id
WHERE mru.id IS NULL
");

// Update message status based on message_rel_user entries
$this->addSql("
UPDATE message m
LEFT JOIN (
SELECT message_id, COUNT(*) AS rel_count
FROM message_rel_user
WHERE receiver_type = 1
GROUP BY message_id
) AS mru ON m.id = mru.message_id
SET m.status = CASE
WHEN mru.rel_count IS NULL THEN 3 -- Message::MESSAGE_STATUS_DELETED
ELSE 0 -- Set to 0 or whatever the default status should be
END
");
}

public function down(Schema $schema): void

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing function doc comment

{
$senderType = MessageRelUser::TYPE_SENDER;

// Remove the entries added during the migration
$this->addSql("
DELETE FROM message_rel_user
WHERE receiver_type = $senderType
");
}
}
21 changes: 16 additions & 5 deletions src/CoreBundle/State/MessageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,22 @@ public function process($data, Operation $operation, array $uriVariables = [], a
if (!$user) {
throw new \LogicException('User not found.');
}
$messageRelUser = new MessageRelUser();
$messageRelUser->setMessage($message);
$messageRelUser->setReceiver($user);
$messageRelUser->setReceiverType(MessageRelUser::TYPE_SENDER);
$this->entityManager->persist($messageRelUser);

// Check if the relationship already exists
$messageRelUserRepository = $this->entityManager->getRepository(MessageRelUser::class);
$existingRelation = $messageRelUserRepository->findOneBy([
'message' => $message,
'receiver' => $user,
'receiverType' => MessageRelUser::TYPE_SENDER
]);

if (!$existingRelation) {
$messageRelUser = new MessageRelUser();
$messageRelUser->setMessage($message);
$messageRelUser->setReceiver($user);
$messageRelUser->setReceiverType(MessageRelUser::TYPE_SENDER);
$this->entityManager->persist($messageRelUser);
}

if ($message->getMsgType() === Message::MESSAGE_TYPE_INBOX) {
$this->saveNotificationForInboxMessage($message);
Expand Down
Loading