Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmackay committed Mar 22, 2016
2 parents 07ffd9b + d33b0f8 commit bdb63ec
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 5 deletions.
6 changes: 5 additions & 1 deletion application/classes/Ushahidi/Console/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions application/classes/Ushahidi/Repository/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions application/config/features.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'chart' => TRUE,
'timeline' => TRUE,
'activity' => TRUE,
'plan' => FALSE,
],

// Data sources
Expand Down
23 changes: 23 additions & 0 deletions migrations/20160322013857_add_message_notification_post_id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Phinx\Migration\AbstractMigration;

class AddMessageNotificationPostId extends AbstractMigration
{
/**
* Change Method.
**/
public function change()
{
$this->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();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Phinx\Migration\AbstractMigration;

class MoveMessagePostIdToNotificationPostId extends AbstractMigration
{

/**
* Migrate Up.
*/
public function up()
{
$this->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;
");
}
}
33 changes: 30 additions & 3 deletions plugins/data-provider-email/classes/DataProvider/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand 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);
Expand Down
4 changes: 3 additions & 1 deletion src/Core/Entity/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Message extends StaticEntity
protected $direction;
protected $created;
protected $additional_data;
protected $notification_post_id;

// DataTransformer
protected function getDefinition()
Expand All @@ -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'
];
}

Expand Down
10 changes: 10 additions & 0 deletions src/Core/Entity/MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit bdb63ec

Please sign in to comment.