_binary From 55817fc8705c65a98be78db332e84639879bebce Mon Sep 17 00:00:00 2001
From: Harika Nukala <harika.nukala@sap.com>
Date: Mon, 28 Nov 2016 21:53:21 -0800
Subject: [PATCH] able to call bot in group posts

---
 src/controllers/components/SocialComponent.php | 54 ++++++++++++++++++++++----
 src/models/GroupModel.php                      | 15 +++++++
 2 files changed, 62 insertions(+), 7 deletions(-)

diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index 4feefe3..adb250b 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -1090,6 +1090,22 @@ class SocialComponent extends Component implements CrawlConstants
                     $title = "-- ".$parent_item['TITLE'];
                     $id = $group_model->addGroupItem($parent_item["ID"],
                         $group_id, $user_id, $title, $description);
+                    $bot_followers = $group_model->getGroupBots($group_id);
+                    $bots = [];
+                    $bots_url = [];
+                    $post_followed = "pre ";
+                    foreach ($bot_followers as $bot_follower) {
+                        $bots[] = $bot_follower['USER_NAME'];
+                    }
+                    if (@preg_match_all('/(?<!\w)@(\w+) (.*)/', $description, $matches)) {
+                        foreach ($matches[1] as $match) {
+                            $index = array_search($match,$bots);
+                            if ($index) {
+                                $bots_url[] = $bot_followers[$index]['CALLBACK_URL'];
+                            }
+                        }
+                        $post_followed = $matches[2][0];
+                    }
                     $result = $this->handleResourceUploads(
                             $group_id, "post".$id);
                     if ($result == self::UPLOAD_FAILED) {
@@ -1109,17 +1125,40 @@ class SocialComponent extends Component implements CrawlConstants
                     }
                     $subject = tl('social_component_thread_notification',
                         $parent_item['TITLE']);
-                    $body = tl('social_component_notify_body')."\n".
+                    $body = tl('social_component_notify_body') . "\n" .
                         $parent_item['TITLE']."\n".
                         $post_url .
                         tl('social_component_notify_closing')."\n".
                         tl('social_component_notify_signature');
                     foreach ($followers as $follower) {
-                        $message = tl('social_component_notify_salutation',
-                            $follower['USER_NAME'])."\n\n";
-                        $message .= $body;
-                        $server->send($subject, C\MAIL_SENDER,
-                            $follower['EMAIL'], $message);
+                        if($user_model->isUserBot($follower['USER_ID']) == false) {
+                            $message = tl('social_component_notify_salutation',
+                                    $follower['USER_NAME']) . "\n\n";
+                            $message .= $body;
+                            $server->send($subject, C\MAIL_SENDER,
+                                $follower['EMAIL'], $message);
+                        } else {
+                            //call bot URL
+                            $index = array_search($follower['USER_NAME'],$bots);
+                            $bots_url[] = $bot_followers[$index]['CALLBACK_URL'];
+                        }
+                    }
+                    if(!empty($bots_url) && !empty($post_followed)){
+                        foreach ($bots_url as $Url) {
+                            $curl = curl_init();
+                            curl_setopt_array($curl, array(
+                                CURLOPT_RETURNTRANSFER => true,
+                                CURLOPT_URL => $Url,
+                                CURLOPT_POST => true,
+                                CURLOPT_POSTFIELDS => array(
+                                    post_followed => $post_followed
+                                )
+                            ));
+                            $data = curl_exec($curl);
+                            $id = $group_model->addGroupItem($parent_item["ID"],
+                                $group_id, $user_id, $title, $data);
+                            curl_close($curl);
+                        }
                     }
                     return $parent->redirectWithMessage(
                         tl('social_component_comment_added'));
@@ -1248,7 +1287,8 @@ class SocialComponent extends Component implements CrawlConstants
                     foreach ($followers as $follower) {
                         if ($follower['USER_ID'] != $user_id &&
                             ($user_id == $group['OWNER_ID'] ||
-                             $follower['USER_ID'] == $group['OWNER_ID'])) {
+                             $follower['USER_ID'] == $group['OWNER_ID'])
+                        && $user_model->isUserBot($follower['USER_ID']) == false) {
                             $message = tl('social_component_notify_salutation',
                                 $follower['USER_NAME'])."\n\n";
                             $message .= $body;
diff --git a/src/models/GroupModel.php b/src/models/GroupModel.php
index 46d3eb8..ae679cc 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -176,6 +176,21 @@ class GroupModel extends Model implements MediaConstants
         unset($users[$i]); //last one will be null
         return $users;
     }
+
+    public function getGroupBots($group_id){
+        $db = $this->db;
+        $sql = "SELECT U.USER_NAME, CB.BOT_TOKEN, CB.CALLBACK_URL".
+            " FROM USER_GROUP UG, USERS U, CHAT_BOT CB".
+            " WHERE UG.GROUP_ID = ? AND UG.USER_ID = U.USER_ID AND" .
+            " CB.USER_ID = UG.USER_ID";
+        $result = $db->execute($sql,[$group_id]);
+        $i = 0;
+        while ($bots[$i] = $db->fetchArray($result)) {
+            $i++;
+        }
+        unset($bots[$i]); //last one will be null
+        return $bots;
+    }
     /**
      * Get the number of users which belong to a group and whose user_name
      * matches a filter
-- 
2.8.4 (Apple Git-73)

