Skip to content

Commit 573efa9

Browse files
authored
Update the quote API implementation (#223)
* Reintroduce the quote handler * Add a `canRead()` method for conversation messages This is now in line with the other implementations that also offload the logic to a separate `canRead()` method. This is also quite cheap to implement with the new collections, I like them a lot! :) * Simplify the code a bit * Add helper methods to simplify working with leftAt/joinedAt This avoids having to explicitly deal with the magic `0` value.
1 parent b918af9 commit 573efa9

File tree

5 files changed

+85
-1
lines changed

5 files changed

+85
-1
lines changed

files/lib/data/conversation/message/ConversationMessage.class.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,26 @@ public function getUserProfile(): UserProfile
225225
{
226226
return $this->getCollection()->getUserProfile($this);
227227
}
228+
229+
/**
230+
* Checks if the current user can read this particular message.
231+
*/
232+
public function canRead(): bool
233+
{
234+
$conversation = $this->getConversation();
235+
if ($conversation === null) {
236+
return false;
237+
}
238+
239+
$participant = $conversation->getParticipant();
240+
if ($participant === null) {
241+
return false;
242+
}
243+
244+
if ($participant->hasJoinedAfter($this->time) || $participant->hasLeftBefore($this->time)) {
245+
return false;
246+
}
247+
248+
return true;
249+
}
228250
}

files/lib/data/conversation/participant/ConversationParticipant.class.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,32 @@ class ConversationParticipant extends DatabaseObject
3737
*/
3838
protected static $databaseTableIndexName = 'conversationParticipantID';
3939

40+
/**
41+
* Returns true if the user has joined the conversation after the provided
42+
* timestamp.
43+
*/
44+
public function hasJoinedAfter(int $timestamp): bool
45+
{
46+
if ($this->joinedAt === 0) {
47+
return false;
48+
}
49+
50+
return $this->joinedAt > $timestamp;
51+
}
52+
53+
/**
54+
* Returns true if the user has left the conversation before the provided
55+
* timestamp.
56+
*/
57+
public function hasLeftBefore(int $timestamp): bool
58+
{
59+
if ($this->leftAt === 0) {
60+
return false;
61+
}
62+
63+
return $this->leftAt < $timestamp;
64+
}
65+
4066
public static function getParticipant(int $conversationID, int $userID): ?static
4167
{
4268
$sql = "SELECT * FROM wcf1_conversation_to_user WHERE conversationID = ? AND userID = ?";
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace wcf\system\message\quote;
4+
5+
use wcf\data\conversation\message\ConversationMessage;
6+
use wcf\data\IMessage;
7+
8+
/**
9+
* IMessageQuoteHandler implementation for conversation messages.
10+
*
11+
* @author Alexander Ebert
12+
* @copyright 2001-2025 WoltLab GmbH
13+
* @license WoltLab License <http://www.woltlab.com/license-agreement.html>
14+
*/
15+
final class ConversationMessageQuoteHandler extends AbstractMessageQuoteHandler
16+
{
17+
#[\Override]
18+
public function getMessage(int $objectID): ?IMessage
19+
{
20+
$message = new ConversationMessage($objectID);
21+
if (!$message->messageID) {
22+
return null;
23+
}
24+
25+
if (!$message->canRead()) {
26+
return null;
27+
}
28+
29+
if ($message->hasEmbeddedObjects) {
30+
$message->getCollection()->loadEmbeddedObjects();
31+
}
32+
33+
return $message;
34+
}
35+
}

objectType.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<type>
3333
<name>com.woltlab.wcf.conversation.message</name>
3434
<definitionname>com.woltlab.wcf.message.quote</definitionname>
35+
<classname>wcf\system\message\quote\ConversationMessageQuoteHandler</classname>
3536
</type>
3637
<type>
3738
<name>com.woltlab.wcf.conversation.message</name>

templates/conversation.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
], ({ UiConversationMessageInlineEditor }, { registerContainer }, { getParticipantList }) => {
8585
new UiConversationMessageInlineEditor({$conversation->conversationID});
8686
87-
registerContainer(".message", ".messageBody", "wcf\\data\\conversation\\message\\ConversationMessage", "com.woltlab.wcf.conversation.message");
87+
registerContainer(".message", ".messageBody", "com.woltlab.wcf.conversation.message");
8888
8989
const contextMenu = document.getElementById('{unsafe:$interactionContextMenu->getContainerID()|encodeJS}')
9090
console.log(contextMenu);

0 commit comments

Comments
 (0)