diff --git a/application/classes/Ushahidi/Console/Notification.php b/application/classes/Ushahidi/Console/Notification.php index db9915abc2..b3f167fef2 100644 --- a/application/classes/Ushahidi/Console/Notification.php +++ b/application/classes/Ushahidi/Console/Notification.php @@ -123,6 +123,10 @@ private function generateMessages($notification) // Create outgoing messages foreach ($contacts as $contact) { + if ($this->messageRepository->notificationMessageExists($post->id, $contact->id)) { + continue; + } + $subs = [ ':sitename' => $site_name, ':title' => $post->title, @@ -135,7 +139,7 @@ private function generateMessages($notification) $state = [ 'contact_id' => $contact->id, - 'post_id' => $post->id, + 'notification_post_id' => $post->id, 'title' => strtr(Kohana::message('notifications', $messageType . '.title', "New post: :title"), $subs), 'message' => strtr(Kohana::message('notifications', $messageType . '.message', "New post: :title"), $subs), 'type' => $messageType, diff --git a/application/classes/Ushahidi/Repository/Message.php b/application/classes/Ushahidi/Repository/Message.php index b8f155b3cd..c000c9ee2f 100644 --- a/application/classes/Ushahidi/Repository/Message.php +++ b/application/classes/Ushahidi/Repository/Message.php @@ -184,4 +184,10 @@ public function parentExists($parent_id) { return $this->selectCount(['id' => $parent_id]) > 0; } + + //MessageRepository + public function notificationMessageExists($post_id, $contact_id) + { + return $this->selectCount(['notification_post_id' => $post_id, 'contact_id' => $contact_id, 'direction' => 'outgoing']) > 0; + } } diff --git a/application/config/features.php b/application/config/features.php index dd0e54444c..288d292b20 100644 --- a/application/config/features.php +++ b/application/config/features.php @@ -19,6 +19,7 @@ 'chart' => TRUE, 'timeline' => TRUE, 'activity' => TRUE, + 'plan' => FALSE, ], // Data sources diff --git a/migrations/20160322013857_add_message_notification_post_id.php b/migrations/20160322013857_add_message_notification_post_id.php new file mode 100644 index 0000000000..f68f373e5f --- /dev/null +++ b/migrations/20160322013857_add_message_notification_post_id.php @@ -0,0 +1,23 @@ +table('messages') + ->addColumn('notification_post_id', 'integer', [ + 'comment' => "Source post this message is a notification for", + 'null' => true, + ]) + ->addForeignKey('notification_post_id', 'posts', 'id', [ + 'delete' => 'SET NULL', + 'update' => 'CASCADE', + ]) + ->update(); + } +} diff --git a/migrations/20160322020552_move_message_post_id_to_notification_post_id.php b/migrations/20160322020552_move_message_post_id_to_notification_post_id.php new file mode 100644 index 0000000000..7acbec78d4 --- /dev/null +++ b/migrations/20160322020552_move_message_post_id_to_notification_post_id.php @@ -0,0 +1,45 @@ +execute(" + UPDATE messages + SET notification_post_id = post_id + WHERE direction = 'outgoing' + AND post_id IS NOT NULL + "); + $this->execute("UPDATE messages + SET post_id = NULL + WHERE direction = 'outgoing' + AND post_id IS NOT NULL + AND post_id = notification_post_id; + "); + } + + /** + * Migrate Down. + */ + public function down() + { + $this->execute(" + UPDATE messages + SET post_id = notification_post_id + WHERE direction = 'outgoing' + AND notification_post_id IS NOT NULL + "); + $this->execute("UPDATE messages + SET notification_post_id = NULL + WHERE direction = 'outgoing' + AND notification_post_id IS NOT NULL + AND notification_post_id = post_id; + "); + } +} diff --git a/plugins/data-provider-email/classes/DataProvider/Email.php b/plugins/data-provider-email/classes/DataProvider/Email.php index 7c5196ce71..6e75665700 100644 --- a/plugins/data-provider-email/classes/DataProvider/Email.php +++ b/plugins/data-provider-email/classes/DataProvider/Email.php @@ -113,7 +113,7 @@ public function fetch($limit = FALSE) // Return on connection error if (! $connection) { -; Kohana::$log->add(Log::ERROR, "Could not connect to incoming email server"); + Kohana::$log->add(Log::ERROR, "Could not connect to incoming email server"); return 0; } $emails = imap_search($connection,'ALL'); @@ -131,10 +131,37 @@ public function fetch($limit = FALSE) break; $overview = imap_fetch_overview($connection, $email_number, 0); - $message = imap_fetchbody($connection, $email_number, 2); + + $structure = imap_fetchstructure($connection, $email_number); + + // Get HTML message from multipart message + if (! empty($structure->parts)) + { + $no_of_parts = count($structure->parts); + + for ($i = 0; $i < $no_of_parts; $i++) + { + $part = $structure->parts[$i]; + + if ($part->subtype == 'HTML') + { + $message = imap_fetchbody($connection, $email_number, $i+1); + } + } + } + else + { + // or just fetch the body if not a multipart message + $message = imap_body($connection, $email_number); + } + + $message = imap_qprint($message); // Process the email - $this->_process_incoming($overview[0], $message); + if (! empty($message)) + { + $this->_process_incoming($overview[0], $message); + } // After processing, delete! imap_delete($connection, $email_number); diff --git a/src/Core/Entity/Message.php b/src/Core/Entity/Message.php index f1057afd77..4a3e386082 100644 --- a/src/Core/Entity/Message.php +++ b/src/Core/Entity/Message.php @@ -46,6 +46,7 @@ class Message extends StaticEntity protected $direction; protected $created; protected $additional_data; + protected $notification_post_id; // DataTransformer protected function getDefinition() @@ -67,7 +68,8 @@ protected function getDefinition() 'data_provider' => 'string', 'data_provider_message_id' => 'string', // any additional message data - 'additional_data' => '*json' + 'additional_data' => '*json', + 'notification_post_id' => 'int' ]; } diff --git a/src/Core/Entity/MessageRepository.php b/src/Core/Entity/MessageRepository.php index 266385bffa..21af59a69b 100644 --- a/src/Core/Entity/MessageRepository.php +++ b/src/Core/Entity/MessageRepository.php @@ -28,4 +28,14 @@ interface MessageRepository extends * @return [Message, ...] */ public function getPendingMessages($status, $data_provider, $limit); + + + /** + * Check whether a notification message has been sent to a contact + * + * @param int $post_id + * @param int $contact_id + * @return bool + */ + public function notificationMessageExists($post_id, $contact_id); }