From 4c1ce6e527b646d7a6f0322bec6050429b6841a2 Mon Sep 17 00:00:00 2001
From: Harika Nukala <harika.nukala@sap.com>
Date: Sun, 4 Dec 2016 02:58:13 -0800
Subject: [PATCH] able to call bots

---
 src/controllers/components/SocialComponent.php | 55 ++++++++++++++++++++++----
 src/models/GroupModel.php                      | 20 ++++++++++
 2 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/src/controllers/components/SocialComponent.php b/src/controllers/components/SocialComponent.php
index 4feefe3..f644788 100644
--- a/src/controllers/components/SocialComponent.php
+++ b/src/controllers/components/SocialComponent.php
@@ -37,6 +37,8 @@ use seekquarry\yioop\library\CrawlConstants;
 use seekquarry\yioop\library\MailServer;
 use seekquarry\yioop\library\UrlParser;
 use seekquarry\yioop\library\WikiParser;
+use seekquarry\yioop\library\FetchUrl;
+
 
 /**
  * Provides activities to AdminController related to creating, updating
@@ -1090,6 +1092,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_called = [];
+                    $post_followed = "";
+                    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_called[] = $bot_followers[$index];
+                            }
+                        }
+                        $post_followed = $matches[2][0];
+                    }
                     $result = $this->handleResourceUploads(
                             $group_id, "post".$id);
                     if ($result == self::UPLOAD_FAILED) {
@@ -1109,17 +1127,39 @@ 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_called[] = $bot_followers[$index];
+                        }
+                    }
+                    $num_bots = count($bots_called);
+                    $sites = [];
+                    $post_data = [];
+                    for ($i = 0; $i < $num_bots; $i++) {
+                        $sites[$i][CrawlConstants::URL] = $bots_called[$i]['CALLBACK_URL'];
+                        $post_data[$i] = "post=$post_followed";
+                    }
+                    $outputs = [];
+                    if (count($sites) > 0) {
+                        $outputs = FetchUrl::getPages($sites, false, 0, null, self::URL,
+                            self::PAGE, true, $post_data);
+                    }
+                    foreach ($outputs as $index => $output) {
+                        $id = $group_model->addGroupItem($parent_item["ID"],
+                                $group_id, $bots_called[$index]['USER_ID'], $title, $output["q"]);
                     }
                     return $parent->redirectWithMessage(
                         tl('social_component_comment_added'));
@@ -1248,7 +1288,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..1036c23 100644
--- a/src/models/GroupModel.php
+++ b/src/models/GroupModel.php
@@ -2205,4 +2205,24 @@ class GroupModel extends Model implements MediaConstants
         }
         return [$total, $pages];
     }
+    /**
+     * Get an array of bots that belong to a group
+     *
+     * @param string $group_id  the group_id to get bots for
+     * @return array of BOTS rows
+     */
+    public function getGroupBots($group_id){
+        $db = $this->db;
+        $sql = "SELECT CB.USER_ID, 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;
+    }
 }
-- 
2.8.4 (Apple Git-73)

