From 22699f1ed5f1abda605f9b74785ecbe70d2d1824 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 30 Oct 2014 10:19:08 +0000 Subject: [PATCH] Add "threadGetMessagesAlter" event --- .../libs/classes/Mibew/EventDispatcher/Events.php | 13 +++++++++++++ src/mibew/libs/classes/Mibew/Thread.php | 14 ++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php b/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php index 3231504f..93b618d8 100644 --- a/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php +++ b/src/mibew/libs/classes/Mibew/EventDispatcher/Events.php @@ -373,6 +373,19 @@ final class Events */ const THREAD_POST_MESSAGE = 'threadPostMessage'; + /** + * Related with a thread messages are loaded. + * + * This event is triggered after messages related with a thread are loaded. + * It provides an ability for plugins to alter messages set. An associative + * array with the following items is passed to the event handlers: + * - "thread": an instance of {@link \Mibew\Thread}. + * - "messages": array, list of messages. Each message is an associative + * array. See {@link \Mibew\Thread::getMessages()} return value for + * details of its structure. + */ + const THREAD_GET_MESSAGES_ALTER = 'threadGetMessagesAlter'; + /** * Threads list is ready to be sent to client. * diff --git a/src/mibew/libs/classes/Mibew/Thread.php b/src/mibew/libs/classes/Mibew/Thread.php index 3ab4a37e..989664f1 100644 --- a/src/mibew/libs/classes/Mibew/Thread.php +++ b/src/mibew/libs/classes/Mibew/Thread.php @@ -846,14 +846,24 @@ class Thread } else { $messages[$key]['data'] = array(); } + } - // Get last message ID + // Trigger the "alter" event + $args = array( + 'messages' => $messages, + 'thread' => $this, + ); + EventDispatcher::getInstance()->triggerEvent(Events::THREAD_GET_MESSAGES_ALTER, $args); + $altered_messages = $args['messages']; + + // Get ID of the last message + foreach ($altered_messages as $msg) { if ($msg['id'] > $last_id) { $last_id = $msg['id']; } } - return $messages; + return $altered_messages; } /**