Repository: freescout-help-desk/freescout (language: php)

Issue:
Add old conversation context to `conversation.state_changed` event
PHP version: 8.2.20
FreeScout version: 1.8.139
Database: MySQL / ~~PostgreSQL~~
Are you using CloudFlare: ~~Yes~~ / No
Are you using non-official modules: Yes / ~~No~~

Hello!

I'm currently writing a plugin that is supposed to show changes to the subject of a conversation, in a similar fashion to status changes. My initial scouring through the Freescout codebase led me to the `conversation.state_changed` event, however that one is crucially missing the old conversation state that I could use to get the old subject.

So my humble request would be to add the old conversation context to that event, or some other way to get the old subject in that event. (If I'm doing this completely wrong to begin with, then please direct me towards the correct way!)


Discussion / hints:
You can submit a pull request with the changes needed.

Fix patch:
diff --git a/app/Conversation.php b/app/Conversation.php
index dbf3a3a8b..53dbdd0c2 100644
--- a/app/Conversation.php
+++ b/app/Conversation.php
@@ -1767,6 +1767,16 @@ public static function getViewersInfo($conversations, $fields = ['id', 'first_na
         return $viewers;
     }
 
+    public function changeSubject($new_subject, $user = null)
+    {
+        $prev_subject = $this->subject;
+
+        $this->subject = $new_subject;
+        $this->save();
+
+        \Eventy::action('conversation.subject_changed', $this, $user, $prev_subject);
+    }
+
     public function changeState($new_state, $user = null)
     {
         if (!array_key_exists($new_state, self::$states)) {
diff --git a/app/Http/Controllers/ConversationsController.php b/app/Http/Controllers/ConversationsController.php
index 29140aee8..26a5de004 100755
--- a/app/Http/Controllers/ConversationsController.php
+++ b/app/Http/Controllers/ConversationsController.php
@@ -2315,8 +2315,7 @@ public function ajax(Request $request)
                 $subject = trim($subject);
 
                 if (!$response['msg'] && $subject) {
-                    $conversation->subject = $subject;
-                    $conversation->save();
+                    $conversation->changeSubject($subject, $user);
 
                     $response['status'] = 'success';
                 }


Test patch: