-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathRebuildConversation.class.php
More file actions
61 lines (51 loc) · 1.65 KB
/
RebuildConversation.class.php
File metadata and controls
61 lines (51 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace wcf\command\conversation;
use wcf\data\conversation\Conversation;
use wcf\data\conversation\ConversationEditor;
use wcf\system\WCF;
/**
* Rebuilds the conversation data.
*
* @author Olaf Braun
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.3
*/
final class RebuildConversation
{
public function __construct(
private readonly Conversation $conversation
) {}
public function __invoke(): void
{
['messages' => $messages, 'attachments' => $attachments] = $this->getConversationStats($this->conversation->conversationID);
$editor = new ConversationEditor($this->conversation);
if ($messages === 0) {
$editor->delete();
return;
}
$editor->update([
'attachments' => $attachments,
'replies' => $messages - 1,
]);
$editor->updateFirstMessage();
$editor->updateLastMessage();
}
/**
* @return array{attachments: int, messages: int}
*/
private function getConversationStats(int $conversationID): array
{
$sql = "SELECT COUNT(messageID) AS messages,
COALESCE(SUM(attachments), 0) AS attachments
FROM wcf1_conversation_message
WHERE conversationID = ?";
$statement = WCF::getDB()->prepare($sql);
$statement->execute([$conversationID]);
$row = $statement->fetchArray();
return [
'messages' => $row['messages'],
'attachments' => $row['attachments'],
];
}
}