From b9baa2ccdfcc2b981b5f611b2b568c82787bbe10 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 18 Mar 2015 20:10:36 -0500
Subject: [PATCH] collabs: Skip activity notice if collabs included in Cc
 header

---
 include/class.thread.php | 27 +++++++++++++++++++++++++++
 include/class.ticket.php | 20 ++++++++++++++++----
 2 files changed, 43 insertions(+), 4 deletions(-)

diff --git a/include/class.thread.php b/include/class.thread.php
index a717e89af..2267056c6 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -380,6 +380,33 @@ Class ThreadEntry {
         return $references;
     }
 
+    /**
+     * Retrieve a list of all the recients of this message if the message
+     * was received via email.
+     *
+     * Returns:
+     * (array<RFC_822>) list of recipients parsed with the Mail/RFC822
+     * address parsing utility. Returns an empty array if the message was
+     * not received via email.
+     */
+    function getAllEmailRecipients() {
+        $headers = self::getEmailHeaderArray();
+        $recipients = array();
+        if (!$headers)
+            return $recipients;
+
+        foreach (array('To', 'Cc') as $H) {
+            if (!isset($headers[$H]))
+                continue;
+
+            if (!($all = Mail_Parse::parseAddressList($headers[$H])))
+                continue;
+
+            $recipients = array_merge($recipients, $all);
+        }
+        return $recipients;
+    }
+
     function getUIDFromEmailReference($ref) {
 
         $info = unpack('Vtid/Vuid',
diff --git a/include/class.ticket.php b/include/class.ticket.php
index e7546870e..b8037ce8f 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -1102,15 +1102,24 @@ class Ticket {
             return;
 
         //Who posted the entry?
-        $uid = 0;
+        $skip = array();
         if ($entry instanceof Message) {
             $poster = $entry->getUser();
             // Skip the person who sent in the message
-            $uid = $entry->getUserId();
+            $skip[$entry->getUserId()] = 1;
+            // Skip all the other recipients of the message
+            foreach ($entry->getEmailAllRecipients() as $R) {
+                foreach ($recipients as $R2) {
+                    if ($R2->getEmail() == ($R->mailbox.'@'.$R->hostname)) {
+                        $skip[$R2->getUserId()] = true;
+                        break;
+                    }
+                }
+            }
         } else {
             $poster = $entry->getStaff();
             // Skip the ticket owner
-            $uid = $this->getUserId();
+            $skip[$this->getUserId()] = 1;
         }
 
         $vars = array_merge($vars, array(
@@ -1125,7 +1134,10 @@ class Ticket {
         $options = array('inreplyto' => $entry->getEmailMessageId(),
                          'thread' => $entry);
         foreach ($recipients as $recipient) {
-            if ($uid == $recipient->getUserId()) continue;
+            // Skip folks who have already been included on this part of
+            // the conversation
+            if (isset($skip[$recipient->getUserId()]))
+                continue;
             $notice = $this->replaceVars($msg, array('recipient' => $recipient));
             $email->send($recipient, $notice['subj'], $notice['body'], $attachments,
                 $options);
-- 
GitLab